core/MenuStyle_Valve.cpp
author Matt Woodrow <pred@alliedmods.net>
Fri Aug 08 00:17:37 2008 +0000 (2008-08-08)
changeset 2224 676c3b826f13
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Initial Import of beta structs extension.
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 "sm_stringutil.h"
ds@1952
    33
#include "PlayerManager.h"
ds@1952
    34
#include "MenuStyle_Valve.h"
ds@1952
    35
#include "Translator.h"
ds@1952
    36
#include "PlayerManager.h"
ds@1952
    37
#include "ConCmdManager.h"
ds@1952
    38
ds@1952
    39
SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
ds@1952
    40
ds@1952
    41
ValveMenuStyle g_ValveMenuStyle;
ds@1952
    42
extern const char *g_OptionNumTable[];
ds@1952
    43
extern const char *g_OptionCmdTable[];
ds@1952
    44
CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
ds@1952
    45
ds@1952
    46
ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
ds@1952
    47
{
ds@1952
    48
}
ds@1952
    49
ds@1952
    50
CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
ds@1952
    51
{
ds@1952
    52
	return &m_players[client];
ds@1952
    53
}
ds@1952
    54
ds@1952
    55
bool ValveMenuStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
ds@1952
    56
{
ds@1952
    57
	if (strcmp(cmdname, "sm_vmenuselect") == 0)
ds@1952
    58
	{
ds@1952
    59
		int key_press = atoi(cmd.Arg(1));
ds@1952
    60
		g_ValveMenuStyle.ClientPressedKey(client, key_press);
ds@1952
    61
		return true;
ds@1952
    62
	}
ds@1952
    63
ds@1952
    64
	return false;
ds@1952
    65
}
ds@1952
    66
ds@1952
    67
void ValveMenuStyle::OnSourceModAllInitialized()
ds@1952
    68
{
ds@1952
    69
	g_Players.AddClientListener(this);
ds@1952
    70
	SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
ds@1952
    71
	g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
ds@1952
    72
}
ds@1952
    73
ds@1952
    74
void ValveMenuStyle::OnSourceModShutdown()
ds@1952
    75
{
ds@1952
    76
	SH_RELEASE_CALLCLASS(g_pSPHCC);
ds@1952
    77
	SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
ds@1952
    78
	g_Players.RemoveClientListener(this);
ds@1952
    79
}
ds@1952
    80
ds@1952
    81
void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
ds@1952
    82
									   DIALOG_TYPE type,
ds@1952
    83
									   KeyValues *kv,
ds@1952
    84
									   IServerPluginCallbacks *plugin)
ds@1952
    85
{
ds@1952
    86
	if (type != DIALOG_MENU)
ds@1952
    87
	{
ds@1952
    88
		return;
ds@1952
    89
	}
ds@1952
    90
ds@1952
    91
	int client = engine->IndexOfEdict(pEdict);
ds@1952
    92
	if (client < 1 || client > 256)
ds@1952
    93
	{
ds@1952
    94
		return;
ds@1952
    95
	}
ds@1952
    96
ds@1952
    97
	CValveMenuPlayer *player = &m_players[client];
ds@1952
    98
	
ds@1952
    99
	/* We don't care if the player is in a menu because, for all intents and purposes,
ds@1952
   100
	 * the menu is completely private.  Instead, we just figure out the level we'll need
ds@1952
   101
	 * in order to override it.
ds@1952
   102
	 */
ds@1952
   103
	player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
ds@1952
   104
ds@1952
   105
	/* Oh no! What happens if we got a menu that overwrites ours?! */
ds@1952
   106
	if (player->bInMenu)
ds@1952
   107
	{
ds@1952
   108
		/* Okay, let the external menu survive for now.  It may live another 
ds@1952
   109
		 * day to avenge its grandfather, killed in the great Menu Interruption
ds@1952
   110
		 * battle.
ds@1952
   111
		 */
ds@1952
   112
		_CancelClientMenu(client, MenuCancel_Interrupted, true);
ds@1952
   113
	}
ds@1952
   114
}
ds@1952
   115
ds@1952
   116
IMenuPanel *ValveMenuStyle::CreatePanel()
ds@1952
   117
{
ds@1952
   118
	return new CValveMenuDisplay();
ds@1952
   119
}
ds@1952
   120
ds@1952
   121
IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
ds@1952
   122
{
ds@1952
   123
	return new CValveMenu(pHandler, pOwner);
ds@1952
   124
}
ds@1952
   125
ds@1952
   126
const char *ValveMenuStyle::GetStyleName()
ds@1952
   127
{
ds@1952
   128
	return "valve";
ds@1952
   129
}
ds@1952
   130
