3 * =============================================================================
5 * Copyright (C) 2004-2007 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"
46 VoteMenuHandler s_VoteHandler;
48 ConVar sm_menu_sounds("sm_menu_sounds", "1", 0, "Sets whether SourceMod menus play trigger sounds");
50 MenuManager::MenuManager()
52 m_Styles.push_back(&g_ValveMenuStyle);
53 SetDefaultStyle(&g_ValveMenuStyle);
56 void MenuManager::OnSourceModAllInitialized()
58 g_ShareSys.AddInterface(NULL, this);
61 g_HandleSys.InitAccessDefaults(NULL, &access);
63 /* Deny cloning to menus */
64 access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
65 m_MenuType = g_HandleSys.CreateType("IBaseMenu", this, 0, NULL, &access, g_pCoreIdent, NULL);
67 /* Also deny deletion to styles */
68 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
69 m_StyleType = g_HandleSys.CreateType("IMenuStyle", this, 0, NULL, &access, g_pCoreIdent, NULL);
72 void MenuManager::OnSourceModAllShutdown()
74 g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
75 g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
78 void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
80 if (type == m_MenuType)
82 IBaseMenu *menu = (IBaseMenu *)object;
84 } else if (type == m_StyleType) {
89 Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
91 if (m_MenuType == NO_HANDLE_TYPE)
96 return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
99 Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
101 if (m_StyleType == NO_HANDLE_TYPE)
106 return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
109 HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
113 sec.pIdentity = g_pCoreIdent;
116 return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
119 HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
123 sec.pIdentity = g_pCoreIdent;
124 sec.pOwner = g_pCoreIdent;
126 return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
129 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
136 m_pDefaultStyle = style;
141 IMenuStyle *MenuManager::GetStyle(unsigned int index)
143 if (index >= GetStyleCount())
148 return m_Styles[index];
151 void MenuManager::AddStyle(IMenuStyle *style)
153 m_Styles.push_back(style);
156 unsigned int MenuManager::GetStyleCount()
158 return (unsigned int)m_Styles.size();
161 IMenuStyle *MenuManager::FindStyleByName(const char *name)
163 unsigned int count = GetStyleCount();
164 for (unsigned int i=0; i<count; i++)
166 IMenuStyle *ptr = GetStyle(i);
167 if (strcasecmp(ptr->GetStyleName(), name) == 0)
176 inline bool IsSlotItem(IMenuPanel *display,
179 if (!display->CanDrawItem(style))
183 if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
187 if (style & ITEMDRAW_RAWLINE)
195 IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
197 IBaseMenu *menu = md.menu;
206 unsigned int position;
210 /* Figure out how many items to draw */
211 IMenuStyle *style = menu->GetDrawStyle();
212 unsigned int pgn = menu->GetPagination();
213 unsigned int maxItems = style->GetMaxPageItems();
214 bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT;
216 if (pgn != MENU_NO_PAGINATION)
225 /* This is very not allowed! */
231 unsigned int totalItems = menu->GetItemCount();
232 unsigned int startItem = 0;
234 /* For pagination, find the starting point. */
235 if (pgn != MENU_NO_PAGINATION)
237 if (order == ItemOrder_Ascending)
239 startItem = md.lastItem;
240 /* This shouldn't happen with well-coded menus.
241 * If the item is out of bounds, switch the order to
242 * Items_Descending and make us start from the top.
244 if (startItem >= totalItems)
246 startItem = totalItems - 1;
247 order = ItemOrder_Descending;
250 else if (order == ItemOrder_Descending)
252 startItem = md.firstItem;
253 /* This shouldn't happen with well-coded menus.
254 * If searching backwards doesn't give us enough room,
255 * start from the beginning and change to ascending.
257 if (startItem <= maxItems)
260 order = ItemOrder_Ascending;
265 /* Get our Display pointer and initialize some crap */
266 IMenuPanel *panel = menu->CreatePanel();
267 IMenuHandler *mh = md.mh;
268 bool foundExtra = false;
269 unsigned int extraItem = 0;
277 * We keep searching until:
278 * 1) There are no more items
279 * 2) We reach one OVER the maximum number of slot items
280 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
282 unsigned int i = startItem;
283 unsigned int foundItems = 0;
286 ItemDrawInfo &dr = drawItems[foundItems].draw;
287 /* Is the item valid? */
288 if (menu->GetItemInfo(i, &dr) != NULL)
290 /* Ask the user to change the style, if necessary */
291 mh->OnMenuDrawItem(menu, client, i, dr.style);
292 /* Check if it's renderable */
293 if (IsSlotItem(panel, dr.style))
295 /* If we've already found the max number of items,
296 * This means we should just cancel out and log our
299 if (foundItems >= maxItems)
305 drawItems[foundItems++].position = i;
308 /* If there's no pagination, stop once the menu is full. */
309 if (pgn == MENU_NO_PAGINATION)
311 /* If we've filled up, then stop */
312 if (foundItems >= maxItems)
317 /* If we're descending and this is the first item, stop */
318 if (order == ItemOrder_Descending)
326 /* If we're ascending and this is the last item, stop */
327 else if (order == ItemOrder_Ascending)
329 if (i >= totalItems - 1)
337 /* There were no items to draw! */
344 bool displayPrev = false;
345 bool displayNext = false;
347 /* This is an annoying process.
348 * Skip it for non-paginated menus, which get special treatment.
350 if (pgn != MENU_NO_PAGINATION)
354 if (order == ItemOrder_Descending)
357 md.firstItem = extraItem;
359 else if (order == ItemOrder_Ascending)
362 md.lastItem = extraItem;
366 unsigned int lastItem = 0;
368 /* Find the last feasible item to search from. */
369 if (order == ItemOrder_Descending)
371 lastItem = drawItems[0].position;
372 if (lastItem >= totalItems - 1)
376 while (++lastItem < totalItems)
378 if (menu->GetItemInfo(lastItem, &dr) != NULL)
380 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
381 if (IsSlotItem(panel, dr.style))
384 md.lastItem = lastItem;
390 else if (order == ItemOrder_Ascending)
392 lastItem = drawItems[0].position;
398 while (lastItem != 0)
400 if (menu->GetItemInfo(lastItem, &dr) != NULL)
402 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
403 if (IsSlotItem(panel, dr.style))
406 md.firstItem = lastItem;
417 /* Draw the item according to the order */
418 menu_slots_t *slots = md.slots;
419 unsigned int position = 0; /* Keep track of the last position */
420 if (order == ItemOrder_Ascending)
422 md.item_on_page = drawItems[0].position;
423 for (unsigned int i = 0; i < foundItems; i++)
425 ItemDrawInfo &dr = drawItems[i].draw;
426 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
428 position = panel->DrawItem(dr);
432 slots[position].item = drawItems[i].position;
433 slots[position].type = ItemSel_Item;
437 else if (order == ItemOrder_Descending)
439 unsigned int i = foundItems;
440 /* NOTE: There will always be at least one item because
441 * of the check earlier.
443 md.item_on_page = drawItems[foundItems - 1].position;
446 ItemDrawInfo &dr = drawItems[i].draw;
447 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
449 position = panel->DrawItem(dr);
453 slots[position].item = drawItems[i].position;
454 slots[position].type = ItemSel_Item;
459 /* Now, we need to check if we need to add anything extra */
460 if (pgn != MENU_NO_PAGINATION || exitButton)
462 bool canDrawDisabled = panel->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
463 bool exitBackButton = false;
466 if (pgn != MENU_NO_PAGINATION
467 && (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK)
469 exitBackButton = true;
472 /* Calculate how many items we are allowed for control stuff */
473 unsigned int padding = style->GetMaxPageItems() - maxItems;
475 /* Add the number of available slots */
476 padding += (maxItems - foundItems);
478 /* Someday, if we are able to re-enable this, we will be very lucky men. */
480 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
483 /* Even if we don't draw an exit button, we invalidate the slot. */
487 /* Otherwise, we don't draw anything and leave the slot available */
492 if (pgn != MENU_NO_PAGINATION)
494 /* Subtract two slots for the displayNext/displayPrev padding */
498 /* If we have an "Exit Back" button and the space to draw it, do so. */
507 exitBackButton = false;
512 * We allow next/prev to be undrawn if neither exists.
513 * Thus, we only need padding if one of them will be drawn,
514 * or the exit button will be drawn.
516 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
517 if (exitButton || (displayNext || displayPrev))
519 /* If there are no control options,
520 * Instead just pad with invisible slots.
522 if (!displayPrev && !displayPrev)
524 padItem.style = ITEMDRAW_NOTEXT;
526 /* Add spacers so we can pad to the end */
527 for (unsigned int i=0; i<padding; i++)
529 position = panel->DrawItem(padItem);
530 slots[position].type = ItemSel_None;
534 /* Put a fake spacer before control stuff, if possible */
535 if ((displayPrev || displayNext) || exitButton)
537 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
538 panel->DrawItem(draw);
541 ItemDrawInfo dr(text, 0);
544 * If we have one or the other, we need to have spacers for both.
546 if (pgn != MENU_NO_PAGINATION)
548 if (displayPrev || displayNext)
551 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
552 if (displayPrev || canDrawDisabled)
556 CorePlayerTranslate(client, text, sizeof(text), "Back", NULL);
557 dr.style = ITEMDRAW_CONTROL;
558 position = panel->DrawItem(dr);
559 slots[position].type = ItemSel_ExitBack;
563 CorePlayerTranslate(client, text, sizeof(text), "Previous", NULL);
564 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
565 position = panel->DrawItem(dr);
566 slots[position].type = ItemSel_Back;
569 else if (displayNext || exitButton)
571 /* If we can't display this, and there is an exit button,
574 position = panel->DrawItem(padCtrlItem);
575 slots[position].type = ItemSel_None;
579 if (displayNext || canDrawDisabled)
581 CorePlayerTranslate(client, text, sizeof(text), "Next", NULL);
582 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
583 position = panel->DrawItem(dr);
584 slots[position].type = ItemSel_Next;
588 /* If we can't display this,
589 * but there is an "exit" button, we need to pad!
591 position = panel->DrawItem(padCtrlItem);
592 slots[position].type = ItemSel_None;
597 /* Otherwise, bump to two slots! */
598 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
599 position = panel->DrawItem(numBump);
600 slots[position].type = ItemSel_None;
601 position = panel->DrawItem(numBump);
602 slots[position].type = ItemSel_None;
609 CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
610 dr.style = ITEMDRAW_CONTROL;
611 position = panel->DrawItem(dr);
612 slots[position].type = ItemSel_Exit;
616 /* Lastly, fill in any slots we could have missed */
617 for (unsigned int i = position + 1; i < 10; i++)
619 slots[i].type = ItemSel_None;
623 mh->OnMenuDisplay(menu, client, panel);
624 panel->DrawTitle(menu->GetDefaultTitle(), true);
629 IMenuStyle *MenuManager::GetDefaultStyle()
631 return m_pDefaultStyle;
634 bool MenuManager::MenuSoundsEnabled()
636 return (sm_menu_sounds.GetInt() != 0);
639 ConfigResult MenuManager::OnSourceModConfigChanged(const char *key,
645 if (strcmp(key, "MenuItemSound") == 0)
647 m_SelectSound.assign(value);
648 return ConfigResult_Accept;
649 } else if (strcmp(key, "MenuExitBackSound") == 0) {
650 m_ExitBackSound.assign(value);
651 return ConfigResult_Accept;
652 } else if (strcmp(key, "MenuExitSound") == 0) {
653 m_ExitSound.assign(value);
654 return ConfigResult_Accept;
657 return ConfigResult_Ignore;
660 const char *MenuManager::GetMenuSound(ItemSelection sel)
662 const char *sound = NULL;
670 if (m_SelectSound.size() > 0)
672 sound = m_SelectSound.c_str();
676 case ItemSel_ExitBack:
678 if (m_ExitBackSound.size() > 0)
680 sound = m_ExitBackSound.c_str();
686 if (m_ExitSound.size() > 0)
688 sound = m_ExitSound.c_str();
701 void MenuManager::OnSourceModLevelChange(const char *mapName)
703 if (m_SelectSound.size() > 0)
705 enginesound->PrecacheSound(m_SelectSound.c_str(), true);
707 if (m_ExitBackSound.size() > 0)
709 enginesound->PrecacheSound(m_ExitBackSound.c_str(), true);
711 if (m_ExitSound.size() > 0)
713 enginesound->PrecacheSound(m_ExitSound.c_str(), true);
717 void MenuManager::CancelMenu(IBaseMenu *menu)
719 if (s_VoteHandler.GetCurrentMenu() == menu
720 && !s_VoteHandler.IsCancelling())
722 s_VoteHandler.CancelVoting();
729 bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
731 return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
734 bool MenuManager::IsVoteInProgress()
736 return s_VoteHandler.IsVoteInProgress();
739 void MenuManager::CancelVoting()
741 s_VoteHandler.CancelVoting();
744 unsigned int MenuManager::GetRemainingVoteDelay()
746 return s_VoteHandler.GetRemainingVoteDelay();