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
18 #include "MenuManager.h"
19 #include "sm_stringutil.h"
20 #include "sourcemm_api.h"
21 #include "PlayerManager.h"
22 #include "MenuStyle_Valve.h"
26 /*************************************
27 *************************************
28 **** BROADCAST HANDLING WRAPPERS ****
29 *************************************
30 *************************************/
32 BroadcastHandler::BroadcastHandler(IMenuHandler *handler) : m_pHandler(handler), numClients(0)
36 unsigned int BroadcastHandler::GetMenuAPIVersion2()
38 return m_pHandler->GetMenuAPIVersion2();
41 void BroadcastHandler::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
43 m_pHandler->OnMenuCancel(menu, client, reason);
46 void BroadcastHandler::OnMenuDisplay(IBaseMenu *menu, int client, IMenuDisplay *display)
49 m_pHandler->OnMenuDisplay(menu, client, display);
52 void BroadcastHandler::OnBroadcastEnd(IBaseMenu *menu)
54 g_Menus.FreeBroadcastHandler(this);
57 void BroadcastHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
59 m_pHandler->OnMenuSelect(menu, client, item);
62 void BroadcastHandler::OnMenuEnd(IBaseMenu *menu)
64 assert(numClients > 0);
66 /* Only fire if all clients have gotten a menu end */
67 if (--numClients == 0)
69 IMenuHandler *pHandler = m_pHandler;
71 pHandler->OnMenuEnd(menu);
75 VoteHandler::VoteHandler(IMenuVoteHandler *handler)
76 : BroadcastHandler(handler), m_pVoteHandler(handler)
80 void VoteHandler::Initialize(IBaseMenu *menu)
82 unsigned int numItems = menu->GetItemCount();
84 if (m_counts.size() >= numItems)
86 for (size_t i=0; i<numItems; i++)
91 for (size_t i=0; i<m_counts.size(); i++)
95 m_counts.resize(numItems, 0);
99 void VoteHandler::OnMenuSelect(IBaseMenu *menu, int client, unsigned int item)
106 BroadcastHandler::OnMenuSelect(menu, client, item);
109 void VoteHandler::OnBroadcastEnd(IBaseMenu *menu)
114 for (size_t i=1; i<numItems; i++)
116 if (m_counts[i] > m_counts[highest])
120 } else if (m_counts[i] == m_counts[highest]) {
127 m_ties.push_back(highest);
128 srand(static_cast<unsigned int>(time(NULL)));
129 highest = m_ties[rand() % m_ties.size()];
132 m_pVoteHandler->OnMenuVoteEnd(menu, highest);
134 g_Menus.FreeVoteHandler(this);
137 /*******************************
138 *******************************
139 ******** MENU MANAGER *********
140 *******************************
141 *******************************/
143 MenuManager::MenuManager()
145 m_Styles.push_back(&g_ValveMenuStyle);
146 SetDefaultStyle(&g_ValveMenuStyle);
149 void MenuManager::OnSourceModAllInitialized()
153 void MenuManager::OnSourceModAllShutdown()
155 while (!m_BroadcastHandlers.empty())
157 delete m_BroadcastHandlers.front();
158 m_BroadcastHandlers.pop();
161 while (!m_VoteHandlers.empty())
163 delete m_VoteHandlers.front();
164 m_VoteHandlers.pop();
168 bool MenuManager::SetDefaultStyle(IMenuStyle *style)
175 m_pDefaultStyle = style;
180 IMenuStyle *MenuManager::GetStyle(unsigned int index)
182 if (index >= GetStyleCount())
187 return m_Styles[index];
190 void MenuManager::AddStyle(IMenuStyle *style)
192 m_Styles.push_back(style);
195 unsigned int MenuManager::GetStyleCount()
197 return (unsigned int)m_Styles.size();
200 IMenuStyle *MenuManager::FindStyleByName(const char *name)
202 unsigned int count = GetStyleCount();
203 for (unsigned int i=0; i<count; i++)
205 IMenuStyle *ptr = GetStyle(i);
206 if (strcasecmp(ptr->GetStyleName(), name) == 0)
215 inline bool IsSlotItem(IMenuDisplay *display,
218 if (!display->CanDrawItem(style))
222 if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
226 if (style & ITEMDRAW_RAWLINE)
234 IMenuDisplay *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
236 IBaseMenu *menu = md.menu;
245 unsigned int position;
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)
258 unsigned int totalItems = menu->GetItemCount();
259 unsigned int startItem = 0;
261 /* For pagination, find the starting point. */
262 if (pgn != MENU_NO_PAGINATION)
264 if (order == ItemOrder_Ascending)
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.
271 if (startItem >= totalItems)
273 startItem = totalItems - 1;
274 order = ItemOrder_Descending;
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.
282 if (startItem <= maxItems)
285 order = ItemOrder_Ascending;
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;
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
302 unsigned int i = startItem;
303 unsigned int foundItems = 0;
306 ItemDrawInfo &dr = drawItems[foundItems].draw;
307 /* Is the item valid? */
308 if (menu->GetItemInfo(i, &dr) != NULL)
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))
315 /* If we've already found the max number of items,
316 * This means we should just cancel out and log our
319 if (foundItems >= maxItems)
325 drawItems[foundItems++].position = i;
328 /* If there's no pagination, stop once the menu is full. */
329 if (pgn == MENU_NO_PAGINATION)
331 /* If we've filled up, then stop */
332 if (foundItems >= maxItems)
337 /* If we're descending and this is the first item, stop */
338 if (order == ItemOrder_Descending)
346 /* If we're ascending and this is the last item, stop */
347 else if (order == ItemOrder_Ascending)
349 if (i >= totalItems - 1)
357 /* There were no items to draw! */
360 display->DeleteThis();
364 /* Check initial buttons */
365 bool displayPrev = false;
366 bool displayNext = false;
369 if (order == ItemOrder_Descending)
372 md.firstItem = extraItem;
373 } else if (order == ItemOrder_Ascending) {
375 md.lastItem = extraItem;
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!
383 if (pgn != MENU_NO_PAGINATION)
385 unsigned int lastItem = 0;
387 /* Find the last feasible item to search from. */
388 if (order == ItemOrder_Descending)
390 lastItem = drawItems[0].position;
391 if (lastItem >= totalItems - 1)
395 while (++lastItem < totalItems)
397 if (menu->GetItemInfo(lastItem, &dr) != NULL)
399 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
400 if (IsSlotItem(display, dr.style))
403 md.lastItem = lastItem;
408 } else if (order == ItemOrder_Ascending) {
409 lastItem = drawItems[0].position;
415 while (lastItem-- != 0)
417 if (menu->GetItemInfo(lastItem, &dr) != NULL)
419 mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
420 if (IsSlotItem(display, dr.style))
423 md.firstItem = lastItem;
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)
437 for (unsigned int i=0; i<foundItems; i++)
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)
444 slots[position].item = drawItems[i].position;
445 slots[position].type = ItemSel_Item;
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.
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)
460 slots[position].item = drawItems[i].position;
461 slots[position].type = ItemSel_Item;
466 /* Now, we need to check if we need to add anything extra */
467 if (pgn != MENU_NO_PAGINATION)
469 bool canDrawDisabled = display->CanDrawItem(ITEMDRAW_DISABLED);
470 bool exitButton = menu->GetExitButton();
473 /* Calculate how many items we are allowed for control stuff */
474 unsigned int padding = style->GetMaxPageItems() - maxItems;
476 /* Add the number of available slots */
477 padding += (maxItems - foundItems);
479 /* Someday, if we are able to re-enable this, we will be very lucky men. */
481 if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
484 /* Even if we don't draw an exit button, we invalidate the slot. */
488 /* Otherwise, we don't draw anything and leave the slot available */
493 /* Subtract two slots for the displayNext/displayPrev padding */
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.
501 ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
502 if (exitButton || (displayNext || displayPrev))
504 /* Add spacers so we can pad to the end */
505 for (unsigned int i=0; i<padding; i++)
507 position = display->DrawItem(padItem);
508 slots[position].type = ItemSel_None;
512 /* Put a fake spacer before control stuff, if possible */
514 ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
515 display->DrawItem(draw);
519 ItemDrawInfo dr(text, 0);
520 if (displayPrev || canDrawDisabled)
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!
530 position = display->DrawItem(padItem);
531 slots[position].type = ItemSel_None;
535 if (displayNext || canDrawDisabled)
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!
545 position = display->DrawItem(padItem);
546 slots[position].type = ItemSel_None;
552 CorePlayerTranslate(client, text, sizeof(text), "Exit", NULL);
554 position = display->DrawItem(dr);
555 slots[position].type = ItemSel_Exit;
559 /* Lastly, fill in any slots we could have missed */
560 for (unsigned int i = position + 1; i < 10; i++)
562 slots[i].type = ItemSel_None;
566 mh->OnMenuDisplay(menu, client, display);
567 display->DrawTitle(menu->GetDefaultTitle(), true);
572 unsigned int MenuManager::BroadcastMenu(IBaseMenu *menu,
573 IMenuHandler *handler,
575 unsigned int numClients,
578 BroadcastHandler *bh;
580 if (m_BroadcastHandlers.empty())
582 bh = new BroadcastHandler(handler);
584 bh = m_BroadcastHandlers.front();
585 m_BroadcastHandlers.pop();
586 bh->m_pHandler = handler;
590 handler->OnMenuStart(menu);
592 unsigned int total = 0;
593 for (unsigned int i=0; i<numClients; i++)
595 /* Only continue if displaying works */
596 if (!menu->Display(clients[i], bh, time))
601 /* :TODO: Allow sourcetv only, not all bots */
602 CPlayer *player = g_Players.GetPlayerByIndex(clients[i]);
603 if (player->IsFakeClient())
613 /* End the broadcast here */
614 handler->OnMenuEnd(menu);
615 FreeBroadcastHandler(bh);
621 unsigned int MenuManager::VoteMenu(IBaseMenu *menu,
622 IMenuVoteHandler *handler,
624 unsigned int numClients,
629 if (m_VoteHandlers.empty())
631 vh = new VoteHandler(handler);
633 vh = m_VoteHandlers.front();
634 m_VoteHandlers.pop();
635 vh->m_pHandler = handler;
639 vh->Initialize(menu);
640 handler->OnMenuStart(menu);
642 unsigned int total = 0;
643 for (unsigned int i=0; i<numClients; i++)
645 /* Only continue if displaying works */
646 if (!menu->Display(clients[i], vh, time))
651 /* :TODO: Allow sourcetv only, not all bots */
652 CPlayer *player = g_Players.GetPlayerByIndex(clients[i]);
653 if (player->IsFakeClient())
663 /* End the broadcast here */
664 handler->OnMenuEnd(menu);
671 void MenuManager::FreeBroadcastHandler(BroadcastHandler *bh)
673 m_BroadcastHandlers.push(bh);
676 void MenuManager::FreeVoteHandler(VoteHandler *vh)
678 m_VoteHandlers.push(vh);
681 IMenuStyle *MenuManager::GetDefaultStyle()
683 return m_pDefaultStyle;