ds@1952
   131
unsigned int ValveMenuStyle::GetMaxPageItems()
ds@1952
   132
{
ds@1952
   133
	return 8;
ds@1952
   134
}
ds@1952
   135
ds@1952
   136
void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
ds@1952
   137
{
ds@1952
   138
	m_players[client].curPrioLevel--;
ds@1952
   139
	CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
ds@1952
   140
	vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
ds@1952
   141
}
ds@1952
   142
ds@1952
   143
bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
ds@1952
   144
{
ds@1952
   145
	if (vsp_interface == NULL)
ds@1952
   146
	{
ds@1952
   147
		return false;
ds@1952
   148
	}
ds@1952
   149
ds@1952
   150
	return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
ds@1952
   151
}
ds@1952
   152
ds@1952
   153
bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, unsigned int first_item, IMenuHandler *mh, unsigned int time)
ds@1952
   154
{
ds@1952
   155
	if (vsp_interface == NULL)
ds@1952
   156
	{
ds@1952
   157
		mh->OnMenuStart(menu);
ds@1952
   158
		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
ds@1952
   159
		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
ds@1952
   160
		return false;
ds@1952
   161
	}
ds@1952
   162
ds@1952
   163
	return BaseMenuStyle::DoClientMenu(client, menu, first_item, mh, time);
ds@1952
   164
}
ds@1952
   165
ds@1952
   166
unsigned int ValveMenuStyle::GetApproxMemUsage()
ds@1952
   167
{
ds@1952
   168
	return sizeof(ValveMenuStyle) + (sizeof(CValveMenuPlayer) * 257);
ds@1952
   169
}
ds@1952
   170
ds@1952
   171
CValveMenuDisplay::CValveMenuDisplay()
ds@1952
   172
{
ds@1952
   173
	m_pKv = NULL;
ds@1952
   174
	Reset();
ds@1952
   175
}
ds@1952
   176
ds@1952
   177
CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
ds@1952
   178
{
ds@1952
   179
	m_pKv = NULL;
ds@1952
   180
	Reset();
ds@1952
   181
	m_pKv->SetColor("color", pMenu->m_IntroColor);
ds@1952
   182
	m_pKv->SetString("title", pMenu->m_IntroMsg);
ds@1952
   183
}
ds@1952
   184
ds@1952
   185
void CValveMenuDisplay::DeleteThis()
ds@1952
   186
{
ds@1952
   187
	delete this;
ds@1952
   188
}
ds@1952
   189
ds@1952
   190
CValveMenuDisplay::~CValveMenuDisplay()
ds@1952
   191
{
ds@1952
   192
	m_pKv->deleteThis();
ds@1952
   193
}
ds@1952
   194
ds@1952
   195
IMenuStyle *CValveMenuDisplay::GetParentStyle()
ds@1952
   196
{
ds@1952
   197
	return &g_ValveMenuStyle;
ds@1952
   198
}
ds@1952
   199
ds@1952
   200
void CValveMenuDisplay::Reset()
ds@1952
   201
{
ds@1952
   202
	if (m_pKv)
ds@1952
   203
	{
ds@1952
   204
		m_pKv->deleteThis();
ds@1952
   205
	}
ds@1952
   206
	m_pKv = new KeyValues("menu");
ds@1952
   207
	m_NextPos = 1;
ds@1952
   208
	m_TitleDrawn = false;
ds@1952
   209
}
ds@1952
   210
ds@1952
   211
unsigned int CValveMenuDisplay::GetCurrentKey()
ds@1952
   212
{
ds@1952
   213
	return m_NextPos;
ds@1952
   214
}
ds@1952
   215
ds@1952
   216
bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
ds@1952
   217
{
ds@1952
   218
	if (key < m_NextPos || key > 9)
ds@1952
   219
	{
ds@1952
   220
		return false;
ds@1952
   221
	}
ds@1952
   222
ds@1952
   223
	m_NextPos = key;
ds@1952
   224
ds@1952
   225
	return true;
ds@1952
   226
}
ds@1952
   227
ds@1952
   228
bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
ds@1952
   229
{
ds@1952
   230
	if (option == MenuOption_IntroMessage)
ds@1952
   231
	{
ds@1952
   232
		m_pKv->SetString("title", (const char *)valuePtr);
ds@1952
   233
		return true;
ds@1952
   234
	} else if (option == MenuOption_IntroColor) {
ds@1952
   235
		int *array = (int *)valuePtr;
ds@1952
   236
		m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
ds@1952
   237
		return true;
ds@1952
   238
	} else if (option == MenuOption_Priority) {
ds@1952
   239
		m_pKv->SetInt("level", *(int *)valuePtr);
ds@1952
   240
		return true;
ds@1952
   241
	}
ds@1952
   242
ds@1952
   243
	return false;
ds@1952
   244
}
ds@1952
   245
