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 if (pgn != MENU_NO_PAGINATION)
219 unsigned int totalItems = menu->GetItemCount();
220 unsigned int startItem = 0;
222 /* For pagination, find the starting point. */
223 if (pgn != MENU_NO_PAGINATION)
225 if (order == ItemOrder_Ascending)
227 startItem = md.lastItem;
228 /* This shouldn't happen with well-coded menus.
229 * If the item is out of bounds, switch the order to
230 * Items_Descending and make us start from the top.
232 if (startItem >= totalItems)
234 startItem = totalItems - 1;
235 order = ItemOrder_Descending;
237 } else if (order == ItemOrder_Descending) {
238 startItem = md.firstItem;
239 /* This shouldn't happen with well-coded menus.
240 * If searching backwards doesn't give us enough room,
241 * start from the beginning and change to ascending.
243 if (startItem <= maxItems)
246 order = ItemOrder_Ascending;
251 /* Get our Display pointer and initialize some crap */
252 IMenuPanel *display = menu->CreatePanel();
253 IMenuHandler *mh = md.mh;
254 bool foundExtra = false;
255 unsigned int extraItem = 0;
258 * We keep searching until:
259 * 1) There are no more items
260 * 2) We reach one OVER the maximum number of slot items
261 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
263 unsigned int i = startItem;
264 unsigned int foundItems = 0;
267 ItemDrawInfo &dr = drawItems[foundItems].draw;
268 /* Is the item valid? */
269 if (menu->GetItemInfo(i, &dr) != NULL)
271 /* Ask the user to change the style, if necessary */
272 mh->OnMenuDrawItem(menu, client, i, dr.style);
273 /* Check if it's renderable */
274 if (IsSlotItem(display, dr.style))
276 /* If we've already found the max number of items,
277 * This means we should just cancel out and log our
280 if (foundItems >= maxItems)
286 drawItems[foundItems++].position = i;
289 /* If there's no pagination, stop once the menu is full. */
290 if (pgn == MENU_NO_PAGINATION)
292 /* If we've filled up, then stop */
293 if (foundItems >= maxItems)
298 /* If we're descending and this is the first item, stop */
299 if (order == ItemOrder_Descending)
307 /* If we're ascending and this is the last item, stop */
308 else if (order == ItemOrder_Ascending)
310 if (i >= totalItems - 1)
318 /* There were no items to draw! */
321 display->DeleteThis();
325 /* Check initial buttons */
326 bool displayPrev = false;
327 bool displayNext = false;
330 if (order == ItemOrder_Descending)
333 md.firstItem = extraItem;
334 } else if (order == ItemOrder_Ascending) {
336 md.lastItem = extraItem;
341 * If we're paginated, we have to find if there is another page.
342 * Sadly, the only way to do this is to try drawing more items!
344 if (pgn != MENU_NO_PAGINATION)
346 unsigned int lastItem = 0;
348 /* Find the last feasible item to search from. */
349 if (order == ItemOrder_Descending)
351 lastItem = drawItems[0].position;
352 if (lastItem >= totalItems - 1)
356 while (++lastItem < totalItems)
358 if (menu->GetItemInfo(lastItem, &dr) != NULL)
360 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
361 if (IsSlotItem(display, dr.style))
364 md.lastItem = lastItem;
370 else if (order == ItemOrder_Ascending)
372 lastItem = drawItems[0].position;
378 while (lastItem != 0)
380 if (menu->GetItemInfo(lastItem, &dr) != NULL)
382 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
383 if (IsSlotItem(display, dr.style))
386 md.firstItem = lastItem;
396 /* Draw the item according to the order */
397 menu_slots_t *slots = md.slots;
398 unsigned int position = 0; /* Keep track of the last position */
399 if (order == ItemOrder_Ascending)
401 for (unsigned int i=0; i<foundItems; i++)
403 ItemDrawInfo &dr = drawItems[i].draw;
404 if ((position = mh->OnMenuDisplayItem(menu, client, display, drawItems[i].position, dr)) == 0)
406 position = display->DrawItem(dr);
410 slots[position].item = drawItems[i].position;
411 slots[position].type = ItemSel_Item;
414 } else if (order == ItemOrder_Descending) {
415 unsigned int i = foundItems;
416 /* NOTE: There will always be at least one item because
417 * of the check earlier.
421 ItemDrawInfo &dr = drawItems[i].draw;
422 if ((position = mh->OnMenuDisplayItem(menu, client, display, drawItems[i].position, dr)) == 0)
424 position = display->DrawItem(dr);
428 slots[position].item = drawItems[i].position;
429 slots[position].type = ItemSel_Item;
434 /* Now, we need to check if we need to add anything extra */
435 if (pgn != MENU_NO_PAGINATION)
437 bool canDrawDisabled = display->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
438 bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT;
439 bool exitBackButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK;
442 /* Calculate how many items we are allowed for control stuff */
443 unsigned int padding = style->GetMaxPageItems() - maxItems;
445 /* Add the number of available slots */
446 padding += (maxItems - foundItems);
448 /* Someday, if we are able to re-enable this, we will be very lucky men. */
450 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
453 /* Even if we don't draw an exit button, we invalidate the slot. */
457 /* Otherwise, we don't draw anything and leave the slot available */
462 /* Subtract two slots for the displayNext/displayPrev padding */
465 /* If we have an "Exit Back" button and the space to draw it, do so. */
472 exitBackButton = false;
477 * We allow next/prev to be undrawn if neither exists.
478 * Thus, we only need padding if one of them will be drawn,
479 * or the exit button will be drawn.
481 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
482 if (exitButton || (displayNext || displayPrev))
484 /* If there are no control options,
485 * Instead just pad with invisible slots.
487 if (!displayPrev && !displayPrev)
489 padItem.style = ITEMDRAW_NOTEXT;
491 /* Add spacers so we can pad to the end */
492 for (unsigned int i=0; i<padding; i++)
494 position = display->DrawItem(padItem);
495 slots[position].type = ItemSel_None;
499 /* Put a fake spacer before control stuff, if possible */
500 if ((displayPrev || displayNext) || exitButton)
502 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
503 display->DrawItem(draw);
506 ItemDrawInfo dr(text, 0);
508 * If we have one or the other, we need to have spacers for both.
510 if (displayPrev || displayNext)
513 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
514 if (displayPrev || canDrawDisabled)
518 CorePlayerTranslate(client, text, sizeof(text), "Back", NULL);
519 dr.style = ITEMDRAW_CONTROL;
520 position = display->DrawItem(dr);
521 slots[position].type = ItemSel_ExitBack;
523 CorePlayerTranslate(client, text, sizeof(text), "Previous", NULL);
524 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
525 position = display->DrawItem(dr);
526 slots[position].type = ItemSel_Back;
528 } else if (displayNext || exitButton) {
529 /* If we can't display this, and there is an exit button,
532 position = display->DrawItem(padCtrlItem);
533 slots[position].type = ItemSel_None;
537 if (displayNext || canDrawDisabled)
539 CorePlayerTranslate(client, text, sizeof(text), "Next", NULL);
540 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
541 position = display->DrawItem(dr);
542 slots[position].type = ItemSel_Next;
543 } else if (exitButton) {
544 /* If we can't display this,
545 * but there is an "exit" button, we need to pad!
547 position = display->DrawItem(padCtrlItem);
548 slots[position].type = ItemSel_None;
551 /* Otherwise, bump to two slots! */
552 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
553 position = display->DrawItem(numBump);
554 slots[position].type = ItemSel_None;
555 position = display->DrawItem(numBump);
556 slots[position].type = ItemSel_None;
562 CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
563 dr.style = ITEMDRAW_CONTROL;
564 position = display->DrawItem(dr);
565 slots[position].type = ItemSel_Exit;
569 /* Lastly, fill in any slots we could have missed */
570 for (unsigned int i = position + 1; i < 10; i++)
572 slots[i].type = ItemSel_None;
576 mh->OnMenuDisplay(menu, client, display);
577 display->DrawTitle(menu->GetDefaultTitle(), true);
582 IMenuStyle *MenuManager::GetDefaultStyle()
584 return m_pDefaultStyle;
587 bool MenuManager::MenuSoundsEnabled()
589 return (sm_menu_sounds.GetInt() != 0);
592 ConfigResult MenuManager::OnSourceModConfigChanged(const char *key,
598 if (strcmp(key, "MenuItemSound") == 0)
600 m_SelectSound.assign(value);
601 return ConfigResult_Accept;
602 } else if (strcmp(key, "MenuExitBackSound") == 0) {
603 m_ExitBackSound.assign(value);
604 return ConfigResult_Accept;
605 } else if (strcmp(key, "MenuExitSound") == 0) {
606 m_ExitSound.assign(value);
607 return ConfigResult_Accept;
610 return ConfigResult_Ignore;
613 const char *MenuManager::GetMenuSound(ItemSelection sel)
615 const char *sound = NULL;
623 if (m_SelectSound.size() > 0)
625 sound = m_SelectSound.c_str();
629 case ItemSel_ExitBack:
631 if (m_ExitBackSound.size() > 0)
633 sound = m_ExitBackSound.c_str();
639 if (m_ExitSound.size() > 0)
641 sound = m_ExitSound.c_str();
654 void MenuManager::OnSourceModLevelChange(const char *mapName)
656 if (m_SelectSound.size() > 0)
658 enginesound->PrecacheSound(m_SelectSound.c_str(), true);
660 if (m_ExitBackSound.size() > 0)
662 enginesound->PrecacheSound(m_ExitBackSound.c_str(), true);
664 if (m_ExitSound.size() > 0)
666 enginesound->PrecacheSound(m_ExitSound.c_str(), true);
670 void MenuManager::CancelMenu(IBaseMenu *menu)
672 if (s_VoteHandler.GetCurrentMenu() == menu
673 && !s_VoteHandler.IsCancelling())
675 s_VoteHandler.CancelVoting();
682 bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
684 return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
687 bool MenuManager::IsVoteInProgress()
689 return s_VoteHandler.IsVoteInProgress();
692 void MenuManager::CancelVoting()
694 s_VoteHandler.CancelVoting();
697 unsigned int MenuManager::GetRemainingVoteDelay()
699 return s_VoteHandler.GetRemainingVoteDelay();