core/MenuStyle_Valve.cpp
author David Anderson <dvander@alliedmods.net>
Sun May 13 05:53:36 2007 +0000 (2007-05-13)
changeset 781 d5a9953793c4
parent 778 07e49a8cf046
child 785 bca431afd754
permissions -rw-r--r--
exposed the interface since the API is almost stable
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_stringutil.h"
    16 #include "PlayerManager.h"
    17 #include "MenuStyle_Valve.h"
    18 #include "Translator.h"
    19 #include "PlayerManager.h"
    20 #include "ConCmdManager.h"
    21 
    22 SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
    23 
    24 ValveMenuStyle g_ValveMenuStyle;
    25 extern const char *g_OptionNumTable[];
    26 extern const char *g_OptionCmdTable[];
    27 IServerPluginCallbacks *g_pVSPHandle = NULL;
    28 CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
    29 
    30 class TestHandler : public IMenuHandler
    31 {
    32 public:
    33 	virtual void OnMenuEnd(IBaseMenu *menu)
    34 	{
    35 		menu->Destroy();
    36 	}
    37 };
    38 
    39 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
    40 {
    41 }
    42 
    43 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
    44 {
    45 	return &m_players[client];
    46 }
    47 
    48 bool ValveMenuStyle::OnClientCommand(int client)
    49 {
    50 	const char *cmd = engine->Cmd_Argv(0);
    51 
    52 	if (strcmp(cmd, "sm_vmenuselect") == 0)
    53 	{
    54 		int key_press = atoi(engine->Cmd_Argv(1));
    55 		g_ValveMenuStyle.ClientPressedKey(client, key_press);
    56 		return true;
    57 	}
    58 
    59 	return false;
    60 }
    61 
    62 void ValveMenuStyle::OnSourceModAllInitialized()
    63 {
    64 	g_Players.AddClientListener(this);
    65 	SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
    66 	g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
    67 }
    68 
    69 void ValveMenuStyle::OnSourceModShutdown()
    70 {
    71 	SH_RELEASE_CALLCLASS(g_pSPHCC);
    72 	SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
    73 	g_Players.RemoveClientListener(this);
    74 }
    75 
    76 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
    77 									   DIALOG_TYPE type,
    78 									   KeyValues *kv,
    79 									   IServerPluginCallbacks *plugin)
    80 {
    81 	if (type != DIALOG_MENU)
    82 	{
    83 		return;
    84 	}
    85 
    86 	int client = engine->IndexOfEdict(pEdict);
    87 	if (client < 1 || client > 256)
    88 	{
    89 		return;
    90 	}
    91 
    92 	CValveMenuPlayer *player = &m_players[client];
    93 	
    94 	/* We don't care if the player is in a menu because, for all intents and purposes,
    95 	 * the menu is completely private.  Instead, we just figure out the level we'll need
    96 	 * in order to override it.
    97 	 */
    98 	player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
    99 
   100 	/* Oh no! What happens if we got a menu that overwrites ours?! */
   101 	if (player->bInMenu)
   102 	{
   103 		/* Okay, let the external menu survive for now.  It may live another 
   104 		 * day to avenge its grandfather, killed in the great Menu Interruption
   105 		 * battle.
   106 		 */
   107 		_CancelClientMenu(client, true);
   108 	}
   109 }
   110 
   111 void ValveMenuStyle::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
   112 {
   113 	g_pVSPHandle = iface;
   114 }
   115 
   116 IMenuDisplay *ValveMenuStyle::CreateDisplay()
   117 {
   118 	return new CValveMenuDisplay();
   119 }
   120 
   121 IBaseMenu *ValveMenuStyle::CreateMenu()
   122 {
   123 	return new CValveMenu();
   124 }
   125 
   126 const char *ValveMenuStyle::GetStyleName()
   127 {
   128 	return "valve";
   129 }
   130 
   131 unsigned int ValveMenuStyle::GetMaxPageItems()
   132 {
   133 	return 8;
   134 }
   135 
   136 void ValveMenuStyle::SendDisplay(int client, IMenuDisplay *display)
   137 {
   138 	m_players[client].curPrioLevel--;
   139 	CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
   140 	vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
   141 }
   142 
   143 bool ValveMenuStyle::DoClientMenu(int client, IMenuDisplay *menu, IMenuHandler *mh, unsigned int time)
   144 {
   145 	if (!g_pVSPHandle)
   146 	{
   147 		return false;
   148 	}
   149 
   150 	return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
   151 }
   152 
   153 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
   154 {
   155 	if (!g_pVSPHandle)
   156 	{
   157 		mh->OnMenuStart(menu);
   158 		mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
   159 		mh->OnMenuEnd(menu);
   160 		return false;
   161 	}
   162 
   163 	return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
   164 }
   165 
   166 CValveMenuDisplay::CValveMenuDisplay()
   167 {
   168 	m_pKv = NULL;
   169 	Reset();
   170 }
   171 
   172 CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
   173 {
   174 	m_pKv = NULL;
   175 	Reset();
   176 	m_pKv->SetColor("color", pMenu->m_IntroColor);
   177 	m_pKv->SetString("title", pMenu->m_IntroMsg);
   178 }
   179 
   180 void CValveMenuDisplay::DeleteThis()
   181 {
   182 	delete this;
   183 }
   184 
   185 CValveMenuDisplay::~CValveMenuDisplay()
   186 {
   187 	m_pKv->deleteThis();
   188 }
   189 
   190 IMenuStyle *CValveMenuDisplay::GetParentStyle()
   191 {
   192 	return &g_ValveMenuStyle;
   193 }
   194 
   195 void CValveMenuDisplay::Reset()
   196 {
   197 	if (m_pKv)
   198 	{
   199 		m_pKv->deleteThis();
   200 	}
   201 	m_pKv = new KeyValues("menu");
   202 	m_NextPos = 1;
   203 	m_TitleDrawn = false;
   204 }
   205 
   206 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
   207 {
   208 	if (option == MenuOption_IntroMessage)
   209 	{
   210 		m_pKv->SetString("title", (const char *)valuePtr);
   211 		return true;
   212 	} else if (option == MenuOption_IntroColor) {
   213 		int *array = (int *)valuePtr;
   214 		m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
   215 		return true;
   216 	} else if (option == MenuOption_Priority) {
   217 		m_pKv->SetInt("level", *(int *)valuePtr);
   218 		return true;
   219 	}
   220 
   221 	return false;
   222 }
   223 
   224 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
   225 {
   226 	/**
   227 	 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
   228 	 *  so it's completely safe to ignore it.
   229 	 * -----------------------------------------
   230 	 */
   231 	if (drawFlags & ITEMDRAW_RAWLINE)
   232 	{
   233 		return false;
   234 	}
   235 
   236 	/**
   237 	 * Special cases, explained in DrawItem()
   238 	 */
   239 	if ((drawFlags & ITEMDRAW_NOTEXT)
   240 		|| (drawFlags & ITEMDRAW_SPACER))
   241 	{
   242 		return true;
   243 	}
   244 
   245 	/**
   246 	 * We can't draw disabled text.  We could bump the position, but we
   247 	 * want DirectDraw() to find some actual items to display.
   248 	 */
   249 	if (drawFlags & ITEMDRAW_DISABLED)
   250 	{
   251 		return false;
   252 	}
   253 
   254 	return true;
   255 }
   256 
   257 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
   258 {
   259 	if (m_NextPos > 9 || !CanDrawItem(item.style))
   260 	{
   261 		return 0;
   262 	}
   263 
   264 	/**
   265 	 * For these cases we can't draw anything at all, but
   266 	 * we can at least bump the position since we were explicitly asked to.
   267 	 */
   268 	if ((item.style & ITEMDRAW_NOTEXT)
   269 		|| (item.style & ITEMDRAW_SPACER))
   270 	{
   271 		return m_NextPos++;
   272 	}
   273 
   274 	char buffer[255];
   275 	UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
   276 
   277 	KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
   278 	ki->SetString("command", g_OptionCmdTable[m_NextPos]);
   279 	ki->SetString("msg", buffer);
   280 
   281 	return m_NextPos++;
   282 }
   283 
   284 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
   285 {
   286 	if (onlyIfEmpty && m_TitleDrawn)
   287 	{
   288 		return;
   289 	}
   290 
   291 	m_pKv->SetString("msg", text);
   292 	m_TitleDrawn = true;
   293 }
   294 
   295 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
   296 {
   297 	return false;
   298 }
   299 
   300 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
   301 {
   302 	m_pKv->SetInt("level", priority);
   303 	m_pKv->SetInt("time", time ? time : 200);
   304 
   305 	SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
   306 		engine->PEntityOfEntIndex(client),
   307 		DIALOG_MENU,
   308 		m_pKv,
   309 		g_pVSPHandle);
   310 }
   311 
   312 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
   313 {
   314 	return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
   315 }
   316 
   317 CValveMenu::CValveMenu() : CBaseMenu(&g_ValveMenuStyle), 
   318 	m_IntroColor(255, 0, 0, 255)
   319 {
   320 	strcpy(m_IntroMsg, "You have a menu, press ESC");
   321 	m_Pagination = 5;
   322 }
   323 
   324 void CValveMenu::Cancel_Finally()
   325 {
   326 	g_ValveMenuStyle.CancelMenu(this);
   327 }
   328 
   329 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
   330 {
   331 	if (itemsPerPage < 1 || itemsPerPage > 5)
   332 	{
   333 		return false;
   334 	}
   335 
   336 	CBaseMenu::SetPagination(itemsPerPage);
   337 
   338 	return true;
   339 }
   340 
   341 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
   342 {
   343 	if (option == MenuOption_IntroMessage)
   344 	{
   345 		strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
   346 		return true;
   347 	} else if (option == MenuOption_IntroColor) {
   348 		unsigned int *array = (unsigned int *)valuePtr;
   349 		m_IntroColor = Color(array[0], array[1], array[2], array[3]);
   350 		return true;
   351 	}
   352 
   353 	return false;
   354 }
   355 
   356 bool CValveMenu::Display(int client, IMenuHandler *handler, unsigned int time)
   357 {
   358 	if (m_bCancelling)
   359 	{
   360 		return false;
   361 	}
   362 
   363 	return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
   364 }
   365 
   366 IMenuDisplay *CValveMenu::CreateDisplay()
   367 {
   368 	return new CValveMenuDisplay(this);
   369 }
   370 
   371 bool CValveMenu::GetExitButton()
   372 {
   373 	return true;
   374 }
   375 
   376 bool CValveMenu::SetExitButton(bool set)
   377 {
   378 	return false;
   379 }
   380 
   381 const char *g_OptionNumTable[] = 
   382 {
   383 	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
   384 };
   385 
   386 const char *g_OptionCmdTable[] = 
   387 {
   388 	"sm_vmenuselect 0", /* INVALID! */
   389 	"sm_vmenuselect 1",
   390 	"sm_vmenuselect 2",
   391 	"sm_vmenuselect 3",
   392 	"sm_vmenuselect 4",
   393 	"sm_vmenuselect 5",
   394 	"sm_vmenuselect 6",
   395 	"sm_vmenuselect 7",
   396 	"sm_vmenuselect 8",
   397 	"sm_vmenuselect 9",
   398 	"sm_vmenuselect 10"
   399 };