core/MenuStyle_Valve.cpp
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/core/MenuStyle_Valve.cpp	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,445 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#include "sm_stringutil.h"
    1.36 -#include "PlayerManager.h"
    1.37 -#include "MenuStyle_Valve.h"
    1.38 -#include "Translator.h"
    1.39 -#include "PlayerManager.h"
    1.40 -#include "ConCmdManager.h"
    1.41 -
    1.42 -SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
    1.43 -
    1.44 -ValveMenuStyle g_ValveMenuStyle;
    1.45 -extern const char *g_OptionNumTable[];
    1.46 -extern const char *g_OptionCmdTable[];
    1.47 -CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
    1.48 -
    1.49 -ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
    1.50 -{
    1.51 -}
    1.52 -
    1.53 -CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
    1.54 -{
    1.55 -	return &m_players[client];
    1.56 -}
    1.57 -
    1.58 -bool ValveMenuStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
    1.59 -{
    1.60 -	if (strcmp(cmdname, "sm_vmenuselect") == 0)
    1.61 -	{
    1.62 -		int key_press = atoi(cmd.Arg(1));
    1.63 -		g_ValveMenuStyle.ClientPressedKey(client, key_press);
    1.64 -		return true;
    1.65 -	}
    1.66 -
    1.67 -	return false;
    1.68 -}
    1.69 -
    1.70 -void ValveMenuStyle::OnSourceModAllInitialized()
    1.71 -{
    1.72 -	g_Players.AddClientListener(this);
    1.73 -	SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
    1.74 -	g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
    1.75 -}
    1.76 -
    1.77 -void ValveMenuStyle::OnSourceModShutdown()
    1.78 -{
    1.79 -	SH_RELEASE_CALLCLASS(g_pSPHCC);
    1.80 -	SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
    1.81 -	g_Players.RemoveClientListener(this);
    1.82 -}
    1.83 -
    1.84 -void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
    1.85 -									   DIALOG_TYPE type,
    1.86 -									   KeyValues *kv,
    1.87 -									   IServerPluginCallbacks *plugin)
    1.88 -{
    1.89 -	if (type != DIALOG_MENU)
    1.90 -	{
    1.91 -		return;
    1.92 -	}
    1.93 -
    1.94 -	int client = engine->IndexOfEdict(pEdict);
    1.95 -	if (client < 1 || client > 256)
    1.96 -	{
    1.97 -		return;
    1.98 -	}
    1.99 -
   1.100 -	CValveMenuPlayer *player = &m_players[client];
   1.101 -	
   1.102 -	/* We don't care if the player is in a menu because, for all intents and purposes,
   1.103 -	 * the menu is completely private.  Instead, we just figure out the level we'll need
   1.104 -	 * in order to override it.
   1.105 -	 */
   1.106 -	player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
   1.107 -
   1.108 -	/* Oh no! What happens if we got a menu that overwrites ours?! */
   1.109 -	if (player->bInMenu)
   1.110 -	{
   1.111 -		/* Okay, let the external menu survive for now.  It may live another 
   1.112 -		 * day to avenge its grandfather, killed in the great Menu Interruption
   1.113 -		 * battle.
   1.114 -		 */
   1.115 -		_CancelClientMenu(client, MenuCancel_Interrupted, true);
   1.116 -	}
   1.117 -}
   1.118 -
   1.119 -IMenuPanel *ValveMenuStyle::CreatePanel()
   1.120 -{
   1.121 -	return new CValveMenuDisplay();
   1.122 -}
   1.123 -
   1.124 -IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
   1.125 -{
   1.126 -	return new CValveMenu(pHandler, pOwner);
   1.127 -}
   1.128 -
   1.129 -const char *ValveMenuStyle::GetStyleName()
   1.130 -{
   1.131 -	return "valve";
   1.132 -}
   1.133 -
   1.134 -unsigned int ValveMenuStyle::GetMaxPageItems()
   1.135 -{
   1.136 -	return 8;
   1.137 -}
   1.138 -
   1.139 -void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
   1.140 -{
   1.141 -	m_players[client].curPrioLevel--;
   1.142 -	CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
   1.143 -	vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
   1.144 -}
   1.145 -
   1.146 -bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
   1.147 -{
   1.148 -	if (vsp_interface == NULL)
   1.149 -	{
   1.150 -		return false;
   1.151 -	}
   1.152 -
   1.153 -	return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
   1.154 -}
   1.155 -
   1.156 -bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, unsigned int first_item, IMenuHandler *mh, unsigned int time)
   1.157 -{
   1.158 -	if (vsp_interface == NULL)
   1.159 -	{
   1.160 -		mh->OnMenuStart(menu);
   1.161 -		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
   1.162 -		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
   1.163 -		return false;
   1.164 -	}
   1.165 -
   1.166 -	return BaseMenuStyle::DoClientMenu(client, menu, first_item, mh, time);
   1.167 -}
   1.168 -
   1.169 -unsigned int ValveMenuStyle::GetApproxMemUsage()
   1.170 -{
   1.171 -	return sizeof(ValveMenuStyle) + (sizeof(CValveMenuPlayer) * 257);
   1.172 -}
   1.173 -
   1.174 -CValveMenuDisplay::CValveMenuDisplay()
   1.175 -{
   1.176 -	m_pKv = NULL;
   1.177 -	Reset();
   1.178 -}
   1.179 -
   1.180 -CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
   1.181 -{
   1.182 -	m_pKv = NULL;
   1.183 -	Reset();
   1.184 -	m_pKv->SetColor("color", pMenu->m_IntroColor);
   1.185 -	m_pKv->SetString("title", pMenu->m_IntroMsg);
   1.186 -}
   1.187 -
   1.188 -void CValveMenuDisplay::DeleteThis()
   1.189 -{
   1.190 -	delete this;
   1.191 -}
   1.192 -
   1.193 -CValveMenuDisplay::~CValveMenuDisplay()
   1.194 -{
   1.195 -	m_pKv->deleteThis();
   1.196 -}
   1.197 -
   1.198 -IMenuStyle *CValveMenuDisplay::GetParentStyle()
   1.199 -{
   1.200 -	return &g_ValveMenuStyle;
   1.201 -}
   1.202 -
   1.203 -void CValveMenuDisplay::Reset()
   1.204 -{
   1.205 -	if (m_pKv)
   1.206 -	{
   1.207 -		m_pKv->deleteThis();
   1.208 -	}
   1.209 -	m_pKv = new KeyValues("menu");
   1.210 -	m_NextPos = 1;
   1.211 -	m_TitleDrawn = false;
   1.212 -}
   1.213 -
   1.214 -unsigned int CValveMenuDisplay::GetCurrentKey()
   1.215 -{
   1.216 -	return m_NextPos;
   1.217 -}
   1.218 -
   1.219 -bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
   1.220 -{
   1.221 -	if (key < m_NextPos || key > 9)
   1.222 -	{
   1.223 -		return false;
   1.224 -	}
   1.225 -
   1.226 -	m_NextPos = key;
   1.227 -
   1.228 -	return true;
   1.229 -}
   1.230 -
   1.231 -bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
   1.232 -{
   1.233 -	if (option == MenuOption_IntroMessage)
   1.234 -	{
   1.235 -		m_pKv->SetString("title", (const char *)valuePtr);
   1.236 -		return true;
   1.237 -	} else if (option == MenuOption_IntroColor) {
   1.238 -		int *array = (int *)valuePtr;
   1.239 -		m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
   1.240 -		return true;
   1.241 -	} else if (option == MenuOption_Priority) {
   1.242 -		m_pKv->SetInt("level", *(int *)valuePtr);
   1.243 -		return true;
   1.244 -	}
   1.245 -
   1.246 -	return false;
   1.247 -}
   1.248 -
   1.249 -bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
   1.250 -{
   1.251 -	/**
   1.252 -	 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
   1.253 -	 *  so it's completely safe to ignore it.
   1.254 -	 * -----------------------------------------
   1.255 -	 */
   1.256 -	if (drawFlags & ITEMDRAW_RAWLINE)
   1.257 -	{
   1.258 -		return false;
   1.259 -	}
   1.260 -
   1.261 -	/**
   1.262 -	 * Special cases, explained in DrawItem()
   1.263 -	 */
   1.264 -	if ((drawFlags & ITEMDRAW_NOTEXT)
   1.265 -		|| (drawFlags & ITEMDRAW_SPACER))
   1.266 -	{
   1.267 -		return true;
   1.268 -	}
   1.269 -
   1.270 -	/**
   1.271 -	 * We can't draw disabled text.  We could bump the position, but we
   1.272 -	 * want DirectDraw() to find some actual items to display.
   1.273 -	 */
   1.274 -	if (drawFlags & ITEMDRAW_DISABLED)
   1.275 -	{
   1.276 -		return false;
   1.277 -	}
   1.278 -
   1.279 -	return true;
   1.280 -}
   1.281 -
   1.282 -unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
   1.283 -{
   1.284 -	if (m_NextPos > 9 || !CanDrawItem(item.style))
   1.285 -	{
   1.286 -		return 0;
   1.287 -	}
   1.288 -
   1.289 -	/**
   1.290 -	 * For these cases we can't draw anything at all, but
   1.291 -	 * we can at least bump the position since we were explicitly asked to.
   1.292 -	 */
   1.293 -	if ((item.style & ITEMDRAW_NOTEXT)
   1.294 -		|| (item.style & ITEMDRAW_SPACER))
   1.295 -	{
   1.296 -		return m_NextPos++;
   1.297 -	}
   1.298 -
   1.299 -	char buffer[255];
   1.300 -	UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
   1.301 -
   1.302 -	KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
   1.303 -	ki->SetString("command", g_OptionCmdTable[m_NextPos]);
   1.304 -	ki->SetString("msg", buffer);
   1.305 -
   1.306 -	return m_NextPos++;
   1.307 -}
   1.308 -
   1.309 -void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
   1.310 -{
   1.311 -	if (onlyIfEmpty && m_TitleDrawn)
   1.312 -	{
   1.313 -		return;
   1.314 -	}
   1.315 -
   1.316 -	m_pKv->SetString("msg", text);
   1.317 -	m_TitleDrawn = true;
   1.318 -}
   1.319 -
   1.320 -bool CValveMenuDisplay::DrawRawLine(const char *rawline)
   1.321 -{
   1.322 -	return false;
   1.323 -}
   1.324 -
   1.325 -void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
   1.326 -{
   1.327 -	m_pKv->SetInt("level", priority);
   1.328 -	m_pKv->SetInt("time", time ? time : 200);
   1.329 -
   1.330 -	SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
   1.331 -		engine->PEntityOfEntIndex(client),
   1.332 -		DIALOG_MENU,
   1.333 -		m_pKv,
   1.334 -		vsp_interface);
   1.335 -}
   1.336 -
   1.337 -bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
   1.338 -{
   1.339 -	return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
   1.340 -}
   1.341 -
   1.342 -bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
   1.343 -{
   1.344 -	return false;
   1.345 -}
   1.346 -
   1.347 -int CValveMenuDisplay::GetAmountRemaining()
   1.348 -{
   1.349 -	/* :TODO: this is a lie, but nothing really seems meaningful... */
   1.350 -	return -1;
   1.351 -}
   1.352 -
   1.353 -unsigned int CValveMenuDisplay::GetApproxMemUsage()
   1.354 -{
   1.355 -	return sizeof(CValveMenuDisplay) + (sizeof(KeyValues) * m_NextPos * 10);
   1.356 -}
   1.357 -
   1.358 -CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) : 
   1.359 -CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner), 
   1.360 -	m_IntroColor(255, 0, 0, 255)
   1.361 -{
   1.362 -	strcpy(m_IntroMsg, "You have a menu, press ESC");
   1.363 -	m_Pagination = 5;
   1.364 -}
   1.365 -
   1.366 -void CValveMenu::Cancel_Finally()
   1.367 -{
   1.368 -	g_ValveMenuStyle.CancelMenu(this);
   1.369 -}
   1.370 -
   1.371 -bool CValveMenu::SetPagination(unsigned int itemsPerPage)
   1.372 -{
   1.373 -	if (itemsPerPage > 5)
   1.374 -	{
   1.375 -		return false;
   1.376 -	}
   1.377 -
   1.378 -	return CBaseMenu::SetPagination(itemsPerPage);
   1.379 -}
   1.380 -
   1.381 -bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
   1.382 -{
   1.383 -	if (option == MenuOption_IntroMessage)
   1.384 -	{
   1.385 -		strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
   1.386 -		return true;
   1.387 -	} else if (option == MenuOption_IntroColor) {
   1.388 -		unsigned int *array = (unsigned int *)valuePtr;
   1.389 -		m_IntroColor = Color(array[0], array[1], array[2], array[3]);
   1.390 -		return true;
   1.391 -	}
   1.392 -
   1.393 -	return false;
   1.394 -}
   1.395 -
   1.396 -bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
   1.397 -{
   1.398 -	return DisplayAtItem(client, time, 0, alt_handler);
   1.399 -}
   1.400 -
   1.401 -bool CValveMenu::DisplayAtItem(int client,
   1.402 -							   unsigned int time,
   1.403 -							   unsigned int start_item,
   1.404 -							   IMenuHandler *alt_handler/* =NULL */)
   1.405 -{
   1.406 -	if (m_bCancelling)
   1.407 -	{
   1.408 -		return false;
   1.409 -	}
   1.410 -
   1.411 -	return g_ValveMenuStyle.DoClientMenu(client, this, start_item, alt_handler ? alt_handler : m_pHandler, time);
   1.412 -}
   1.413 -
   1.414 -IMenuPanel *CValveMenu::CreatePanel()
   1.415 -{
   1.416 -	return new CValveMenuDisplay(this);
   1.417 -}
   1.418 -
   1.419 -void CValveMenu::SetMenuOptionFlags(unsigned int flags)
   1.420 -{
   1.421 -	flags |= MENUFLAG_BUTTON_EXIT;
   1.422 -	CBaseMenu::SetMenuOptionFlags(flags);
   1.423 -}
   1.424 -
   1.425 -unsigned int CValveMenu::GetApproxMemUsage()
   1.426 -{
   1.427 -	return sizeof(CValveMenu) + GetBaseMemUsage();
   1.428 -}
   1.429 -
   1.430 -const char *g_OptionNumTable[] = 
   1.431 -{
   1.432 -	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
   1.433 -};
   1.434 -
   1.435 -const char *g_OptionCmdTable[] = 
   1.436 -{
   1.437 -	"sm_vmenuselect 0", /* INVALID! */
   1.438 -	"sm_vmenuselect 1",
   1.439 -	"sm_vmenuselect 2",
   1.440 -	"sm_vmenuselect 3",
   1.441 -	"sm_vmenuselect 4",
   1.442 -	"sm_vmenuselect 5",
   1.443 -	"sm_vmenuselect 6",
   1.444 -	"sm_vmenuselect 7",
   1.445 -	"sm_vmenuselect 8",
   1.446 -	"sm_vmenuselect 9",
   1.447 -	"sm_vmenuselect 10"
   1.448 -};