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