core/MenuManager.cpp
author David Anderson <dvander@alliedmods.net>
Tue May 22 20:50:48 2007 +0000 (2007-05-22)
changeset 833 732bedfcbe3e
parent 797 8ed539963aa4
child 877 898c39fbfde9
permissions -rw-r--r--
added ReplaceString and ReplaceStringEx (phew!)
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     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
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 
    16 #include <stdarg.h>
    17 #include <time.h>
    18 #include "MenuManager.h"
    19 #include "sm_stringutil.h"
    20 #include "sourcemm_api.h"
    21 #include "PlayerManager.h"
    22 #include "MenuStyle_Valve.h"
    23 #include "ShareSys.h"
    24 #include "HandleSys.h"
    25 
    26 MenuManager g_Menus;
    27 
    28 /*************************************
    29  *************************************
    30  **** BROADCAST HANDLING WRAPPERS ****
    31  *************************************
    32  *************************************/
    33 
    34 BroadcastHandler::BroadcastHandler(IMenuHandler *handler) : m_pHandler(handler), numClients(0)
    35 {
    36 }
    37 
    38 unsigned int BroadcastHandler::GetMenuAPIVersion2()
    39 {
    40 	return m_pHandler->GetMenuAPIVersion2();
    41 }
    42 
    43 void BroadcastHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
    44 {
    45 	m_pHandler->OnMenuCancel(menu, client, reason);
    46 }
    47 
    48 void BroadcastHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuPanel *display)
    49 {
    50 	numClients++;
    51 	m_pHandler->OnMenuDisplay(menu, client, display);
    52 }
    53 
    54 void BroadcastHandler::OnBroadcastEnd(IBaseMenu *menu)
    55 {
    56 	g_Menus.FreeBroadcastHandler(this);
    57 }
    58 
    59 void BroadcastHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
    60 {
    61 	m_pHandler->OnMenuSelect(menu, client, item);
    62 }
    63 
    64 void BroadcastHandler::OnMenuEnd(IBaseMenu *menu)
    65 {
    66 	assert(numClients > 0);
    67 
    68 	/* Only fire if all clients have gotten a menu end */
    69 	if (--numClients == 0)
    70 	{
    71 		IMenuHandler *pHandler = m_pHandler;
    72 		OnBroadcastEnd(menu);
    73 		pHandler->OnMenuEnd(menu);
    74 	}
    75 }
    76 
    77 VoteHandler::VoteHandler(IMenuVoteHandler *handler) 
    78 : BroadcastHandler(handler), m_pVoteHandler(handler)
    79 {
    80 }
    81 
    82 void VoteHandler::Initialize(IBaseMenu *menu)
    83 {
    84 	unsigned int numItems = menu->GetItemCount();
    85 
    86 	if (m_counts.size() >= numItems)
    87 	{
    88 		for (size_t i=0; i<numItems; i++)
    89 		{
    90 			m_counts[i] = 0;
    91 		}
    92 	} else {
    93 		for (size_t i=0; i<m_counts.size(); i++)
    94 		{
    95 			m_counts[i] = 0;
    96 		}
    97 		m_counts.resize(numItems, 0);
    98 	}
    99 }
   100 
   101 void VoteHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
   102 {
   103 	if (item < numItems)
   104 	{
   105 		m_counts[item]++;
   106 	}
   107 
   108 	BroadcastHandler::OnMenuSelect(menu, client, item);
   109 }
   110 
   111 void VoteHandler::OnBroadcastEnd(IBaseMenu *menu)
   112 {
   113 	m_ties.clear();
   114 
   115 	size_t highest = 0;
   116 	for (size_t i=1; i<numItems; i++)
   117 	{
   118 		if (m_counts[i] > m_counts[highest])
   119 		{
   120 			m_ties.clear();
   121 			highest = i;
   122 		} else if (m_counts[i] == m_counts[highest]) {
   123 			m_ties.push_back(i);
   124 		}
   125 	}
   126 
   127 	if (m_ties.size())
   128 	{
   129 		m_ties.push_back(highest);
   130 		srand(static_cast<unsigned int>(time(NULL)));
   131 		highest = m_ties[rand() % m_ties.size()];
   132 	}
   133 
   134 	m_pVoteHandler->OnMenuVoteEnd(menu, highest);
   135 
   136 	g_Menus.FreeVoteHandler(this);
   137 }
   138 
   139 /*******************************
   140  *******************************
   141  ******** MENU MANAGER *********
   142  *******************************
   143  *******************************/
   144 
   145 MenuManager::MenuManager()
   146 {
   147 	m_Styles.push_back(&g_ValveMenuStyle);
   148 	SetDefaultStyle(&g_ValveMenuStyle);
   149 }
   150 
   151 void MenuManager::OnSourceModAllInitialized()
   152 {
   153 	g_ShareSys.AddInterface(NULL, this);
   154 
   155 	HandleAccess access;
   156 	g_HandleSys.InitAccessDefaults(NULL, &access);
   157 
   158 	/* Deny cloning to menus */
   159 	access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
   160 	m_MenuType = g_HandleSys.CreateType("IBaseMenu", this, 0, NULL, &access, g_pCoreIdent, NULL);
   161 
   162 	/* Also deny deletion to styles */
   163 	access.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
   164 	m_StyleType = g_HandleSys.CreateType("IMenuStyle", this, 0, NULL, &access, g_pCoreIdent, NULL);
   165 }
   166 
   167 void MenuManager::OnSourceModAllShutdown()
   168 {
   169 	g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
   170 	g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
   171 
   172 	while (!m_BroadcastHandlers.empty())
   173 	{
   174 		delete m_BroadcastHandlers.front();
   175 		m_BroadcastHandlers.pop();
   176 	}
   177 
   178 	while (!m_VoteHandlers.empty())
   179 	{
   180 		delete m_VoteHandlers.front();
   181 		m_VoteHandlers.pop();
   182 	}
   183 }
   184 
   185 void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
   186 {
   187 	if (type == m_MenuType)
   188 	{
   189 		IBaseMenu *menu = (IBaseMenu *)object;
   190 		menu->Destroy(false);
   191 	} else if (type == m_StyleType) {
   192 		/* Do nothing */
   193 	}
   194 }
   195 
   196 Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
   197 {
   198 	if (m_MenuType == NO_HANDLE_TYPE)
   199 	{
   200 		return BAD_HANDLE;
   201 	}
   202 
   203 	return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
   204 }
   205 
   206 Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
   207 {
   208 	if (m_StyleType == NO_HANDLE_TYPE)
   209 	{
   210 		return BAD_HANDLE;
   211 	}
   212 
   213 	return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
   214 }
   215 
   216 HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
   217 {
   218 	HandleSecurity sec;
   219 
   220 	sec.pIdentity = g_pCoreIdent;
   221 	sec.pOwner = NULL;
   222 
   223 	return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
   224 }
   225 
   226 HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
   227 {
   228 	HandleSecurity sec;
   229 
   230 	sec.pIdentity = g_pCoreIdent;
   231 	sec.pOwner = g_pCoreIdent;
   232 
   233 	return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
   234 }
   235 
   236 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
   237 {
   238 	if (!style)
   239 	{
   240 		return false;
   241 	}
   242 
   243 	m_pDefaultStyle = style;
   244 
   245 	return true;
   246 }
   247 
   248 IMenuStyle *MenuManager::GetStyle(unsigned int index)
   249 {
   250 	if (index >= GetStyleCount())
   251 	{
   252 		return NULL;
   253 	}
   254 
   255 	return m_Styles[index];
   256 }
   257 
   258 void MenuManager::AddStyle(IMenuStyle *style)
   259 {
   260 	m_Styles.push_back(style);
   261 }
   262 
   263 unsigned int MenuManager::GetStyleCount()
   264 {
   265 	return (unsigned int)m_Styles.size();
   266 }
   267 
   268 IMenuStyle *MenuManager::FindStyleByName(const char *name)
   269 {
   270 	unsigned int count = GetStyleCount();
   271 	for (unsigned int i=0; i<count; i++)
   272 	{
   273 		IMenuStyle *ptr = GetStyle(i);
   274 		if (strcasecmp(ptr->GetStyleName(), name) == 0)
   275 		{
   276 			return ptr;
   277 		}
   278 	}
   279 
   280 	return NULL;
   281 }
   282 
   283 inline bool IsSlotItem(IMenuPanel *display,
   284 					   unsigned int style)
   285 {
   286 	if (!display->CanDrawItem(style))
   287 	{
   288 	return false;
   289 	}
   290 	if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
   291 	{
   292 		return false;
   293 	}
   294 	if (style & ITEMDRAW_RAWLINE)
   295 	{
   296 		return false;
   297 	}
   298 
   299 	return true;
   300 }
   301 
   302 IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
   303 {
   304 	IBaseMenu *menu = md.menu;
   305 
   306 	if (!menu)
   307 	{
   308 		return NULL;
   309 	}
   310 
   311 	struct
   312 	{
   313 		unsigned int position;
   314 		ItemDrawInfo draw;
   315 	} drawItems[10];
   316 
   317 	/* Figure out how many items to draw */
   318 	IMenuStyle *style = menu->GetDrawStyle();
   319 	unsigned int pgn = menu->GetPagination();
   320 	unsigned int maxItems = style->GetMaxPageItems();
   321 	if (pgn != MENU_NO_PAGINATION)
   322 	{
   323 		maxItems = pgn;
   324 	}
   325 
   326 	unsigned int totalItems = menu->GetItemCount();
   327 	unsigned int startItem = 0;
   328 
   329 	/* For pagination, find the starting point. */
   330 	if (pgn != MENU_NO_PAGINATION)
   331 	{
   332 		if (order == ItemOrder_Ascending)
   333 		{
   334 			startItem = md.lastItem;
   335 			/* This shouldn't happen with well-coded menus.
   336 			 * If the item is out of bounds, switch the order to
   337 			 * Items_Descending and make us start from the top.
   338 			 */
   339 			if (startItem >= totalItems)
   340 			{
   341 				startItem = totalItems - 1;
   342 				order = ItemOrder_Descending;
   343 			}
   344 		} else if (order == ItemOrder_Descending) {
   345 			startItem = md.firstItem;
   346 			/* This shouldn't happen with well-coded menus.
   347 			 * If searching backwards doesn't give us enough room,
   348 			 * start from the beginning and change to ascending.
   349 			 */
   350 			if (startItem <= maxItems)
   351 			{
   352 				startItem = 0;
   353 				order = ItemOrder_Ascending;
   354 			}
   355 		}
   356 	}
   357 
   358 	/* Get our Display pointer and initialize some crap */
   359 	IMenuPanel *display = menu->CreatePanel();
   360 	IMenuHandler *mh = md.mh;
   361 	bool foundExtra = false;
   362 	unsigned int extraItem = 0;
   363 
   364 	/**
   365 	 * We keep searching until:
   366 	 * 1) There are no more items
   367 	 * 2) We reach one OVER the maximum number of slot items
   368 	 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
   369 	 */
   370 	unsigned int i = startItem;
   371 	unsigned int foundItems = 0;
   372 	while (true)
   373 	{
   374 		ItemDrawInfo &dr = drawItems[foundItems].draw;
   375 		/* Is the item valid? */
   376 		if (menu->GetItemInfo(i, &dr) != NULL)
   377 		{
   378 			/* Ask the user to change the style, if necessary */
   379 			mh->OnMenuDrawItem(menu, client, i, dr.style);
   380 			/* Check if it's renderable */
   381 			if (IsSlotItem(display, dr.style))
   382 			{
   383 				/* If we've already found the max number of items,
   384 				 * This means we should just cancel out and log our
   385 				 * "last item."
   386 				 */
   387 				if (foundItems >= maxItems)
   388 				{
   389 					foundExtra = true;
   390 					extraItem = i;
   391 					break;
   392 				}
   393 				drawItems[foundItems++].position = i;
   394 			}
   395 		}
   396 		/* If there's no pagination, stop once the menu is full. */
   397 		if (pgn == MENU_NO_PAGINATION)
   398 		{
   399 			/* If we've filled up, then stop */
   400 			if (foundItems >= maxItems)
   401 			{
   402 				break;
   403 			}
   404 		}
   405 		/* If we're descending and this is the first item, stop */
   406 		if (order == ItemOrder_Descending)
   407 		{
   408 			if (i == 0)
   409 			{
   410 				break;
   411 			}
   412 			i--;
   413 		} 
   414 		/* If we're ascending and this is the last item, stop */
   415 		else if (order == ItemOrder_Ascending)
   416 		{
   417 			if (i >= totalItems - 1)
   418 			{
   419 				break;
   420 			}
   421 			i++;
   422 		}
   423 	}
   424 
   425 	/* There were no items to draw! */
   426 	if (!foundItems)
   427 	{
   428 		display->DeleteThis();
   429 		return NULL;
   430 	}
   431 
   432 	/* Check initial buttons */
   433 	bool displayPrev = false;
   434 	bool displayNext = false;
   435 	if (foundExtra)
   436 	{
   437 		if (order == ItemOrder_Descending)
   438 		{
   439 			displayPrev = true;
   440 			md.firstItem = extraItem;
   441 		} else if (order == ItemOrder_Ascending) {
   442 			displayNext = true;
   443 			md.lastItem = extraItem;
   444 		}
   445 	}
   446 
   447 	/**
   448 	 * If we're paginated, we have to find if there is another page.
   449 	 * Sadly, the only way to do this is to try drawing more items!
   450 	 */
   451 	if (pgn != MENU_NO_PAGINATION)
   452 	{
   453 		unsigned int lastItem = 0;
   454 		ItemDrawInfo dr;
   455 		/* Find the last feasible item to search from. */
   456 		if (order == ItemOrder_Descending)
   457 		{
   458 			lastItem = drawItems[0].position;
   459 			if (lastItem >= totalItems - 1)
   460 			{
   461 				goto skip_search;
   462 			}
   463 			while (++lastItem < totalItems)
   464 			{
   465 				if (menu->GetItemInfo(lastItem, &dr) != NULL)
   466 				{
   467 					mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
   468 					if (IsSlotItem(display, dr.style))
   469 					{
   470 						displayNext = true;
   471 						md.lastItem = lastItem;
   472 						break;
   473 					}
   474 				}
   475 			}
   476 		} else if (order == ItemOrder_Ascending) {
   477 			lastItem = drawItems[0].position;
   478 			if (lastItem == 0)
   479 			{
   480 				goto skip_search;
   481 			}
   482 			lastItem--;
   483 			while (lastItem-- != 0)
   484 			{
   485 				if (menu->GetItemInfo(lastItem, &dr) != NULL)
   486 				{
   487 					mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
   488 					if (IsSlotItem(display, dr.style))
   489 					{
   490 						displayPrev = true;
   491 						md.firstItem = lastItem;
   492 						break;
   493 					}
   494 				}
   495 			}
   496 		}
   497 	}
   498 skip_search:
   499 
   500 	/* Draw the item according to the order */
   501 	menu_slots_t *slots = md.slots;
   502 	unsigned int position = 0;			/* Keep track of the last position */
   503 	if (order == ItemOrder_Ascending)
   504 	{
   505 		for (unsigned int i=0; i<foundItems; i++)
   506 		{
   507 			ItemDrawInfo &dr = drawItems[i].draw;
   508 			menu->GetItemInfo(drawItems[i].position, &dr);
   509 			mh->OnMenuDisplayItem(menu, client, drawItems[i].position, &(dr.display));
   510 			if ((position = display->DrawItem(dr)) != 0)
   511 			{
   512 				slots[position].item = drawItems[i].position;
   513 				slots[position].type = ItemSel_Item;
   514 			}
   515 		}
   516 	} else if (order == ItemOrder_Descending) {
   517 		unsigned int i = foundItems;
   518 		/* NOTE: There will always be at least one item because
   519 		 * of the check earlier.
   520 		 */
   521 		while (i--)
   522 		{
   523 			ItemDrawInfo &dr = drawItems[i].draw;
   524 			menu->GetItemInfo(drawItems[i].position, &dr);
   525 			mh->OnMenuDisplayItem(menu, client, drawItems[i].position, &(dr.display));
   526 			if ((position = display->DrawItem(dr)) != 0)
   527 			{
   528 				slots[position].item = drawItems[i].position;
   529 				slots[position].type = ItemSel_Item;
   530 			}
   531 		}
   532 	}
   533 
   534 	/* Now, we need to check if we need to add anything extra */
   535 	if (pgn != MENU_NO_PAGINATION)
   536 	{
   537 		bool canDrawDisabled = display->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
   538 		bool exitButton = menu->GetExitButton();
   539 		char text[50];
   540 
   541 		/* Calculate how many items we are allowed for control stuff */
   542 		unsigned int padding = style->GetMaxPageItems() - maxItems;
   543 		
   544 		/* Add the number of available slots */
   545 		padding += (maxItems - foundItems);
   546 
   547 		/* Someday, if we are able to re-enable this, we will be very lucky men. */
   548 #if 0
   549 		if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
   550 		{
   551 #endif
   552 		/* Even if we don't draw an exit button, we invalidate the slot. */
   553 		padding--;
   554 #if 0
   555 		} else {
   556 			/* Otherwise, we don't draw anything and leave the slot available */
   557 			exitButton = false;
   558 		}
   559 #endif
   560 
   561 		/* Subtract two slots for the displayNext/displayPrev padding */
   562 		padding -= 2;
   563 
   564 		/**
   565 		 * We allow next/prev to be undrawn if neither exists.
   566 		 * Thus, we only need padding if one of them will be drawn,
   567 		 * or the exit button will be drawn.
   568 		 */
   569 		ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
   570 		if (exitButton || (displayNext || displayPrev))
   571 		{
   572 			/* If there are no control options,
   573 			 * Instead just pad with invisible slots.
   574 			 */
   575 			if (!displayPrev && !displayPrev)
   576 			{
   577 				padItem.style = ITEMDRAW_NOTEXT;
   578 			}
   579 			/* Add spacers so we can pad to the end */
   580 			for (unsigned int i=0; i<padding; i++)
   581 			{
   582 				position = display->DrawItem(padItem);
   583 				slots[position].type = ItemSel_None;
   584 			}
   585 		}
   586 
   587 		/* Put a fake spacer before control stuff, if possible */
   588 		if ((displayPrev || displayNext) || exitButton)
   589 		{
   590 			ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
   591 			display->DrawItem(draw);
   592 		}
   593 
   594 		ItemDrawInfo dr(text, 0);
   595 		/**
   596 		 * If we have one or the other, we need to have spacers for both.
   597 		 */
   598 		if (displayPrev || displayNext)
   599 		{
   600 			/* PREVIOUS */
   601 			ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
   602 			if (displayPrev || canDrawDisabled)
   603 			{
   604 				CorePlayerTranslate(client, text, sizeof(text), "Back", NULL);
   605 				dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
   606 				position = display->DrawItem(dr);
   607 				slots[position].type = ItemSel_Back;
   608 			} else if (displayNext || exitButton) {
   609 				/* If we can't display this, and there is an exit button,
   610 				 * we need to pad!
   611 				 */
   612 				position = display->DrawItem(padCtrlItem);
   613 				slots[position].type = ItemSel_None;
   614 			}
   615 
   616 			/* NEXT */
   617 			if (displayNext || canDrawDisabled)
   618 			{
   619 				CorePlayerTranslate(client, text, sizeof(text), "Next", NULL);
   620 				dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
   621 				position = display->DrawItem(dr);
   622 				slots[position].type = ItemSel_Next;
   623 			} else if (exitButton) {
   624 				/* If we can't display this,
   625 				 * but there is an "exit" button, we need to pad!
   626 				 */
   627 				position = display->DrawItem(padCtrlItem);
   628 				slots[position].type = ItemSel_None;
   629 			}
   630 		} else {
   631 			/* Otherwise, bump to two slots! */
   632 			ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
   633 			position = display->DrawItem(numBump);
   634 			slots[position].type = ItemSel_None;
   635 			position = display->DrawItem(numBump);
   636 			slots[position].type = ItemSel_None;
   637 		}
   638 
   639 		/* EXIT */
   640 		if (exitButton)
   641 		{
   642 			CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
   643 			dr.style = ITEMDRAW_CONTROL;
   644 			position = display->DrawItem(dr);
   645 			slots[position].type = ItemSel_Exit;
   646 		}
   647 	}
   648 
   649 	/* Lastly, fill in any slots we could have missed */
   650 	for (unsigned int i = position + 1; i < 10; i++)
   651 	{
   652 		slots[i].type = ItemSel_None;
   653 	}
   654 
   655 	/* Do title stuff */
   656 	mh->OnMenuDisplay(menu, client, display);
   657 	display->DrawTitle(menu->GetDefaultTitle(), true);
   658 
   659 	return display;
   660 }
   661 
   662 #if 0
   663 unsigned int MenuManager::BroadcastMenu(IBaseMenu *menu, 
   664 										IMenuHandler *handler, 
   665 										int clients[], 
   666 										unsigned int numClients, 
   667 										unsigned int time)
   668 {
   669 	BroadcastHandler *bh;
   670 
   671 	if (m_BroadcastHandlers.empty())
   672 	{
   673 		bh = new BroadcastHandler(handler);
   674 	} else {
   675 		bh = m_BroadcastHandlers.front();
   676 		m_BroadcastHandlers.pop();
   677 		bh->m_pHandler = handler;
   678 		bh->numClients = 0;
   679 	}
   680 
   681 	handler->OnMenuStart(menu);
   682 
   683 	unsigned int total = 0;
   684 	for (unsigned int i=0; i<numClients; i++)
   685 	{
   686 		/* Only continue if displaying works */
   687 		if (!menu->Display(clients[i], bh, time))
   688 		{
   689 			continue;
   690 		}
   691 
   692 		/* :TODO: Allow sourcetv only, not all bots */
   693 		CPlayer *player = g_Players.GetPlayerByIndex(clients[i]);
   694 		if (player->IsFakeClient())
   695 		{
   696 			continue;
   697 		}
   698 
   699 		total++;
   700 	}
   701 
   702 	if (!total)
   703 	{
   704 		/* End the broadcast here */
   705 		handler->OnMenuEnd(menu);
   706 		FreeBroadcastHandler(bh);
   707 	}
   708 
   709 	return total;
   710 }
   711 
   712 unsigned int MenuManager::VoteMenu(IBaseMenu *menu,
   713 								   IMenuVoteHandler *handler,
   714 								   int clients[],
   715 								   unsigned int numClients,
   716 								   unsigned int time)
   717 {
   718 	VoteHandler *vh;
   719 
   720 	if (m_VoteHandlers.empty())
   721 	{
   722 		vh = new VoteHandler(handler);
   723 	} else {
   724 		vh = m_VoteHandlers.front();
   725 		m_VoteHandlers.pop();
   726 		vh->m_pHandler = handler;
   727 		vh->numClients = 0;
   728 	}
   729 
   730 	vh->Initialize(menu);
   731 	handler->OnMenuStart(menu);
   732 
   733 	unsigned int total = 0;
   734 	for (unsigned int i=0; i<numClients; i++)
   735 	{
   736 		/* Only continue if displaying works */
   737 		if (!menu->Display(clients[i], vh, time))
   738 		{
   739 			continue;
   740 		}
   741 
   742 		/* :TODO: Allow sourcetv only, not all bots */
   743 		CPlayer *player = g_Players.GetPlayerByIndex(clients[i]);
   744 		if (player->IsFakeClient())
   745 		{
   746 			continue;
   747 		}
   748 
   749 		total++;
   750 	}
   751 
   752 	if (!total)
   753 	{
   754 		/* End the broadcast here */
   755 		handler->OnMenuEnd(menu);
   756 		FreeVoteHandler(vh);
   757 	}
   758 
   759 	return total;
   760 }
   761 #endif
   762 
   763 void MenuManager::FreeBroadcastHandler(BroadcastHandler *bh)
   764 {
   765 	m_BroadcastHandlers.push(bh);
   766 }
   767 
   768 void MenuManager::FreeVoteHandler(VoteHandler *vh)
   769 {
   770 	m_VoteHandlers.push(vh);
   771 }
   772 
   773 IMenuStyle *MenuManager::GetDefaultStyle()
   774 {
   775 	return m_pDefaultStyle;
   776 }
   777