ds@1952
   246
bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
ds@1952
   247
{
ds@1952
   248
	/**
ds@1952
   249
	 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
ds@1952
   250
	 *  so it's completely safe to ignore it.
ds@1952
   251
	 * -----------------------------------------
ds@1952
   252
	 */
ds@1952
   253
	if (drawFlags & ITEMDRAW_RAWLINE)
ds@1952
   254
	{
ds@1952
   255
		return false;
ds@1952
   256
	}
ds@1952
   257
ds@1952
   258
	/**
ds@1952
   259
	 * Special cases, explained in DrawItem()
ds@1952
   260
	 */
ds@1952
   261
	if ((drawFlags & ITEMDRAW_NOTEXT)
ds@1952
   262
		|| (drawFlags & ITEMDRAW_SPACER))
ds@1952
   263
	{
ds@1952
   264
		return true;
ds@1952
   265
	}
ds@1952
   266
ds@1952
   267
	/**
ds@1952
   268
	 * We can't draw disabled text.  We could bump the position, but we
ds@1952
   269
	 * want DirectDraw() to find some actual items to display.
ds@1952
   270
	 */
ds@1952
   271
	if (drawFlags & ITEMDRAW_DISABLED)
ds@1952
   272
	{
ds@1952
   273
		return false;
ds@1952
   274
	}
ds@1952
   275
ds@1952
   276
	return true;
ds@1952
   277
}
ds@1952
   278
ds@1952
   279
unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
ds@1952
   280
{
ds@1952
   281
	if (m_NextPos > 9 || !CanDrawItem(item.style))
ds@1952
   282
	{
ds@1952
   283
		return 0;
ds@1952
   284
	}
ds@1952
   285
ds@1952
   286
	/**
ds@1952
   287
	 * For these cases we can't draw anything at all, but
ds@1952
   288
	 * we can at least bump the position since we were explicitly asked to.
ds@1952
   289
	 */
ds@1952
   290
	if ((item.style & ITEMDRAW_NOTEXT)
ds@1952
   291
		|| (item.style & ITEMDRAW_SPACER))
ds@1952
   292
	{
ds@1952
   293
		return m_NextPos++;
ds@1952
   294
	}
ds@1952
   295
ds@1952
   296
	char buffer[255];
ds@1952
   297
	UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
ds@1952
   298
ds@1952
   299
	KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
ds@1952
   300
	ki->SetString("command", g_OptionCmdTable[m_NextPos]);
ds@1952
   301
	ki->SetString("msg", buffer);
ds@1952
   302
ds@1952
   303
	return m_NextPos++;
ds@1952
   304
}
ds@1952
   305
ds@1952
   306
void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
ds@1952
   307
{
ds@1952
   308
	if (onlyIfEmpty && m_TitleDrawn)
ds@1952
   309
	{
ds@1952
   310
		return;
ds@1952
   311
	}
ds@1952
   312
ds@1952
   313
	m_pKv->SetString("msg", text);
ds@1952
   314
	m_TitleDrawn = true;
ds@1952
   315
}
ds@1952
   316
ds@1952
   317
bool CValveMenuDisplay::DrawRawLine(const char *rawline)
ds@1952
   318
{
ds@1952
   319
	return false;
ds@1952
   320
}
ds@1952
   321
ds@1952
   322
void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
ds@1952
   323
{
ds@1952
   324
	m_pKv->SetInt("level", priority);
ds@1952
   325
	m_pKv->SetInt("time", time ? time : 200);
ds@1952
   326
ds@1952
   327
	SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
ds@1952
   328
		engine->PEntityOfEntIndex(client),
ds@1952
   329
		DIALOG_MENU,
ds@1952
   330
		m_pKv,
ds@1952
   331
		vsp_interface);
ds@1952
   332
}
ds@1952
   333
ds@1952
   334
bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
ds@1952
   335
{
ds@1952
   336
	return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
ds@1952
   337
}
ds@1952
   338
ds@1952
   339
bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
ds@1952
   340
{
ds@1952
   341
	return false;
ds@1952
   342
}
ds@1952
   343
ds@1952
   344
int CValveMenuDisplay::GetAmountRemaining()
ds@1952
   345
{
ds@1952
   346
	/* :TODO: this is a lie, but nothing really seems meaningful... */
ds@1952
   347
	return -1;
ds@1952
   348
}
ds@1952
   349
ds@1952
   350
