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 */
87 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
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 */
314 MenuEndReason end_reason =
318 (reason == MenuCancel_Exit ?
322 mh->OnMenuEnd(menu, end_reason);
326 bool BaseMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
328 #if defined MENU_DEBUG
329 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (panel %p) (mh %p) (time %d)",
335 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
336 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
341 CBaseMenuPlayer *player = GetMenuPlayer(client);
342 if (player->bAutoIgnore)
347 /* For the duration of this, we are going to totally ignore whether
348 * the player is already in a menu or not (except to cancel the old one).
349 * Instead, we are simply going to ignore any further menu displays, so
350 * this display can't be interrupted.
352 player->bAutoIgnore = true;
354 /* Cancel any old menus */
355 menu_states_t &states = player->states;
358 _CancelClientMenu(client, true);
361 states.firstItem = 0;
365 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
366 player->bInMenu = true;
367 player->bInExternMenu = false;
368 player->menuStartTime = gpGlobals->curtime;
369 player->menuHoldTime = time;
373 AddClientToWatch(client);
376 /* Draw the display */
377 SendDisplay(client, menu);
379 /* We can be interrupted again! */
380 player->bAutoIgnore = false;
385 bool BaseMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
387 #if defined MENU_DEBUG
388 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (menu %p) (mh %p) (time %d)",
394 mh->OnMenuStart(menu);
396 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
397 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
399 #if defined MENU_DEBUG
400 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to display to client %d", client);
402 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
403 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
407 CBaseMenuPlayer *player = GetMenuPlayer(client);
408 if (player->bAutoIgnore)
410 #if defined MENU_DEBUG
411 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Client %d is autoIgnoring", client);
413 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
414 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
418 /* For the duration of this, we are going to totally ignore whether
419 * the player is already in a menu or not (except to cancel the old one).
420 * Instead, we are simply going to ignore any further menu displays, so
421 * this display can't be interrupted.
423 player->bAutoIgnore = true;
425 /* Cancel any old menus */
426 menu_states_t &states = player->states;
429 #if defined MENU_DEBUG
430 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Cancelling old menu to client %d", client);
432 _CancelClientMenu(client, true);
435 states.firstItem = 0;
439 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
441 IMenuPanel *display = g_Menus.RenderMenu(client, states, ItemOrder_Ascending);
444 #if defined MENU_DEBUG
445 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to render to client %d", client);
447 player->bAutoIgnore = false;
448 player->bInMenu = false;
449 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
450 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
454 /* Finally, set our states */
455 player->bInMenu = true;
456 player->bInExternMenu = false;
457 player->menuStartTime = gpGlobals->curtime;
458 player->menuHoldTime = time;
462 AddClientToWatch(client);
465 /* Draw the display */
466 SendDisplay(client, display);
468 /* Free the display pointer */
469 display->DeleteThis();
471 /* We can be interrupted again! */
472 player->bAutoIgnore = false;
474 #if defined MENU_DEBUG
475 g_Logger.LogMessage("[SM_MENU] DoClientMenu() finished successfully (client %d)", client);
481 bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
483 #if defined MENU_DEBUG
484 g_Logger.LogMessage("[SM_MENU] RedoClientMenu() (client %d) (order %d)", client, order);
486 CBaseMenuPlayer *player = GetMenuPlayer(client);
487 menu_states_t &states = player->states;
489 player->bAutoIgnore = true;
490 IMenuPanel *display = g_Menus.RenderMenu(client, states, order);
493 #if defined MENU_DEBUG
494 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Failed to render menu");
496 if (player->menuHoldTime)
498 RemoveClientFromWatch(client);
500 player->bAutoIgnore = false;
504 SendDisplay(client, display);
506 display->DeleteThis();
508 player->bAutoIgnore = false;
510 #if defined MENU_DEBUG
511 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Succeeded to client %d", client);
517 CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) :
518 m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_ExitButton(true),
519 m_bShouldDelete(false), m_bCancelling(false), m_pOwner(pOwner ? pOwner : g_pCoreIdent),
520 m_bDeleting(false), m_bWillFreeHandle(false), m_hHandle(BAD_HANDLE), m_pHandler(pHandler),
525 CBaseMenu::~CBaseMenu()
527 g_Menus.ReleaseVoteWrapper(m_pVoteHandler);
530 Handle_t CBaseMenu::GetHandle()
534 m_hHandle = g_Menus.CreateMenuHandle(this, m_pOwner);
540 bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
542 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
543 && m_items.size() >= m_pStyle->GetMaxPageItems())
550 item.infoString = m_Strings.AddString(info);
553 item.displayString = m_Strings.AddString(draw.display);
555 item.style = draw.style;
558 m_items.push_back(item);
563 bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
565 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
566 && m_items.size() >= m_pStyle->GetMaxPageItems())
571 if (position >= m_items.size())
577 item.infoString = m_Strings.AddString(info);
580 item.displayString = m_Strings.AddString(draw.display);
582 item.style = draw.style;
584 CVector<CItem>::iterator iter = m_items.iterAt(position);
585 m_items.insert(iter, item);
590 bool CBaseMenu::RemoveItem(unsigned int position)
592 if (position >= m_items.size())
597 m_items.erase(m_items.iterAt(position));
599 if (m_items.size() == 0)
607 void CBaseMenu::RemoveAllItems()
613 const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
615 if (position >= m_items.size())
622 draw->display = m_Strings.GetString(m_items[position].displayString);
623 draw->style = m_items[position].style;
626 return m_Strings.GetString(m_items[position].infoString);
629 unsigned int CBaseMenu::GetItemCount()
631 return m_items.size();
634 bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
636 if (itemsPerPage > 7 || itemsPerPage == 1)
641 m_Pagination = itemsPerPage;
646 unsigned int CBaseMenu::GetPagination()
651 IMenuStyle *CBaseMenu::GetDrawStyle()
656 void CBaseMenu::SetDefaultTitle(const char *message)
658 m_Title.assign(message);
661 const char *CBaseMenu::GetDefaultTitle()
663 return m_Title.c_str();
666 bool CBaseMenu::GetExitButton()
671 bool CBaseMenu::SetExitButton(bool set)
677 void CBaseMenu::Cancel()
684 #if defined MENU_DEBUG
685 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Cancel(%p) (m_pVote %p) (inVote %d) (m_bShouldDelete %d)",
688 m_pVoteHandler ? m_pVoteHandler->IsVoteInProgress() : false,
692 if (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress())
694 m_pVoteHandler->CancelVoting();
697 m_bCancelling = true;
699 m_bCancelling = false;
707 void CBaseMenu::Destroy(bool releaseHandle)
709 /* Check if we shouldn't be here */
715 #if defined MENU_DEBUG
716 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Destroy(%p) (release %d) (m_bCancelling %d) (m_bShouldDelete %d)",
723 /* Save the destruction hint about our handle */
724 m_bWillFreeHandle = releaseHandle;
726 /* Now actually do stuff */
727 if (!m_bCancelling || m_bShouldDelete)
732 m_bShouldDelete = true;
736 void CBaseMenu::InternalDelete()
738 if (m_bWillFreeHandle && m_hHandle != BAD_HANDLE)
740 Handle_t hndl = m_hHandle;
743 sec.pOwner = m_pOwner;
744 sec.pIdentity = g_pCoreIdent;
746 m_hHandle = BAD_HANDLE;
748 g_HandleSys.FreeHandle(hndl, &sec);
751 m_pHandler->OnMenuDestroy(this);
756 bool CBaseMenu::BroadcastVote(int clients[],
757 unsigned int numClients,
758 unsigned int maxTime,
761 #if defined MENU_DEBUG
762 g_Logger.LogMessage("[SM_MENU] CBaseMenu::BroadcastVote(%p) (maxTime %d) (numClients %d) (m_pVote %p) (inVote %d)",
767 m_pVoteHandler ? m_pVoteHandler->IsVoteInProgress() : false);
771 m_pVoteHandler = g_Menus.CreateVoteWrapper(m_pHandler);
772 } else if (m_pVoteHandler->IsVoteInProgress()) {
776 m_pVoteHandler->InitializeVoting(this);
778 for (unsigned int i=0; i<numClients; i++)
780 VoteDisplay(clients[i], maxTime);
783 m_pVoteHandler->StartVoting();
788 bool CBaseMenu::IsVoteInProgress()
790 return (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress());