core/MenuManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Aug 24 04:35:48 2008 +0000 (2008-08-24)
changeset 2237 4403a982ec91
parent 2056 f1fcde46ac7c
child 2289 44045ead61cd
permissions -rw-r--r--
Improved bot detection slightly. This was meant to go along with the fix for amb1801.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <time.h>
ds@1952
    33
#include <stdarg.h>
ds@1952
    34
#include <stdlib.h>
ds@1952
    35
#include "MenuManager.h"
ds@1952
    36
#include "MenuVoting.h"
ds@1952
    37
#include "sm_stringutil.h"
ds@1952
    38
#include "sourcemm_api.h"
ds@1952
    39
#include "PlayerManager.h"
ds@1952
    40
#include "MenuStyle_Valve.h"
ds@1952
    41
#include "ShareSys.h"
ds@1952
    42
#include "HandleSys.h"
ds@1952
    43
#include "sourcemm_api.h"
dvander@2042
    44
#include "Translator.h"
ds@1952
    45
ds@1952
    46
MenuManager g_Menus;
ds@1952
    47
VoteMenuHandler s_VoteHandler;
ds@1952
    48
ds@1952
    49
ConVar sm_menu_sounds("sm_menu_sounds", "1", 0, "Sets whether SourceMod menus play trigger sounds");
ds@1952
    50
ds@1952
    51
MenuManager::MenuManager()
ds@1952
    52
{
ds@1952
    53
	m_Styles.push_back(&g_ValveMenuStyle);
ds@1952
    54
	SetDefaultStyle(&g_ValveMenuStyle);
ds@1952
    55
}
ds@1952
    56
ds@1952
    57
void MenuManager::OnSourceModAllInitialized()
ds@1952
    58
{
ds@1952
    59
	g_ShareSys.AddInterface(NULL, this);
ds@1952
    60
ds@1952
    61
	HandleAccess access;
ds@1952
    62
	g_HandleSys.InitAccessDefaults(NULL, &access);
ds@1952
    63
ds@1952
    64
	/* Deny cloning to menus */
ds@1952
    65
	access.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
ds@1952
    66
	m_MenuType = g_HandleSys.CreateType("IBaseMenu", this, 0, NULL, &access, g_pCoreIdent, NULL);
ds@1952
    67
ds@1952
    68
	/* Also deny deletion to styles */
ds@1952
    69
	access.access[HandleAccess_Delete] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
ds@1952
    70
	m_StyleType = g_HandleSys.CreateType("IMenuStyle", this, 0, NULL, &access, g_pCoreIdent, NULL);
ds@1952
    71
}
ds@1952
    72
ds@1952
    73
void MenuManager::OnSourceModAllShutdown()
ds@1952
    74
{
ds@1952
    75
	g_HandleSys.RemoveType(m_MenuType, g_pCoreIdent);
ds@1952
    76
	g_HandleSys.RemoveType(m_StyleType, g_pCoreIdent);
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
void MenuManager::OnHandleDestroy(HandleType_t type, void *object)
ds@1952
    80
{
ds@1952
    81
	if (type == m_MenuType)
ds@1952
    82
	{
ds@1952
    83
		IBaseMenu *menu = (IBaseMenu *)object;
ds@1952
    84
		menu->Destroy(false);
ds@1952
    85
	}
ds@1952
    86
	else if (type == m_StyleType)
ds@1952
    87
	{
ds@1952
    88
		/* Do nothing */
ds@1952
    89
	}
ds@1952
    90
}
ds@1952
    91
ds@1952
    92
bool MenuManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
ds@1952
    93
{
ds@1952
    94
	if (type == m_MenuType)
ds@1952
    95
	{
ds@1952
    96
		*pSize = ((IBaseMenu *)object)->GetApproxMemUsage();
ds@1952
    97
		return true;
ds@1952
    98
	}
ds@1952
    99
	else
ds@1952
   100
	{
ds@1952
   101
		*pSize = ((IMenuStyle *)object)->GetApproxMemUsage();
ds@1952
   102
		return true;
ds@1952
   103
	}
ds@1952
   104
ds@1952
   105
	return false;
ds@1952
   106
}
ds@1952
   107
ds@1952
   108
Handle_t MenuManager::CreateMenuHandle(IBaseMenu *menu, IdentityToken_t *pOwner)
ds@1952
   109
{
ds@1952
   110
	if (m_MenuType == NO_HANDLE_TYPE)
ds@1952
   111
	{
ds@1952
   112
		return BAD_HANDLE;
ds@1952
   113
	}
ds@1952
   114
ds@1952
   115
	return g_HandleSys.CreateHandle(m_MenuType, menu, pOwner, g_pCoreIdent, NULL);
ds@1952
   116
}
ds@1952
   117
ds@1952
   118
Handle_t MenuManager::CreateStyleHandle(IMenuStyle *style)
ds@1952
   119
{
ds@1952
   120
	if (m_StyleType == NO_HANDLE_TYPE)
ds@1952
   121
	{
ds@1952
   122
		return BAD_HANDLE;
ds@1952
   123
	}
ds@1952
   124
ds@1952
   125
	return g_HandleSys.CreateHandle(m_StyleType, style, g_pCoreIdent, g_pCoreIdent, NULL);
ds@1952
   126
}
ds@1952
   127
ds@1952
   128
HandleError MenuManager::ReadMenuHandle(Handle_t handle, IBaseMenu **menu)
ds@1952
   129
{
ds@1952
   130
	HandleSecurity sec;
ds@1952
   131
ds@1952
   132
	sec.pIdentity = g_pCoreIdent;
ds@1952
   133
	sec.pOwner = NULL;
ds@1952
   134
ds@1952
   135
	return g_HandleSys.ReadHandle(handle, m_MenuType, &sec, (void **)menu);
ds@1952
   136
}
ds@1952
   137
ds@1952
   138
HandleError MenuManager::ReadStyleHandle(Handle_t handle, IMenuStyle **style)
ds@1952
   139
{
ds@1952
   140
	HandleSecurity sec;
ds@1952
   141
ds@1952
   142
	sec.pIdentity = g_pCoreIdent;
ds@1952
   143
	sec.pOwner = g_pCoreIdent;
ds@1952
   144
ds@1952
   145
	return g_HandleSys.ReadHandle(handle, m_StyleType, &sec, (void **)style);
ds@1952
   146
}
ds@1952
   147
ds@1952
   148
bool MenuManager::SetDefaultStyle(IMenuStyle *style)
ds@1952
   149
{
ds@1952
   150
	if (!style)
ds@1952
   151
	{
ds@1952
   152
		return false;
ds@1952
   153
	}
ds@1952
   154
ds@1952
   155
	m_pDefaultStyle = style;
ds@1952
   156
ds@1952
   157
	return true;
ds@1952
   158
}
ds@1952
   159
ds@1952
   160
IMenuStyle *MenuManager::GetStyle(unsigned int index)
ds@1952
   161
{
ds@1952
   162
	if (index >= GetStyleCount())
ds@1952
   163
	{
ds@1952
   164
		return NULL;
ds@1952
   165
	}
ds@1952
   166
ds@1952
   167
	return m_Styles[index];
ds@1952
   168
}
ds@1952
   169
ds@1952
   170
void MenuManager::AddStyle(IMenuStyle *style)
ds@1952
   171
{
ds@1952
   172
	m_Styles.push_back(style);
ds@1952
   173
}
ds@1952
   174
ds@1952
   175
unsigned int MenuManager::GetStyleCount()
ds@1952
   176
{
ds@1952
   177
	return (unsigned int)m_Styles.size();
ds@1952
   178
}
ds@1952
   179
ds@1952
   180
IMenuStyle *MenuManager::FindStyleByName(const char *name)
ds@1952
   181
{
ds@1952
   182
	unsigned int count = GetStyleCount();
ds@1952
   183
	for (unsigned int i=0; i<count; i++)
ds@1952
   184
	{
ds@1952
   185
		IMenuStyle *ptr = GetStyle(i);
ds@1952
   186
		if (strcasecmp(ptr->GetStyleName(), name) == 0)
ds@1952
   187
		{
ds@1952
   188
			return ptr;
ds@1952
   189
		}
ds@1952
   190
	}
ds@1952
   191
ds@1952
   192
	return NULL;
ds@1952
   193
}
ds@1952
   194
ds@1952
   195
inline bool IsSlotItem(IMenuPanel *display,
ds@1952
   196
					   unsigned int style)
ds@1952
   197
{
ds@1952
   198
	if (!display->CanDrawItem(style))
ds@1952
   199
	{
ds@1952
   200
	return false;
ds@1952
   201
	}
ds@1952
   202
	if ((style & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
ds@1952
   203
	{
ds@1952
   204
		return false;
ds@1952
   205
	}
ds@1952
   206
	if (style & ITEMDRAW_RAWLINE)
ds@1952
   207
	{
ds@1952
   208
		return false;
ds@1952
   209
	}
ds@1952
   210
ds@1952
   211
	return true;
ds@1952
   212
}
ds@1952
   213
ds@1952
   214
IMenuPanel *MenuManager::RenderMenu(int client, menu_states_t &md, ItemOrder order)
ds@1952
   215
{
ds@1952
   216
	IBaseMenu *menu = md.menu;
ds@1952
   217
ds@1952
   218
	if (!menu)
ds@1952
   219
	{
ds@1952
   220
		return NULL;
ds@1952
   221
	}
ds@1952
   222
ds@1952
   223
	struct
ds@1952
   224
	{
ds@1952
   225
		unsigned int position;
ds@1952
   226
		ItemDrawInfo draw;
ds@1952
   227
	} drawItems[10];
ds@1952
   228
ds@1952
   229
	/* Figure out how many items to draw */
ds@1952
   230
	IMenuStyle *style = menu->GetDrawStyle();
ds@1952
   231
	unsigned int pgn = menu->GetPagination();
ds@1952
   232
	unsigned int maxItems = style->GetMaxPageItems();
ds@1952
   233
	bool exitButton = (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXIT) == MENUFLAG_BUTTON_EXIT;
ds@1952
   234
ds@1952
   235
	if (pgn != MENU_NO_PAGINATION)
ds@1952
   236
	{
ds@1952
   237
		maxItems = pgn;
ds@1952
   238
	}
ds@1952
   239
	else if (exitButton)
ds@1952
   240
	{
ds@1952
   241
		maxItems--;
ds@1952
   242
	}
ds@1952
   243
ds@1952
   244
	/* This is very not allowed! */
ds@1952
   245
	if (maxItems < 2)
ds@1952
   246
	{
ds@1952
   247
		return NULL;
ds@1952
   248
	}
ds@1952
   249
ds@1952
   250
	unsigned int totalItems = menu->GetItemCount();
ds@1952
   251
	unsigned int startItem = 0;
ds@1952
   252
ds@1952
   253
	/* For pagination, find the starting point. */
ds@1952
   254
	if (pgn != MENU_NO_PAGINATION)
ds@1952
   255
	{
ds@1952
   256
		if (order == ItemOrder_Ascending)
ds@1952
   257
		{
ds@1952
   258
			startItem = md.lastItem;
ds@1952
   259
			/* This shouldn't happen with well-coded menus.
ds@1952
   260
			 * If the item is out of bounds, switch the order to
ds@1952
   261
			 * Items_Descending and make us start from the top.
ds@1952
   262
			 */
ds@1952
   263
			if (startItem >= totalItems)
ds@1952
   264
			{
ds@1952
   265
				startItem = totalItems - 1;
ds@1952
   266
				order = ItemOrder_Descending;
ds@1952
   267
			}
ds@1952
   268
		}
ds@1952
   269
		else if (order == ItemOrder_Descending)
ds@1952
   270
		{
ds@1952
   271
			startItem = md.firstItem;
ds@1952
   272
			/* This shouldn't happen with well-coded menus.
ds@1952
   273
			 * If searching backwards doesn't give us enough room,
ds@1952
   274
			 * start from the beginning and change to ascending.
ds@1952
   275
			 */
ds@1952
   276
			if (startItem <= maxItems)
ds@1952
   277
			{
ds@1952
   278
				startItem = 0;
ds@1952
   279
				order = ItemOrder_Ascending;
ds@1952
   280
			}
ds@1952
   281
		}
ds@1952
   282
	}
ds@1952
   283
ds@1952
   284
	/* Get our Display pointer and initialize some crap */
ds@1952
   285
	IMenuPanel *panel = menu->CreatePanel();
ds@1952
   286
	IMenuHandler *mh = md.mh;
ds@1952
   287
	bool foundExtra = false;
ds@1952
   288
	unsigned int extraItem = 0;
ds@1952
   289
ds@1952
   290
	if (panel == NULL)
ds@1952
   291
	{
ds@1952
   292
		return NULL;
ds@1952
   293
	}
ds@1952
   294
ds@1952
   295
	/**
ds@1952
   296
	 * We keep searching until:
ds@1952
   297
	 * 1) There are no more items
ds@1952
   298
	 * 2) We reach one OVER the maximum number of slot items
ds@1952
   299
	 * 3) We have reached maxItems and pagination is MENU_NO_PAGINATION
ds@1952
   300
	 */
ds@1952
   301
	unsigned int i = startItem;
ds@1952
   302
	unsigned int foundItems = 0;
ds@1952
   303
	while (totalItems)
ds@1952
   304
	{
ds@1952
   305
		ItemDrawInfo &dr = drawItems[foundItems].draw;
ds@1952
   306
		/* Is the item valid? */
ds@1952
   307
		if (menu->GetItemInfo(i, &dr) != NULL)
ds@1952
   308
		{
ds@1952
   309
			/* Ask the user to change the style, if necessary */
ds@1952
   310
			mh->OnMenuDrawItem(menu, client, i, dr.style);
ds@1952
   311
			/* Check if it's renderable */
ds@1952
   312
			if (IsSlotItem(panel, dr.style))
ds@1952
   313
			{
ds@1952
   314
				/* If we've already found the max number of items,
ds@1952
   315
				 * This means we should just cancel out and log our
ds@1952
   316
				 * "last item."
ds@1952
   317
				 */
ds@1952
   318
				if (foundItems >= maxItems)
ds@1952
   319
				{
ds@1952
   320
					foundExtra = true;
ds@1952
   321
					extraItem = i;
ds@1952
   322
					break;
ds@1952
   323
				}
ds@1952
   324
				drawItems[foundItems++].position = i;
ds@1952
   325
			}
ds@1952
   326
		}
ds@1952
   327
		/* If there's no pagination, stop once the menu is full. */
ds@1952
   328
		if (pgn == MENU_NO_PAGINATION)
ds@1952
   329
		{
ds@1952
   330
			/* If we've filled up, then stop */
ds@1952
   331
			if (foundItems >= maxItems)
ds@1952
   332
			{
ds@1952
   333
				break;
ds@1952
   334
			}
ds@1952
   335
		}
ds@1952
   336
		/* If we're descending and this is the first item, stop */
ds@1952
   337
		if (order == ItemOrder_Descending)
ds@1952
   338
		{
ds@1952
   339
			if (i == 0)
ds@1952
   340
			{
ds@1952
   341
				break;
ds@1952
   342
			}
ds@1952
   343
			i--;
ds@1952
   344
		} 
ds@1952
   345
		/* If we're ascending and this is the last item, stop */
ds@1952
   346
		else if (order == ItemOrder_Ascending)
ds@1952
   347
		{
ds@1952
   348
			if (i >= totalItems - 1)
ds@1952
   349
			{
ds@1952
   350
				break;
ds@1952
   351
			}
ds@1952
   352
			i++;
ds@1952
   353
		}
ds@1952
   354
	}
ds@1952
   355
ds@1952
   356
	/* There were no items to draw! */
ds@1952
   357
	if (!foundItems)
ds@1952
   358
	{
ds@1952
   359
		panel->DeleteThis();
ds@1952
   360
		return NULL;
ds@1952
   361
	}
ds@1952
   362
ds@1952
   363
	bool displayPrev = false;
ds@1952
   364
	bool displayNext = false;
ds@1952
   365
ds@1952
   366
	/* This is an annoying process.
ds@1952
   367
	 * Skip it for non-paginated menus, which get special treatment.
ds@1952
   368
	 */
ds@1952
   369
	if (pgn != MENU_NO_PAGINATION)
ds@1952
   370
	{
ds@1952
   371
		if (foundExtra)
ds@1952
   372
		{
ds@1952
   373
			if (order == ItemOrder_Descending)
ds@1952
   374
			{
ds@1952
   375
				displayPrev = true;
ds@1952
   376
				md.firstItem = extraItem;
ds@1952
   377
			}
ds@1952
   378
			else if (order == ItemOrder_Ascending)
ds@1952
   379
			{
ds@1952
   380
				displayNext = true;
ds@1952
   381
				md.lastItem = extraItem;
ds@1952
   382
			}
ds@1952
   383
		}
ds@1952
   384
ds@1952
   385
		unsigned int lastItem = 0;
ds@1952
   386
		ItemDrawInfo dr;
ds@1952
   387
		/* Find the last feasible item to search from. */
ds@1952
   388
		if (order == ItemOrder_Descending)
ds@1952
   389
		{
ds@1952
   390
			lastItem = drawItems[0].position;
ds@1952
   391
			if (lastItem >= totalItems - 1)
ds@1952
   392
			{
ds@1952
   393
				goto skip_search;
ds@1952
   394
			}
ds@1952
   395
			while (++lastItem < totalItems)
ds@1952
   396
			{
ds@1952
   397
				if (menu->GetItemInfo(lastItem, &dr) != NULL)
ds@1952
   398
				{
ds@1952
   399
					mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
ds@1952
   400
					if (IsSlotItem(panel, dr.style))
ds@1952
   401
					{
ds@1952
   402
						displayNext = true;
ds@1952
   403
						md.lastItem = lastItem;
ds@1952
   404
						break;
ds@1952
   405
					}
ds@1952
   406
				}
ds@1952
   407
			}
ds@1952
   408
		}
ds@1952
   409
		else if (order == ItemOrder_Ascending)
ds@1952
   410
		{
ds@1952
   411
			lastItem = drawItems[0].position;
ds@1952
   412
			if (lastItem == 0)
ds@1952
   413
			{
ds@1952
   414
				goto skip_search;
ds@1952
   415
			}
ds@1952
   416
			lastItem--;
ds@1952
   417
			while (lastItem != 0)
ds@1952
   418
			{
ds@1952
   419
				if (menu->GetItemInfo(lastItem, &dr) != NULL)
ds@1952
   420
				{
ds@1952
   421
					mh->OnMenuDrawItem(menu, client, lastItem, dr.style);
ds@1952
   422
					if (IsSlotItem(panel, dr.style))
ds@1952
   423
					{
ds@1952
   424
						displayPrev = true;
ds@1952
   425
						md.firstItem = lastItem;
ds@1952
   426
						break;
ds@1952
   427
					}
ds@1952
   428
				}
ds@1952
   429
				lastItem--;
ds@1952
   430
			}
ds@1952
   431
		}
ds@1952
   432
	}
ds@1952
   433
ds@1952
   434
skip_search:
ds@1952
   435
ds@1952
   436
	/* Draw the item according to the order */
ds@1952
   437
	menu_slots_t *slots = md.slots;
ds@1952
   438
	unsigned int position = 0;			/* Keep track of the last position */
ds@1952
   439
	if (order == ItemOrder_Ascending)
ds@1952
   440
	{
ds@1952
   441
		md.item_on_page = drawItems[0].position;
ds@1952
   442
		for (unsigned int i = 0; i < foundItems; i++)
ds@1952
   443
		{
ds@1952
   444
			ItemDrawInfo &dr = drawItems[i].draw;
ds@1952
   445
			if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
ds@1952
   446
			{
ds@1952
   447
				position = panel->DrawItem(dr);
ds@1952
   448
			}
ds@1952
   449
			if (position != 0)
ds@1952
   450
			{
ds@1952
   451
				slots[position].item = drawItems[i].position;
ds@1952
   452
				if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
ds@1952
   453
				{
ds@1952
   454
					slots[position].type = ItemSel_None;
ds@1952
   455
				}
ds@1952
   456
				else
ds@1952
   457
				{
ds@1952
   458
					slots[position].type = ItemSel_Item;
ds@1952
   459
				}
ds@1952
   460
			}
ds@1952
   461
		}
ds@1952
   462
	}
ds@1952
   463
	else if (order == ItemOrder_Descending)
ds@1952
   464
	{
ds@1952
   465
		unsigned int i = foundItems;
ds@1952
   466
		/* NOTE: There will always be at least one item because
ds@1952
   467
		 * of the check earlier.
ds@1952
   468
		 */
ds@1952
   469
		md.item_on_page = drawItems[foundItems - 1].position;
ds@1952
   470
		while (i--)
ds@1952
   471
		{
ds@1952
   472
			ItemDrawInfo &dr = drawItems[i].draw;
ds@1952
   473
			if ((position = mh->OnMenuDisplayItem(menu, client, panel, drawItems[i].position, dr)) == 0)
ds@1952
   474
			{
ds@1952
   475
				position = panel->DrawItem(dr);
ds@1952
   476
			}
ds@1952
   477
			if (position != 0)
ds@1952
   478
			{
ds@1952
   479
				slots[position].item = drawItems[i].position;
ds@1952
   480
				if ((dr.style & ITEMDRAW_DISABLED) == ITEMDRAW_DISABLED)
ds@1952
   481
				{
ds@1952
   482
					slots[position].type = ItemSel_None;
ds@1952
   483
				}
ds@1952
   484
				else
ds@1952
   485
				{
ds@1952
   486
					slots[position].type = ItemSel_Item;
ds@1952
   487
				}
ds@1952
   488
			}
ds@1952
   489
		}
ds@1952
   490
	}
ds@1952
   491
ds@1952
   492
	/* Now, we need to check if we need to add anything extra */
ds@1952
   493
	if (pgn != MENU_NO_PAGINATION || exitButton)
ds@1952
   494
	{
ds@1952
   495
		bool canDrawDisabled = panel->CanDrawItem(ITEMDRAW_DISABLED|ITEMDRAW_CONTROL);
ds@1952
   496
		bool exitBackButton = false;
ds@1952
   497
		char text[50];
ds@1952
   498
ds@1952
   499
		if (pgn != MENU_NO_PAGINATION
ds@1952
   500
			&& (menu->GetMenuOptionFlags() & MENUFLAG_BUTTON_EXITBACK) == MENUFLAG_BUTTON_EXITBACK)
ds@1952
   501
		{
ds@1952
   502
			exitBackButton = true;
ds@1952
   503
		}
ds@1952
   504
ds@1952
   505
		/* Calculate how many items we are allowed for control stuff */
ds@1952
   506
		unsigned int padding = style->GetMaxPageItems() - maxItems;
ds@1952
   507
		
ds@1952
   508
		/* Add the number of available slots */
ds@1952
   509
		padding += (maxItems - foundItems);
ds@1952
   510
ds@1952
   511
		/* Someday, if we are able to re-enable this, we will be very lucky men. */
ds@1952
   512
#if 0
ds@1952
   513
		if (!style->FeatureExists(MenuStyleFeature_ImplicitExit))
ds@1952
   514
		{
ds@1952
   515
#endif
ds@1952
   516
		/* Even if we don't draw an exit button, we invalidate the slot. */
ds@1952
   517
		padding--;
ds@1952
   518
#if 0
ds@1952
   519
		} else {
ds@1952
   520
			/* Otherwise, we don't draw anything and leave the slot available */
ds@1952
   521
			exitButton = false;
ds@1952
   522
		}
ds@1952
   523
#endif
ds@1952
   524
ds@1952
   525
		if (pgn != MENU_NO_PAGINATION)
ds@1952
   526
		{
ds@1952
   527
			/* Subtract two slots for the displayNext/displayPrev padding */
ds@1952
   528
			padding -= 2;
ds@1952
   529
		}
ds@1952
   530
ds@1952
   531
		/* If we have an "Exit Back" button and the space to draw it, do so. */
ds@1952
   532
		if (exitBackButton)
ds@1952
   533
		{
ds@1952
   534
			if (!displayPrev)
ds@1952
   535
			{
ds@1952
   536
				displayPrev = true;
ds@1952
   537
			}
ds@1952
   538
			else
ds@1952
   539
			{
ds@1952
   540
				exitBackButton = false;
ds@1952
   541
			}
ds@1952
   542
		}
ds@1952
   543
ds@1952
   544
		/**
ds@1952
   545
		 * We allow next/prev to be undrawn if neither exists.
ds@1952
   546
		 * Thus, we only need padding if one of them will be drawn,
ds@1952
   547
		 * or the exit button will be drawn.
ds@1952
   548
		 */
ds@1952
   549
		ItemDrawInfo padItem(NULL, ITEMDRAW_SPACER);
ds@1952
   550
		if (exitButton || (displayNext || displayPrev))
ds@1952
   551
		{
ds@1952
   552
			/* If there are no control options,
ds@1952
   553
			 * Instead just pad with invisible slots.
ds@1952
   554
			 */
ds@1952
   555
			if (!displayPrev && !displayPrev)
ds@1952
   556
			{
ds@1952
   557
				padItem.style = ITEMDRAW_NOTEXT;
ds@1952
   558
			}
ds@1952
   559
			/* Add spacers so we can pad to the end */
ds@1952
   560
			for (unsigned int i=0; i<padding; i++)
ds@1952
   561
			{
ds@1952
   562
				position = panel->DrawItem(padItem);
ds@1952
   563
				slots[position].type = ItemSel_None;
ds@1952
   564
			}
ds@1952
   565
		}
ds@1952
   566
ds@1952
   567
		/* Put a fake spacer before control stuff, if possible */
ds@1952
   568
		if ((displayPrev || displayNext) || exitButton)
ds@1952
   569
		{
ds@1952
   570
			ItemDrawInfo draw("", ITEMDRAW_RAWLINE|ITEMDRAW_SPACER);
ds@1952
   571
			panel->DrawItem(draw);
ds@1952
   572
		}
ds@1952
   573
ds@1952
   574
		ItemDrawInfo dr(text, 0);
ds@1952
   575
ds@1952
   576
		/**
ds@1952
   577
		 * If we have one or the other, we need to have spacers for both.
ds@1952
   578
		 */
ds@1952
   579
		if (pgn != MENU_NO_PAGINATION)
ds@1952
   580
		{
ds@1952
   581
			if (displayPrev || displayNext)
ds@1952
   582
			{
ds@1952
   583
				/* PREVIOUS */
ds@1952
   584
				ItemDrawInfo padCtrlItem(NULL, ITEMDRAW_SPACER|ITEMDRAW_CONTROL);
ds@1952
   585
				if (displayPrev || canDrawDisabled)
ds@1952
   586
				{
ds@1952
   587
					if (exitBackButton)
ds@1952
   588
					{
dvander@2042
   589
						if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Back", &client))
dvander@2042
   590
						{
dvander@2042
   591
							UTIL_Format(text, sizeof(text), "Back");
dvander@2042
   592
						}
ds@1952
   593
						dr.style = ITEMDRAW_CONTROL;
ds@1952
   594
						position = panel->DrawItem(dr);
ds@1952
   595
						slots[position].type = ItemSel_ExitBack;
ds@1952
   596
					}
ds@1952
   597
					else
ds@1952
   598
					{
dvander@2042
   599
						if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Previous", &client))
dvander@2042
   600
						{
dvander@2042
   601
							UTIL_Format(text, sizeof(text), "Previous");
dvander@2042
   602
						}
ds@1952
   603
						dr.style = (displayPrev ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
ds@1952
   604
						position = panel->DrawItem(dr);
ds@1952
   605
						slots[position].type = ItemSel_Back;
ds@1952
   606
					}
ds@1952
   607
				}
ds@1952
   608
				else if (displayNext || exitButton)
ds@1952
   609
				{
ds@1952
   610
					/* If we can't display this, and there is an exit button,
ds@1952
   611
					 * we need to pad!
ds@1952
   612
					 */
ds@1952
   613
					position = panel->DrawItem(padCtrlItem);
ds@1952
   614
					slots[position].type = ItemSel_None;
ds@1952
   615
				}
ds@1952
   616
ds@1952
   617
				/* NEXT */
ds@1952
   618
				if (displayNext || canDrawDisabled)
ds@1952
   619
				{
dvander@2042
   620
					if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Next", &client))
dvander@2042
   621
					{
dvander@2042
   622
						UTIL_Format(text, sizeof(text), "Next");
dvander@2042
   623
					}
ds@1952
   624
					dr.style = (displayNext ? 0 : ITEMDRAW_DISABLED)|ITEMDRAW_CONTROL;
ds@1952
   625
					position = panel->DrawItem(dr);
ds@1952
   626
					slots[position].type = ItemSel_Next;
ds@1952
   627
				}
ds@1952
   628
				else if (exitButton)
ds@1952
   629
				{
ds@1952
   630
					/* If we can't display this,
ds@1952
   631
					 * but there is an "exit" button, we need to pad!
ds@1952
   632
					 */
ds@1952
   633
					position = panel->DrawItem(padCtrlItem);
ds@1952
   634
					slots[position].type = ItemSel_None;
ds@1952
   635
				}
ds@1952
   636
			}
ds@1952
   637
			else
ds@1952
   638
			{
ds@1952
   639
				/* Otherwise, bump to two slots! */
ds@1952
   640
				ItemDrawInfo numBump(NULL, ITEMDRAW_NOTEXT);
ds@1952
   641
				position = panel->DrawItem(numBump);
ds@1952
   642
				slots[position].type = ItemSel_None;
ds@1952
   643
				position = panel->DrawItem(numBump);
ds@1952
   644
				slots[position].type = ItemSel_None;
ds@1952
   645
			}
ds@1952
   646
		}
ds@1952
   647
ds@1952
   648
		/* EXIT */
ds@1952
   649
		if (exitButton)
ds@1952
   650
		{
dvander@2042
   651
			if (!CoreTranslate(text, sizeof(text), "%T", 2, NULL, "Exit", &client))
dvander@2042
   652
			{
dvander@2042
   653
				UTIL_Format(text, sizeof(text), "Exit");
dvander@2042
   654
			}
ds@1952
   655
			dr.style = ITEMDRAW_CONTROL;
ds@1952
   656
			position = panel->DrawItem(dr);
ds@1952
   657
			slots[position].type = ItemSel_Exit;
ds@1952
   658
		}
ds@1952
   659
	}
ds@1952
   660
ds@1952
   661
	/* Lastly, fill in any slots we could have missed */
ds@1952
   662
	for (unsigned int i = position + 1; i < 10; i++)
ds@1952
   663
	{
ds@1952
   664
		slots[i].type = ItemSel_None;
ds@1952
   665
	}
ds@1952
   666
ds@1952
   667
	/* Do title stuff */
ds@1952
   668
	mh->OnMenuDisplay(menu, client, panel);
ds@1952
   669
	panel->DrawTitle(menu->GetDefaultTitle(), true);
ds@1952
   670
ds@1952
   671
	return panel;
ds@1952
   672
}
ds@1952
   673
ds@1952
   674
IMenuStyle *MenuManager::GetDefaultStyle()
ds@1952
   675
{
ds@1952
   676
	return m_pDefaultStyle;
ds@1952
   677
}
ds@1952
   678
ds@1952
   679
bool MenuManager::MenuSoundsEnabled()
ds@1952
   680
{
ds@1952
   681
	return (sm_menu_sounds.GetInt() != 0);
ds@1952
   682
}
ds@1952
   683
ds@1952
   684
ConfigResult MenuManager::OnSourceModConfigChanged(const char *key,
ds@1952
   685
												   const char *value,
ds@1952
   686
												   ConfigSource source,
ds@1952
   687
												   char *error,
ds@1952
   688
												   size_t maxlength)
ds@1952
   689
{
ds@1952
   690
	if (strcmp(key, "MenuItemSound") == 0)
ds@1952
   691
	{
ds@1952
   692
		m_SelectSound.assign(value);
ds@1952
   693
		return ConfigResult_Accept;
ds@1952
   694
	} else if (strcmp(key, "MenuExitBackSound") == 0) {
ds@1952
   695
		m_ExitBackSound.assign(value);
ds@1952
   696
		return ConfigResult_Accept;
ds@1952
   697
	} else if (strcmp(key, "MenuExitSound") == 0) {
ds@1952
   698
		m_ExitSound.assign(value);
ds@1952
   699
		return ConfigResult_Accept;
ds@1952
   700
	}
ds@1952
   701
ds@1952
   702
	return ConfigResult_Ignore;
ds@1952
   703
}
ds@1952
   704
ds@1952
   705
const char *MenuManager::GetMenuSound(ItemSelection sel)
ds@1952
   706
{
ds@1952
   707
	const char *sound = NULL;
ds@1952
   708
ds@1952
   709
	switch (sel)
ds@1952
   710
	{
ds@1952
   711
	case ItemSel_Back:
ds@1952
   712
	case ItemSel_Next:
ds@1952
   713
	case ItemSel_Item:
ds@1952
   714
		{
ds@1952
   715
			if (m_SelectSound.size() > 0)
ds@1952
   716
			{
ds@1952
   717
				sound = m_SelectSound.c_str();
ds@1952
   718
			}
ds@1952
   719
			break;
ds@1952
   720
		}
ds@1952
   721
	case ItemSel_ExitBack:
ds@1952
   722
		{
ds@1952
   723
			if (m_ExitBackSound.size() > 0)
ds@1952
   724
			{
ds@1952
   725
				sound = m_ExitBackSound.c_str();
ds@1952
   726
			}
ds@1952
   727
			break;
ds@1952
   728
		}
ds@1952
   729
	case ItemSel_Exit:
ds@1952
   730
		{
ds@1952
   731
			if (m_ExitSound.size() > 0)
ds@1952
   732
			{
ds@1952
   733
				sound = m_ExitSound.c_str();
ds@1952
   734
			}
ds@1952
   735
			break;
ds@1952
   736
		}
ds@1952
   737
	default:
ds@1952
   738
		{
ds@1952
   739
			break;
ds@1952
   740
		}
ds@1952
   741
	}
ds@1952
   742
ds@1952
   743
	return sound;
ds@1952
   744
}
ds@1952
   745
ds@1952
   746
void MenuManager::OnSourceModLevelChange(const char *mapName)
ds@1952
   747
{
ds@1952
   748
	if (m_SelectSound.size() > 0)
ds@1952
   749
	{
ds@1952
   750
		enginesound->PrecacheSound(m_SelectSound.c_str(), true);
ds@1952
   751
	}
ds@1952
   752
	if (m_ExitBackSound.size() > 0)
ds@1952
   753
	{
ds@1952
   754
		enginesound->PrecacheSound(m_ExitBackSound.c_str(), true);
ds@1952
   755
	}
ds@1952
   756
	if (m_ExitSound.size() > 0)
ds@1952
   757
	{
ds@1952
   758
		enginesound->PrecacheSound(m_ExitSound.c_str(), true);
ds@1952
   759
	}
ds@1952
   760
}
ds@1952
   761
ds@1952
   762
void MenuManager::CancelMenu(IBaseMenu *menu)
ds@1952
   763
{
ds@1952
   764
	if (s_VoteHandler.GetCurrentMenu() == menu
ds@1952
   765
		&& !s_VoteHandler.IsCancelling())
ds@1952
   766
	{
ds@1952
   767
		s_VoteHandler.CancelVoting();
ds@1952
   768
		return;
ds@1952
   769
	}
ds@1952
   770
ds@1952
   771
	menu->Cancel();
ds@1952
   772
}
ds@1952
   773
ds@1952
   774
bool MenuManager::StartVote(IBaseMenu *menu, unsigned int num_clients, int clients[], unsigned int max_time, unsigned int flags)
ds@1952
   775
{
ds@1952
   776
	return s_VoteHandler.StartVote(menu, num_clients, clients, max_time, flags);
ds@1952
   777
}
ds@1952
   778
ds@1952
   779
bool MenuManager::IsVoteInProgress()
ds@1952
   780
{
ds@1952
   781
	return s_VoteHandler.IsVoteInProgress();
ds@1952
   782
}
ds@1952
   783
ds@1952
   784
void MenuManager::CancelVoting()
ds@1952
   785
{
ds@1952
   786
	s_VoteHandler.CancelVoting();
ds@1952
   787
}
ds@1952
   788
ds@1952
   789
unsigned int MenuManager::GetRemainingVoteDelay()
ds@1952
   790
{
ds@1952
   791
	return s_VoteHandler.GetRemainingVoteDelay();
ds@1952
   792
}
dvander@2056
   793
dvander@2056
   794
bool MenuManager::IsClientInVotePool(int client)
dvander@2056
   795
{
dvander@2056
   796
	return s_VoteHandler.IsClientInVotePool(client);
dvander@2056
   797
}
dvander@2056
   798
dvander@2056
   799
bool MenuManager::RedrawClientVoteMenu(int client)
dvander@2056
   800
{
dvander@2056
   801
	return s_VoteHandler.RedrawToClient(client);
dvander@2056
   802
}