3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "sm_stringutil.h"
34 #include "PlayerManager.h"
35 #include "MenuStyle_Valve.h"
36 #include "Translator.h"
37 #include "PlayerManager.h"
38 #include "ConCmdManager.h"
40 SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
42 ValveMenuStyle g_ValveMenuStyle;
43 extern const char *g_OptionNumTable[];
44 extern const char *g_OptionCmdTable[];
45 IServerPluginCallbacks *g_pVSPHandle = NULL;
46 CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
48 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
52 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
54 return &m_players[client];
57 bool ValveMenuStyle::OnClientCommand(int client)
59 const char *cmd = engine->Cmd_Argv(0);
61 if (strcmp(cmd, "sm_vmenuselect") == 0)
63 int key_press = atoi(engine->Cmd_Argv(1));
64 g_ValveMenuStyle.ClientPressedKey(client, key_press);
71 void ValveMenuStyle::OnSourceModAllInitialized()
73 g_Players.AddClientListener(this);
74 SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
75 g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
78 void ValveMenuStyle::OnSourceModShutdown()
80 SH_RELEASE_CALLCLASS(g_pSPHCC);
81 SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
82 g_Players.RemoveClientListener(this);
85 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
88 IServerPluginCallbacks *plugin)
90 if (type != DIALOG_MENU)
95 int client = engine->IndexOfEdict(pEdict);
96 if (client < 1 || client > 256)
101 CValveMenuPlayer *player = &m_players[client];
103 /* We don't care if the player is in a menu because, for all intents and purposes,
104 * the menu is completely private. Instead, we just figure out the level we'll need
105 * in order to override it.
107 player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
109 /* Oh no! What happens if we got a menu that overwrites ours?! */
112 /* Okay, let the external menu survive for now. It may live another
113 * day to avenge its grandfather, killed in the great Menu Interruption
116 _CancelClientMenu(client, MenuCancel_Interrupted, true);
120 void ValveMenuStyle::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
122 g_pVSPHandle = iface;
125 IMenuPanel *ValveMenuStyle::CreatePanel()
127 return new CValveMenuDisplay();
130 IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
132 return new CValveMenu(pHandler, pOwner);
135 const char *ValveMenuStyle::GetStyleName()
140 unsigned int ValveMenuStyle::GetMaxPageItems()
145 void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
147 m_players[client].curPrioLevel--;
148 CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
149 vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
152 bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
159 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
162 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
166 mh->OnMenuStart(menu);
167 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
168 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
172 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
175 CValveMenuDisplay::CValveMenuDisplay()
181 CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
185 m_pKv->SetColor("color", pMenu->m_IntroColor);
186 m_pKv->SetString("title", pMenu->m_IntroMsg);
189 void CValveMenuDisplay::DeleteThis()
194 CValveMenuDisplay::~CValveMenuDisplay()
199 IMenuStyle *CValveMenuDisplay::GetParentStyle()
201 return &g_ValveMenuStyle;
204 void CValveMenuDisplay::Reset()
210 m_pKv = new KeyValues("menu");
212 m_TitleDrawn = false;
215 unsigned int CValveMenuDisplay::GetCurrentKey()
220 bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
222 if (key < m_NextPos || key > 9)
232 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
234 if (option == MenuOption_IntroMessage)
236 m_pKv->SetString("title", (const char *)valuePtr);
238 } else if (option == MenuOption_IntroColor) {
239 int *array = (int *)valuePtr;
240 m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
242 } else if (option == MenuOption_Priority) {
243 m_pKv->SetInt("level", *(int *)valuePtr);
250 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
253 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
254 * so it's completely safe to ignore it.
255 * -----------------------------------------
257 if (drawFlags & ITEMDRAW_RAWLINE)
263 * Special cases, explained in DrawItem()
265 if ((drawFlags & ITEMDRAW_NOTEXT)
266 || (drawFlags & ITEMDRAW_SPACER))
272 * We can't draw disabled text. We could bump the position, but we
273 * want DirectDraw() to find some actual items to display.
275 if (drawFlags & ITEMDRAW_DISABLED)
283 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
285 if (m_NextPos > 9 || !CanDrawItem(item.style))
291 * For these cases we can't draw anything at all, but
292 * we can at least bump the position since we were explicitly asked to.
294 if ((item.style & ITEMDRAW_NOTEXT)
295 || (item.style & ITEMDRAW_SPACER))
301 UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
303 KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
304 ki->SetString("command", g_OptionCmdTable[m_NextPos]);
305 ki->SetString("msg", buffer);
310 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
312 if (onlyIfEmpty && m_TitleDrawn)
317 m_pKv->SetString("msg", text);
321 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
326 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
328 m_pKv->SetInt("level", priority);
329 m_pKv->SetInt("time", time ? time : 200);
331 SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
332 engine->PEntityOfEntIndex(client),
338 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
340 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
343 bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
348 CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
349 CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner),
350 m_IntroColor(255, 0, 0, 255)
352 strcpy(m_IntroMsg, "You have a menu, press ESC");
356 void CValveMenu::Cancel_Finally()
358 g_ValveMenuStyle.CancelMenu(this);
361 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
363 if (itemsPerPage > 5)
368 return CBaseMenu::SetPagination(itemsPerPage);
371 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
373 if (option == MenuOption_IntroMessage)
375 strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
377 } else if (option == MenuOption_IntroColor) {
378 unsigned int *array = (unsigned int *)valuePtr;
379 m_IntroColor = Color(array[0], array[1], array[2], array[3]);
386 bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
393 return g_ValveMenuStyle.DoClientMenu(client, this, alt_handler ? alt_handler : m_pHandler, time);
396 IMenuPanel *CValveMenu::CreatePanel()
398 return new CValveMenuDisplay(this);
401 void CValveMenu::SetMenuOptionFlags(unsigned int flags)
403 flags |= MENUFLAG_BUTTON_EXIT;
404 CBaseMenu::SetMenuOptionFlags(flags);
407 const char *g_OptionNumTable[] =
409 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
412 const char *g_OptionCmdTable[] =
414 "sm_vmenuselect 0", /* INVALID! */