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