unsigned int CValveMenuDisplay::GetApproxMemUsage()
ds@1952
   351
{
ds@1952
   352
	return sizeof(CValveMenuDisplay) + (sizeof(KeyValues) * m_NextPos * 10);
ds@1952
   353
}
ds@1952
   354
ds@1952
   355
CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) : 
ds@1952
   356
CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner), 
ds@1952
   357
	m_IntroColor(255, 0, 0, 255)
ds@1952
   358
{
ds@1952
   359
	strcpy(m_IntroMsg, "You have a menu, press ESC");
ds@1952
   360
	m_Pagination = 5;
ds@1952
   361
}
ds@1952
   362
ds@1952
   363
void CValveMenu::Cancel_Finally()
ds@1952
   364
{
ds@1952
   365
	g_ValveMenuStyle.CancelMenu(this);
ds@1952
   366
}
ds@1952
   367
ds@1952
   368
bool CValveMenu::SetPagination(unsigned int itemsPerPage)
ds@1952
   369
{
ds@1952
   370
	if (itemsPerPage > 5)
ds@1952
   371
	{
ds@1952
   372
		return false;
ds@1952
   373
	}
ds@1952
   374
ds@1952
   375
	return CBaseMenu::SetPagination(itemsPerPage);
ds@1952
   376
}
ds@1952
   377
ds@1952
   378
bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
ds@1952
   379
{
ds@1952
   380
	if (option == MenuOption_IntroMessage)
ds@1952
   381
	{
ds@1952
   382
		strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
ds@1952
   383
		return true;
ds@1952
   384
	} else if (option == MenuOption_IntroColor) {
ds@1952
   385
		unsigned int *array = (unsigned int *)valuePtr;
ds@1952
   386
		m_IntroColor = Color(array[0], array[1], array[2], array[3]);
ds@1952
   387
		return true;
ds@1952
   388
	}
ds@1952
   389
ds@1952
   390
	return false;
ds@1952
   391
}
ds@1952
   392
ds@1952
   393
bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
ds@1952
   394
{
ds@1952
   395
	return DisplayAtItem(client, time, 0, alt_handler);
ds@1952
   396
}
ds@1952
   397
ds@1952
   398
bool CValveMenu::DisplayAtItem(int client,
ds@1952
   399
							   unsigned int time,
ds@1952
   400
							   unsigned int start_item,
ds@1952
   401
							   IMenuHandler *alt_handler/* =NULL */)
ds@1952
   402
{
ds@1952
   403
	if (m_bCancelling)
ds@1952
   404
	{
ds@1952
   405
		return false;
ds@1952
   406
	}
ds@1952
   407
ds@1952
   408
	return g_ValveMenuStyle.DoClientMenu(client, this, start_item, alt_handler ? alt_handler : m_pHandler, time);
ds@1952
   409
}
ds@1952
   410
ds@1952
   411
IMenuPanel *CValveMenu::CreatePanel()
ds@1952
   412
{
ds@1952
   413
	return new CValveMenuDisplay(this);
ds@1952
   414
}
ds@1952
   415
ds@1952
   416
void CValveMenu::SetMenuOptionFlags(unsigned int flags)
ds@1952
   417
{
ds@1952
   418
	flags |= MENUFLAG_BUTTON_EXIT;
ds@1952
   419
	CBaseMenu::SetMenuOptionFlags(flags);
ds@1952
   420
}
ds@1952
   421
ds@1952
   422
unsigned int CValveMenu::GetApproxMemUsage()
ds@1952
   423
{
ds@1952
   424
	return sizeof(CValveMenu) + GetBaseMemUsage();
ds@1952
   425
}
ds@1952
   426
ds@1952
   427
const char *g_OptionNumTable[] = 
ds@1952
   428
{
ds@1952
   429
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
ds@1952
   430
};
ds@1952
   431
ds@1952
   432
const char *g_OptionCmdTable[] = 
ds@1952
   433
{
ds@1952
   434
	"sm_vmenuselect 0", /* INVALID! */
ds@1952
   435
	"sm_vmenuselect 1",
ds@1952
   436
	"sm_vmenuselect 2",
ds@1952
   437
	"sm_vmenuselect 3",
ds@1952
   438
	"sm_vmenuselect 4",
ds@1952
   439
	"sm_vmenuselect 5",
ds@1952
   440
	"sm_vmenuselect 6",
ds@1952
   441
	"sm_vmenuselect 7",
ds@1952
   442
	"sm_vmenuselect 8",
ds@1952
   443
	"sm_vmenuselect 9",
ds@1952
   444
	"sm_vmenuselect 10"
ds@1952
   445
};