core/MenuManager.cpp
changeset 1269 27397584df9c
parent 1258 f89bcd42fff5
child 1291 c6d26de53926
     1.1 --- a/core/MenuManager.cpp	Tue Aug 07 22:11:00 2007 +0000
     1.2 +++ b/core/MenuManager.cpp	Wed Aug 08 02:27:10 2007 +0000
     1.3 @@ -34,6 +34,7 @@
     1.4  #include <stdarg.h>
     1.5  #include <stdlib.h>
     1.6  #include "MenuManager.h"
     1.7 +#include "MenuVoting.h"
     1.8  #include "sm_stringutil.h"
     1.9  #include "sourcemm_api.h"
    1.10  #include "PlayerManager.h"
    1.11 @@ -43,220 +44,10 @@
    1.12  #include "sourcemm_api.h"
    1.13  
    1.14  MenuManager g_Menus;
    1.15 +VoteMenuHandler s_VoteHandler;
    1.16  
    1.17  ConVar sm_menu_sounds("sm_menu_sounds", "1", 0, "Sets whether SourceMod menus play trigger sounds");
    1.18  
    1.19 -/*******************************
    1.20 - *******************************
    1.21 - ******** VOTE HANDLER *********
    1.22 - *******************************
    1.23 - *******************************/
    1.24 -
    1.25 -unsigned int VoteMenuHandler::GetMenuAPIVersion2()
    1.26 -{
    1.27 -	return m_pHandler->GetMenuAPIVersion2();
    1.28 -}
    1.29 -
    1.30 -bool VoteMenuHandler::IsVoteInProgress()
    1.31 -{
    1.32 -	return (m_pCurMenu != NULL);
    1.33 -}
    1.34 -
    1.35 -void VoteMenuHandler::InitializeVoting(IBaseMenu *menu)
    1.36 -{
    1.37 -	m_Items = menu->GetItemCount();
    1.38 -
    1.39 -	if (m_Votes.size() < (size_t)m_Items)
    1.40 -	{
    1.41 -		/* Only clear the items we need to... */
    1.42 -		size_t size = m_Votes.size();
    1.43 -		for (size_t i=0; i<size; i++)
    1.44 -		{
    1.45 -			m_Votes[i] = 0;
    1.46 -		}
    1.47 -		m_Votes.resize(m_Items, 0);
    1.48 -	} else {
    1.49 -		for (unsigned int i=0; i<m_Items; i++)
    1.50 -		{
    1.51 -			m_Votes[i] = 0;
    1.52 -		}
    1.53 -	}
    1.54 -
    1.55 -	m_pCurMenu = menu;
    1.56 -
    1.57 -	m_pHandler->OnMenuStart(m_pCurMenu);
    1.58 -}
    1.59 -
    1.60 -void VoteMenuHandler::StartVoting()
    1.61 -{
    1.62 -	m_bStarted = true;
    1.63 -
    1.64 -	m_pHandler->OnMenuVoteStart(m_pCurMenu);
    1.65 -
    1.66 -	/* By now we know how many clients were set.  
    1.67 -	 * If there are none, we should end IMMEDIATELY.
    1.68 -	 */
    1.69 -	if (m_Clients == 0)
    1.70 -	{
    1.71 -		EndVoting();
    1.72 -	}
    1.73 -}
    1.74 -
    1.75 -void VoteMenuHandler::DecrementPlayerCount()
    1.76 -{
    1.77 -	assert(m_Clients > 0);
    1.78 -
    1.79 -	m_Clients--;
    1.80 -
    1.81 -	if (m_bStarted && m_Clients == 0)
    1.82 -	{
    1.83 -		EndVoting();
    1.84 -	}
    1.85 -}
    1.86 -
    1.87 -void VoteMenuHandler::EndVoting()
    1.88 -{
    1.89 -	if (m_bCancelled)
    1.90 -	{
    1.91 -		/* If we were cancelled, don't bother tabulating anything.
    1.92 -		 * Reset just in case someone tries to redraw, which means
    1.93 -		 * we need to save our states.
    1.94 -		 */
    1.95 -		IBaseMenu *menu = m_pCurMenu;
    1.96 -		InternalReset();
    1.97 -		m_pHandler->OnMenuVoteCancel(menu);
    1.98 -		m_pHandler->OnMenuEnd(menu, MenuEnd_VotingCancelled);
    1.99 -		return;
   1.100 -	}
   1.101 -
   1.102 -	unsigned int chosen = 0;
   1.103 -	unsigned int highest = 0;
   1.104 -	unsigned int dup_count = 0;
   1.105 -	unsigned int total = m_Votes.size() ? m_Votes[0] : 0;
   1.106 -
   1.107 -	/* If we got zero votes, take a shortcut. */
   1.108 -	if (m_NumVotes == 0)
   1.109 -	{
   1.110 -		/* Pick a random item and then jump far, far away. */
   1.111 -		srand((unsigned int)(time(NULL)));
   1.112 -		chosen = (unsigned int)rand() % m_Items;
   1.113 -		goto picked_item;
   1.114 -	}
   1.115 -
   1.116 -	/* We can't have more dups than this!
   1.117 -	 * This is the max number of players.
   1.118 -	 */
   1.119 -	unsigned int dup_array[256];
   1.120 -
   1.121 -	for (size_t i=1; i<m_Items; i++)
   1.122 -	{
   1.123 -		if (m_Votes[i] > m_Votes[highest])
   1.124 -		{
   1.125 -			/* If we have a new highest count, mark it and trash the duplicate
   1.126 -			 * list by setting the total to 0.
   1.127 -			 */
   1.128 -			highest = i;
   1.129 -			dup_count = 0;
   1.130 -		} else if (m_Votes[i] == m_Votes[highest]) {
   1.131 -			/* If they're equal, mark it in the duplicate list.
   1.132 -			 * We'll add in the original later.
   1.133 -			 */
   1.134 -			dup_array[dup_count++] = i;
   1.135 -		}
   1.136 -		total += m_Votes[i];
   1.137 -	}
   1.138 -
   1.139 -	/* Check if we need to pick from the duplicate list */
   1.140 -	if (dup_count)
   1.141 -	{
   1.142 -		/* Re-add the original to the list because it's not in there. */
   1.143 -		dup_array[dup_count++] = highest;
   1.144 -
   1.145 -		/* Pick a random slot. */
   1.146 -		srand((unsigned int)(time(NULL)));
   1.147 -		unsigned int r = (unsigned int)rand() % dup_count;
   1.148 -
   1.149 -		/* Pick the item. */
   1.150 -		chosen = dup_array[r];
   1.151 -	} else {
   1.152 -		chosen = highest;
   1.153 -	}
   1.154 -
   1.155 -picked_item:
   1.156 -	m_pHandler->OnMenuVoteEnd(m_pCurMenu, chosen, m_Votes[highest], total);
   1.157 -	m_pHandler->OnMenuEnd(m_pCurMenu, MenuEnd_VotingDone);
   1.158 -	InternalReset();
   1.159 -}
   1.160 -
   1.161 -void VoteMenuHandler::OnMenuStart(IBaseMenu *menu)
   1.162 -{
   1.163 -	m_Clients++;
   1.164 -}
   1.165 -
   1.166 -void VoteMenuHandler::OnMenuEnd(IBaseMenu *menu, MenuEndReason reason)
   1.167 -{
   1.168 -	DecrementPlayerCount();
   1.169 -}
   1.170 -
   1.171 -void VoteMenuHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
   1.172 -{
   1.173 -	m_pHandler->OnMenuCancel(menu, client, reason);
   1.174 -}
   1.175 -
   1.176 -void VoteMenuHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
   1.177 -{
   1.178 -	m_pHandler->OnMenuDisplay(menu, client, display);
   1.179 -}
   1.180 -
   1.181 -unsigned int VoteMenuHandler::OnMenuDisplayItem(IBaseMenu *menu, int client, IMenuPanel *panel, unsigned int item, const ItemDrawInfo &dr)
   1.182 -{
   1.183 -	return m_pHandler->OnMenuDisplayItem(menu, client, panel, item, dr);
   1.184 -}
   1.185 -
   1.186 -void VoteMenuHandler::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
   1.187 -{
   1.188 -	m_pHandler->OnMenuDrawItem(menu, client, item, style);
   1.189 -}
   1.190 -
   1.191 -void VoteMenuHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
   1.192 -{
   1.193 -	/* Check by our item count, NOT the vote array size */
   1.194 -	if (item < m_Items)
   1.195 -	{
   1.196 -		m_Votes[item]++;
   1.197 -		m_NumVotes++;
   1.198 -	}
   1.199 -
   1.200 -	m_pHandler->OnMenuSelect(menu, client, item);
   1.201 -}
   1.202 -
   1.203 -void VoteMenuHandler::Reset(IMenuHandler *mh)
   1.204 -{
   1.205 -	m_pHandler = mh;
   1.206 -	InternalReset();
   1.207 -}
   1.208 -
   1.209 -void VoteMenuHandler::InternalReset()
   1.210 -{
   1.211 -	m_Clients = 0;
   1.212 -	m_Items = 0;
   1.213 -	m_bStarted = false;
   1.214 -	m_pCurMenu = NULL;
   1.215 -	m_NumVotes = 0;
   1.216 -	m_bCancelled = false;
   1.217 -}
   1.218 -
   1.219 -void VoteMenuHandler::CancelVoting()
   1.220 -{
   1.221 -	m_bCancelled = true;
   1.222 -}
   1.223 -
   1.224 -/*******************************
   1.225 - *******************************
   1.226 - ******** MENU MANAGER *********
   1.227 - *******************************
   1.228 - *******************************/
   1.229 -
   1.230  MenuManager::MenuManager()
   1.231  {
   1.232  	m_Styles.push_back(&g_ValveMenuStyle);
   1.233 @@ -283,12 +74,6 @@
   1.234  {
   1.235  	g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
   1.236  	g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
   1.237 -
   1.238 -	while (!m_VoteHandlers.empty())
   1.239 -	{
   1.240 -		delete m_VoteHandlers.front();
   1.241 -		m_VoteHandlers.pop();
   1.242 -	}
   1.243  }
   1.244  
   1.245  void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
   1.246 @@ -649,8 +434,8 @@
   1.247  	if (pgn != MENU_NO_PAGINATION)
   1.248  	{
   1.249  		bool canDrawDisabled = display->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
   1.250 -		bool exitButton = menu->GetExitButton();
   1.251 -		bool exitBackButton = menu->GetExitBackButton();
   1.252 +		bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT;
   1.253 +		bool exitBackButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK;
   1.254  		char text[50];
   1.255  
   1.256  		/* Calculate how many items we are allowed for control stuff */
   1.257 @@ -798,33 +583,6 @@
   1.258  	return m_pDefaultStyle;
   1.259  }
   1.260  
   1.261 -IVoteMenuHandler *MenuManager::CreateVoteWrapper(IMenuHandler *mh)
   1.262 -{
   1.263 -	VoteMenuHandler *vh = NULL;
   1.264 -
   1.265 -	if (m_VoteHandlers.empty())
   1.266 -	{
   1.267 -		vh = new VoteMenuHandler;
   1.268 -	} else {
   1.269 -		vh = m_VoteHandlers.front();
   1.270 -		m_VoteHandlers.pop();
   1.271 -	}
   1.272 -
   1.273 -	vh->Reset(mh);
   1.274 -
   1.275 -	return vh;
   1.276 -}
   1.277 -
   1.278 -void MenuManager::ReleaseVoteWrapper(IVoteMenuHandler *mh)
   1.279 -{
   1.280 -	if (mh == NULL)
   1.281 -	{
   1.282 -		return;
   1.283 -	}
   1.284 -
   1.285 -	m_VoteHandlers.push((VoteMenuHandler *)mh);
   1.286 -}
   1.287 -
   1.288  bool MenuManager::MenuSoundsEnabled()
   1.289  {
   1.290  	return (sm_menu_sounds.GetInt() != 0);
   1.291 @@ -879,7 +637,7 @@
   1.292  		{
   1.293  			if (m_ExitSound.size() > 0)
   1.294  			{
   1.295 -				sound= m_ExitSound.c_str();
   1.296 +				sound = m_ExitSound.c_str();
   1.297  			}
   1.298  			break;
   1.299  		}
   1.300 @@ -907,3 +665,30 @@
   1.301  		enginesound->PrecacheSound(m_ExitSound.c_str(), true);
   1.302  	}
   1.303  }
   1.304 +
   1.305 +void MenuManager::CancelMenu(IBaseMenu *menu)
   1.306 +{
   1.307 +	if (s_VoteHandler.GetCurrentMenu() == menu
   1.308 +		&& !s_VoteHandler.IsCancelling())
   1.309 +	{
   1.310 +		s_VoteHandler.CancelVoting();
   1.311 +		return;
   1.312 +	}
   1.313 +
   1.314 +	menu->Cancel();
   1.315 +}
   1.316 +
   1.317 +bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
   1.318 +{
   1.319 +	return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
   1.320 +}
   1.321 +
   1.322 +bool MenuManager::IsVoteInProgress()
   1.323 +{
   1.324 +	return s_VoteHandler.IsVoteInProgress();
   1.325 +}
   1.326 +
   1.327 +void MenuManager::CancelVoting()
   1.328 +{
   1.329 +	s_VoteHandler.CancelVoting();
   1.330 +}