static cell_t SetVoteResultCallback(IPluginContext *pContext, const cell_t *params) { Handle_t hndl = (Handle_t)params[1]; HandleError err; IBaseMenu *menu; if ((err=g_Menus.ReadMenuHandle(params[1], &menu)) != HandleError_None) { return pContext->ThrowNativeError("Menu handle %x is invalid (error %d)", hndl, err); } IPluginFunction *pFunction = pContext->GetFunctionById(params[2]); if (!pFunction) { return pContext->ThrowNativeError("Invalid function %x", params[2]); } void *array[2]; array[0] = pFunction; array[1] = (void *)¶ms[2]; IMenuHandler *pHandler = menu->GetHandler(); if (!pHandler->OnSetHandlerOption("set_vote_results_handler", (const void *)array)) { return pContext->ThrowNativeError("The given menu does not support this option"); } return 1; }
void BaseMenuStyle::_CancelClientMenu(int client, MenuCancelReason reason, bool bAutoIgnore/* =false */) { #if defined MENU_DEBUG g_Logger.LogMessage("[SM_MENU] _CancelClientMenu() (client %d) (bAutoIgnore %d) (reason %d)", client, bAutoIgnore, reason); #endif CBaseMenuPlayer *player = GetMenuPlayer(client); menu_states_t &states = player->states; bool bOldIgnore = player->bAutoIgnore; if (bAutoIgnore) { player->bAutoIgnore = true; } /* Save states */ IMenuHandler *mh = states.mh; IBaseMenu *menu = states.menu; /* Clear menu */ player->bInMenu = false; if (player->menuHoldTime) { RemoveClientFromWatch(client); } /* Fire callbacks */ mh->OnMenuCancel(menu, client, reason); /* Only fire end if there's a valid menu */ if (menu) { mh->OnMenuEnd(menu, MenuEnd_Cancelled); } if (bAutoIgnore) { player->bAutoIgnore = bOldIgnore; } }
void BaseMenuStyle::ClientPressedKey(int client, unsigned int key_press) { #if defined MENU_DEBUG g_Logger.LogMessage("[SM_MENU] ClientPressedKey() (client %d) (key_press %d)", client, key_press); #endif CBaseMenuPlayer *player = GetMenuPlayer(client); /* First question: Are we in a menu? */ if (!player->bInMenu) { return; } bool cancel = false; unsigned int item = 0; MenuCancelReason reason = MenuCancel_Exit; MenuEndReason end_reason = MenuEnd_Selected; menu_states_t &states = player->states; /* Save variables */ IMenuHandler *mh = states.mh; IBaseMenu *menu = states.menu; unsigned int item_on_page = states.item_on_page; assert(mh != NULL); if (menu == NULL) { item = key_press; } else if (key_press < 1 || key_press > GetMaxPageItems()) { cancel = true; } else { ItemSelection type = states.slots[key_press].type; /* Check if we should play a sound about the type */ if (g_Menus.MenuSoundsEnabled() && (!menu || (menu->GetMenuOptionFlags() & MENUFLAG_NO_SOUND) != MENUFLAG_NO_SOUND)) { CellRecipientFilter filter; cell_t clients[1]; clients[0] = client; filter.Initialize(clients, 1); const char *sound = g_Menus.GetMenuSound(type); if (sound != NULL) { edict_t *pEdict = PEntityOfEntIndex(client); if (pEdict) { ICollideable *pCollideable = pEdict->GetCollideable(); if (pCollideable) { const Vector & pos = pCollideable->GetCollisionOrigin(); enginesound->EmitSound(filter, client, CHAN_AUTO, #if SOURCE_ENGINE >= SE_PORTAL2 sound, -1, #endif sound, VOL_NORM, ATTN_NORM, #if SOURCE_ENGINE >= SE_PORTAL2 0, #endif 0, PITCH_NORM, #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS \ || SOURCE_ENGINE == SE_SDK2013 || SOURCE_ENGINE == SE_BMS || SOURCE_ENGINE == SE_TF2 0, #endif &pos); } } } } /* For navigational items, we're going to redisplay */ if (type == ItemSel_Back) { if (!RedoClientMenu(client, ItemOrder_Descending)) { cancel = true; reason = MenuCancel_NoDisplay; end_reason = MenuEnd_Cancelled; } else { return; } } else if (type == ItemSel_Next) { if (!RedoClientMenu(client, ItemOrder_Ascending)) { cancel = true; /* I like Saltines. */ reason = MenuCancel_NoDisplay; end_reason = MenuEnd_Cancelled; } else { return; } } else if (type == ItemSel_Exit || type == ItemSel_None) { cancel = true; reason = MenuCancel_Exit; end_reason = MenuEnd_Exit; } else if (type == ItemSel_ExitBack) { cancel = true; reason = MenuCancel_ExitBack; end_reason = MenuEnd_ExitBack; } else { item = states.slots[key_press].item; } } /* Clear states */ player->bInMenu = false; if (player->menuHoldTime) { RemoveClientFromWatch(client); } Handle_t hndl = menu ? menu->GetHandle() : BAD_HANDLE; AutoHandleRooter ahr(hndl); if (cancel) { mh->OnMenuCancel(menu, client, reason); } else { mh->OnMenuSelect(menu, client, item); if (mh->GetMenuAPIVersion2() >= 13) { mh->OnMenuSelect2(menu, client, item, item_on_page); } } /* Only fire end for valid menus */ if (menu) { mh->OnMenuEnd(menu, end_reason); } }
void VoteMenuHandler::EndVoting() { /* Set when the next delay ends. We ignore cancellation because a menu * was, at one point, displayed, which is all that counts. However, we * do re-calculate the time just in case the menu had no time limit. */ float fVoteDelay = sm_vote_delay.GetFloat(); if (fVoteDelay < 1.0) { g_next_vote = 0.0; } else { g_next_vote = gpGlobals->curtime + fVoteDelay; } if (m_bCancelled) { /* If we were cancelled, don't bother tabulating anything. * Reset just in case someone tries to redraw, which means * we need to save our states. */ IBaseMenu *menu = m_pCurMenu; IMenuHandler *handler = m_pHandler; InternalReset(); handler->OnMenuVoteCancel(menu, VoteCancel_Generic); handler->OnMenuEnd(menu, MenuEnd_VotingCancelled); return; } menu_vote_result_t vote; menu_vote_result_t::menu_client_vote_t client_vote[256]; menu_vote_result_t::menu_item_vote_t item_vote[256]; memset(&vote, 0, sizeof(vote)); /* Build the item list */ for (unsigned int i=0; i<m_Items; i++) { if (m_Votes[i] > 0) { item_vote[vote.num_items].count = m_Votes[i]; item_vote[vote.num_items].item = i; vote.num_votes += m_Votes[i]; vote.num_items++; } } vote.item_list = item_vote; if (!vote.num_votes) { IBaseMenu *menu = m_pCurMenu; IMenuHandler *handler = m_pHandler; InternalReset(); handler->OnMenuVoteCancel(menu, VoteCancel_NoVotes); handler->OnMenuEnd(menu, MenuEnd_VotingCancelled); return; } /* Build the client list */ for (int i=1; i<=gpGlobals->maxClients; i++) { if (m_ClientVotes[i] >= -1) { client_vote[vote.num_clients].client = i; client_vote[vote.num_clients].item = m_ClientVotes[i]; vote.num_clients++; } } vote.client_list = client_vote; /* Sort the item list descending like we promised */ qsort(item_vote, vote.num_items, sizeof(menu_vote_result_t::menu_item_vote_t), SortVoteItems); /* Save states, then clear what we've saved. * This makes us re-entrant, which is always the safe way to go. */ IBaseMenu *menu = m_pCurMenu; IMenuHandler *handler = m_pHandler; InternalReset(); /* Send vote info */ handler->OnMenuVoteResults(menu, &vote); handler->OnMenuEnd(menu, MenuEnd_VotingDone); }
IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order) { IBaseMenu *menu = md.menu; if (!menu) { return NULL; } struct { unsigned int position; ItemDrawInfo draw; } drawItems[10]; /* Figure out how many items to draw */ IMenuStyle *style = menu->GetDrawStyle(); unsigned int pgn = menu->GetPagination(); unsigned int maxItems = style->GetMaxPageItems(); bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT; bool novoteButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_NOVOTE) == MENUFLAG_BUTTON_NOVOTE; if (pgn != MENU_NO_PAGINATION) { maxItems = pgn; } else if (exitButton) { maxItems--; } if (novoteButton) { maxItems--; } /* This is very not allowed! */ if (maxItems < 2) { return NULL; } unsigned int totalItems = menu->GetItemCount(); unsigned int startItem = 0; /* For pagination, find the starting point. */ if (pgn != MENU_NO_PAGINATION) { if (order == ItemOrder_Ascending) { startItem = md.lastItem; /* This shouldn't happen with well-coded menus. * If the item is out of bounds, switch the order to * Items_Descending and make us start from the top. */ if (startItem >= totalItems) { startItem = totalItems - 1; order = ItemOrder_Descending; } } else if (order == ItemOrder_Descending) { startItem = md.firstItem; /* This shouldn't happen with well-coded menus. * If searching backwards doesn't give us enough room, * start from the beginning and change to ascending. */ if (startItem <= maxItems) { startItem = 0; order = ItemOrder_Ascending; } } } /* Get our Display pointer and initialize some crap */ IMenuPanel *panel = menu->CreatePanel(); IMenuHandler *mh = md.mh; bool foundExtra = false; unsigned int extraItem = 0; if (panel == NULL) { return NULL; } /** * We keep searching until: * 1) There are no more items * 2) We reach one OVER the maximum number of slot items * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION */ unsigned int i = startItem; unsigned int foundItems = 0; while (totalItems) { ItemDrawInfo &dr = drawItems[foundItems].draw; /* Is the item valid? */ if (menu->GetItemInfo(i, &dr) != NULL) { /* Ask the user to change the style, if necessary */ mh->OnMenuDrawItem(menu, client, i, dr.style); /* Check if it's renderable */ if (IsSlotItem(panel, dr.style)) { /* If we've already found the max number of items, * This means we should just cancel out and log our * "last item." */ if (foundItems >= maxItems) { foundExtra = true; extraItem = i; break; } drawItems[foundItems++].position = i; } } /* If there's no pagination, stop once the menu is full. */ if (pgn == MENU_NO_PAGINATION) { /* If we've filled up, then stop */ if (foundItems >= maxItems) { break; } } /* If we're descending and this is the first item, stop */ if (order == ItemOrder_Descending) { if (i == 0) { break; } i--; } /* If we're ascending and this is the last item, stop */ else if (order == ItemOrder_Ascending) { if (i >= totalItems - 1) { break; } i++; } } /* There were no items to draw! */ if (!foundItems) { panel->DeleteThis(); return NULL; } bool displayPrev = false; bool displayNext = false; /* This is an annoying process. * Skip it for non-paginated menus, which get special treatment. */ if (pgn != MENU_NO_PAGINATION) { if (foundExtra) { if (order == ItemOrder_Descending) { displayPrev = true; md.firstItem = extraItem; } else if (order == ItemOrder_Ascending) { displayNext = true; md.lastItem = extraItem; } } unsigned int lastItem = 0; ItemDrawInfo dr; /* Find the last feasible item to search from. */ if (order == ItemOrder_Descending) { lastItem = drawItems[0].position; if (lastItem >= totalItems - 1) { goto skip_search; } while (++lastItem < totalItems) { if (menu->GetItemInfo(lastItem, &dr) != NULL) { mh->OnMenuDrawItem(menu, client, lastItem, dr.style); if (IsSlotItem(panel, dr.style)) { displayNext = true; md.lastItem = lastItem; break; } } } } else if (order == ItemOrder_Ascending) { lastItem = drawItems[0].position; if (lastItem == 0) { goto skip_search; } lastItem--; while (lastItem != 0) { if (menu->GetItemInfo(lastItem, &dr) != NULL) { mh->OnMenuDrawItem(menu, client, lastItem, dr.style); if (IsSlotItem(panel, dr.style)) { displayPrev = true; md.firstItem = lastItem; break; } } lastItem--; } } } skip_search: /* Draw the item according to the order */ menu_slots_t *slots = md.slots; unsigned int position = 0; /* Keep track of the last position */ if (novoteButton) { char text[50]; if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "No Vote", &client)) { UTIL_Format(text, sizeof(text), "No Vote"); } ItemDrawInfo dr(text, 0); position = panel->DrawItem(dr); slots[position].type = ItemSel_Exit; position++; } if (order == ItemOrder_Ascending) { md.item_on_page = drawItems[0].position; for (unsigned int i = 0; i < foundItems; i++) { ItemDrawInfo &dr = drawItems[i].draw; if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0) { position = panel->DrawItem(dr); } if (position != 0) { slots[position].item = drawItems[i].position; if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED) { slots[position].type = ItemSel_None; } else { slots[position].type = ItemSel_Item; } } } } else if (order == ItemOrder_Descending) { unsigned int i = foundItems; /* NOTE: There will always be at least one item because * of the check earlier. */ md.item_on_page = drawItems[foundItems - 1].position; while (i--) { ItemDrawInfo &dr = drawItems[i].draw; if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0) { position = panel->DrawItem(dr); } if (position != 0) { slots[position].item = drawItems[i].position; if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED) { slots[position].type = ItemSel_None; } else { slots[position].type = ItemSel_Item; } } } } /* Now, we need to check if we need to add anything extra */ if (pgn != MENU_NO_PAGINATION || exitButton) { bool canDrawDisabled = panel->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL); bool exitBackButton = false; char text[50]; if (pgn != MENU_NO_PAGINATION && (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK) { exitBackButton = true; } /* Calculate how many items we are allowed for control stuff */ unsigned int padding = style->GetMaxPageItems() - maxItems; /* Add the number of available slots */ padding += (maxItems - foundItems); /* Someday, if we are able to re-enable this, we will be very lucky men. */ #if 0 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit)) { #endif /* Even if we don't draw an exit button, we invalidate the slot. */ padding--; #if 0 } else { /* Otherwise, we don't draw anything and leave the slot available */ exitButton = false; } #endif if (pgn != MENU_NO_PAGINATION) { /* Subtract two slots for the displayNext/displayPrev padding */ padding -= 2; } /* If we have an "Exit Back" button and the space to draw it, do so. */ if (exitBackButton) { if (!displayPrev) { displayPrev = true; } else { exitBackButton = false; } } /** * We allow next/prev to be undrawn if neither exists. * Thus, we only need padding if one of them will be drawn, * or the exit button will be drawn. */ ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER); if (exitButton || (displayNext || displayPrev)) { /* If there are no control options, * Instead just pad with invisible slots. */ if (!displayPrev && !displayPrev) { padItem.style = ITEMDRAW_NOTEXT; } /* Add spacers so we can pad to the end */ for (unsigned int i=0; i<padding; i++) { position = panel->DrawItem(padItem); slots[position].type = ItemSel_None; } } /* Put a fake spacer before control stuff, if possible */ if ((displayPrev || displayNext) || exitButton) { ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER); panel->DrawItem(draw); } ItemDrawInfo dr(text, 0); /** * If we have one or the other, we need to have spacers for both. */ if (pgn != MENU_NO_PAGINATION) { if (displayPrev || displayNext) { /* PREVIOUS */ ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL); if (displayPrev || canDrawDisabled) { if (exitBackButton) { if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Back", &client)) { UTIL_Format(text, sizeof(text), "Back"); } dr.style = ITEMDRAW_CONTROL; position = panel->DrawItem(dr); slots[position].type = ItemSel_ExitBack; } else { if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Previous", &client)) { UTIL_Format(text, sizeof(text), "Previous"); } dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL; position = panel->DrawItem(dr); slots[position].type = ItemSel_Back; } } else if (displayNext || exitButton) { /* If we can't display this, and there is an exit button, * we need to pad! */ position = panel->DrawItem(padCtrlItem); slots[position].type = ItemSel_None; } /* NEXT */ if (displayNext || canDrawDisabled) { if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Next", &client)) { UTIL_Format(text, sizeof(text), "Next"); } dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL; position = panel->DrawItem(dr); slots[position].type = ItemSel_Next; } else if (exitButton) { /* If we can't display this, * but there is an "exit" button, we need to pad! */ position = panel->DrawItem(padCtrlItem); slots[position].type = ItemSel_None; } } else { /* Otherwise, bump to two slots! */ ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT); position = panel->DrawItem(numBump); slots[position].type = ItemSel_None; position = panel->DrawItem(numBump); slots[position].type = ItemSel_None; } } /* EXIT */ if (exitButton) { if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Exit", &client)) { UTIL_Format(text, sizeof(text), "Exit"); } dr.style = ITEMDRAW_CONTROL; position = panel->DrawItem(dr); slots[position].type = ItemSel_Exit; } } /* Lastly, fill in any slots we could have missed */ for (unsigned int i = position + 1; i < 10; i++) { slots[i].type = ItemSel_None; } /* Do title stuff */ mh->OnMenuDisplay(menu, client, panel); panel->DrawTitle(menu->GetDefaultTitle(), true); return panel; }