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()
98 unsigned int chosen = 0;
99 unsigned int highest = 0;
100 unsigned int dup_count = 0;
102 /* If we got zero votes, take a shortcut. */
105 /* Pick a random item and then jump far, far away. */
106 srand((unsigned int)(time(NULL)));
107 chosen = (unsigned int)rand() % static_cast<unsigned int>(m_Votes.size());
111 /* We can't have more dups than this!
112 * This is the max number of players.
114 unsigned int dup_array[256];
116 for (size_t i=1; i<m_Items; i++)
118 if (m_Votes[i] > m_Votes[highest])
120 /* If we have a new highest count, mark it and trash the duplicate
121 * list by setting the total to 0.
125 } else if (m_Votes[i] == m_Votes[highest]) {
126 /* If they're equal, mark it in the duplicate list.
127 * We'll add in the original later.
129 dup_array[dup_count++] = i;
133 /* Check if we need to pick from the duplicate list */
136 /* Re-add the original to the list because it's not in there. */
137 dup_array[dup_count++] = highest;
139 /* Pick a random slot. */
140 srand((unsigned int)(time(NULL)));
141 unsigned int r = (unsigned int)rand() % dup_count;
144 chosen = dup_array[r];
150 m_pHandler->OnMenuVoteEnd(m_pCurMenu, chosen);
151 m_pHandler->OnMenuEnd(m_pCurMenu);
155 void VoteMenuHandler::OnMenuStart(IBaseMenu *menu)
160 void VoteMenuHandler::OnMenuEnd(IBaseMenu *menu)
162 DecrementPlayerCount();
165 void VoteMenuHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
167 m_pHandler->OnMenuCancel(menu, client, reason);
170 void VoteMenuHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
172 m_pHandler->OnMenuDisplay(menu, client, display);
175 void VoteMenuHandler::OnMenuDisplayItem(IBaseMenu *menu, int client, unsigned int item, const char **display)
177 m_pHandler->OnMenuDisplayItem(menu, client, item, display);
180 void VoteMenuHandler::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
182 m_pHandler->OnMenuDrawItem(menu, client, item, style);
185 void VoteMenuHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
187 /* Check by our item count, NOT the vote array size */
194 m_pHandler->OnMenuSelect(menu, client, item);
197 void VoteMenuHandler::Reset(IMenuHandler *mh)
203 void VoteMenuHandler::InternalReset()
212 /*******************************
213 *******************************
214 ******** MENU MANAGER *********
215 *******************************
216 *******************************/
218 MenuManager::MenuManager()
220 m_Styles.push_back(&g_ValveMenuStyle);
221 SetDefaultStyle(&g_ValveMenuStyle);
224 void MenuManager::OnSourceModAllInitialized()
226 g_ShareSys.AddInterface(NULL, this);
229 g_HandleSys.InitAccessDefaults(NULL, &access);
231 /* Deny cloning to menus */
232 access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
233 m_MenuType = g_HandleSys.CreateType("IBaseMenu", this, 0, NULL, &access, g_pCoreIdent, NULL);
235 /* Also deny deletion to styles */
236 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
237 m_StyleType = g_HandleSys.CreateType("IMenuStyle", this, 0, NULL, &access, g_pCoreIdent, NULL);
240 void MenuManager::OnSourceModAllShutdown()
242 g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
243 g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
245 while (!m_VoteHandlers.empty())
247 delete m_VoteHandlers.front();
248 m_VoteHandlers.pop();
252 void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
254 if (type == m_MenuType)
256 IBaseMenu *menu = (IBaseMenu *)object;
257 menu->Destroy(false);
258 } else if (type == m_StyleType) {
263 Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
265 if (m_MenuType == NO_HANDLE_TYPE)
270 return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
273 Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
275 if (m_StyleType == NO_HANDLE_TYPE)
280 return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
283 HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
287 sec.pIdentity = g_pCoreIdent;
290 return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
293 HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
297 sec.pIdentity = g_pCoreIdent;
298 sec.pOwner = g_pCoreIdent;
300 return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
303 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
310 m_pDefaultStyle = style;
315 IMenuStyle *MenuManager::GetStyle(unsigned int index)
317 if (index >= GetStyleCount())
322 return m_Styles[index];
325 void MenuManager::AddStyle(IMenuStyle *style)
327 m_Styles.push_back(style);
330 unsigned int MenuManager::GetStyleCount()
332 return (unsigned int)m_Styles.size();
335 IMenuStyle *MenuManager::FindStyleByName(const char *name)
337 unsigned int count = GetStyleCount();
338 for (unsigned int i=0; i<count; i++)
340 IMenuStyle *ptr = GetStyle(i);
341 if (strcasecmp(ptr->GetStyleName(), name) == 0)
350 inline bool IsSlotItem(IMenuPanel *display,
353 if (!display->CanDrawItem(style))
357 if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
361 if (style & ITEMDRAW_RAWLINE)
369 IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
371 IBaseMenu *menu = md.menu;
380 unsigned int position;
384 /* Figure out how many items to draw */
385 IMenuStyle *style = menu->GetDrawStyle();
386 unsigned int pgn = menu->GetPagination();
387 unsigned int maxItems = style->GetMaxPageItems();
388 if (pgn != MENU_NO_PAGINATION)
393 unsigned int totalItems = menu->GetItemCount();
394 unsigned int startItem = 0;
396 /* For pagination, find the starting point. */
397 if (pgn != MENU_NO_PAGINATION)
399 if (order == ItemOrder_Ascending)
401 startItem = md.lastItem;
402 /* This shouldn't happen with well-coded menus.
403 * If the item is out of bounds, switch the order to
404 * Items_Descending and make us start from the top.
406 if (startItem >= totalItems)
408 startItem = totalItems - 1;
409 order = ItemOrder_Descending;
411 } else if (order == ItemOrder_Descending) {
412 startItem = md.firstItem;
413 /* This shouldn't happen with well-coded menus.
414 * If searching backwards doesn't give us enough room,
415 * start from the beginning and change to ascending.
417 if (startItem <= maxItems)
420 order = ItemOrder_Ascending;
425 /* Get our Display pointer and initialize some crap */
426 IMenuPanel *display = menu->CreatePanel();
427 IMenuHandler *mh = md.mh;
428 bool foundExtra = false;
429 unsigned int extraItem = 0;
432 * We keep searching until:
433 * 1) There are no more items
434 * 2) We reach one OVER the maximum number of slot items
435 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
437 unsigned int i = startItem;
438 unsigned int foundItems = 0;
441 ItemDrawInfo &dr = drawItems[foundItems].draw;
442 /* Is the item valid? */
443 if (menu->GetItemInfo(i, &dr) != NULL)
445 /* Ask the user to change the style, if necessary */
446 mh->OnMenuDrawItem(menu, client, i, dr.style);
447 /* Check if it's renderable */
448 if (IsSlotItem(display, dr.style))
450 /* If we've already found the max number of items,
451 * This means we should just cancel out and log our
454 if (foundItems >= maxItems)
460 drawItems[foundItems++].position = i;
463 /* If there's no pagination, stop once the menu is full. */
464 if (pgn == MENU_NO_PAGINATION)
466 /* If we've filled up, then stop */
467 if (foundItems >= maxItems)
472 /* If we're descending and this is the first item, stop */
473 if (order == ItemOrder_Descending)
481 /* If we're ascending and this is the last item, stop */
482 else if (order == ItemOrder_Ascending)
484 if (i >= totalItems - 1)
492 /* There were no items to draw! */
495 display->DeleteThis();
499 /* Check initial buttons */
500 bool displayPrev = false;
501 bool displayNext = false;
504 if (order == ItemOrder_Descending)
507 md.firstItem = extraItem;
508 } else if (order == ItemOrder_Ascending) {
510 md.lastItem = extraItem;
515 * If we're paginated, we have to find if there is another page.
516 * Sadly, the only way to do this is to try drawing more items!
518 if (pgn != MENU_NO_PAGINATION)
520 unsigned int lastItem = 0;
522 /* Find the last feasible item to search from. */
523 if (order == ItemOrder_Descending)
525 lastItem = drawItems[0].position;
526 if (lastItem >= totalItems - 1)
530 while (++lastItem < totalItems)
532 if (menu->GetItemInfo(lastItem, &dr) != NULL)
534 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
535 if (IsSlotItem(display, dr.style))
538 md.lastItem = lastItem;
543 } else if (order == ItemOrder_Ascending) {
544 lastItem = drawItems[0].position;
550 while (lastItem-- != 0)
552 if (menu->GetItemInfo(lastItem, &dr) != NULL)
554 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
555 if (IsSlotItem(display, dr.style))
558 md.firstItem = lastItem;
567 /* Draw the item according to the order */
568 menu_slots_t *slots = md.slots;
569 unsigned int position = 0; /* Keep track of the last position */
570 if (order == ItemOrder_Ascending)
572 for (unsigned int i=0; i<foundItems; i++)
574 ItemDrawInfo &dr = drawItems[i].draw;
575 menu->GetItemInfo(drawItems[i].position, &dr);
576 mh->OnMenuDisplayItem(menu, client, drawItems[i].position, &(dr.display));
577 if ((position = display->DrawItem(dr)) != 0)
579 slots[position].item = drawItems[i].position;
580 slots[position].type = ItemSel_Item;
583 } else if (order == ItemOrder_Descending) {
584 unsigned int i = foundItems;
585 /* NOTE: There will always be at least one item because
586 * of the check earlier.
590 ItemDrawInfo &dr = drawItems[i].draw;
591 menu->GetItemInfo(drawItems[i].position, &dr);
592 mh->OnMenuDisplayItem(menu, client, drawItems[i].position, &(dr.display));
593 if ((position = display->DrawItem(dr)) != 0)
595 slots[position].item = drawItems[i].position;
596 slots[position].type = ItemSel_Item;
601 /* Now, we need to check if we need to add anything extra */
602 if (pgn != MENU_NO_PAGINATION)
604 bool canDrawDisabled = display->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
605 bool exitButton = menu->GetExitButton();
608 /* Calculate how many items we are allowed for control stuff */
609 unsigned int padding = style->GetMaxPageItems() - maxItems;
611 /* Add the number of available slots */
612 padding += (maxItems - foundItems);
614 /* Someday, if we are able to re-enable this, we will be very lucky men. */
616 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
619 /* Even if we don't draw an exit button, we invalidate the slot. */
623 /* Otherwise, we don't draw anything and leave the slot available */
628 /* Subtract two slots for the displayNext/displayPrev padding */
632 * We allow next/prev to be undrawn if neither exists.
633 * Thus, we only need padding if one of them will be drawn,
634 * or the exit button will be drawn.
636 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
637 if (exitButton || (displayNext || displayPrev))
639 /* If there are no control options,
640 * Instead just pad with invisible slots.
642 if (!displayPrev && !displayPrev)
644 padItem.style = ITEMDRAW_NOTEXT;
646 /* Add spacers so we can pad to the end */
647 for (unsigned int i=0; i<padding; i++)
649 position = display->DrawItem(padItem);
650 slots[position].type = ItemSel_None;
654 /* Put a fake spacer before control stuff, if possible */
655 if ((displayPrev || displayNext) || exitButton)
657 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
658 display->DrawItem(draw);
661 ItemDrawInfo dr(text, 0);
663 * If we have one or the other, we need to have spacers for both.
665 if (displayPrev || displayNext)
668 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
669 if (displayPrev || canDrawDisabled)
671 CorePlayerTranslate(client, text, sizeof(text), "Back", NULL);
672 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
673 position = display->DrawItem(dr);
674 slots[position].type = ItemSel_Back;
675 } else if (displayNext || exitButton) {
676 /* If we can't display this, and there is an exit button,
679 position = display->DrawItem(padCtrlItem);
680 slots[position].type = ItemSel_None;
684 if (displayNext || canDrawDisabled)
686 CorePlayerTranslate(client, text, sizeof(text), "Next", NULL);
687 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
688 position = display->DrawItem(dr);
689 slots[position].type = ItemSel_Next;
690 } else if (exitButton) {
691 /* If we can't display this,
692 * but there is an "exit" button, we need to pad!
694 position = display->DrawItem(padCtrlItem);
695 slots[position].type = ItemSel_None;
698 /* Otherwise, bump to two slots! */
699 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
700 position = display->DrawItem(numBump);
701 slots[position].type = ItemSel_None;
702 position = display->DrawItem(numBump);
703 slots[position].type = ItemSel_None;
709 CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
710 dr.style = ITEMDRAW_CONTROL;
711 position = display->DrawItem(dr);
712 slots[position].type = ItemSel_Exit;
716 /* Lastly, fill in any slots we could have missed */
717 for (unsigned int i = position + 1; i < 10; i++)
719 slots[i].type = ItemSel_None;
723 mh->OnMenuDisplay(menu, client, display);
724 display->DrawTitle(menu->GetDefaultTitle(), true);
729 IMenuStyle *MenuManager::GetDefaultStyle()
731 return m_pDefaultStyle;
734 IVoteMenuHandler *MenuManager::CreateVoteWrapper(IMenuHandler *mh)
736 VoteMenuHandler *vh = NULL;
738 if (m_VoteHandlers.empty())
740 vh = new VoteMenuHandler;
742 vh = m_VoteHandlers.front();
743 m_VoteHandlers.pop();
751 void MenuManager::ReleaseVoteWrapper(IVoteMenuHandler *mh)
758 m_VoteHandlers.push((VoteMenuHandler *)mh);