3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
18 #include "MenuManager.h"
19 #include "sm_stringutil.h"
20 #include "sourcemm_api.h"
21 #include "PlayerManager.h"
22 #include "MenuStyle_Valve.h"
24 #include "HandleSys.h"
28 /*******************************
29 *******************************
30 ******** VOTE HANDLER *********
31 *******************************
32 *******************************/
34 unsigned int VoteMenuHandler::GetMenuAPIVersion2()
36 return m_pHandler->GetMenuAPIVersion2();
39 bool VoteMenuHandler::IsVoteInProgress()
41 return (m_pCurMenu != NULL);
44 void VoteMenuHandler::InitializeVoting(IBaseMenu *menu)
46 m_Items = menu->GetItemCount();
48 if (m_Votes.size() < (size_t)m_Items)
50 /* Only clear the items we need to... */
51 size_t size = m_Votes.size();
52 for (size_t i=0; i<size; i++)
56 m_Votes.resize(m_Items, 0);
58 for (unsigned int i=0; i<m_Items; i++)
66 m_pHandler->OnMenuStart(m_pCurMenu);
69 void VoteMenuHandler::StartVoting()
73 m_pHandler->OnMenuVoteStart(m_pCurMenu);
75 /* By now we know how many clients were set.
76 * If there are none, we should end IMMEDIATELY.
84 void VoteMenuHandler::DecrementPlayerCount()
86 assert(m_Clients > 0);
90 if (m_bStarted && m_Clients == 0)
96 void VoteMenuHandler::EndVoting()
100 /* If we were cancelled, don't bother tabulating anything.
101 * Reset just in case someone tries to redraw, which means
102 * we need to save our states.
104 IBaseMenu *menu = m_pCurMenu;
106 m_pHandler->OnMenuVoteCancel(menu);
107 m_pHandler->OnMenuEnd(menu);
111 unsigned int chosen = 0;
112 unsigned int highest = 0;
113 unsigned int dup_count = 0;
115 /* If we got zero votes, take a shortcut. */
118 /* Pick a random item and then jump far, far away. */
119 srand((unsigned int)(time(NULL)));
120 chosen = (unsigned int)rand() % m_Items;
124 /* We can't have more dups than this!
125 * This is the max number of players.
127 unsigned int dup_array[256];
129 for (size_t i=1; i<m_Items; i++)
131 if (m_Votes[i] > m_Votes[highest])
133 /* If we have a new highest count, mark it and trash the duplicate
134 * list by setting the total to 0.
138 } else if (m_Votes[i] == m_Votes[highest]) {
139 /* If they're equal, mark it in the duplicate list.
140 * We'll add in the original later.
142 dup_array[dup_count++] = i;
146 /* Check if we need to pick from the duplicate list */
149 /* Re-add the original to the list because it's not in there. */
150 dup_array[dup_count++] = highest;
152 /* Pick a random slot. */
153 srand((unsigned int)(time(NULL)));
154 unsigned int r = (unsigned int)rand() % dup_count;
157 chosen = dup_array[r];
163 m_pHandler->OnMenuVoteEnd(m_pCurMenu, chosen);
164 m_pHandler->OnMenuEnd(m_pCurMenu);
168 void VoteMenuHandler::OnMenuStart(IBaseMenu *menu)
173 void VoteMenuHandler::OnMenuEnd(IBaseMenu *menu)
175 DecrementPlayerCount();
178 void VoteMenuHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
180 m_pHandler->OnMenuCancel(menu, client, reason);
183 void VoteMenuHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
185 m_pHandler->OnMenuDisplay(menu, client, display);
188 void VoteMenuHandler::OnMenuDisplayItem(IBaseMenu *menu, int client, unsigned int item, const char **display)
190 m_pHandler->OnMenuDisplayItem(menu, client, item, display);
193 void VoteMenuHandler::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
195 m_pHandler->OnMenuDrawItem(menu, client, item, style);
198 void VoteMenuHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
200 /* Check by our item count, NOT the vote array size */
207 m_pHandler->OnMenuSelect(menu, client, item);
210 void VoteMenuHandler::Reset(IMenuHandler *mh)
216 void VoteMenuHandler::InternalReset()
223 m_bCancelled = false;
226 void VoteMenuHandler::CancelVoting()
231 /*******************************
232 *******************************
233 ******** MENU MANAGER *********
234 *******************************
235 *******************************/
237 MenuManager::MenuManager()
239 m_Styles.push_back(&g_ValveMenuStyle);
240 SetDefaultStyle(&g_ValveMenuStyle);
243 void MenuManager::OnSourceModAllInitialized()
245 g_ShareSys.AddInterface(NULL, this);
248 g_HandleSys.InitAccessDefaults(NULL, &access);
250 /* Deny cloning to menus */
251 access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
252 m_MenuType = g_HandleSys.CreateType("IBaseMenu", this, 0, NULL, &access, g_pCoreIdent, NULL);
254 /* Also deny deletion to styles */
255 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
256 m_StyleType = g_HandleSys.CreateType("IMenuStyle", this, 0, NULL, &access, g_pCoreIdent, NULL);
259 void MenuManager::OnSourceModAllShutdown()
261 g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
262 g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
264 while (!m_VoteHandlers.empty())
266 delete m_VoteHandlers.front();
267 m_VoteHandlers.pop();
271 void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
273 if (type == m_MenuType)
275 IBaseMenu *menu = (IBaseMenu *)object;
276 menu->Destroy(false);
277 } else if (type == m_StyleType) {
282 Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
284 if (m_MenuType == NO_HANDLE_TYPE)
289 return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
292 Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
294 if (m_StyleType == NO_HANDLE_TYPE)
299 return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
302 HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
306 sec.pIdentity = g_pCoreIdent;
309 return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
312 HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
316 sec.pIdentity = g_pCoreIdent;
317 sec.pOwner = g_pCoreIdent;
319 return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
322 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
329 m_pDefaultStyle = style;
334 IMenuStyle *MenuManager::GetStyle(unsigned int index)
336 if (index >= GetStyleCount())
341 return m_Styles[index];
344 void MenuManager::AddStyle(IMenuStyle *style)
346 m_Styles.push_back(style);
349 unsigned int MenuManager::GetStyleCount()
351 return (unsigned int)m_Styles.size();
354 IMenuStyle *MenuManager::FindStyleByName(const char *name)
356 unsigned int count = GetStyleCount();
357 for (unsigned int i=0; i<count; i++)
359 IMenuStyle *ptr = GetStyle(i);
360 if (strcasecmp(ptr->GetStyleName(), name) == 0)
369 inline bool IsSlotItem(IMenuPanel *display,
372 if (!display->CanDrawItem(style))
376 if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
380 if (style & ITEMDRAW_RAWLINE)
388 IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
390 IBaseMenu *menu = md.menu;
399 unsigned int position;
403 /* Figure out how many items to draw */
404 IMenuStyle *style = menu->GetDrawStyle();
405 unsigned int pgn = menu->GetPagination();
406 unsigned int maxItems = style->GetMaxPageItems();
407 if (pgn != MENU_NO_PAGINATION)
412 unsigned int totalItems = menu->GetItemCount();
413 unsigned int startItem = 0;
415 /* For pagination, find the starting point. */
416 if (pgn != MENU_NO_PAGINATION)
418 if (order == ItemOrder_Ascending)
420 startItem = md.lastItem;
421 /* This shouldn't happen with well-coded menus.
422 * If the item is out of bounds, switch the order to
423 * Items_Descending and make us start from the top.
425 if (startItem >= totalItems)
427 startItem = totalItems - 1;
428 order = ItemOrder_Descending;
430 } else if (order == ItemOrder_Descending) {
431 startItem = md.firstItem;
432 /* This shouldn't happen with well-coded menus.
433 * If searching backwards doesn't give us enough room,
434 * start from the beginning and change to ascending.
436 if (startItem <= maxItems)
439 order = ItemOrder_Ascending;
444 /* Get our Display pointer and initialize some crap */
445 IMenuPanel *display = menu->CreatePanel();
446 IMenuHandler *mh = md.mh;
447 bool foundExtra = false;
448 unsigned int extraItem = 0;
451 * We keep searching until:
452 * 1) There are no more items
453 * 2) We reach one OVER the maximum number of slot items
454 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
456 unsigned int i = startItem;
457 unsigned int foundItems = 0;
460 ItemDrawInfo &dr = drawItems[foundItems].draw;
461 /* Is the item valid? */
462 if (menu->GetItemInfo(i, &dr) != NULL)
464 /* Ask the user to change the style, if necessary */
465 mh->OnMenuDrawItem(menu, client, i, dr.style);
466 /* Check if it's renderable */
467 if (IsSlotItem(display, dr.style))
469 /* If we've already found the max number of items,
470 * This means we should just cancel out and log our
473 if (foundItems >= maxItems)
479 drawItems[foundItems++].position = i;
482 /* If there's no pagination, stop once the menu is full. */
483 if (pgn == MENU_NO_PAGINATION)
485 /* If we've filled up, then stop */
486 if (foundItems >= maxItems)
491 /* If we're descending and this is the first item, stop */
492 if (order == ItemOrder_Descending)
500 /* If we're ascending and this is the last item, stop */
501 else if (order == ItemOrder_Ascending)
503 if (i >= totalItems - 1)
511 /* There were no items to draw! */
514 display->DeleteThis();
518 /* Check initial buttons */
519 bool displayPrev = false;
520 bool displayNext = false;
523 if (order == ItemOrder_Descending)
526 md.firstItem = extraItem;
527 } else if (order == ItemOrder_Ascending) {
529 md.lastItem = extraItem;
534 * If we're paginated, we have to find if there is another page.
535 * Sadly, the only way to do this is to try drawing more items!
537 if (pgn != MENU_NO_PAGINATION)
539 unsigned int lastItem = 0;
541 /* Find the last feasible item to search from. */
542 if (order == ItemOrder_Descending)
544 lastItem = drawItems[0].position;
545 if (lastItem >= totalItems - 1)
549 while (++lastItem < totalItems)
551 if (menu->GetItemInfo(lastItem, &dr) != NULL)
553 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
554 if (IsSlotItem(display, dr.style))
557 md.lastItem = lastItem;
562 } else if (order == ItemOrder_Ascending) {
563 lastItem = drawItems[0].position;
569 while (lastItem-- != 0)
571 if (menu->GetItemInfo(lastItem, &dr) != NULL)
573 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
574 if (IsSlotItem(display, dr.style))
577 md.firstItem = lastItem;
586 /* Draw the item according to the order */
587 menu_slots_t *slots = md.slots;
588 unsigned int position = 0; /* Keep track of the last position */
589 if (order == ItemOrder_Ascending)
591 for (unsigned int i=0; i<foundItems; i++)
593 ItemDrawInfo &dr = drawItems[i].draw;
594 menu->GetItemInfo(drawItems[i].position, &dr);
595 mh->OnMenuDisplayItem(menu, client, drawItems[i].position, &(dr.display));
596 if ((position = display->DrawItem(dr)) != 0)
598 slots[position].item = drawItems[i].position;
599 slots[position].type = ItemSel_Item;
602 } else if (order == ItemOrder_Descending) {
603 unsigned int i = foundItems;
604 /* NOTE: There will always be at least one item because
605 * of the check earlier.
609 ItemDrawInfo &dr = drawItems[i].draw;
610 menu->GetItemInfo(drawItems[i].position, &dr);
611 mh->OnMenuDisplayItem(menu, client, drawItems[i].position, &(dr.display));
612 if ((position = display->DrawItem(dr)) != 0)
614 slots[position].item = drawItems[i].position;
615 slots[position].type = ItemSel_Item;
620 /* Now, we need to check if we need to add anything extra */
621 if (pgn != MENU_NO_PAGINATION)
623 bool canDrawDisabled = display->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
624 bool exitButton = menu->GetExitButton();
627 /* Calculate how many items we are allowed for control stuff */
628 unsigned int padding = style->GetMaxPageItems() - maxItems;
630 /* Add the number of available slots */
631 padding += (maxItems - foundItems);
633 /* Someday, if we are able to re-enable this, we will be very lucky men. */
635 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
638 /* Even if we don't draw an exit button, we invalidate the slot. */
642 /* Otherwise, we don't draw anything and leave the slot available */
647 /* Subtract two slots for the displayNext/displayPrev padding */
651 * We allow next/prev to be undrawn if neither exists.
652 * Thus, we only need padding if one of them will be drawn,
653 * or the exit button will be drawn.
655 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
656 if (exitButton || (displayNext || displayPrev))
658 /* If there are no control options,
659 * Instead just pad with invisible slots.
661 if (!displayPrev && !displayPrev)
663 padItem.style = ITEMDRAW_NOTEXT;
665 /* Add spacers so we can pad to the end */
666 for (unsigned int i=0; i<padding; i++)
668 position = display->DrawItem(padItem);
669 slots[position].type = ItemSel_None;
673 /* Put a fake spacer before control stuff, if possible */
674 if ((displayPrev || displayNext) || exitButton)
676 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
677 display->DrawItem(draw);
680 ItemDrawInfo dr(text, 0);
682 * If we have one or the other, we need to have spacers for both.
684 if (displayPrev || displayNext)
687 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
688 if (displayPrev || canDrawDisabled)
690 CorePlayerTranslate(client, text, sizeof(text), "Back", NULL);
691 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
692 position = display->DrawItem(dr);
693 slots[position].type = ItemSel_Back;
694 } else if (displayNext || exitButton) {
695 /* If we can't display this, and there is an exit button,
698 position = display->DrawItem(padCtrlItem);
699 slots[position].type = ItemSel_None;
703 if (displayNext || canDrawDisabled)
705 CorePlayerTranslate(client, text, sizeof(text), "Next", NULL);
706 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
707 position = display->DrawItem(dr);
708 slots[position].type = ItemSel_Next;
709 } else if (exitButton) {
710 /* If we can't display this,
711 * but there is an "exit" button, we need to pad!
713 position = display->DrawItem(padCtrlItem);
714 slots[position].type = ItemSel_None;
717 /* Otherwise, bump to two slots! */
718 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
719 position = display->DrawItem(numBump);
720 slots[position].type = ItemSel_None;
721 position = display->DrawItem(numBump);
722 slots[position].type = ItemSel_None;
728 CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
729 dr.style = ITEMDRAW_CONTROL;
730 position = display->DrawItem(dr);
731 slots[position].type = ItemSel_Exit;
735 /* Lastly, fill in any slots we could have missed */
736 for (unsigned int i = position + 1; i < 10; i++)
738 slots[i].type = ItemSel_None;
742 mh->OnMenuDisplay(menu, client, display);
743 display->DrawTitle(menu->GetDefaultTitle(), true);
748 IMenuStyle *MenuManager::GetDefaultStyle()
750 return m_pDefaultStyle;
753 IVoteMenuHandler *MenuManager::CreateVoteWrapper(IMenuHandler *mh)
755 VoteMenuHandler *vh = NULL;
757 if (m_VoteHandlers.empty())
759 vh = new VoteMenuHandler;
761 vh = m_VoteHandlers.front();
762 m_VoteHandlers.pop();
770 void MenuManager::ReleaseVoteWrapper(IVoteMenuHandler *mh)
777 m_VoteHandlers.push((VoteMenuHandler *)mh);