Initial Import of beta structs extension.
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 "Translator.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;
235 if (pgn != MENU_NO_PAGINATION)
244 /* This is very not allowed! */
250 unsigned int totalItems = menu->GetItemCount();
251 unsigned int startItem = 0;
253 /* For pagination, find the starting point. */
254 if (pgn != MENU_NO_PAGINATION)
256 if (order == ItemOrder_Ascending)
258 startItem = md.lastItem;
259 /* This shouldn't happen with well-coded menus.
260 * If the item is out of bounds, switch the order to
261 * Items_Descending and make us start from the top.
263 if (startItem >= totalItems)
265 startItem = totalItems - 1;
266 order = ItemOrder_Descending;
269 else if (order == ItemOrder_Descending)
271 startItem = md.firstItem;
272 /* This shouldn't happen with well-coded menus.
273 * If searching backwards doesn't give us enough room,
274 * start from the beginning and change to ascending.
276 if (startItem <= maxItems)
279 order = ItemOrder_Ascending;
284 /* Get our Display pointer and initialize some crap */
285 IMenuPanel *panel = menu->CreatePanel();
286 IMenuHandler *mh = md.mh;
287 bool foundExtra = false;
288 unsigned int extraItem = 0;
296 * We keep searching until:
297 * 1) There are no more items
298 * 2) We reach one OVER the maximum number of slot items
299 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
301 unsigned int i = startItem;
302 unsigned int foundItems = 0;
305 ItemDrawInfo &dr = drawItems[foundItems].draw;
306 /* Is the item valid? */
307 if (menu->GetItemInfo(i, &dr) != NULL)
309 /* Ask the user to change the style, if necessary */
310 mh->OnMenuDrawItem(menu, client, i, dr.style);
311 /* Check if it's renderable */
312 if (IsSlotItem(panel, dr.style))
314 /* If we've already found the max number of items,
315 * This means we should just cancel out and log our
318 if (foundItems >= maxItems)
324 drawItems[foundItems++].position = i;
327 /* If there's no pagination, stop once the menu is full. */
328 if (pgn == MENU_NO_PAGINATION)
330 /* If we've filled up, then stop */
331 if (foundItems >= maxItems)
336 /* If we're descending and this is the first item, stop */
337 if (order == ItemOrder_Descending)
345 /* If we're ascending and this is the last item, stop */
346 else if (order == ItemOrder_Ascending)
348 if (i >= totalItems - 1)
356 /* There were no items to draw! */
363 bool displayPrev = false;
364 bool displayNext = false;
366 /* This is an annoying process.
367 * Skip it for non-paginated menus, which get special treatment.
369 if (pgn != MENU_NO_PAGINATION)
373 if (order == ItemOrder_Descending)
376 md.firstItem = extraItem;
378 else if (order == ItemOrder_Ascending)
381 md.lastItem = extraItem;
385 unsigned int lastItem = 0;
387 /* Find the last feasible item to search from. */
388 if (order == ItemOrder_Descending)
390 lastItem = drawItems[0].position;
391 if (lastItem >= totalItems - 1)
395 while (++lastItem < totalItems)
397 if (menu->GetItemInfo(lastItem, &dr) != NULL)
399 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
400 if (IsSlotItem(panel, dr.style))
403 md.lastItem = lastItem;
409 else if (order == ItemOrder_Ascending)
411 lastItem = drawItems[0].position;
417 while (lastItem != 0)
419 if (menu->GetItemInfo(lastItem, &dr) != NULL)
421 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
422 if (IsSlotItem(panel, dr.style))
425 md.firstItem = lastItem;
436 /* Draw the item according to the order */
437 menu_slots_t *slots = md.slots;
438 unsigned int position = 0; /* Keep track of the last position */
439 if (order == ItemOrder_Ascending)
441 md.item_on_page = drawItems[0].position;
442 for (unsigned int i = 0; i < foundItems; i++)
444 ItemDrawInfo &dr = drawItems[i].draw;
445 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
447 position = panel->DrawItem(dr);
451 slots[position].item = drawItems[i].position;
452 if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
454 slots[position].type = ItemSel_None;
458 slots[position].type = ItemSel_Item;
463 else if (order == ItemOrder_Descending)
465 unsigned int i = foundItems;
466 /* NOTE: There will always be at least one item because
467 * of the check earlier.
469 md.item_on_page = drawItems[foundItems - 1].position;
472 ItemDrawInfo &dr = drawItems[i].draw;
473 if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
475 position = panel->DrawItem(dr);
479 slots[position].item = drawItems[i].position;
480 if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
482 slots[position].type = ItemSel_None;
486 slots[position].type = ItemSel_Item;
492 /* Now, we need to check if we need to add anything extra */
493 if (pgn != MENU_NO_PAGINATION || exitButton)
495 bool canDrawDisabled = panel->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
496 bool exitBackButton = false;
499 if (pgn != MENU_NO_PAGINATION
500 && (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK)
502 exitBackButton = true;
505 /* Calculate how many items we are allowed for control stuff */
506 unsigned int padding = style->GetMaxPageItems() - maxItems;
508 /* Add the number of available slots */
509 padding += (maxItems - foundItems);
511 /* Someday, if we are able to re-enable this, we will be very lucky men. */
513 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
516 /* Even if we don't draw an exit button, we invalidate the slot. */
520 /* Otherwise, we don't draw anything and leave the slot available */
525 if (pgn != MENU_NO_PAGINATION)
527 /* Subtract two slots for the displayNext/displayPrev padding */
531 /* If we have an "Exit Back" button and the space to draw it, do so. */
540 exitBackButton = false;
545 * We allow next/prev to be undrawn if neither exists.
546 * Thus, we only need padding if one of them will be drawn,
547 * or the exit button will be drawn.
549 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
550 if (exitButton || (displayNext || displayPrev))
552 /* If there are no control options,
553 * Instead just pad with invisible slots.
555 if (!displayPrev && !displayPrev)
557 padItem.style = ITEMDRAW_NOTEXT;
559 /* Add spacers so we can pad to the end */
560 for (unsigned int i=0; i<padding; i++)
562 position = panel->DrawItem(padItem);
563 slots[position].type = ItemSel_None;
567 /* Put a fake spacer before control stuff, if possible */
568 if ((displayPrev || displayNext) || exitButton)
570 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
571 panel->DrawItem(draw);
574 ItemDrawInfo dr(text, 0);
577 * If we have one or the other, we need to have spacers for both.
579 if (pgn != MENU_NO_PAGINATION)
581 if (displayPrev || displayNext)
584 ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
585 if (displayPrev || canDrawDisabled)
589 if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Back", &client))
591 UTIL_Format(text, sizeof(text), "Back");
593 dr.style = ITEMDRAW_CONTROL;
594 position = panel->DrawItem(dr);
595 slots[position].type = ItemSel_ExitBack;
599 if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Previous", &client))
601 UTIL_Format(text, sizeof(text), "Previous");
603 dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
604 position = panel->DrawItem(dr);
605 slots[position].type = ItemSel_Back;
608 else if (displayNext || exitButton)
610 /* If we can't display this, and there is an exit button,
613 position = panel->DrawItem(padCtrlItem);
614 slots[position].type = ItemSel_None;
618 if (displayNext || canDrawDisabled)
620 if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Next", &client))
622 UTIL_Format(text, sizeof(text), "Next");
624 dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
625 position = panel->DrawItem(dr);
626 slots[position].type = ItemSel_Next;
630 /* If we can't display this,
631 * but there is an "exit" button, we need to pad!
633 position = panel->DrawItem(padCtrlItem);
634 slots[position].type = ItemSel_None;
639 /* Otherwise, bump to two slots! */
640 ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
641 position = panel->DrawItem(numBump);
642 slots[position].type = ItemSel_None;
643 position = panel->DrawItem(numBump);
644 slots[position].type = ItemSel_None;
651 if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Exit", &client))
653 UTIL_Format(text, sizeof(text), "Exit");
655 dr.style = ITEMDRAW_CONTROL;
656 position = panel->DrawItem(dr);
657 slots[position].type = ItemSel_Exit;
661 /* Lastly, fill in any slots we could have missed */
662 for (unsigned int i = position + 1; i < 10; i++)
664 slots[i].type = ItemSel_None;
668 mh->OnMenuDisplay(menu, client, panel);
669 panel->DrawTitle(menu->GetDefaultTitle(), true);
674 IMenuStyle *MenuManager::GetDefaultStyle()
676 return m_pDefaultStyle;
679 bool MenuManager::MenuSoundsEnabled()
681 return (sm_menu_sounds.GetInt() != 0);
684 ConfigResult MenuManager::OnSourceModConfigChanged(const char *key,
690 if (strcmp(key, "MenuItemSound") == 0)
692 m_SelectSound.assign(value);
693 return ConfigResult_Accept;
694 } else if (strcmp(key, "MenuExitBackSound") == 0) {
695 m_ExitBackSound.assign(value);
696 return ConfigResult_Accept;
697 } else if (strcmp(key, "MenuExitSound") == 0) {
698 m_ExitSound.assign(value);
699 return ConfigResult_Accept;
702 return ConfigResult_Ignore;
705 const char *MenuManager::GetMenuSound(ItemSelection sel)
707 const char *sound = NULL;
715 if (m_SelectSound.size() > 0)
717 sound = m_SelectSound.c_str();
721 case ItemSel_ExitBack:
723 if (m_ExitBackSound.size() > 0)
725 sound = m_ExitBackSound.c_str();
731 if (m_ExitSound.size() > 0)
733 sound = m_ExitSound.c_str();
746 void MenuManager::OnSourceModLevelChange(const char *mapName)
748 if (m_SelectSound.size() > 0)
750 enginesound->PrecacheSound(m_SelectSound.c_str(), true);
752 if (m_ExitBackSound.size() > 0)
754 enginesound->PrecacheSound(m_ExitBackSound.c_str(), true);
756 if (m_ExitSound.size() > 0)
758 enginesound->PrecacheSound(m_ExitSound.c_str(), true);
762 void MenuManager::CancelMenu(IBaseMenu *menu)
764 if (s_VoteHandler.GetCurrentMenu() == menu
765 && !s_VoteHandler.IsCancelling())
767 s_VoteHandler.CancelVoting();
774 bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
776 return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
779 bool MenuManager::IsVoteInProgress()
781 return s_VoteHandler.IsVoteInProgress();
784 void MenuManager::CancelVoting()
786 s_VoteHandler.CancelVoting();
789 unsigned int MenuManager::GetRemainingVoteDelay()
791 return s_VoteHandler.GetRemainingVoteDelay();
794 bool MenuManager::IsClientInVotePool(int client)
796 return s_VoteHandler.IsClientInVotePool(client);
799 bool MenuManager::RedrawClientVoteMenu(int client)
801 return s_VoteHandler.RedrawToClient(client);