3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "sm_stringutil.h"
33 #include "PlayerManager.h"
34 #include "MenuStyle_Valve.h"
35 #include "Translator.h"
36 #include "PlayerManager.h"
37 #include "ConCmdManager.h"
39 SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
41 ValveMenuStyle g_ValveMenuStyle;
42 extern const char *g_OptionNumTable[];
43 extern const char *g_OptionCmdTable[];
44 CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
46 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
50 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
52 return &m_players[client];
55 bool ValveMenuStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
57 if (strcmp(cmdname, "sm_vmenuselect") == 0)
59 int key_press = atoi(cmd.Arg(1));
60 g_ValveMenuStyle.ClientPressedKey(client, key_press);
67 void ValveMenuStyle::OnSourceModAllInitialized()
69 g_Players.AddClientListener(this);
70 SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
71 g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
74 void ValveMenuStyle::OnSourceModShutdown()
76 SH_RELEASE_CALLCLASS(g_pSPHCC);
77 SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
78 g_Players.RemoveClientListener(this);
81 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
84 IServerPluginCallbacks *plugin)
86 if (type != DIALOG_MENU)
91 int client = engine->IndexOfEdict(pEdict);
92 if (client < 1 || client > 256)
97 CValveMenuPlayer *player = &m_players[client];
99 /* We don't care if the player is in a menu because, for all intents and purposes,
100 * the menu is completely private. Instead, we just figure out the level we'll need
101 * in order to override it.
103 player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
105 /* Oh no! What happens if we got a menu that overwrites ours?! */
108 /* Okay, let the external menu survive for now. It may live another
109 * day to avenge its grandfather, killed in the great Menu Interruption
112 _CancelClientMenu(client, MenuCancel_Interrupted, true);
116 IMenuPanel *ValveMenuStyle::CreatePanel()
118 return new CValveMenuDisplay();
121 IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
123 return new CValveMenu(pHandler, pOwner);
126 const char *ValveMenuStyle::GetStyleName()
131 unsigned int ValveMenuStyle::GetMaxPageItems()
136 void ValveMenuStyle::SendDisplay(int client, IMenuPanel *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, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
145 if (vsp_interface == NULL)
150 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
153 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, unsigned int first_item, IMenuHandler *mh, unsigned int time)
155 if (vsp_interface == NULL)
157 mh->OnMenuStart(menu);
158 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
159 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
163 return BaseMenuStyle::DoClientMenu(client, menu, first_item, 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 unsigned int CValveMenuDisplay::GetCurrentKey()
211 bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
213 if (key < m_NextPos || key > 9)
223 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
225 if (option == MenuOption_IntroMessage)
227 m_pKv->SetString("title", (const char *)valuePtr);
229 } else if (option == MenuOption_IntroColor) {
230 int *array = (int *)valuePtr;
231 m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
233 } else if (option == MenuOption_Priority) {
234 m_pKv->SetInt("level", *(int *)valuePtr);
241 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
244 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
245 * so it's completely safe to ignore it.
246 * -----------------------------------------
248 if (drawFlags & ITEMDRAW_RAWLINE)
254 * Special cases, explained in DrawItem()
256 if ((drawFlags & ITEMDRAW_NOTEXT)
257 || (drawFlags & ITEMDRAW_SPACER))
263 * We can't draw disabled text. We could bump the position, but we
264 * want DirectDraw() to find some actual items to display.
266 if (drawFlags & ITEMDRAW_DISABLED)
274 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
276 if (m_NextPos > 9 || !CanDrawItem(item.style))
282 * For these cases we can't draw anything at all, but
283 * we can at least bump the position since we were explicitly asked to.
285 if ((item.style & ITEMDRAW_NOTEXT)
286 || (item.style & ITEMDRAW_SPACER))
292 UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
294 KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
295 ki->SetString("command", g_OptionCmdTable[m_NextPos]);
296 ki->SetString("msg", buffer);
301 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
303 if (onlyIfEmpty && m_TitleDrawn)
308 m_pKv->SetString("msg", text);
312 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
317 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
319 m_pKv->SetInt("level", priority);
320 m_pKv->SetInt("time", time ? time : 200);
322 SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
323 engine->PEntityOfEntIndex(client),
329 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
331 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
334 bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
339 int CValveMenuDisplay::GetAmountRemaining()
341 /* :TODO: this is a lie, but nothing really seems meaningful... */
345 CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
346 CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner),
347 m_IntroColor(255, 0, 0, 255)
349 strcpy(m_IntroMsg, "You have a menu, press ESC");
353 void CValveMenu::Cancel_Finally()
355 g_ValveMenuStyle.CancelMenu(this);
358 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
360 if (itemsPerPage > 5)
365 return CBaseMenu::SetPagination(itemsPerPage);
368 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
370 if (option == MenuOption_IntroMessage)
372 strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
374 } else if (option == MenuOption_IntroColor) {
375 unsigned int *array = (unsigned int *)valuePtr;
376 m_IntroColor = Color(array[0], array[1], array[2], array[3]);
383 bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
385 return DisplayAtItem(client, time, 0, alt_handler);
388 bool CValveMenu::DisplayAtItem(int client,
390 unsigned int start_item,
391 IMenuHandler *alt_handler/* =NULL */)
398 return g_ValveMenuStyle.DoClientMenu(client, this, start_item, alt_handler ? alt_handler : m_pHandler, time);
401 IMenuPanel *CValveMenu::CreatePanel()
403 return new CValveMenuDisplay(this);
406 void CValveMenu::SetMenuOptionFlags(unsigned int flags)
408 flags |= MENUFLAG_BUTTON_EXIT;
409 CBaseMenu::SetMenuOptionFlags(flags);
412 const char *g_OptionNumTable[] =
414 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
417 const char *g_OptionCmdTable[] =
419 "sm_vmenuselect 0", /* INVALID! */