1.1 --- a/core/MenuStyle_Valve.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,430 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "sm_stringutil.h"
1.36 -#include "PlayerManager.h"
1.37 -#include "MenuStyle_Valve.h"
1.38 -#include "Translator.h"
1.39 -#include "PlayerManager.h"
1.40 -#include "ConCmdManager.h"
1.41 -
1.42 -SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
1.43 -
1.44 -ValveMenuStyle g_ValveMenuStyle;
1.45 -extern const char *g_OptionNumTable[];
1.46 -extern const char *g_OptionCmdTable[];
1.47 -CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
1.48 -
1.49 -ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
1.50 -{
1.51 -}
1.52 -
1.53 -CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
1.54 -{
1.55 - return &m_players[client];
1.56 -}
1.57 -
1.58 -bool ValveMenuStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
1.59 -{
1.60 - if (strcmp(cmdname, "sm_vmenuselect") == 0)
1.61 - {
1.62 - int key_press = atoi(cmd.Arg(1));
1.63 - g_ValveMenuStyle.ClientPressedKey(client, key_press);
1.64 - return true;
1.65 - }
1.66 -
1.67 - return false;
1.68 -}
1.69 -
1.70 -void ValveMenuStyle::OnSourceModAllInitialized()
1.71 -{
1.72 - g_Players.AddClientListener(this);
1.73 - SH_ADD_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
1.74 - g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
1.75 -}
1.76 -
1.77 -void ValveMenuStyle::OnSourceModShutdown()
1.78 -{
1.79 - SH_RELEASE_CALLCLASS(g_pSPHCC);
1.80 - SH_REMOVE_HOOK_MEMFUNC(IServerPluginHelpers, CreateMessage, serverpluginhelpers, this, &ValveMenuStyle::HookCreateMessage, false);
1.81 - g_Players.RemoveClientListener(this);
1.82 -}
1.83 -
1.84 -void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
1.85 - DIALOG_TYPE type,
1.86 - KeyValues *kv,
1.87 - IServerPluginCallbacks *plugin)
1.88 -{
1.89 - if (type != DIALOG_MENU)
1.90 - {
1.91 - return;
1.92 - }
1.93 -
1.94 - int client = engine->IndexOfEdict(pEdict);
1.95 - if (client < 1 || client > 256)
1.96 - {
1.97 - return;
1.98 - }
1.99 -
1.100 - CValveMenuPlayer *player = &m_players[client];
1.101 -
1.102 - /* We don't care if the player is in a menu because, for all intents and purposes,
1.103 - * the menu is completely private. Instead, we just figure out the level we'll need
1.104 - * in order to override it.
1.105 - */
1.106 - player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
1.107 -
1.108 - /* Oh no! What happens if we got a menu that overwrites ours?! */
1.109 - if (player->bInMenu)
1.110 - {
1.111 - /* Okay, let the external menu survive for now. It may live another
1.112 - * day to avenge its grandfather, killed in the great Menu Interruption
1.113 - * battle.
1.114 - */
1.115 - _CancelClientMenu(client, MenuCancel_Interrupted, true);
1.116 - }
1.117 -}
1.118 -
1.119 -IMenuPanel *ValveMenuStyle::CreatePanel()
1.120 -{
1.121 - return new CValveMenuDisplay();
1.122 -}
1.123 -
1.124 -IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
1.125 -{
1.126 - return new CValveMenu(pHandler, pOwner);
1.127 -}
1.128 -
1.129 -const char *ValveMenuStyle::GetStyleName()
1.130 -{
1.131 - return "valve";
1.132 -}
1.133 -
1.134 -unsigned int ValveMenuStyle::GetMaxPageItems()
1.135 -{
1.136 - return 8;
1.137 -}
1.138 -
1.139 -void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
1.140 -{
1.141 - m_players[client].curPrioLevel--;
1.142 - CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
1.143 - vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
1.144 -}
1.145 -
1.146 -bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
1.147 -{
1.148 - if (vsp_interface == NULL)
1.149 - {
1.150 - return false;
1.151 - }
1.152 -
1.153 - return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
1.154 -}
1.155 -
1.156 -bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, unsigned int first_item, IMenuHandler *mh, unsigned int time)
1.157 -{
1.158 - if (vsp_interface == NULL)
1.159 - {
1.160 - mh->OnMenuStart(menu);
1.161 - mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
1.162 - mh->OnMenuEnd(menu, MenuEnd_Cancelled);
1.163 - return false;
1.164 - }
1.165 -
1.166 - return BaseMenuStyle::DoClientMenu(client, menu, first_item, mh, time);
1.167 -}
1.168 -
1.169 -CValveMenuDisplay::CValveMenuDisplay()
1.170 -{
1.171 - m_pKv = NULL;
1.172 - Reset();
1.173 -}
1.174 -
1.175 -CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
1.176 -{
1.177 - m_pKv = NULL;
1.178 - Reset();
1.179 - m_pKv->SetColor("color", pMenu->m_IntroColor);
1.180 - m_pKv->SetString("title", pMenu->m_IntroMsg);
1.181 -}
1.182 -
1.183 -void CValveMenuDisplay::DeleteThis()
1.184 -{
1.185 - delete this;
1.186 -}
1.187 -
1.188 -CValveMenuDisplay::~CValveMenuDisplay()
1.189 -{
1.190 - m_pKv->deleteThis();
1.191 -}
1.192 -
1.193 -IMenuStyle *CValveMenuDisplay::GetParentStyle()
1.194 -{
1.195 - return &g_ValveMenuStyle;
1.196 -}
1.197 -
1.198 -void CValveMenuDisplay::Reset()
1.199 -{
1.200 - if (m_pKv)
1.201 - {
1.202 - m_pKv->deleteThis();
1.203 - }
1.204 - m_pKv = new KeyValues("menu");
1.205 - m_NextPos = 1;
1.206 - m_TitleDrawn = false;
1.207 -}
1.208 -
1.209 -unsigned int CValveMenuDisplay::GetCurrentKey()
1.210 -{
1.211 - return m_NextPos;
1.212 -}
1.213 -
1.214 -bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
1.215 -{
1.216 - if (key < m_NextPos || key > 9)
1.217 - {
1.218 - return false;
1.219 - }
1.220 -
1.221 - m_NextPos = key;
1.222 -
1.223 - return true;
1.224 -}
1.225 -
1.226 -bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
1.227 -{
1.228 - if (option == MenuOption_IntroMessage)
1.229 - {
1.230 - m_pKv->SetString("title", (const char *)valuePtr);
1.231 - return true;
1.232 - } else if (option == MenuOption_IntroColor) {
1.233 - int *array = (int *)valuePtr;
1.234 - m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
1.235 - return true;
1.236 - } else if (option == MenuOption_Priority) {
1.237 - m_pKv->SetInt("level", *(int *)valuePtr);
1.238 - return true;
1.239 - }
1.240 -
1.241 - return false;
1.242 -}
1.243 -
1.244 -bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
1.245 -{
1.246 - /**
1.247 - * ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
1.248 - * so it's completely safe to ignore it.
1.249 - * -----------------------------------------
1.250 - */
1.251 - if (drawFlags & ITEMDRAW_RAWLINE)
1.252 - {
1.253 - return false;
1.254 - }
1.255 -
1.256 - /**
1.257 - * Special cases, explained in DrawItem()
1.258 - */
1.259 - if ((drawFlags & ITEMDRAW_NOTEXT)
1.260 - || (drawFlags & ITEMDRAW_SPACER))
1.261 - {
1.262 - return true;
1.263 - }
1.264 -
1.265 - /**
1.266 - * We can't draw disabled text. We could bump the position, but we
1.267 - * want DirectDraw() to find some actual items to display.
1.268 - */
1.269 - if (drawFlags & ITEMDRAW_DISABLED)
1.270 - {
1.271 - return false;
1.272 - }
1.273 -
1.274 - return true;
1.275 -}
1.276 -
1.277 -unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
1.278 -{
1.279 - if (m_NextPos > 9 || !CanDrawItem(item.style))
1.280 - {
1.281 - return 0;
1.282 - }
1.283 -
1.284 - /**
1.285 - * For these cases we can't draw anything at all, but
1.286 - * we can at least bump the position since we were explicitly asked to.
1.287 - */
1.288 - if ((item.style & ITEMDRAW_NOTEXT)
1.289 - || (item.style & ITEMDRAW_SPACER))
1.290 - {
1.291 - return m_NextPos++;
1.292 - }
1.293 -
1.294 - char buffer[255];
1.295 - UTIL_Format(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
1.296 -
1.297 - KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
1.298 - ki->SetString("command", g_OptionCmdTable[m_NextPos]);
1.299 - ki->SetString("msg", buffer);
1.300 -
1.301 - return m_NextPos++;
1.302 -}
1.303 -
1.304 -void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
1.305 -{
1.306 - if (onlyIfEmpty && m_TitleDrawn)
1.307 - {
1.308 - return;
1.309 - }
1.310 -
1.311 - m_pKv->SetString("msg", text);
1.312 - m_TitleDrawn = true;
1.313 -}
1.314 -
1.315 -bool CValveMenuDisplay::DrawRawLine(const char *rawline)
1.316 -{
1.317 - return false;
1.318 -}
1.319 -
1.320 -void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
1.321 -{
1.322 - m_pKv->SetInt("level", priority);
1.323 - m_pKv->SetInt("time", time ? time : 200);
1.324 -
1.325 - SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
1.326 - engine->PEntityOfEntIndex(client),
1.327 - DIALOG_MENU,
1.328 - m_pKv,
1.329 - vsp_interface);
1.330 -}
1.331 -
1.332 -bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
1.333 -{
1.334 - return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
1.335 -}
1.336 -
1.337 -bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
1.338 -{
1.339 - return false;
1.340 -}
1.341 -
1.342 -int CValveMenuDisplay::GetAmountRemaining()
1.343 -{
1.344 - /* :TODO: this is a lie, but nothing really seems meaningful... */
1.345 - return -1;
1.346 -}
1.347 -
1.348 -CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
1.349 -CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner),
1.350 - m_IntroColor(255, 0, 0, 255)
1.351 -{
1.352 - strcpy(m_IntroMsg, "You have a menu, press ESC");
1.353 - m_Pagination = 5;
1.354 -}
1.355 -
1.356 -void CValveMenu::Cancel_Finally()
1.357 -{
1.358 - g_ValveMenuStyle.CancelMenu(this);
1.359 -}
1.360 -
1.361 -bool CValveMenu::SetPagination(unsigned int itemsPerPage)
1.362 -{
1.363 - if (itemsPerPage > 5)
1.364 - {
1.365 - return false;
1.366 - }
1.367 -
1.368 - return CBaseMenu::SetPagination(itemsPerPage);
1.369 -}
1.370 -
1.371 -bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
1.372 -{
1.373 - if (option == MenuOption_IntroMessage)
1.374 - {
1.375 - strncopy(m_IntroMsg, (const char *)valuePtr, sizeof(m_IntroMsg));
1.376 - return true;
1.377 - } else if (option == MenuOption_IntroColor) {
1.378 - unsigned int *array = (unsigned int *)valuePtr;
1.379 - m_IntroColor = Color(array[0], array[1], array[2], array[3]);
1.380 - return true;
1.381 - }
1.382 -
1.383 - return false;
1.384 -}
1.385 -
1.386 -bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
1.387 -{
1.388 - return DisplayAtItem(client, time, 0, alt_handler);
1.389 -}
1.390 -
1.391 -bool CValveMenu::DisplayAtItem(int client,
1.392 - unsigned int time,
1.393 - unsigned int start_item,
1.394 - IMenuHandler *alt_handler/* =NULL */)
1.395 -{
1.396 - if (m_bCancelling)
1.397 - {
1.398 - return false;
1.399 - }
1.400 -
1.401 - return g_ValveMenuStyle.DoClientMenu(client, this, start_item, alt_handler ? alt_handler : m_pHandler, time);
1.402 -}
1.403 -
1.404 -IMenuPanel *CValveMenu::CreatePanel()
1.405 -{
1.406 - return new CValveMenuDisplay(this);
1.407 -}
1.408 -
1.409 -void CValveMenu::SetMenuOptionFlags(unsigned int flags)
1.410 -{
1.411 - flags |= MENUFLAG_BUTTON_EXIT;
1.412 - CBaseMenu::SetMenuOptionFlags(flags);
1.413 -}
1.414 -
1.415 -const char *g_OptionNumTable[] =
1.416 -{
1.417 - "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
1.418 -};
1.419 -
1.420 -const char *g_OptionCmdTable[] =
1.421 -{
1.422 - "sm_vmenuselect 0", /* INVALID! */
1.423 - "sm_vmenuselect 1",
1.424 - "sm_vmenuselect 2",
1.425 - "sm_vmenuselect 3",
1.426 - "sm_vmenuselect 4",
1.427 - "sm_vmenuselect 5",
1.428 - "sm_vmenuselect 6",
1.429 - "sm_vmenuselect 7",
1.430 - "sm_vmenuselect 8",
1.431 - "sm_vmenuselect 9",
1.432 - "sm_vmenuselect 10"
1.433 -};