3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
35 #include "MenuManager.h"
36 #include "MenuVoting.h"
37 #include "sm_stringutil.h"
38 #include "sourcemm_api.h"
39 #include "PlayerManager.h"
40 #include "MenuStyle_Valve.h"
42 #include "HandleSys.h"
43 #include "sourcemm_api.h"
44 #include "logic_bridge.h"
47 VoteMenuHandler s_VoteHandler;
49 ConVar sm_menu_sounds("sm_menu_sounds", "1", 0, "Sets whether SourceMod menus play trigger sounds");
51 MenuManager::MenuManager()
53 m_Styles.push_back(&g_ValveMenuStyle);
54 SetDefaultStyle(&g_ValveMenuStyle);
57 void MenuManager::OnSourceModAllInitialized()
59 g_ShareSys.AddInterface(NULL, this);
62 g_HandleSys.InitAccessDefaults(NULL, &access);
64 /* Deny cloning to menus */
65 access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
66 m_MenuType = g_HandleSys.CreateType("IBaseMenu", this, 0, NULL, &access, g_pCoreIdent, NULL);
68 /* Also deny deletion to styles */
69 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
70 m_StyleType = g_HandleSys.CreateType("IMenuStyle", this, 0, NULL, &access, g_pCoreIdent, NULL);
73 void MenuManager::OnSourceModAllShutdown()
75 g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
76 g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
79 void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
81 if (type == m_MenuType)
83 IBaseMenu *menu = (IBaseMenu *)object;
86 else if (type == m_StyleType)
92 bool MenuManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
94 if (type == m_MenuType)
96 *pSize = ((IBaseMenu *)object)->GetApproxMemUsage();
101 *pSize = ((IMenuStyle *)object)->GetApproxMemUsage();
108 Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
110 if (m_MenuType == NO_HANDLE_TYPE)
115 return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
118 Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
120 if (m_StyleType == NO_HANDLE_TYPE)
125 return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
128 HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
132 sec.pIdentity = g_pCoreIdent;
135 return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
138 HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
142 sec.pIdentity = g_pCoreIdent;
143 sec.pOwner = g_pCoreIdent;
145 return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
148 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
155 m_pDefaultStyle = style;
160 IMenuStyle *MenuManager::GetStyle(unsigned int index)
162 if (index >= GetStyleCount())
167 return m_Styles[index];
170 void MenuManager::AddStyle(IMenuStyle *style)
172 m_Styles.push_back(style);
175 unsigned int MenuManager::GetStyleCount()
177 return (unsigned int)m_Styles.size();
180 IMenuStyle *MenuManager::FindStyleByName(const char *name)
182 unsigned int count = GetStyleCount();
183 for (unsigned int i=0; i<count; i++)
185 IMenuStyle *ptr = GetStyle(i);
186 if (strcasecmp(ptr->GetStyleName(), name) == 0)
195 inline bool IsSlotItem(IMenuPanel *display,
198 if (!display->CanDrawItem(style))
202 if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
206 if (style & ITEMDRAW_RAWLINE)
214 IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
216 IBaseMenu *menu = md.menu;
225 unsigned int position;
229 /* Figure out how many items to draw */
230 IMenuStyle *style = menu->GetDrawStyle();
231 unsigned int pgn = menu->GetPagination();
232 unsigned int maxItems = style->GetMaxPageItems();
233 bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT;
234 bool novoteButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_NOVOTE) == MENUFLAG_BUTTON_NOVOTE;
236 if (pgn != MENU_NO_PAGINATION)
250 /* This is very not allowed! */
256 unsigned int totalItems = menu->GetItemCount();
257 unsigned int startItem = 0;
259 /* For pagination, find the starting point. */
260 if (pgn != MENU_NO_PAGINATION)
262 if (order == ItemOrder_Ascending)
264 startItem = md.lastItem;
265 /* This shouldn't happen with well-coded menus.
266 * If the item is out of bounds, switch the order to
267 * Items_Descending and make us start from the top.
269 if (startItem >= totalItems)
271 startItem = totalItems - 1;
272 order = ItemOrder_Descending;
275 else if (order == ItemOrder_Descending)
277 startItem = md.firstItem;
278 /* This shouldn't happen with well-coded menus.
279 * If searching backwards doesn't give us enough room,
280 * start from the beginning and change to ascending.
282 if (startItem <= maxItems)
285 order = ItemOrder_Ascending;
290 /* Get our Display pointer and initialize some crap */
291 IMenuPanel *panel = menu->CreatePanel();
292 IMenuHandler *mh = md.mh;
293 bool foundExtra = false;
294 unsigned int extraItem = 0;
302 * We keep searching until:
303 * 1) There are no more items
304 * 2) We reach one OVER the maximum number of slot items
305 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
307 unsigned int i = startItem;
308 unsigned int foundItems = 0;
311 ItemDrawInfo &dr = drawItems[foundItems].draw;
312 /* Is the item valid? */
313 if (menu->GetItemInfo(i, &dr) != NULL)
315 /* Ask the user to change the style, if necessary */
316 mh->OnMenuDrawItem(menu, client, i, dr.style);
317 /* Check if it's renderable */
318 if (IsSlotItem(panel, dr.style))
320 /* If we've already found the max number of items,
321 * This means we should just cancel out and log our
324 if (foundItems >= maxItems)
330 drawItems[foundItems++].position = i;
333 /* If there's no pagination, stop once the menu is full. */
334 if (pgn == MENU_NO_PAGINATION)
336 /* If we've filled up, then stop */
337 if (foundItems >= maxItems)
342 /* If we're descending and this is the first item, stop */
343 if (order == ItemOrder_Descending)
351 /* If we're ascending and this is the last item, stop */
352 else if (order == ItemOrder_Ascending)
354 if (i >= totalItems - 1)
362 /* There were no items to draw! */
369 bool displayPrev = false;
370 bool displayNext = false;
372 /* This is an annoying process.
373 * Skip it for non-paginated menus, which get special treatment.
375 if (pgn != MENU_NO_PAGINATION)
379 if (order == ItemOrder_Descending)
382 md.firstItem = extraItem;
384 else if (order == ItemOrder_Ascending)
387 md.lastItem = extraItem;
391 unsigned int lastItem = 0;
393 /* Find the last feasible item to search from. */
394 if (order == ItemOrder_Descending)
396 lastItem = drawItems[0].position;
397 if (lastItem >= totalItems - 1)
401 while (++lastItem < totalItems)
403 if (menu->GetItemInfo(lastItem, &dr) != NULL)
405 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
406 if (IsSlotItem(panel, dr.style))
409 md.lastItem = lastItem;
415 else if (order == ItemOrder_Ascending)
417 lastItem = drawItems[0].position;
423 while (lastItem != 0)
425 if (menu->GetItemInfo(lastItem, &dr) != NULL)
427 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
428 if (IsSlotItem(panel, dr.style))
431 md.firstItem = lastItem;
442 /* Draw the item according to the order */
443 menu_slots_t *slots = md.slots;
444 unsigned int position = 0; /* Keep track of the last position */
449 if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "No Vote", &client))
451 UTIL_Format(text, sizeof(text), "No Vote");
453 ItemDrawInfo dr(text, 0);
454 position = panel->DrawItem(dr);
455 slots[position].type = ItemSel_Exit;
459 if (order == ItemOrder_Ascending)
461 md.item_on_page = drawItems[0].position;
462 for (unsigned int i = 0; i < foundItems; i++)
464 ItemDrawInfo &dr = drawItems[i].draw;
465 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
467 position = panel->DrawItem(dr);
471 slots[position].item = drawItems[i].position;
472 if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
474 slots[position].type = ItemSel_None;
478 slots[position].type = ItemSel_Item;
483 else if (order == ItemOrder_Descending)
485 unsigned int i = foundItems;
486 /* NOTE: There will always be at least one item because
487 * of the check earlier.
489 md.item_on_page = drawItems[foundItems - 1].position;
492 ItemDrawInfo &dr = drawItems[i].draw;
493 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
495 position = panel->DrawItem(dr);
499 slots[position].item = drawItems[i].position;
500 if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
502 slots[position].type = ItemSel_None;
506 slots[position].type = ItemSel_Item;
512 /* Now, we need to check if we need to add anything extra */
513 if (pgn != MENU_NO_PAGINATION || exitButton)
515 bool canDrawDisabled = panel->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
516 bool exitBackButton = false;
519 if (pgn != MENU_NO_PAGINATION
520 && (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK)
522 exitBackButton = true;
525 /* Calculate how many items we are allowed for control stuff */
526 unsigned int padding = style->GetMaxPageItems() - maxItems;
528 /* Add the number of available slots */
529 padding += (maxItems - foundItems);
531 /* Someday, if we are able to re-enable this, we will be very lucky men. */
533 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
536 /* Even if we don't draw an exit button, we invalidate the slot. */
540 /* Otherwise, we don't draw anything and leave the slot available */
545 if (pgn != MENU_NO_PAGINATION)
547 /* Subtract two slots for the displayNext/displayPrev padding */
551 /* If we have an "Exit Back" button and the space to draw it, do so. */
560 exitBackButton = false;
565 * We allow next/prev to be undrawn if neither exists.
566 * Thus, we only need padding if one of them will be drawn,
567 * or the exit button will be drawn.
569 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
570 if (exitButton || (displayNext || displayPrev))
572 /* If there are no control options,
573 * Instead just pad with invisible slots.
575 if (!displayPrev && !displayPrev)
577 padItem.style = ITEMDRAW_NOTEXT;
579 /* Add spacers so we can pad to the end */
580 for (unsigned int i=0; i<padding; i++)
582 position = panel->DrawItem(padItem);
583 slots[position].type = ItemSel_None;
587 /* Put a fake spacer before control stuff, if possible */
588 if ((displayPrev || displayNext) || exitButton)
590 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
591 panel->DrawItem(draw);
594 ItemDrawInfo dr(text, 0);
597 * If we have one or the other, we need to have spacers for both.
599 if (pgn != MENU_NO_PAGINATION)
601 if (displayPrev || displayNext)
604 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
605 if (displayPrev || canDrawDisabled)
609 if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Back", &client))
611 UTIL_Format(text, sizeof(text), "Back");
613 dr.style = ITEMDRAW_CONTROL;
614 position = panel->DrawItem(dr);
615 slots[position].type = ItemSel_ExitBack;
619 if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Previous", &client))
621 UTIL_Format(text, sizeof(text), "Previous");
623 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
624 position = panel->DrawItem(dr);
625 slots[position].type = ItemSel_Back;
628 else if (displayNext || exitButton)
630 /* If we can't display this, and there is an exit button,
633 position = panel->DrawItem(padCtrlItem);
634 slots[position].type = ItemSel_None;
638 if (displayNext || canDrawDisabled)
640 if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Next", &client))
642 UTIL_Format(text, sizeof(text), "Next");
644 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
645 position = panel->DrawItem(dr);
646 slots[position].type = ItemSel_Next;
650 /* If we can't display this,
651 * but there is an "exit" button, we need to pad!
653 position = panel->DrawItem(padCtrlItem);
654 slots[position].type = ItemSel_None;
659 /* Otherwise, bump to two slots! */
660 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
661 position = panel->DrawItem(numBump);
662 slots[position].type = ItemSel_None;
663 position = panel->DrawItem(numBump);
664 slots[position].type = ItemSel_None;
671 if (!logicore.CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Exit", &client))
673 UTIL_Format(text, sizeof(text), "Exit");
675 dr.style = ITEMDRAW_CONTROL;
676 position = panel->DrawItem(dr);
677 slots[position].type = ItemSel_Exit;
681 /* Lastly, fill in any slots we could have missed */
682 for (unsigned int i = position + 1; i < 10; i++)
684 slots[i].type = ItemSel_None;
688 mh->OnMenuDisplay(menu, client, panel);
689 panel->DrawTitle(menu->GetDefaultTitle(), true);
694 IMenuStyle *MenuManager::GetDefaultStyle()
696 return m_pDefaultStyle;
699 bool MenuManager::MenuSoundsEnabled()
701 return (sm_menu_sounds.GetInt() != 0);
704 ConfigResult MenuManager::OnSourceModConfigChanged(const char *key,
710 if (strcmp(key, "MenuItemSound") == 0)
712 m_SelectSound.assign(value);
713 return ConfigResult_Accept;
714 } else if (strcmp(key, "MenuExitBackSound") == 0) {
715 m_ExitBackSound.assign(value);
716 return ConfigResult_Accept;
717 } else if (strcmp(key, "MenuExitSound") == 0) {
718 m_ExitSound.assign(value);
719 return ConfigResult_Accept;
722 return ConfigResult_Ignore;
725 const char *MenuManager::GetMenuSound(ItemSelection sel)
727 const char *sound = NULL;
735 if (m_SelectSound.size() > 0)
737 sound = m_SelectSound.c_str();
741 case ItemSel_ExitBack:
743 if (m_ExitBackSound.size() > 0)
745 sound = m_ExitBackSound.c_str();
751 if (m_ExitSound.size() > 0)
753 sound = m_ExitSound.c_str();
766 void MenuManager::OnSourceModLevelChange(const char *mapName)
768 if (m_SelectSound.size() > 0)
770 enginesound->PrecacheSound(m_SelectSound.c_str(), true);
772 if (m_ExitBackSound.size() > 0)
774 enginesound->PrecacheSound(m_ExitBackSound.c_str(), true);
776 if (m_ExitSound.size() > 0)
778 enginesound->PrecacheSound(m_ExitSound.c_str(), true);
782 void MenuManager::CancelMenu(IBaseMenu *menu)
784 if (s_VoteHandler.GetCurrentMenu() == menu
785 && !s_VoteHandler.IsCancelling())
787 s_VoteHandler.CancelVoting();
794 bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
796 return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
799 bool MenuManager::IsVoteInProgress()
801 return s_VoteHandler.IsVoteInProgress();
804 void MenuManager::CancelVoting()
806 s_VoteHandler.CancelVoting();
809 unsigned int MenuManager::GetRemainingVoteDelay()
811 return s_VoteHandler.GetRemainingVoteDelay();
814 bool MenuManager::IsClientInVotePool(int client)
816 return s_VoteHandler.IsClientInVotePool(client);
819 bool MenuManager::RedrawClientVoteMenu(int client)
821 return RedrawClientVoteMenu2(client, true);
824 bool MenuManager::RedrawClientVoteMenu2(int client, bool revote)
826 return s_VoteHandler.RedrawToClient(client, revote);