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, MenuCancelReason reason, bool bAutoIgnore/* =false */)
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, MenuCancel_Interrupted);
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, MenuCancel_Interrupted, 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, MenuCancel_Disconnected, true);
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, MenuCancel_Timeout, 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 MenuEndReason end_reason = MenuEnd_Selected;
257 menu_states_t &states = player->states;
259 assert(states.mh != NULL);
261 if (states.menu == NULL)
264 } else if (key_press < 1 || key_press > GetMaxPageItems()) {
267 ItemSelection type = states.slots[key_press].type;
269 /* For navigational items, we're going to redisplay */
270 if (type == ItemSel_Back)
272 if (!RedoClientMenu(client, ItemOrder_Descending))
275 reason = MenuCancel_NoDisplay;
276 end_reason = MenuEnd_Cancelled;
280 } else if (type == ItemSel_Next) {
281 if (!RedoClientMenu(client, ItemOrder_Ascending))
283 cancel = true; /* I like Saltines. */
284 reason = MenuCancel_NoDisplay;
285 end_reason = MenuEnd_Cancelled;
289 } else if (type == ItemSel_Exit || type == ItemSel_None) {
291 reason = MenuCancel_Exit;
292 end_reason = MenuEnd_Exit;
293 } else if (type == ItemSel_ExitBack) {
295 reason = MenuCancel_ExitBack;
296 end_reason = MenuEnd_ExitBack;
298 item = states.slots[key_press].item;
303 IMenuHandler *mh = states.mh;
304 IBaseMenu *menu = states.menu;
307 player->bInMenu = false;
308 if (player->menuHoldTime)
310 RemoveClientFromWatch(client);
315 mh->OnMenuCancel(menu, client, reason);
317 mh->OnMenuSelect(menu, client, item);
320 /* Only fire end for valid menus */
323 mh->OnMenuEnd(menu, end_reason);
327 bool BaseMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
329 #if defined MENU_DEBUG
330 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (panel %p) (mh %p) (time %d)",
336 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
337 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
342 CBaseMenuPlayer *player = GetMenuPlayer(client);
343 if (player->bAutoIgnore)
348 /* For the duration of this, we are going to totally ignore whether
349 * the player is already in a menu or not (except to cancel the old one).
350 * Instead, we are simply going to ignore any further menu displays, so
351 * this display can't be interrupted.
353 player->bAutoIgnore = true;
355 /* Cancel any old menus */
356 menu_states_t &states = player->states;
359 _CancelClientMenu(client, MenuCancel_Interrupted, true);
362 states.firstItem = 0;
366 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
367 player->bInMenu = true;
368 player->bInExternMenu = false;
369 player->menuStartTime = gpGlobals->curtime;
370 player->menuHoldTime = time;
374 AddClientToWatch(client);
377 /* Draw the display */
378 SendDisplay(client, menu);
380 /* We can be interrupted again! */
381 player->bAutoIgnore = false;
386 bool BaseMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
388 #if defined MENU_DEBUG
389 g_Logger.LogMessage("[SM_MENU] DoClientMenu() (client %d) (menu %p) (mh %p) (time %d)",
395 mh->OnMenuStart(menu);
397 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
398 if (!pPlayer || pPlayer->IsFakeClient() || !pPlayer->IsInGame())
400 #if defined MENU_DEBUG
401 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to display to client %d", client);
403 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
404 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
408 CBaseMenuPlayer *player = GetMenuPlayer(client);
409 if (player->bAutoIgnore)
411 #if defined MENU_DEBUG
412 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Client %d is autoIgnoring", client);
414 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
415 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
419 /* For the duration of this, we are going to totally ignore whether
420 * the player is already in a menu or not (except to cancel the old one).
421 * Instead, we are simply going to ignore any further menu displays, so
422 * this display can't be interrupted.
424 player->bAutoIgnore = true;
426 /* Cancel any old menus */
427 menu_states_t &states = player->states;
430 #if defined MENU_DEBUG
431 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Cancelling old menu to client %d", client);
433 _CancelClientMenu(client, MenuCancel_Interrupted, true);
436 states.firstItem = 0;
440 states.apiVers = SMINTERFACE_MENUMANAGER_VERSION;
442 IMenuPanel *display = g_Menus.RenderMenu(client, states, ItemOrder_Ascending);
445 #if defined MENU_DEBUG
446 g_Logger.LogMessage("[SM_MENU] DoClientMenu(): Failed to render to client %d", client);
448 player->bAutoIgnore = false;
449 player->bInMenu = false;
450 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
451 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
455 /* Finally, set our states */
456 player->bInMenu = true;
457 player->bInExternMenu = false;
458 player->menuStartTime = gpGlobals->curtime;
459 player->menuHoldTime = time;
463 AddClientToWatch(client);
466 /* Draw the display */
467 SendDisplay(client, display);
469 /* Free the display pointer */
470 display->DeleteThis();
472 /* We can be interrupted again! */
473 player->bAutoIgnore = false;
475 #if defined MENU_DEBUG
476 g_Logger.LogMessage("[SM_MENU] DoClientMenu() finished successfully (client %d)", client);
482 bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
484 #if defined MENU_DEBUG
485 g_Logger.LogMessage("[SM_MENU] RedoClientMenu() (client %d) (order %d)", client, order);
487 CBaseMenuPlayer *player = GetMenuPlayer(client);
488 menu_states_t &states = player->states;
490 player->bAutoIgnore = true;
491 IMenuPanel *display = g_Menus.RenderMenu(client, states, order);
494 #if defined MENU_DEBUG
495 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Failed to render menu");
497 if (player->menuHoldTime)
499 RemoveClientFromWatch(client);
501 player->bAutoIgnore = false;
505 SendDisplay(client, display);
507 display->DeleteThis();
509 player->bAutoIgnore = false;
511 #if defined MENU_DEBUG
512 g_Logger.LogMessage("[SM_MENU] RedoClientMenu(): Succeeded to client %d", client);
518 CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) :
519 m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_ExitButton(true),
520 m_bShouldDelete(false), m_bCancelling(false), m_pOwner(pOwner ? pOwner : g_pCoreIdent),
521 m_bDeleting(false), m_bWillFreeHandle(false), m_hHandle(BAD_HANDLE), m_pHandler(pHandler),
522 m_pVoteHandler(NULL), m_ExitBackButton(false)
526 CBaseMenu::~CBaseMenu()
528 g_Menus.ReleaseVoteWrapper(m_pVoteHandler);
531 Handle_t CBaseMenu::GetHandle()
535 m_hHandle = g_Menus.CreateMenuHandle(this, m_pOwner);
541 bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
543 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
544 && m_items.size() >= m_pStyle->GetMaxPageItems())
551 item.infoString = m_Strings.AddString(info);
554 item.displayString = m_Strings.AddString(draw.display);
556 item.style = draw.style;
559 m_items.push_back(item);
564 bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
566 if (m_Pagination == (unsigned)MENU_NO_PAGINATION
567 && m_items.size() >= m_pStyle->GetMaxPageItems())
572 if (position >= m_items.size())
578 item.infoString = m_Strings.AddString(info);
581 item.displayString = m_Strings.AddString(draw.display);
583 item.style = draw.style;
585 CVector<CItem>::iterator iter = m_items.iterAt(position);
586 m_items.insert(iter, item);
591 bool CBaseMenu::RemoveItem(unsigned int position)
593 if (position >= m_items.size())
598 m_items.erase(m_items.iterAt(position));
600 if (m_items.size() == 0)
608 void CBaseMenu::RemoveAllItems()
614 const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
616 if (position >= m_items.size())
623 draw->display = m_Strings.GetString(m_items[position].displayString);
624 draw->style = m_items[position].style;
627 return m_Strings.GetString(m_items[position].infoString);
630 unsigned int CBaseMenu::GetItemCount()
632 return m_items.size();
635 bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
637 if (itemsPerPage > 7 || itemsPerPage == 1)
642 m_Pagination = itemsPerPage;
647 unsigned int CBaseMenu::GetPagination()
652 IMenuStyle *CBaseMenu::GetDrawStyle()
657 void CBaseMenu::SetDefaultTitle(const char *message)
659 m_Title.assign(message);
662 const char *CBaseMenu::GetDefaultTitle()
664 return m_Title.c_str();
667 bool CBaseMenu::GetExitButton()
672 bool CBaseMenu::SetExitButton(bool set)
678 void CBaseMenu::Cancel()
685 #if defined MENU_DEBUG
686 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Cancel(%p) (m_pVote %p) (inVote %d) (m_bShouldDelete %d)",
689 m_pVoteHandler ? m_pVoteHandler->IsVoteInProgress() : false,
693 if (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress())
695 m_pVoteHandler->CancelVoting();
698 m_bCancelling = true;
700 m_bCancelling = false;
708 void CBaseMenu::Destroy(bool releaseHandle)
710 /* Check if we shouldn't be here */
716 #if defined MENU_DEBUG
717 g_Logger.LogMessage("[SM_MENU] CBaseMenu::Destroy(%p) (release %d) (m_bCancelling %d) (m_bShouldDelete %d)",
724 /* Save the destruction hint about our handle */
725 m_bWillFreeHandle = releaseHandle;
727 /* Now actually do stuff */
728 if (!m_bCancelling || m_bShouldDelete)
733 m_bShouldDelete = true;
737 void CBaseMenu::InternalDelete()
739 if (m_bWillFreeHandle && m_hHandle != BAD_HANDLE)
741 Handle_t hndl = m_hHandle;
744 sec.pOwner = m_pOwner;
745 sec.pIdentity = g_pCoreIdent;
747 m_hHandle = BAD_HANDLE;
749 g_HandleSys.FreeHandle(hndl, &sec);
752 m_pHandler->OnMenuDestroy(this);
757 bool CBaseMenu::BroadcastVote(int clients[],
758 unsigned int numClients,
759 unsigned int maxTime,
762 #if defined MENU_DEBUG
763 g_Logger.LogMessage("[SM_MENU] CBaseMenu::BroadcastVote(%p) (maxTime %d) (numClients %d) (m_pVote %p) (inVote %d)",
768 m_pVoteHandler ? m_pVoteHandler->IsVoteInProgress() : false);
772 m_pVoteHandler = g_Menus.CreateVoteWrapper(m_pHandler);
773 } else if (m_pVoteHandler->IsVoteInProgress()) {
777 m_pVoteHandler->InitializeVoting(this);
779 for (unsigned int i=0; i<numClients; i++)
781 VoteDisplay(clients[i], maxTime);
784 m_pVoteHandler->StartVoting();
789 bool CBaseMenu::IsVoteInProgress()
791 return (m_pVoteHandler && m_pVoteHandler->IsVoteInProgress());
794 bool CBaseMenu::GetExitBackButton()
796 return m_ExitBackButton;
799 void CBaseMenu::SetExitBackButton(bool set)
801 m_ExitBackButton = set;