3 * =============================================================================
5 * Copyright (C) 2004-2008 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 "PlayerManager.h"
36 #include "ConCmdManager.h"
38 SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
40 ValveMenuStyle g_ValveMenuStyle;
41 extern const char *g_OptionNumTable[];
42 extern const char *g_OptionCmdTable[];
43 CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
45 ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
49 CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
51 return &m_players[client];
54 bool ValveMenuStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
56 if (strcmp(cmdname, "sm_vmenuselect") == 0)
58 int key_press = atoi(cmd.Arg(1));
59 g_ValveMenuStyle.ClientPressedKey(client, key_press);
66 void ValveMenuStyle::OnSourceModAllInitialized()
68 g_Players.AddClientListener(this);
69 SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
70 g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
73 void ValveMenuStyle::OnSourceModShutdown()
75 SH_RELEASE_CALLCLASS(g_pSPHCC);
76 SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
77 g_Players.RemoveClientListener(this);
80 void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
83 IServerPluginCallbacks *plugin)
85 if (type != DIALOG_MENU)
90 int client = IndexOfEdict(pEdict);
91 if (client < 1 || client > 256)
96 CValveMenuPlayer *player = &m_players[client];
98 /* We don't care if the player is in a menu because, for all intents and purposes,
99 * the menu is completely private. Instead, we just figure out the level we'll need
100 * in order to override it.
102 player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
104 /* Oh no! What happens if we got a menu that overwrites ours?! */
107 /* Okay, let the external menu survive for now. It may live another
108 * day to avenge its grandfather, killed in the great Menu Interruption
111 _CancelClientMenu(client, MenuCancel_Interrupted, true);
115 IMenuPanel *ValveMenuStyle::CreatePanel()
117 return new CValveMenuDisplay();
120 IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
122 return new CValveMenu(pHandler, pOwner);
125 const char *ValveMenuStyle::GetStyleName()
130 unsigned int ValveMenuStyle::GetMaxPageItems()
135 void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
137 m_players[client].curPrioLevel--;
138 CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
139 vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
142 bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
144 if (vsp_interface == NULL)
149 return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
152 bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, unsigned int first_item, IMenuHandler *mh, unsigned int time)
154 if (vsp_interface == NULL)
156 mh->OnMenuStart(menu);
157 mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
158 mh->OnMenuEnd(menu, MenuEnd_Cancelled);
162 return BaseMenuStyle::DoClientMenu(client, menu, first_item, mh, time);
165 unsigned int ValveMenuStyle::GetApproxMemUsage()
167 return sizeof(ValveMenuStyle) + (sizeof(CValveMenuPlayer) * 257);
170 CValveMenuDisplay::CValveMenuDisplay()
176 CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
180 m_pKv->SetColor("color", pMenu->m_IntroColor);
181 m_pKv->SetString("title", pMenu->m_IntroMsg);
184 void CValveMenuDisplay::DeleteThis()
189 CValveMenuDisplay::~CValveMenuDisplay()
194 IMenuStyle *CValveMenuDisplay::GetParentStyle()
196 return &g_ValveMenuStyle;
199 void CValveMenuDisplay::Reset()
205 m_pKv = new KeyValues("menu");
207 m_TitleDrawn = false;
210 unsigned int CValveMenuDisplay::GetCurrentKey()
215 bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
217 if (key < m_NextPos || key > 9)
227 bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
229 if (option == MenuOption_IntroMessage)
231 m_pKv->SetString("title", (const char *)valuePtr);
233 } else if (option == MenuOption_IntroColor) {
234 int *array = (int *)valuePtr;
235 m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
237 } else if (option == MenuOption_Priority) {
238 m_pKv->SetInt("level", *(int *)valuePtr);
245 bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
248 * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
249 * so it's completely safe to ignore it.
250 * -----------------------------------------
252 if (drawFlags & ITEMDRAW_RAWLINE)
258 * Special cases, explained in DrawItem()
260 if ((drawFlags & ITEMDRAW_NOTEXT)
261 || (drawFlags & ITEMDRAW_SPACER))
267 * We can't draw disabled text. We could bump the position, but we
268 * want DirectDraw() to find some actual items to display.
270 if (drawFlags & ITEMDRAW_DISABLED)
278 unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
280 if (m_NextPos > 9 || !CanDrawItem(item.style))
286 * For these cases we can't draw anything at all, but
287 * we can at least bump the position since we were explicitly asked to.
289 if ((item.style & ITEMDRAW_NOTEXT)
290 || (item.style & ITEMDRAW_SPACER))
296 UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
298 KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
299 ki->SetString("command", g_OptionCmdTable[m_NextPos]);
300 ki->SetString("msg", buffer);
305 void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
307 if (onlyIfEmpty && m_TitleDrawn)
312 m_pKv->SetString("msg", text);
316 bool CValveMenuDisplay::DrawRawLine(const char *rawline)
321 void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
323 m_pKv->SetInt("level", priority);
324 m_pKv->SetInt("time", time ? time : 200);
326 SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
327 PEntityOfEntIndex(client),
333 bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
335 return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
338 bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
343 int CValveMenuDisplay::GetAmountRemaining()
345 /* :TODO: this is a lie, but nothing really seems meaningful... */
349 unsigned int CValveMenuDisplay::GetApproxMemUsage()
351 return sizeof(CValveMenuDisplay) + (sizeof(KeyValues) * m_NextPos * 10);
354 CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
355 CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner),
356 m_IntroColor(255, 0, 0, 255)
358 strcpy(m_IntroMsg, "You have a menu, press ESC");
362 void CValveMenu::Cancel_Finally()
364 g_ValveMenuStyle.CancelMenu(this);
367 bool CValveMenu::SetPagination(unsigned int itemsPerPage)
369 if (itemsPerPage > 5)
374 return CBaseMenu::SetPagination(itemsPerPage);
377 bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
379 if (option == MenuOption_IntroMessage)
381 strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
383 } else if (option == MenuOption_IntroColor) {
384 unsigned int *array = (unsigned int *)valuePtr;
385 m_IntroColor = Color(array[0], array[1], array[2], array[3]);
392 bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
394 return DisplayAtItem(client, time, 0, alt_handler);
397 bool CValveMenu::DisplayAtItem(int client,
399 unsigned int start_item,
400 IMenuHandler *alt_handler/* =NULL */)
407 return g_ValveMenuStyle.DoClientMenu(client, this, start_item, alt_handler ? alt_handler : m_pHandler, time);
410 IMenuPanel *CValveMenu::CreatePanel()
412 return new CValveMenuDisplay(this);
415 void CValveMenu::SetMenuOptionFlags(unsigned int flags)
417 flags |= MENUFLAG_BUTTON_EXIT;
418 CBaseMenu::SetMenuOptionFlags(flags);
421 unsigned int CValveMenu::GetApproxMemUsage()
423 return sizeof(CValveMenu) + GetBaseMemUsage();
426 const char *g_OptionNumTable[] =
428 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
431 const char *g_OptionCmdTable[] =
433 "sm_vmenuselect 0", /* INVALID! */