Updated for latest hl2sdk-dota changes.
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>.
33 #include "sm_stringutil.h"
34 #include "MenuStyle_Base.h"
35 #include "PlayerManager.h"
36 #include "MenuManager.h"
37 #include "CellRecipientFilter.h"
38 #if defined MENU_DEBUG
41 #include "logic_bridge.h"
42 #include "AutoHandleRooter.h"
44 BaseMenuStyle::BaseMenuStyle() : m_WatchList(256), m_hHandle(BAD_HANDLE)
48 Handle_t BaseMenuStyle::GetHandle()
50 /* Don't create the handle until we need it */
51 if (m_hHandle == BAD_HANDLE)
53 m_hHandle = g_Menus.CreateStyleHandle(this);
59 void BaseMenuStyle::AddClientToWatch(int client)
61 #if defined MENU_DEBUG
62 g_Logger.LogMessage("[SM_MENU] AddClientToWatch(%d)", client);
64 m_WatchList.push_back(client);
67 void BaseMenuStyle::RemoveClientFromWatch(int client)
69 #if defined MENU_DEBUG
70 g_Logger.LogMessage("[SM_MENU] RemoveClientFromWatch(%d)", client);
72 m_WatchList.remove(client);
75 void BaseMenuStyle::_CancelClientMenu(int client, MenuCancelReason reason, bool bAutoIgnore/* =false */)
77 #if defined MENU_DEBUG
78 g_Logger.LogMessage("[SM_MENU] _CancelClientMenu() (client %d) (bAutoIgnore %d) (reason %d)", client, bAutoIgnore, reason);
80 CBaseMenuPlayer *player = GetMenuPlayer(client);
81 menu_states_t &states = player->states;
83 bool bOldIgnore = player->bAutoIgnore;
86 player->bAutoIgnore = true;
90 IMenuHandler *mh = states.mh;
91 IBaseMenu *menu = states.menu;
94 player->bInMenu = false;
95 if (player->menuHoldTime)
97 RemoveClientFromWatch(client);
101 mh->OnMenuCancel(menu, client, reason);
103 /* Only fire end if there's a valid menu */
106 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
111 player->bAutoIgnore = bOldIgnore;
115 void BaseMenuStyle::CancelMenu(CBaseMenu *menu)
117 #if defined MENU_DEBUG
118 g_Logger.LogMessage("[SM_MENU] CancelMenu() (menu %p)", menu);
120 int maxClients = g_Players.GetMaxClients();
121 for (int i=1; i<=maxClients; i++)
123 CBaseMenuPlayer *player = GetMenuPlayer(i);
126 menu_states_t &states = player->states;
127 if (states.menu == menu)
129 _CancelClientMenu(i, MenuCancel_Interrupted);
135 bool BaseMenuStyle::CancelClientMenu(int client, bool autoIgnore)
137 #if defined MENU_DEBUG
138 g_Logger.LogMessage("[SM_MENU] CancelClientMenu() (client %d) (bAutoIgnore %d)", client, autoIgnore);
140 if (client < 1 || client > g_Players.MaxClients())
145 CBaseMenuPlayer *player = GetMenuPlayer(client);
146 if (!player->bInMenu)
151 _CancelClientMenu(client, MenuCancel_Interrupted, autoIgnore);
156 MenuSource BaseMenuStyle::GetClientMenu(int client, void **object)
158 if (client < 1 || client > g_Players.GetMaxClients())
160 return MenuSource_None;
163 CBaseMenuPlayer *player = GetMenuPlayer(client);
168 if ((menu=player->states.menu) != NULL)
174 return MenuSource_BaseMenu;
177 return MenuSource_Display;
178 } else if (player->bInExternMenu) {
179 if (player->menuHoldTime != 0
180 && (gpGlobals->curtime > player->menuStartTime + player->menuHoldTime))
182 player->bInExternMenu = false;
183 return MenuSource_None;
185 return MenuSource_External;
188 return MenuSource_None;
191 void BaseMenuStyle::OnClientDisconnected(int client)
193 #if defined MENU_DEBUG
194 g_Logger.LogMessage("[SM_MENU] OnClientDisconnected(%d)", client);
196 CBaseMenuPlayer *player = GetMenuPlayer(client);
197 if (!player->bInMenu)
202 _CancelClientMenu(client, MenuCancel_Disconnected, true);
204 player->bInMenu = false;
205 player->bInExternMenu = false;
208 static int do_lookup[256];
209 void BaseMenuStyle::ProcessWatchList()
211 if (!m_WatchList.size())
216 #if defined MENU_DEBUG
217 g_Logger.LogMessage("BaseMenuStyle::ProcessWatchList(%d,%d,%d,%d,%d,%p)",
219 m_WatchList.m_FirstLink,
220 m_WatchList.m_FreeNodes,
221 m_WatchList.m_LastLink,
222 m_WatchList.m_MaxSize,
223 m_WatchList.m_Nodes);
226 unsigned int total = 0;
227 for (FastLink<int>::iterator iter=m_WatchList.begin(); iter!=m_WatchList.end(); ++iter)
229 do_lookup[total++] = (*iter);
232 #if defined MENU_DEBUG
235 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() found %d clients", total);
240 CBaseMenuPlayer *player;
241 float curTime = gpGlobals->curtime;
242 for (unsigned int i=0; i<total; i++)
244 client = do_lookup[i];
245 player = GetMenuPlayer(client);
246 #if defined MENU_DEBUG
247 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() (client %d) (bInMenu %d) (menuHoldTime %d) (curTime %f) (menuStartTime %f)",
250 player->menuHoldTime,
252 player->menuStartTime);
254 if (!player->bInMenu || !player->menuHoldTime)
256 #if defined MENU_DEBUG
257 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() removing client %d", client);
259 m_WatchList.remove(client);
262 if (curTime > player->menuStartTime + player->menuHoldTime)
264 _CancelClientMenu(client, MenuCancel_Timeout, false);
269 void BaseMenuStyle::ClientPressedKey(int client, unsigned int key_press)
271 #if defined MENU_DEBUG
272 g_Logger.LogMessage("[SM_MENU] ClientPressedKey() (client %d) (key_press %d)", client, key_press);
274 CBaseMenuPlayer *player = GetMenuPlayer(client);
276 /* First question: Are we in a menu? */
277 if (!player->bInMenu)
283 unsigned int item = 0;
284 MenuCancelReason reason = MenuCancel_Exit;
285 MenuEndReason end_reason = MenuEnd_Selected;
286 menu_states_t &states = player->states;
289 IMenuHandler *mh = states.mh;
290 IBaseMenu *menu = states.menu;
292 unsigned int item_on_page = states.item_on_page;
299 } else if (key_press < 1 || key_press > GetMaxPageItems()) {
302 ItemSelection type = states.slots[key_press].type;
304 /* Check if we should play a sound about the type */
305 if (g_Menus.MenuSoundsEnabled() &&
306 (!menu || (menu->GetMenuOptionFlags() & MENUFLAG_NO_SOUND) != MENUFLAG_NO_SOUND))
308 CellRecipientFilter filter;
312 filter.Initialize(clients, 1);
314 const char *sound = g_Menus.GetMenuSound(type);
318 edict_t *pEdict = PEntityOfEntIndex(client);
321 ICollideable *pCollideable = pEdict->GetCollideable();
325 const Vector & pos = pCollideable->GetCollisionOrigin();
326 enginesound->EmitSound(filter,
329 #if SOURCE_ENGINE >= SE_PORTAL2
336 #if SOURCE_ENGINE >= SE_PORTAL2
341 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_ORANGEBOXVALVE
350 /* For navigational items, we're going to redisplay */
351 if (type == ItemSel_Back)
353 if (!RedoClientMenu(client, ItemOrder_Descending))
356 reason = MenuCancel_NoDisplay;
357 end_reason = MenuEnd_Cancelled;
361 } else if (type == ItemSel_Next) {
362 if (!RedoClientMenu(client, ItemOrder_Ascending))
364 cancel = true; /* I like Saltines. */
365 reason = MenuCancel_NoDisplay;
366 end_reason = MenuEnd_Cancelled;
370 } else if (type == ItemSel_Exit || type == ItemSel_None) {
372 reason = MenuCancel_Exit;
373 end_reason = MenuEnd_Exit;
374 } else if (type == ItemSel_ExitBack) {
376 reason = MenuCancel_ExitBack;
377 end_reason = MenuEnd_ExitBack;
379 item = states.slots[key_press].item;
384 player->bInMenu = false;
385 if (player->menuHoldTime)
387 RemoveClientFromWatch(client);
390 Handle_t hndl = menu ? menu->GetHandle() : BAD_HANDLE;
391 AutoHandleRooter ahr(hndl);
395 mh->OnMenuCancel(menu, client, reason);
397 mh->OnMenuSelect(menu, client, item);
398 if (mh->GetMenuAPIVersion2() >= 13)
400 mh->OnMenuSelect2(menu, client, item, item_on_page);
404 /* Only fire end for valid menus */
407 mh->OnMenuEnd(menu, end_reason);
411 bool BaseMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
413 #if defined MENU_DEBUG
414 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (panel %p) (mh %p) (time %d)",
420 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
421 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
426 CBaseMenuPlayer *player = GetMenuPlayer(client);
427 if (player->bAutoIgnore)
432 /* For the duration of this, we are going to totally ignore whether
433 * the player is already in a menu or not (except to cancel the old one).
434 * Instead, we are simply going to ignore any further menu displays, so
435 * this display can't be interrupted.
437 player->bAutoIgnore = true;
439 /* Cancel any old menus */
440 menu_states_t &states = player->states;
443 _CancelClientMenu(client, MenuCancel_Interrupted, true);
446 states.firstItem = 0;
450 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
451 player->bInMenu = true;
452 player->bInExternMenu = false;
453 player->menuStartTime = gpGlobals->curtime;
454 player->menuHoldTime = time;
458 AddClientToWatch(client);
461 /* Draw the display */
462 SendDisplay(client, menu);
464 /* We can be interrupted again! */
465 player->bAutoIgnore = false;
470 bool BaseMenuStyle::DoClientMenu(int client,
472 unsigned int first_item,
476 #if defined MENU_DEBUG
477 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (menu %p) (mh %p) (time %d)",
483 mh->OnMenuStart(menu);
485 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
486 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
488 #if defined MENU_DEBUG
489 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to display to client %d", client);
491 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
492 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
496 CBaseMenuPlayer *player = GetMenuPlayer(client);
497 if (player->bAutoIgnore)
499 #if defined MENU_DEBUG
500 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Client %d is autoIgnoring", client);
502 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
503 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
507 /* For the duration of this, we are going to totally ignore whether
508 * the player is already in a menu or not (except to cancel the old one).
509 * Instead, we are simply going to ignore any further menu displays, so
510 * this display can't be interrupted.
512 player->bAutoIgnore = true;
514 /* Cancel any old menus */
515 menu_states_t &states = player->states;
518 #if defined MENU_DEBUG
519 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Cancelling old menu to client %d", client);
521 _CancelClientMenu(client, MenuCancel_Interrupted, true);
524 states.firstItem = 0;
525 states.lastItem = first_item;
528 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
530 IMenuPanel *display = g_Menus.RenderMenu(client, states, ItemOrder_Ascending);
533 #if defined MENU_DEBUG
534 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to render to client %d", client);
536 player->bAutoIgnore = false;
537 player->bInMenu = false;
538 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
539 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
543 /* Finally, set our states */
544 player->bInMenu = true;
545 player->bInExternMenu = false;
546 player->menuStartTime = gpGlobals->curtime;
547 player->menuHoldTime = time;
551 AddClientToWatch(client);
554 /* Draw the display */
555 SendDisplay(client, display);
557 /* Free the display pointer */
558 display->DeleteThis();
560 /* We can be interrupted again! */
561 player->bAutoIgnore = false;
563 #if defined MENU_DEBUG
564 g_Logger.LogMessage("[SM_MENU] DoClientMenu() finished successfully (client %d)", client);
570 bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
572 #if defined MENU_DEBUG
573 g_Logger.LogMessage("[SM_MENU] RedoClientMenu() (client %d) (order %d)", client, order);
575 CBaseMenuPlayer *player = GetMenuPlayer(client);
576 menu_states_t &states = player->states;
578 player->bAutoIgnore = true;
579 IMenuPanel *display = g_Menus.RenderMenu(client, states, order);
582 #if defined MENU_DEBUG
583 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Failed to render menu");
585 if (player->menuHoldTime)
587 RemoveClientFromWatch(client);
589 player->bAutoIgnore = false;
593 SendDisplay(client, display);
595 display->DeleteThis();
597 player->bAutoIgnore = false;
599 #if defined MENU_DEBUG
600 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Succeeded to client %d", client);
606 CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) :
607 m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false),
608 m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false),
609 m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT)
613 CBaseMenu::~CBaseMenu()
617 Handle_t CBaseMenu::GetHandle()
621 m_hHandle = g_Menus.CreateMenuHandle(this, m_pOwner);
627 bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
629 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
630 && m_items.size() >= m_pStyle->GetMaxPageItems())
637 item.infoString = m_Strings.AddString(info);
640 item.displayString = m_Strings.AddString(draw.display);
642 item.style = draw.style;
644 m_items.push_back(item);
649 bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
651 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
652 && m_items.size() >= m_pStyle->GetMaxPageItems())
657 if (position >= m_items.size())
663 item.infoString = m_Strings.AddString(info);
666 item.displayString = m_Strings.AddString(draw.display);
668 item.style = draw.style;
670 CVector<CItem>::iterator iter = m_items.iterAt(position);
671 m_items.insert(iter, item);
676 bool CBaseMenu::RemoveItem(unsigned int position)
678 if (position >= m_items.size())
683 m_items.erase(m_items.iterAt(position));
685 if (m_items.size() == 0)
693 void CBaseMenu::RemoveAllItems()
699 const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
701 if (position >= m_items.size())
708 draw->display = m_Strings.GetString(m_items[position].displayString);
709 draw->style = m_items[position].style;
712 return m_Strings.GetString(m_items[position].infoString);
715 unsigned int CBaseMenu::GetItemCount()
717 return m_items.size();
720 bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
722 if (itemsPerPage > 7 || itemsPerPage == 1)
727 if (itemsPerPage == MENU_NO_PAGINATION
728 && m_Pagination != MENU_NO_PAGINATION)
730 m_nFlags &= ~MENUFLAG_BUTTON_EXIT;
733 m_Pagination = itemsPerPage;
738 unsigned int CBaseMenu::GetPagination()
743 IMenuStyle *CBaseMenu::GetDrawStyle()
748 void CBaseMenu::SetDefaultTitle(const char *message)
750 m_Title.assign(message);
753 const char *CBaseMenu::GetDefaultTitle()
755 return m_Title.c_str();
758 void CBaseMenu::Cancel()
765 #if defined MENU_DEBUG
766 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Cancel(%p) (m_bShouldDelete %d)",
771 m_bCancelling = true;
773 m_bCancelling = false;
781 void CBaseMenu::Destroy(bool releaseHandle)
783 /* Check if we shouldn't be here */
789 #if defined MENU_DEBUG
790 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Destroy(%p) (release %d) (m_bCancelling %d) (m_bShouldDelete %d)",
797 /* Save the destruction hint about our handle */
798 m_bWillFreeHandle = releaseHandle;
800 /* Now actually do stuff */
801 if (!m_bCancelling || m_bShouldDelete)
806 m_bShouldDelete = true;
810 void CBaseMenu::InternalDelete()
812 if (m_bWillFreeHandle && m_hHandle != BAD_HANDLE)
814 Handle_t hndl = m_hHandle;
817 sec.pOwner = m_pOwner;
818 sec.pIdentity = g_pCoreIdent;
820 m_hHandle = BAD_HANDLE;
822 handlesys->FreeHandle(hndl, &sec);
825 m_pHandler->OnMenuDestroy(this);
830 unsigned int CBaseMenu::GetMenuOptionFlags()
835 void CBaseMenu::SetMenuOptionFlags(unsigned int flags)
840 IMenuHandler *CBaseMenu::GetHandler()
845 unsigned int CBaseMenu::GetBaseMemUsage()
847 return m_Title.size()
848 + m_Strings.GetMemTable()->GetMemUsage()
849 + (m_items.size() * sizeof(CItem));