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