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 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
34 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
36 return &m_players[client];
39 bool ValveMenuStyle::OnClientCommand(int client)
41 const char *cmd = engine->Cmd_Argv(0);
43 if (strcmp(cmd, "sm_vmenuselect") == 0)
45 int key_press = atoi(engine->Cmd_Argv(1));
46 g_ValveMenuStyle.ClientPressedKey(client, key_press);
53 void ValveMenuStyle::OnSourceModAllInitialized()
55 g_Players.AddClientListener(this);
56 SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
57 g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
60 void ValveMenuStyle::OnSourceModShutdown()
62 SH_RELEASE_CALLCLASS(g_pSPHCC);
63 SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
64 g_Players.RemoveClientListener(this);
67 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
70 IServerPluginCallbacks *plugin)
72 if (type != DIALOG_MENU)
77 int client = engine->IndexOfEdict(pEdict);
78 if (client < 1 || client > 256)
83 CValveMenuPlayer *player = &m_players[client];
85 /* We don't care if the player is in a menu because, for all intents and purposes,
86 * the menu is completely private. Instead, we just figure out the level we'll need
87 * in order to override it.
89 player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
91 /* Oh no! What happens if we got a menu that overwrites ours?! */
94 /* Okay, let the external menu survive for now. It may live another
95 * day to avenge its grandfather, killed in the great Menu Interruption
98 _CancelClientMenu(client, true);
102 void ValveMenuStyle::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
104 g_pVSPHandle = iface;
107 IMenuDisplay *ValveMenuStyle::CreateDisplay()
109 return new CValveMenuDisplay();
112 IBaseMenu *ValveMenuStyle::CreateMenu()
114 return new CValveMenu();
117 const char *ValveMenuStyle::GetStyleName()
122 unsigned int ValveMenuStyle::GetMaxPageItems()
127 void ValveMenuStyle::SendDisplay(int client, IMenuDisplay *display)
129 m_players[client].curPrioLevel--;
130 CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
131 vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
134 bool ValveMenuStyle::DoClientMenu(int client, IMenuDisplay *menu, IMenuHandler *mh, unsigned int time)
141 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
144 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, IMenuHandler *mh, unsigned int time)
148 mh->OnMenuStart(menu);
149 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
154 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
157 CValveMenuDisplay::CValveMenuDisplay()
163 CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
167 m_pKv->SetColor("color", pMenu->m_IntroColor);
168 m_pKv->SetString("title", pMenu->m_IntroMsg);
171 void CValveMenuDisplay::DeleteThis()
176 CValveMenuDisplay::~CValveMenuDisplay()
181 IMenuStyle *CValveMenuDisplay::GetParentStyle()
183 return &g_ValveMenuStyle;
186 void CValveMenuDisplay::Reset()
192 m_pKv = new KeyValues("menu");
194 m_TitleDrawn = false;
197 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
199 if (option == MenuOption_IntroMessage)
201 m_pKv->SetString("title", (const char *)valuePtr);
203 } else if (option == MenuOption_IntroColor) {
204 int *array = (int *)valuePtr;
205 m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
207 } else if (option == MenuOption_Priority) {
208 m_pKv->SetInt("level", *(int *)valuePtr);
215 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
218 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
219 * so it's completely safe to ignore it.
220 * -----------------------------------------
222 if (drawFlags & ITEMDRAW_RAWLINE)
228 * Special cases, explained in DrawItem()
230 if ((drawFlags & ITEMDRAW_NOTEXT)
231 || (drawFlags & ITEMDRAW_SPACER))
237 * We can't draw disabled text. We could bump the position, but we
238 * want DirectDraw() to find some actual items to display.
240 if (drawFlags & ITEMDRAW_DISABLED)
248 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
250 if (m_NextPos > 9 || !CanDrawItem(item.style))
256 * For these cases we can't draw anything at all, but
257 * we can at least bump the position since we were explicitly asked to.
259 if ((item.style & ITEMDRAW_NOTEXT)
260 || (item.style & ITEMDRAW_SPACER))
266 UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
268 KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
269 ki->SetString("command", g_OptionCmdTable[m_NextPos]);
270 ki->SetString("msg", buffer);
275 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
277 if (onlyIfEmpty && m_TitleDrawn)
282 m_pKv->SetString("msg", text);
286 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
291 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
293 m_pKv->SetInt("level", priority);
294 m_pKv->SetInt("time", time ? time : 200);
296 SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
297 engine->PEntityOfEntIndex(client),
303 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
305 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
308 CValveMenu::CValveMenu() : CBaseMenu(&g_ValveMenuStyle),
309 m_IntroColor(255, 0, 0, 255)
311 strcpy(m_IntroMsg, "You have a menu, press ESC");
315 void CValveMenu::Cancel_Finally()
317 g_ValveMenuStyle.CancelMenu(this);
320 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
322 if (itemsPerPage < 1 || itemsPerPage > 5)
327 CBaseMenu::SetPagination(itemsPerPage);
332 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
334 if (option == MenuOption_IntroMessage)
336 strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
338 } else if (option == MenuOption_IntroColor) {
339 unsigned int *array = (unsigned int *)valuePtr;
340 m_IntroColor = Color(array[0], array[1], array[2], array[3]);
347 bool CValveMenu::Display(int client, IMenuHandler *handler, unsigned int time)
354 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
357 IMenuDisplay *CValveMenu::CreateDisplay()
359 return new CValveMenuDisplay(this);
362 bool CValveMenu::GetExitButton()
367 bool CValveMenu::SetExitButton(bool set)
372 const char *g_OptionNumTable[] =
374 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
377 const char *g_OptionCmdTable[] =
379 "sm_vmenuselect 0", /* INVALID! */