core/MenuStyle_Valve.cpp
author David Anderson <dvander@alliedmods.net>
Tue Sep 11 18:08:49 2007 +0000 (2007-09-11)
changeset 1408 2fd63cc4bd9c
parent 1361 591f6d2fd11d
child 1474 a3b73df84d88
permissions -rw-r--r--
timelimit function now gets initialized
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_stringutil.h"
    33 #include "PlayerManager.h"
    34 #include "MenuStyle_Valve.h"
    35 #include "Translator.h"
    36 #include "PlayerManager.h"
    37 #include "ConCmdManager.h"
    38 
    39 SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
    40 
    41 ValveMenuStyle g_ValveMenuStyle;
    42 extern const char *g_OptionNumTable[];
    43 extern const char *g_OptionCmdTable[];
    44 IServerPluginCallbacks *g_pVSPHandle = NULL;
    45 CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
    46 
    47 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
    48 {
    49 }
    50 
    51 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
    52 {
    53 	return &m_players[client];
    54 }
    55 
    56 bool ValveMenuStyle::OnClientCommand(int client, const char *cmd)
    57 {
    58 	if (strcmp(cmd, "sm_vmenuselect") == 0)
    59 	{
    60 		int key_press = atoi(engine->Cmd_Argv(1));
    61 		g_ValveMenuStyle.ClientPressedKey(client, key_press);
    62 		return true;
    63 	}
    64 
    65 	return false;
    66 }
    67 
    68 void ValveMenuStyle::OnSourceModAllInitialized()
    69 {
    70 	g_Players.AddClientListener(this);
    71 	SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
    72 	g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
    73 }
    74 
    75 void ValveMenuStyle::OnSourceModShutdown()
    76 {
    77 	SH_RELEASE_CALLCLASS(g_pSPHCC);
    78 	SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
    79 	g_Players.RemoveClientListener(this);
    80 }
    81 
    82 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
    83 									   DIALOG_TYPE type,
    84 									   KeyValues *kv,
    85 									   IServerPluginCallbacks *plugin)
    86 {
    87 	if (type != DIALOG_MENU)
    88 	{
    89 		return;
    90 	}
    91 
    92 	int client = engine->IndexOfEdict(pEdict);
    93 	if (client < 1 || client > 256)
    94 	{
    95 		return;
    96 	}
    97 
    98 	CValveMenuPlayer *player = &m_players[client];
    99 	
   100 	/* We don't care if the player is in a menu because, for all intents and purposes,
   101 	 * the menu is completely private.  Instead, we just figure out the level we'll need
   102 	 * in order to override it.
   103 	 */
   104 	player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
   105 
   106 	/* Oh no! What happens if we got a menu that overwrites ours?! */
   107 	if (player->bInMenu)
   108 	{
   109 		/* Okay, let the external menu survive for now.  It may live another 
   110 		 * day to avenge its grandfather, killed in the great Menu Interruption
   111 		 * battle.
   112 		 */
   113 		_CancelClientMenu(client, MenuCancel_Interrupted, true);
   114 	}
   115 }
   116 
   117 void ValveMenuStyle::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
   118 {
   119 	g_pVSPHandle = iface;
   120 }
   121 
   122 IMenuPanel *ValveMenuStyle::CreatePanel()
   123 {
   124 	return new CValveMenuDisplay();
   125 }
   126 
   127 IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
   128 {
   129 	return new CValveMenu(pHandler, pOwner);
   130 }
   131 
   132 const char *ValveMenuStyle::GetStyleName()
   133 {
   134 	return "valve";
   135 }
   136 
   137 unsigned int ValveMenuStyle::GetMaxPageItems()
   138 {
   139 	return 8;
   140 }
   141 
   142 void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
   143 {
   144 	m_players[client].curPrioLevel--;
   145 	CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
   146 	vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
   147 }
   148 
   149 bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
   150 {
   151 	if (!g_pVSPHandle)
   152 	{
   153 		return false;
   154 	}
   155 
   156 	return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
   157 }
   158 
   159 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
   160 {
   161 	if (!g_pVSPHandle)
   162 	{
   163 		mh->OnMenuStart(menu);
   164 		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
   165 		mh->OnMenuEnd(menu, MenuEnd_Cancelled);
   166 		return false;
   167 	}
   168 
   169 	return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
   170 }
   171 
   172 CValveMenuDisplay::CValveMenuDisplay()
   173 {
   174 	m_pKv = NULL;
   175 	Reset();
   176 }
   177 
   178 CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
   179 {
   180 	m_pKv = NULL;
   181 	Reset();
   182 	m_pKv->SetColor("color", pMenu->m_IntroColor);
   183 	m_pKv->SetString("title", pMenu->m_IntroMsg);
   184 }
   185 
   186 void CValveMenuDisplay::DeleteThis()
   187 {
   188 	delete this;
   189 }
   190 
   191 CValveMenuDisplay::~CValveMenuDisplay()
   192 {
   193 	m_pKv->deleteThis();
   194 }
   195 
   196 IMenuStyle *CValveMenuDisplay::GetParentStyle()
   197 {
   198 	return &g_ValveMenuStyle;
   199 }
   200 
   201 void CValveMenuDisplay::Reset()
   202 {
   203 	if (m_pKv)
   204 	{
   205 		m_pKv->deleteThis();
   206 	}
   207 	m_pKv = new KeyValues("menu");
   208 	m_NextPos = 1;
   209 	m_TitleDrawn = false;
   210 }
   211 
   212 unsigned int CValveMenuDisplay::GetCurrentKey()
   213 {
   214 	return m_NextPos;
   215 }
   216 
   217 bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
   218 {
   219 	if (key < m_NextPos || key > 9)
   220 	{
   221 		return false;
   222 	}
   223 
   224 	m_NextPos = key;
   225 
   226 	return true;
   227 }
   228 
   229 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
   230 {
   231 	if (option == MenuOption_IntroMessage)
   232 	{
   233 		m_pKv->SetString("title", (const char *)valuePtr);
   234 		return true;
   235 	} else if (option == MenuOption_IntroColor) {
   236 		int *array = (int *)valuePtr;
   237 		m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
   238 		return true;
   239 	} else if (option == MenuOption_Priority) {
   240 		m_pKv->SetInt("level", *(int *)valuePtr);
   241 		return true;
   242 	}
   243 
   244 	return false;
   245 }
   246 
   247 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
   248 {
   249 	/**
   250 	 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
   251 	 *  so it's completely safe to ignore it.
   252 	 * -----------------------------------------
   253 	 */
   254 	if (drawFlags & ITEMDRAW_RAWLINE)
   255 	{
   256 		return false;
   257 	}
   258 
   259 	/**
   260 	 * Special cases, explained in DrawItem()
   261 	 */
   262 	if ((drawFlags & ITEMDRAW_NOTEXT)
   263 		|| (drawFlags & ITEMDRAW_SPACER))
   264 	{
   265 		return true;
   266 	}
   267 
   268 	/**
   269 	 * We can't draw disabled text.  We could bump the position, but we
   270 	 * want DirectDraw() to find some actual items to display.
   271 	 */
   272 	if (drawFlags & ITEMDRAW_DISABLED)
   273 	{
   274 		return false;
   275 	}
   276 
   277 	return true;
   278 }
   279 
   280 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
   281 {
   282 	if (m_NextPos > 9 || !CanDrawItem(item.style))
   283 	{
   284 		return 0;
   285 	}
   286 
   287 	/**
   288 	 * For these cases we can't draw anything at all, but
   289 	 * we can at least bump the position since we were explicitly asked to.
   290 	 */
   291 	if ((item.style & ITEMDRAW_NOTEXT)
   292 		|| (item.style & ITEMDRAW_SPACER))
   293 	{
   294 		return m_NextPos++;
   295 	}
   296 
   297 	char buffer[255];
   298 	UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
   299 
   300 	KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
   301 	ki->SetString("command", g_OptionCmdTable[m_NextPos]);
   302 	ki->SetString("msg", buffer);
   303 
   304 	return m_NextPos++;
   305 }
   306 
   307 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
   308 {
   309 	if (onlyIfEmpty && m_TitleDrawn)
   310 	{
   311 		return;
   312 	}
   313 
   314 	m_pKv->SetString("msg", text);
   315 	m_TitleDrawn = true;
   316 }
   317 
   318 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
   319 {
   320 	return false;
   321 }
   322 
   323 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
   324 {
   325 	m_pKv->SetInt("level", priority);
   326 	m_pKv->SetInt("time", time ? time : 200);
   327 
   328 	SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
   329 		engine->PEntityOfEntIndex(client),
   330 		DIALOG_MENU,
   331 		m_pKv,
   332 		g_pVSPHandle);
   333 }
   334 
   335 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
   336 {
   337 	return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
   338 }
   339 
   340 bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
   341 {
   342 	return false;
   343 }
   344 
   345 int CValveMenuDisplay::GetAmountRemaining()
   346 {
   347 	/* :TODO: this is a lie, but nothing really seems meaningful... */
   348 	return -1;
   349 }
   350 
   351 CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) : 
   352 CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner), 
   353 	m_IntroColor(255, 0, 0, 255)
   354 {
   355 	strcpy(m_IntroMsg, "You have a menu, press ESC");
   356 	m_Pagination = 5;
   357 }
   358 
   359 void CValveMenu::Cancel_Finally()
   360 {
   361 	g_ValveMenuStyle.CancelMenu(this);
   362 }
   363 
   364 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
   365 {
   366 	if (itemsPerPage > 5)
   367 	{
   368 		return false;
   369 	}
   370 
   371 	return CBaseMenu::SetPagination(itemsPerPage);
   372 }
   373 
   374 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
   375 {
   376 	if (option == MenuOption_IntroMessage)
   377 	{
   378 		strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
   379 		return true;
   380 	} else if (option == MenuOption_IntroColor) {
   381 		unsigned int *array = (unsigned int *)valuePtr;
   382 		m_IntroColor = Color(array[0], array[1], array[2], array[3]);
   383 		return true;
   384 	}
   385 
   386 	return false;
   387 }
   388 
   389 bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
   390 {
   391 	if (m_bCancelling)
   392 	{
   393 		return false;
   394 	}
   395 
   396 	return g_ValveMenuStyle.DoClientMenu(client, this, alt_handler ? alt_handler : m_pHandler, time);
   397 }
   398 
   399 IMenuPanel *CValveMenu::CreatePanel()
   400 {
   401 	return new CValveMenuDisplay(this);
   402 }
   403 
   404 void CValveMenu::SetMenuOptionFlags(unsigned int flags)
   405 {
   406 	flags |= MENUFLAG_BUTTON_EXIT;
   407 	CBaseMenu::SetMenuOptionFlags(flags);
   408 }
   409 
   410 const char *g_OptionNumTable[] = 
   411 {
   412 	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
   413 };
   414 
   415 const char *g_OptionCmdTable[] = 
   416 {
   417 	"sm_vmenuselect 0", /* INVALID! */
   418 	"sm_vmenuselect 1",
   419 	"sm_vmenuselect 2",
   420 	"sm_vmenuselect 3",
   421 	"sm_vmenuselect 4",
   422 	"sm_vmenuselect 5",
   423 	"sm_vmenuselect 6",
   424 	"sm_vmenuselect 7",
   425 	"sm_vmenuselect 8",
   426 	"sm_vmenuselect 9",
   427 	"sm_vmenuselect 10"
   428 };