3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #include "sm_stringutil.h"
17 #include "MenuStyle_Base.h"
18 #include "PlayerManager.h"
19 #include "MenuManager.h"
20 #include "HandleSys.h"
21 #if defined MENU_DEBUG
25 BaseMenuStyle::BaseMenuStyle() : m_WatchList(256), m_hHandle(BAD_HANDLE)
29 Handle_t BaseMenuStyle::GetHandle()
31 /* Don't create the handle until we need it */
32 if (m_hHandle == BAD_HANDLE)
34 m_hHandle = g_Menus.CreateStyleHandle(this);
40 void BaseMenuStyle::AddClientToWatch(int client)
42 #if defined MENU_DEBUG
43 g_Logger.LogMessage("[SM_MENU] AddClientToWatch(%d)", client);
45 m_WatchList.push_back(client);
48 void BaseMenuStyle::RemoveClientFromWatch(int client)
50 #if defined MENU_DEBUG
51 g_Logger.LogMessage("[SM_MENU] RemoveClientFromWatch(%d)", client);
53 m_WatchList.remove(client);
56 void BaseMenuStyle::_CancelClientMenu(int client, bool bAutoIgnore/* =false */, MenuCancelReason reason/* =MenuCancel_Interrupt */)
58 #if defined MENU_DEBUG
59 g_Logger.LogMessage("[SM_MENU] _CancelClientMenu() (client %d) (bAutoIgnore %d) (reason %d)", client, bAutoIgnore, reason);
61 CBaseMenuPlayer *player = GetMenuPlayer(client);
62 menu_states_t &states = player->states;
64 bool bOldIgnore = player->bAutoIgnore;
67 player->bAutoIgnore = true;
71 IMenuHandler *mh = states.mh;
72 IBaseMenu *menu = states.menu;
75 player->bInMenu = false;
76 if (player->menuHoldTime)
78 RemoveClientFromWatch(client);
82 mh->OnMenuCancel(menu, client, reason);
84 /* Only fire end if there's a valid menu */
92 player->bAutoIgnore = bOldIgnore;
96 void BaseMenuStyle::CancelMenu(CBaseMenu *menu)
98 #if defined MENU_DEBUG
99 g_Logger.LogMessage("[SM_MENU] CancelMenu() (menu %p)", menu);
101 int maxClients = g_Players.GetMaxClients();
102 for (int i=1; i<=maxClients; i++)
104 CBaseMenuPlayer *player = GetMenuPlayer(i);
107 menu_states_t &states = player->states;
108 if (states.menu == menu)
110 _CancelClientMenu(i);
116 bool BaseMenuStyle::CancelClientMenu(int client, bool autoIgnore)
118 #if defined MENU_DEBUG
119 g_Logger.LogMessage("[SM_MENU] CancelClientMenu() (client %d) (bAutoIgnore %d)", client, autoIgnore);
121 if (client < 1 || client > g_Players.MaxClients())
126 CBaseMenuPlayer *player = GetMenuPlayer(client);
127 if (!player->bInMenu)
132 _CancelClientMenu(client, autoIgnore);
137 MenuSource BaseMenuStyle::GetClientMenu(int client, void **object)
139 if (client < 1 || client > g_Players.GetMaxClients())
141 return MenuSource_None;
144 CBaseMenuPlayer *player = GetMenuPlayer(client);
149 if ((menu=player->states.menu) != NULL)
155 return MenuSource_BaseMenu;
158 return MenuSource_Display;
159 } else if (player->bInExternMenu) {
160 if (player->menuHoldTime != 0
161 && (gpGlobals->curtime > player->menuStartTime + player->menuHoldTime))
163 player->bInExternMenu = false;
164 return MenuSource_None;
166 return MenuSource_External;
169 return MenuSource_None;
172 void BaseMenuStyle::OnClientDisconnected(int client)
174 #if defined MENU_DEBUG
175 g_Logger.LogMessage("[SM_MENU] OnClientDisconnected(%d)", client);
177 CBaseMenuPlayer *player = GetMenuPlayer(client);
178 if (!player->bInMenu)
183 _CancelClientMenu(client, true, MenuCancel_Disconnect);
185 player->bInMenu = false;
186 player->bInExternMenu = false;
189 static int do_lookup[256];
190 void BaseMenuStyle::ProcessWatchList()
192 if (!m_WatchList.size())
197 unsigned int total = 0;
198 for (FastLink<int>::iterator iter=m_WatchList.begin(); iter!=m_WatchList.end(); ++iter)
200 do_lookup[total++] = (*iter);
203 #if defined MENU_DEBUG
206 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() found %d clients", total);
211 CBaseMenuPlayer *player;
212 float curTime = gpGlobals->curtime;
213 for (unsigned int i=0; i<total; i++)
215 client = do_lookup[i];
216 player = GetMenuPlayer(client);
217 #if defined MENU_DEBUG
218 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() (client %d) (bInMenu %d) (menuHoldTime %d) (curTime %f) (menuStartTime %f)",
221 player->menuHoldTime,
223 player->menuStartTime);
225 if (!player->bInMenu || !player->menuHoldTime)
227 #if defined MENU_DEBUG
228 g_Logger.LogMessage("[SM_MENU] ProcessWatchList() removing client %d", client);
230 m_WatchList.remove(client);
233 if (curTime > player->menuStartTime + player->menuHoldTime)
235 _CancelClientMenu(client, false);
240 void BaseMenuStyle::ClientPressedKey(int client, unsigned int key_press)
242 #if defined MENU_DEBUG
243 g_Logger.LogMessage("[SM_MENU] ClientPressedKey() (client %d) (key_press %d)", client, key_press);
245 CBaseMenuPlayer *player = GetMenuPlayer(client);
247 /* First question: Are we in a menu? */
248 if (!player->bInMenu)
254 unsigned int item = 0;
255 MenuCancelReason reason = MenuCancel_Exit;
256 menu_states_t &states = player->states;
258 assert(states.mh != NULL);
260 if (states.menu == NULL)
263 } else if (key_press < 1 || key_press > GetMaxPageItems()) {
266 ItemSelection type = states.slots[key_press].type;
268 /* For navigational items, we're going to redisplay */
269 if (type == ItemSel_Back)
271 if (!RedoClientMenu(client, ItemOrder_Descending))
274 reason = MenuCancel_NoDisplay;
278 } else if (type == ItemSel_Next) {
279 if (!RedoClientMenu(client, ItemOrder_Ascending))
281 cancel = true; /* I like Saltines. */
282 reason = MenuCancel_NoDisplay;
286 } else if (type == ItemSel_Exit || type == ItemSel_None) {
289 item = states.slots[key_press].item;
294 IMenuHandler *mh = states.mh;
295 IBaseMenu *menu = states.menu;
298 player->bInMenu = false;
299 if (player->menuHoldTime)
301 RemoveClientFromWatch(client);
306 mh->OnMenuCancel(menu, client, reason);
308 mh->OnMenuSelect(menu, client, item);
311 /* Only fire end for valid menus */
318 bool BaseMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
320 #if defined MENU_DEBUG
321 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (panel %p) (mh %p) (time %d)",
327 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
328 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
333 CBaseMenuPlayer *player = GetMenuPlayer(client);
334 if (player->bAutoIgnore)
339 /* For the duration of this, we are going to totally ignore whether
340 * the player is already in a menu or not (except to cancel the old one).
341 * Instead, we are simply going to ignore any further menu displays, so
342 * this display can't be interrupted.
344 player->bAutoIgnore = true;
346 /* Cancel any old menus */
347 menu_states_t &states = player->states;
350 _CancelClientMenu(client, true);
353 states.firstItem = 0;
357 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
358 player->bInMenu = true;
359 player->bInExternMenu = false;
360 player->menuStartTime = gpGlobals->curtime;
361 player->menuHoldTime = time;
365 AddClientToWatch(client);
368 /* Draw the display */
369 SendDisplay(client, menu);
371 /* We can be interrupted again! */
372 player->bAutoIgnore = false;
377 bool BaseMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
379 #if defined MENU_DEBUG
380 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (menu %p) (mh %p) (time %d)",
386 mh->OnMenuStart(menu);
388 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
389 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
391 #if defined MENU_DEBUG
392 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to display to client %d", client);
394 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
399 CBaseMenuPlayer *player = GetMenuPlayer(client);
400 if (player->bAutoIgnore)
402 #if defined MENU_DEBUG
403 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Client %d is autoIgnoring", client);
405 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
410 /* For the duration of this, we are going to totally ignore whether
411 * the player is already in a menu or not (except to cancel the old one).
412 * Instead, we are simply going to ignore any further menu displays, so
413 * this display can't be interrupted.
415 player->bAutoIgnore = true;
417 /* Cancel any old menus */
418 menu_states_t &states = player->states;
421 #if defined MENU_DEBUG
422 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Cancelling old menu to client %d", client);
424 _CancelClientMenu(client, true);
427 states.firstItem = 0;
431 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
433 IMenuPanel *display = g_Menus.RenderMenu(client, states, ItemOrder_Ascending);
436 #if defined MENU_DEBUG
437 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to render to client %d", client);
439 player->bAutoIgnore = false;
440 player->bInMenu = false;
441 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
446 /* Finally, set our states */
447 player->bInMenu = true;
448 player->bInExternMenu = false;
449 player->menuStartTime = gpGlobals->curtime;
450 player->menuHoldTime = time;
454 AddClientToWatch(client);
457 /* Draw the display */
458 SendDisplay(client, display);
460 /* Free the display pointer */
461 display->DeleteThis();
463 /* We can be interrupted again! */
464 player->bAutoIgnore = false;
466 #if defined MENU_DEBUG
467 g_Logger.LogMessage("[SM_MENU] DoClientMenu() finished successfully (client %d)", client);
473 bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
475 #if defined MENU_DEBUG
476 g_Logger.LogMessage("[SM_MENU] RedoClientMenu() (client %d) (order %d)", client, order);
478 CBaseMenuPlayer *player = GetMenuPlayer(client);
479 menu_states_t &states = player->states;
481 player->bAutoIgnore = true;
482 IMenuPanel *display = g_Menus.RenderMenu(client, states, order);
485 #if defined MENU_DEBUG
486 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Failed to render menu");
488 if (player->menuHoldTime)
490 RemoveClientFromWatch(client);
492 player->bAutoIgnore = false;
496 SendDisplay(client, display);
498 display->DeleteThis();
500 player->bAutoIgnore = false;
502 #if defined MENU_DEBUG
503 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Succeeded to client %d", client);
509 CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) :
510 m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_ExitButton(true),
511 m_bShouldDelete(false), m_bCancelling(false), m_pOwner(pOwner ? pOwner : g_pCoreIdent),
512 m_bDeleting(false), m_bWillFreeHandle(false), m_hHandle(BAD_HANDLE), m_pHandler(pHandler),
517 CBaseMenu::~CBaseMenu()
519 g_Menus.ReleaseVoteWrapper(m_pVoteHandler);
522 Handle_t CBaseMenu::GetHandle()
526 m_hHandle = g_Menus.CreateMenuHandle(this, m_pOwner);
532 bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
534 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
535 && m_items.size() >= m_pStyle->GetMaxPageItems())
542 item.infoString = m_Strings.AddString(info);
545 item.displayString = m_Strings.AddString(draw.display);
547 item.style = draw.style;
550 m_items.push_back(item);
555 bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
557 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
558 && m_items.size() >= m_pStyle->GetMaxPageItems())
563 if (position >= m_items.size())
569 item.infoString = m_Strings.AddString(info);
572 item.displayString = m_Strings.AddString(draw.display);
574 item.style = draw.style;
576 CVector<CItem>::iterator iter = m_items.iterAt(position);
577 m_items.insert(iter, item);
582 bool CBaseMenu::RemoveItem(unsigned int position)
584 if (position >= m_items.size())
589 m_items.erase(m_items.iterAt(position));
591 if (m_items.size() == 0)
599 void CBaseMenu::RemoveAllItems()
605 const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
607 if (position >= m_items.size())
614 draw->display = m_Strings.GetString(m_items[position].displayString);
615 draw->style = m_items[position].style;
618 return m_Strings.GetString(m_items[position].infoString);
621 unsigned int CBaseMenu::GetItemCount()
623 return m_items.size();
626 bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
628 if (itemsPerPage > 7 || itemsPerPage == 1)
633 m_Pagination = itemsPerPage;
638 unsigned int CBaseMenu::GetPagination()
643 IMenuStyle *CBaseMenu::GetDrawStyle()
648 void CBaseMenu::SetDefaultTitle(const char *message)
650 m_Title.assign(message);
653 const char *CBaseMenu::GetDefaultTitle()
655 return m_Title.c_str();
658 bool CBaseMenu::GetExitButton()
663 bool CBaseMenu::SetExitButton(bool set)
669 void CBaseMenu::Cancel()
676 #if defined MENU_DEBUG
677 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Cancel(%p) (m_pVote %p) (inVote %d) (m_bShouldDelete %d)",
680 m_pVoteHandler ? m_pVoteHandler->IsVoteInProgress() : false,
684 if (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress())
686 m_pVoteHandler->CancelVoting();
689 m_bCancelling = true;
691 m_bCancelling = false;
699 void CBaseMenu::Destroy(bool releaseHandle)
701 /* Check if we shouldn't be here */
707 #if defined MENU_DEBUG
708 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Destroy(%p) (release %d) (m_bCancelling %d) (m_bShouldDelete %d)",
715 /* Save the destruction hint about our handle */
716 m_bWillFreeHandle = releaseHandle;
718 /* Now actually do stuff */
719 if (!m_bCancelling || m_bShouldDelete)
724 m_bShouldDelete = true;
728 void CBaseMenu::InternalDelete()
730 if (m_bWillFreeHandle && m_hHandle != BAD_HANDLE)
732 Handle_t hndl = m_hHandle;
735 sec.pOwner = m_pOwner;
736 sec.pIdentity = g_pCoreIdent;
738 m_hHandle = BAD_HANDLE;
740 g_HandleSys.FreeHandle(hndl, &sec);
743 m_pHandler->OnMenuDestroy(this);
748 bool CBaseMenu::BroadcastVote(int clients[],
749 unsigned int numClients,
750 unsigned int maxTime,
753 #if defined MENU_DEBUG
754 g_Logger.LogMessage("[SM_MENU] CBaseMenu::BroadcastVote(%p) (maxTime %d) (numClients %d) (m_pVote %p) (inVote %d)",
759 m_pVoteHandler ? m_pVoteHandler->IsVoteInProgress() : false);
763 m_pVoteHandler = g_Menus.CreateVoteWrapper(m_pHandler);
764 } else if (m_pVoteHandler->IsVoteInProgress()) {
768 m_pVoteHandler->InitializeVoting(this);
770 for (unsigned int i=0; i<numClients; i++)
772 VoteDisplay(clients[i], maxTime);
775 m_pVoteHandler->StartVoting();
780 bool CBaseMenu::IsVoteInProgress()
782 return (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress());