3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
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"
22 SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
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;
30 class TestHandler : public IMenuHandler
33 virtual void OnMenuEnd(IBaseMenu *menu)
39 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
43 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
45 return &m_players[client];
48 bool ValveMenuStyle::OnClientCommand(int client)
50 const char *cmd = engine->Cmd_Argv(0);
52 if (strcmp(cmd, "sm_vmenuselect") == 0)
54 int key_press = atoi(engine->Cmd_Argv(1));
55 g_ValveMenuStyle.ClientPressedKey(client, key_press);
62 void ValveMenuStyle::OnSourceModAllInitialized()
64 g_Players.AddClientListener(this);
65 SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
66 g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
69 void ValveMenuStyle::OnSourceModShutdown()
71 SH_RELEASE_CALLCLASS(g_pSPHCC);
72 SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
73 g_Players.RemoveClientListener(this);
76 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
79 IServerPluginCallbacks *plugin)
81 if (type != DIALOG_MENU)
86 int client = engine->IndexOfEdict(pEdict);
87 if (client < 1 || client > 256)
92 CValveMenuPlayer *player = &m_players[client];
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.
98 player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
100 /* Oh no! What happens if we got a menu that overwrites ours?! */
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
107 _CancelClientMenu(client, true);
111 void ValveMenuStyle::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
113 g_pVSPHandle = iface;
116 IMenuDisplay *ValveMenuStyle::CreateDisplay()
118 return new CValveMenuDisplay();
121 IBaseMenu *ValveMenuStyle::CreateMenu()
123 return new CValveMenu();
126 const char *ValveMenuStyle::GetStyleName()
131 unsigned int ValveMenuStyle::GetMaxPageItems()
136 void ValveMenuStyle::SendDisplay(int client, IMenuDisplay *display)
138 m_players[client].curPrioLevel--;
139 CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
140 vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
143 bool ValveMenuStyle::DoClientMenu(int client, IMenuDisplay *menu, IMenuHandler *mh, unsigned int time)
150 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
153 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
157 mh->OnMenuStart(menu);
158 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
163 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
166 CValveMenuDisplay::CValveMenuDisplay()
172 CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
176 m_pKv->SetColor("color", pMenu->m_IntroColor);
177 m_pKv->SetString("title", pMenu->m_IntroMsg);
180 void CValveMenuDisplay::DeleteThis()
185 CValveMenuDisplay::~CValveMenuDisplay()
190 IMenuStyle *CValveMenuDisplay::GetParentStyle()
192 return &g_ValveMenuStyle;
195 void CValveMenuDisplay::Reset()
201 m_pKv = new KeyValues("menu");
203 m_TitleDrawn = false;
206 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
208 if (option == MenuOption_IntroMessage)
210 m_pKv->SetString("title", (const char *)valuePtr);
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]));
216 } else if (option == MenuOption_Priority) {
217 m_pKv->SetInt("level", *(int *)valuePtr);
224 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
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 * -----------------------------------------
231 if (drawFlags & ITEMDRAW_RAWLINE)
237 * Special cases, explained in DrawItem()
239 if ((drawFlags & ITEMDRAW_NOTEXT)
240 || (drawFlags & ITEMDRAW_SPACER))
246 * We can't draw disabled text. We could bump the position, but we
247 * want DirectDraw() to find some actual items to display.
249 if (drawFlags & ITEMDRAW_DISABLED)
257 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
259 if (m_NextPos > 9 || !CanDrawItem(item.style))
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.
268 if ((item.style & ITEMDRAW_NOTEXT)
269 || (item.style & ITEMDRAW_SPACER))
275 UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
277 KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
278 ki->SetString("command", g_OptionCmdTable[m_NextPos]);
279 ki->SetString("msg", buffer);
284 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
286 if (onlyIfEmpty && m_TitleDrawn)
291 m_pKv->SetString("msg", text);
295 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
300 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
302 m_pKv->SetInt("level", priority);
303 m_pKv->SetInt("time", time ? time : 200);
305 SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
306 engine->PEntityOfEntIndex(client),
312 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
314 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
317 CValveMenu::CValveMenu() : CBaseMenu(&g_ValveMenuStyle),
318 m_IntroColor(255, 0, 0, 255)
320 strcpy(m_IntroMsg, "You have a menu, press ESC");
324 void CValveMenu::Cancel_Finally()
326 g_ValveMenuStyle.CancelMenu(this);
329 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
331 if (itemsPerPage < 1 || itemsPerPage > 5)
336 CBaseMenu::SetPagination(itemsPerPage);
341 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
343 if (option == MenuOption_IntroMessage)
345 strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
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]);
356 bool CValveMenu::Display(int client, IMenuHandler *handler, unsigned int time)
363 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
366 IMenuDisplay *CValveMenu::CreateDisplay()
368 return new CValveMenuDisplay(this);
371 bool CValveMenu::GetExitButton()
376 bool CValveMenu::SetExitButton(bool set)
381 const char *g_OptionNumTable[] =
383 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
386 const char *g_OptionCmdTable[] =
388 "sm_vmenuselect 0", /* INVALID! */