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"
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;
85 else if (type == m_StyleType)
91 bool MenuManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
93 if (type == m_MenuType)
95 *pSize = ((IBaseMenu *)object)->GetApproxMemUsage();
100 *pSize = ((IMenuStyle *)object)->GetApproxMemUsage();
107 Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
109 if (m_MenuType == NO_HANDLE_TYPE)
114 return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
117 Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
119 if (m_StyleType == NO_HANDLE_TYPE)
124 return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
127 HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
131 sec.pIdentity = g_pCoreIdent;
134 return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
137 HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
141 sec.pIdentity = g_pCoreIdent;
142 sec.pOwner = g_pCoreIdent;
144 return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
147 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
154 m_pDefaultStyle = style;
159 IMenuStyle *MenuManager::GetStyle(unsigned int index)
161 if (index >= GetStyleCount())
166 return m_Styles[index];
169 void MenuManager::AddStyle(IMenuStyle *style)
171 m_Styles.push_back(style);
174 unsigned int MenuManager::GetStyleCount()
176 return (unsigned int)m_Styles.size();
179 IMenuStyle *MenuManager::FindStyleByName(const char *name)
181 unsigned int count = GetStyleCount();
182 for (unsigned int i=0; i<count; i++)
184 IMenuStyle *ptr = GetStyle(i);
185 if (strcasecmp(ptr->GetStyleName(), name) == 0)
194 inline bool IsSlotItem(IMenuPanel *display,
197 if (!display->CanDrawItem(style))
201 if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
205 if (style & ITEMDRAW_RAWLINE)
213 IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
215 IBaseMenu *menu = md.menu;
224 unsigned int position;
228 /* Figure out how many items to draw */
229 IMenuStyle *style = menu->GetDrawStyle();
230 unsigned int pgn = menu->GetPagination();
231 unsigned int maxItems = style->GetMaxPageItems();
232 bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT;
234 if (pgn != MENU_NO_PAGINATION)
243 /* This is very not allowed! */
249 unsigned int totalItems = menu->GetItemCount();
250 unsigned int startItem = 0;
252 /* For pagination, find the starting point. */
253 if (pgn != MENU_NO_PAGINATION)
255 if (order == ItemOrder_Ascending)
257 startItem = md.lastItem;
258 /* This shouldn't happen with well-coded menus.
259 * If the item is out of bounds, switch the order to
260 * Items_Descending and make us start from the top.
262 if (startItem >= totalItems)
264 startItem = totalItems - 1;
265 order = ItemOrder_Descending;
268 else if (order == ItemOrder_Descending)
270 startItem = md.firstItem;
271 /* This shouldn't happen with well-coded menus.
272 * If searching backwards doesn't give us enough room,
273 * start from the beginning and change to ascending.
275 if (startItem <= maxItems)
278 order = ItemOrder_Ascending;
283 /* Get our Display pointer and initialize some crap */
284 IMenuPanel *panel = menu->CreatePanel();
285 IMenuHandler *mh = md.mh;
286 bool foundExtra = false;
287 unsigned int extraItem = 0;
295 * We keep searching until:
296 * 1) There are no more items
297 * 2) We reach one OVER the maximum number of slot items
298 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
300 unsigned int i = startItem;
301 unsigned int foundItems = 0;
304 ItemDrawInfo &dr = drawItems[foundItems].draw;
305 /* Is the item valid? */
306 if (menu->GetItemInfo(i, &dr) != NULL)
308 /* Ask the user to change the style, if necessary */
309 mh->OnMenuDrawItem(menu, client, i, dr.style);
310 /* Check if it's renderable */
311 if (IsSlotItem(panel, dr.style))
313 /* If we've already found the max number of items,
314 * This means we should just cancel out and log our
317 if (foundItems >= maxItems)
323 drawItems[foundItems++].position = i;
326 /* If there's no pagination, stop once the menu is full. */
327 if (pgn == MENU_NO_PAGINATION)
329 /* If we've filled up, then stop */
330 if (foundItems >= maxItems)
335 /* If we're descending and this is the first item, stop */
336 if (order == ItemOrder_Descending)
344 /* If we're ascending and this is the last item, stop */
345 else if (order == ItemOrder_Ascending)
347 if (i >= totalItems - 1)
355 /* There were no items to draw! */
362 bool displayPrev = false;
363 bool displayNext = false;
365 /* This is an annoying process.
366 * Skip it for non-paginated menus, which get special treatment.
368 if (pgn != MENU_NO_PAGINATION)
372 if (order == ItemOrder_Descending)
375 md.firstItem = extraItem;
377 else if (order == ItemOrder_Ascending)
380 md.lastItem = extraItem;
384 unsigned int lastItem = 0;
386 /* Find the last feasible item to search from. */
387 if (order == ItemOrder_Descending)
389 lastItem = drawItems[0].position;
390 if (lastItem >= totalItems - 1)
394 while (++lastItem < totalItems)
396 if (menu->GetItemInfo(lastItem, &dr) != NULL)
398 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
399 if (IsSlotItem(panel, dr.style))
402 md.lastItem = lastItem;
408 else if (order == ItemOrder_Ascending)
410 lastItem = drawItems[0].position;
416 while (lastItem != 0)
418 if (menu->GetItemInfo(lastItem, &dr) != NULL)
420 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
421 if (IsSlotItem(panel, dr.style))
424 md.firstItem = lastItem;
435 /* Draw the item according to the order */
436 menu_slots_t *slots = md.slots;
437 unsigned int position = 0; /* Keep track of the last position */
438 if (order == ItemOrder_Ascending)
440 md.item_on_page = drawItems[0].position;
441 for (unsigned int i = 0; i < foundItems; i++)
443 ItemDrawInfo &dr = drawItems[i].draw;
444 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
446 position = panel->DrawItem(dr);
450 slots[position].item = drawItems[i].position;
451 if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
453 slots[position].type = ItemSel_None;
457 slots[position].type = ItemSel_Item;
462 else if (order == ItemOrder_Descending)
464 unsigned int i = foundItems;
465 /* NOTE: There will always be at least one item because
466 * of the check earlier.
468 md.item_on_page = drawItems[foundItems - 1].position;
471 ItemDrawInfo &dr = drawItems[i].draw;
472 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
474 position = panel->DrawItem(dr);
478 slots[position].item = drawItems[i].position;
479 if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
481 slots[position].type = ItemSel_None;
485 slots[position].type = ItemSel_Item;
491 /* Now, we need to check if we need to add anything extra */
492 if (pgn != MENU_NO_PAGINATION || exitButton)
494 bool canDrawDisabled = panel->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
495 bool exitBackButton = false;
498 if (pgn != MENU_NO_PAGINATION
499 && (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK)
501 exitBackButton = true;
504 /* Calculate how many items we are allowed for control stuff */
505 unsigned int padding = style->GetMaxPageItems() - maxItems;
507 /* Add the number of available slots */
508 padding += (maxItems - foundItems);
510 /* Someday, if we are able to re-enable this, we will be very lucky men. */
512 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
515 /* Even if we don't draw an exit button, we invalidate the slot. */
519 /* Otherwise, we don't draw anything and leave the slot available */
524 if (pgn != MENU_NO_PAGINATION)
526 /* Subtract two slots for the displayNext/displayPrev padding */
530 /* If we have an "Exit Back" button and the space to draw it, do so. */
539 exitBackButton = false;
544 * We allow next/prev to be undrawn if neither exists.
545 * Thus, we only need padding if one of them will be drawn,
546 * or the exit button will be drawn.
548 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
549 if (exitButton || (displayNext || displayPrev))
551 /* If there are no control options,
552 * Instead just pad with invisible slots.
554 if (!displayPrev && !displayPrev)
556 padItem.style = ITEMDRAW_NOTEXT;
558 /* Add spacers so we can pad to the end */
559 for (unsigned int i=0; i<padding; i++)
561 position = panel->DrawItem(padItem);
562 slots[position].type = ItemSel_None;
566 /* Put a fake spacer before control stuff, if possible */
567 if ((displayPrev || displayNext) || exitButton)
569 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
570 panel->DrawItem(draw);
573 ItemDrawInfo dr(text, 0);
576 * If we have one or the other, we need to have spacers for both.
578 if (pgn != MENU_NO_PAGINATION)
580 if (displayPrev || displayNext)
583 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
584 if (displayPrev || canDrawDisabled)
588 CorePlayerTranslate(client, text, sizeof(text), "Back", NULL);
589 dr.style = ITEMDRAW_CONTROL;
590 position = panel->DrawItem(dr);
591 slots[position].type = ItemSel_ExitBack;
595 CorePlayerTranslate(client, text, sizeof(text), "Previous", NULL);
596 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
597 position = panel->DrawItem(dr);
598 slots[position].type = ItemSel_Back;
601 else if (displayNext || exitButton)
603 /* If we can't display this, and there is an exit button,
606 position = panel->DrawItem(padCtrlItem);
607 slots[position].type = ItemSel_None;
611 if (displayNext || canDrawDisabled)
613 CorePlayerTranslate(client, text, sizeof(text), "Next", NULL);
614 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
615 position = panel->DrawItem(dr);
616 slots[position].type = ItemSel_Next;
620 /* If we can't display this,
621 * but there is an "exit" button, we need to pad!
623 position = panel->DrawItem(padCtrlItem);
624 slots[position].type = ItemSel_None;
629 /* Otherwise, bump to two slots! */
630 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
631 position = panel->DrawItem(numBump);
632 slots[position].type = ItemSel_None;
633 position = panel->DrawItem(numBump);
634 slots[position].type = ItemSel_None;
641 CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
642 dr.style = ITEMDRAW_CONTROL;
643 position = panel->DrawItem(dr);
644 slots[position].type = ItemSel_Exit;
648 /* Lastly, fill in any slots we could have missed */
649 for (unsigned int i = position + 1; i < 10; i++)
651 slots[i].type = ItemSel_None;
655 mh->OnMenuDisplay(menu, client, panel);
656 panel->DrawTitle(menu->GetDefaultTitle(), true);
661 IMenuStyle *MenuManager::GetDefaultStyle()
663 return m_pDefaultStyle;
666 bool MenuManager::MenuSoundsEnabled()
668 return (sm_menu_sounds.GetInt() != 0);
671 ConfigResult MenuManager::OnSourceModConfigChanged(const char *key,
677 if (strcmp(key, "MenuItemSound") == 0)
679 m_SelectSound.assign(value);
680 return ConfigResult_Accept;
681 } else if (strcmp(key, "MenuExitBackSound") == 0) {
682 m_ExitBackSound.assign(value);
683 return ConfigResult_Accept;
684 } else if (strcmp(key, "MenuExitSound") == 0) {
685 m_ExitSound.assign(value);
686 return ConfigResult_Accept;
689 return ConfigResult_Ignore;
692 const char *MenuManager::GetMenuSound(ItemSelection sel)
694 const char *sound = NULL;
702 if (m_SelectSound.size() > 0)
704 sound = m_SelectSound.c_str();
708 case ItemSel_ExitBack:
710 if (m_ExitBackSound.size() > 0)
712 sound = m_ExitBackSound.c_str();
718 if (m_ExitSound.size() > 0)
720 sound = m_ExitSound.c_str();
733 void MenuManager::OnSourceModLevelChange(const char *mapName)
735 if (m_SelectSound.size() > 0)
737 enginesound->PrecacheSound(m_SelectSound.c_str(), true);
739 if (m_ExitBackSound.size() > 0)
741 enginesound->PrecacheSound(m_ExitBackSound.c_str(), true);
743 if (m_ExitSound.size() > 0)
745 enginesound->PrecacheSound(m_ExitSound.c_str(), true);
749 void MenuManager::CancelMenu(IBaseMenu *menu)
751 if (s_VoteHandler.GetCurrentMenu() == menu
752 && !s_VoteHandler.IsCancelling())
754 s_VoteHandler.CancelVoting();
761 bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
763 return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
766 bool MenuManager::IsVoteInProgress()
768 return s_VoteHandler.IsVoteInProgress();
771 void MenuManager::CancelVoting()
773 s_VoteHandler.CancelVoting();
776 unsigned int MenuManager::GetRemainingVoteDelay()
778 return s_VoteHandler.GetRemainingVoteDelay();