core/MenuStyle_Radio.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/MenuStyle_Radio.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,548 +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 "MenuStyle_Radio.h"
    1.36 -#include "sm_stringutil.h"
    1.37 -#include "UserMessages.h"
    1.38 -#include "GameConfigs.h"
    1.39 -#include "PlayerManager.h"
    1.40 -#if defined MENU_DEBUG
    1.41 -#include "Logger.h"
    1.42 -#endif
    1.43 -
    1.44 -extern const char *g_RadioNumTable[];
    1.45 -CRadioStyle g_RadioMenuStyle;
    1.46 -int g_ShowMenuId = -1;
    1.47 -bool g_bRadioInit = false;
    1.48 -unsigned int g_RadioMenuTimeout = 0;
    1.49 -
    1.50 -CRadioStyle::CRadioStyle()
    1.51 -{
    1.52 -	m_players = new CRadioMenuPlayer[256+1];
    1.53 -	for (size_t i = 0; i < 256+1; i++)
    1.54 -	{
    1.55 -		m_players[i].Radio_SetIndex(i);
    1.56 -	}
    1.57 -}
    1.58 -
    1.59 -void CRadioStyle::OnSourceModAllInitialized()
    1.60 -{
    1.61 -	g_Players.AddClientListener(this);
    1.62 -}
    1.63 -
    1.64 -void CRadioStyle::OnSourceModLevelChange(const char *mapName)
    1.65 -{
    1.66 -	if (g_bRadioInit)
    1.67 -	{
    1.68 -		return;
    1.69 -	}
    1.70 -
    1.71 -	g_bRadioInit = true;
    1.72 -	const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
    1.73 -	if (!msg || msg[0] == '\0')
    1.74 -	{
    1.75 -		return;
    1.76 -	}
    1.77 -
    1.78 -	g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
    1.79 -
    1.80 -	if (!IsSupported())
    1.81 -	{
    1.82 -		return;
    1.83 -	}
    1.84 -
    1.85 -	const char *val = g_pGameConf->GetKeyValue("RadioMenuTimeout");
    1.86 -	if (val != NULL)
    1.87 -	{
    1.88 -		g_RadioMenuTimeout = atoi(val);
    1.89 -	}
    1.90 -	else
    1.91 -	{
    1.92 -		g_RadioMenuTimeout = 0;
    1.93 -	}
    1.94 -
    1.95 -	g_Menus.AddStyle(this);
    1.96 -	g_Menus.SetDefaultStyle(this);
    1.97 -
    1.98 -	g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
    1.99 -}
   1.100 -
   1.101 -void CRadioStyle::OnSourceModShutdown()
   1.102 -{
   1.103 -	g_Players.RemoveClientListener(this);
   1.104 -	g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
   1.105 -
   1.106 -	while (!m_FreeDisplays.empty())
   1.107 -	{
   1.108 -		delete m_FreeDisplays.front();
   1.109 -		m_FreeDisplays.pop();
   1.110 -	}
   1.111 -}
   1.112 -
   1.113 -bool CRadioStyle::IsSupported()
   1.114 -{
   1.115 -	return (g_ShowMenuId != -1);
   1.116 -}
   1.117 -
   1.118 -bool CRadioStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
   1.119 -{
   1.120 -	if (strcmp(cmdname, "menuselect") == 0)
   1.121 -	{
   1.122 -		if (!m_players[client].bInMenu)
   1.123 -		{
   1.124 -			return false;
   1.125 -		}
   1.126 -
   1.127 -		int arg = atoi(cmd.Arg(1));
   1.128 -		ClientPressedKey(client, arg);
   1.129 -		return true;
   1.130 -	}
   1.131 -
   1.132 -	return false;
   1.133 -}
   1.134 -
   1.135 -static unsigned int g_last_holdtime = 0;
   1.136 -static unsigned int g_last_client_count = 0;
   1.137 -static int g_last_clients[256];
   1.138 -
   1.139 -void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
   1.140 -{
   1.141 -	int count = pFilter->GetRecipientCount();
   1.142 -	bf_read br(bf->GetBasePointer(), 2);
   1.143 -
   1.144 -	br.ReadWord();
   1.145 -	int c = br.ReadChar();
   1.146 -
   1.147 -	g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
   1.148 -
   1.149 -	for (int i=0; i<count; i++)
   1.150 -	{
   1.151 -		g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
   1.152 -	}
   1.153 -}
   1.154 -
   1.155 -void CRadioStyle::OnUserMessageSent(int msg_id)
   1.156 -{
   1.157 -	for (unsigned int i=0; i<g_last_client_count; i++)
   1.158 -	{
   1.159 -		int client = g_last_clients[i];
   1.160 -#if defined MENU_DEBUG
   1.161 -		g_Logger.LogMessage("[SM_MENU] CRadioStyle got ShowMenu (client %d) (bInMenu %d)",
   1.162 -			client,
   1.163 -			m_players[client].bInExternMenu);
   1.164 -#endif
   1.165 -		if (m_players[client].bInMenu)
   1.166 -		{
   1.167 -			_CancelClientMenu(client, MenuCancel_Interrupted, true);
   1.168 -		}
   1.169 -		m_players[client].bInExternMenu = true;
   1.170 -		m_players[client].menuHoldTime = g_last_holdtime;
   1.171 -	}
   1.172 -	g_last_client_count = 0;
   1.173 -}
   1.174 -
   1.175 -void CRadioStyle::SendDisplay(int client, IMenuPanel *display)
   1.176 -{
   1.177 -	CRadioDisplay *rDisplay = (CRadioDisplay *)display;
   1.178 -	rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
   1.179 -}
   1.180 -
   1.181 -IMenuPanel *CRadioStyle::CreatePanel()
   1.182 -{
   1.183 -	return g_RadioMenuStyle.MakeRadioDisplay();
   1.184 -}
   1.185 -
   1.186 -IBaseMenu *CRadioStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
   1.187 -{
   1.188 -	return new CRadioMenu(pHandler, pOwner);
   1.189 -}
   1.190 -
   1.191 -unsigned int CRadioStyle::GetMaxPageItems()
   1.192 -{
   1.193 -	return 10;
   1.194 -}
   1.195 -
   1.196 -const char *CRadioStyle::GetStyleName()
   1.197 -{
   1.198 -	return "radio";
   1.199 -}
   1.200 -
   1.201 -CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
   1.202 -{
   1.203 -	return &m_players[client];
   1.204 -}
   1.205 -
   1.206 -CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
   1.207 -{
   1.208 -	CRadioDisplay *display;
   1.209 -	if (m_FreeDisplays.empty())
   1.210 -	{
   1.211 -		display = new CRadioDisplay();
   1.212 -	}
   1.213 -	else
   1.214 -	{
   1.215 -		display = m_FreeDisplays.front();
   1.216 -		m_FreeDisplays.pop();
   1.217 -		display->Reset();
   1.218 -	}
   1.219 -	return display;
   1.220 -}
   1.221 -
   1.222 -IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
   1.223 -{
   1.224 -	CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
   1.225 -
   1.226 -	pPanel->DirectSet(str, keys);
   1.227 -
   1.228 -	return pPanel;
   1.229 -}
   1.230 -
   1.231 -void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
   1.232 -{
   1.233 -	m_FreeDisplays.push(display);
   1.234 -}
   1.235 -
   1.236 -CRadioMenuPlayer *CRadioStyle::GetRadioMenuPlayer(int client)
   1.237 -{
   1.238 -	return &m_players[client];
   1.239 -}
   1.240 -
   1.241 -void CRadioStyle::ProcessWatchList()
   1.242 -{
   1.243 -	if (!g_RadioMenuTimeout)
   1.244 -	{
   1.245 -		BaseMenuStyle::ProcessWatchList();
   1.246 -		return;
   1.247 -	}
   1.248 -
   1.249 -	BaseMenuStyle::ProcessWatchList();
   1.250 -
   1.251 -	CRadioMenuPlayer *pPlayer;
   1.252 -	unsigned int max_clients = g_Players.GetMaxClients();
   1.253 -	for (unsigned int i = 1; i <= max_clients; i++)
   1.254 -	{
   1.255 -		pPlayer = GetRadioMenuPlayer(i);
   1.256 -		if (!pPlayer->bInMenu || pPlayer->bInExternMenu)
   1.257 -		{
   1.258 -			continue;
   1.259 -		}
   1.260 -		if (pPlayer->Radio_NeedsRefresh())
   1.261 -		{
   1.262 -			pPlayer->Radio_Refresh();
   1.263 -		}
   1.264 -	}
   1.265 -}
   1.266 -
   1.267 -CRadioDisplay::CRadioDisplay()
   1.268 -{
   1.269 -	Reset();
   1.270 -}
   1.271 -
   1.272 -CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
   1.273 -{
   1.274 -	Reset();
   1.275 -}
   1.276 -
   1.277 -bool CRadioDisplay::DrawRawLine(const char *rawline)
   1.278 -{
   1.279 -	m_BufferText.append(rawline);
   1.280 -	m_BufferText.append("\n");
   1.281 -	return true;
   1.282 -}
   1.283 -
   1.284 -void CRadioDisplay::Reset()
   1.285 -{
   1.286 -	m_BufferText.assign("");
   1.287 -	m_Title.assign("");
   1.288 -	m_NextPos = 1;
   1.289 -	keys = 0;
   1.290 -}
   1.291 -
   1.292 -void CRadioDisplay::DirectSet(const char *str, int keymap)
   1.293 -{
   1.294 -	m_Title.clear();
   1.295 -	m_BufferText.assign(str);
   1.296 -	keys = keymap;
   1.297 -}
   1.298 -
   1.299 -unsigned int CRadioDisplay::GetCurrentKey()
   1.300 -{
   1.301 -	return m_NextPos;
   1.302 -}
   1.303 -
   1.304 -bool CRadioDisplay::SetCurrentKey(unsigned int key)
   1.305 -{
   1.306 -	if (key < m_NextPos || m_NextPos > 10)
   1.307 -	{
   1.308 -		return false;
   1.309 -	}
   1.310 -
   1.311 -	m_NextPos = key;
   1.312 -
   1.313 -	return true;
   1.314 -}
   1.315 -
   1.316 -bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
   1.317 -{
   1.318 -	return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
   1.319 -}
   1.320 -
   1.321 -bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
   1.322 -{
   1.323 -	return false;
   1.324 -}
   1.325 -
   1.326 -IMenuStyle *CRadioDisplay::GetParentStyle()
   1.327 -{
   1.328 -	return &g_RadioMenuStyle;
   1.329 -}
   1.330 -
   1.331 -void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
   1.332 -{
   1.333 -	if (m_Title.size() != 0 && onlyIfEmpty)
   1.334 -	{
   1.335 -		return;
   1.336 -	}
   1.337 -	m_Title.assign(text);
   1.338 -}
   1.339 -
   1.340 -unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
   1.341 -{
   1.342 -	if (m_NextPos > 10 || !CanDrawItem(item.style))
   1.343 -	{
   1.344 -		return 0;
   1.345 -	}
   1.346 -
   1.347 -	if (item.style & ITEMDRAW_RAWLINE)
   1.348 -	{
   1.349 -		if (item.style & ITEMDRAW_SPACER)
   1.350 -		{
   1.351 -			m_BufferText.append(" \n");
   1.352 -		} else {
   1.353 -			m_BufferText.append(item.display);
   1.354 -			m_BufferText.append("\n");
   1.355 -		}
   1.356 -		return 0;
   1.357 -	} else if (item.style & ITEMDRAW_SPACER) {
   1.358 -		m_BufferText.append(" \n");
   1.359 -		return m_NextPos++;
   1.360 -	} else if (item.style & ITEMDRAW_NOTEXT) {
   1.361 -		return m_NextPos++;
   1.362 -	}
   1.363 -
   1.364 -	if (item.style & ITEMDRAW_DISABLED)
   1.365 -	{
   1.366 -		m_BufferText.append(g_RadioNumTable[m_NextPos]);
   1.367 -		m_BufferText.append(item.display);
   1.368 -		m_BufferText.append("\n");
   1.369 -	} else {
   1.370 -		m_BufferText.append("->");
   1.371 -		m_BufferText.append(g_RadioNumTable[m_NextPos]);
   1.372 -		m_BufferText.append(item.display);
   1.373 -		m_BufferText.append("\n");
   1.374 -		keys |= (1<<(m_NextPos-1));
   1.375 -	}
   1.376 -
   1.377 -	return m_NextPos++;
   1.378 -}
   1.379 -
   1.380 -bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
   1.381 -{
   1.382 -	if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
   1.383 -	{
   1.384 -		return false;
   1.385 -	}
   1.386 -
   1.387 -	if ((drawFlags & ITEMDRAW_DISABLED) && (drawFlags & ITEMDRAW_CONTROL))
   1.388 -	{
   1.389 -		return false;
   1.390 -	}
   1.391 -
   1.392 -	return true;
   1.393 -}
   1.394 -
   1.395 -void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
   1.396 -{
   1.397 -	int _sel_keys = (keys == 0) ? (1<<9) : keys;
   1.398 -	CRadioMenuPlayer *pPlayer = g_RadioMenuStyle.GetRadioMenuPlayer(client);
   1.399 -	pPlayer->Radio_Init(_sel_keys, m_Title.c_str(), m_BufferText.c_str());
   1.400 -	pPlayer->Radio_Refresh();
   1.401 -}
   1.402 -
   1.403 -void CRadioMenuPlayer::Radio_SetIndex(unsigned int index)
   1.404 -{
   1.405 -	m_index = index;
   1.406 -}
   1.407 -
   1.408 -bool CRadioMenuPlayer::Radio_NeedsRefresh()
   1.409 -{
   1.410 -	return (gpGlobals->curtime - display_last_refresh >= g_RadioMenuTimeout);
   1.411 -}
   1.412 -
   1.413 -void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
   1.414 -{
   1.415 -	if (title[0] != '\0')
   1.416 -	{
   1.417 -		display_len = UTIL_Format(display_pkt, 
   1.418 -			sizeof(display_pkt), 
   1.419 -			"%s\n%s", 
   1.420 -			title,
   1.421 -			text);
   1.422 -	}
   1.423 -	else
   1.424 -	{
   1.425 -		display_len = UTIL_Format(display_pkt, 
   1.426 -			sizeof(display_pkt), 
   1.427 -			"%s", 
   1.428 -			text);
   1.429 -	}
   1.430 -	display_keys = keys;
   1.431 -}
   1.432 -
   1.433 -void CRadioMenuPlayer::Radio_Refresh()
   1.434 -{
   1.435 -	cell_t players[1] = {m_index};
   1.436 -	char *ptr = display_pkt;
   1.437 -	char save = 0;
   1.438 -	size_t len = display_len;
   1.439 -	unsigned int time;
   1.440 -
   1.441 -	/* Compute the new time */
   1.442 -	if (menuHoldTime == 0)
   1.443 -	{
   1.444 -		time = 0;
   1.445 -	}
   1.446 -	else
   1.447 -	{
   1.448 -		time = menuHoldTime - (unsigned int)(gpGlobals->curtime - menuStartTime);
   1.449 -	}
   1.450 -
   1.451 -	while (true)
   1.452 -	{
   1.453 -		if (len > 240)
   1.454 -		{
   1.455 -			save = ptr[240];
   1.456 -			ptr[240] = '\0';
   1.457 -		}
   1.458 -		bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
   1.459 -		buffer->WriteWord(display_keys);
   1.460 -		buffer->WriteChar(time ? time : -1);
   1.461 -		buffer->WriteByte( (len > 240) ? 1 : 0 );
   1.462 -		buffer->WriteString(ptr);
   1.463 -		g_UserMsgs.EndMessage();
   1.464 -		if (len > 240)
   1.465 -		{
   1.466 -			ptr[240] = save;
   1.467 -			ptr = &ptr[240];
   1.468 -			len -= 240;
   1.469 -		}
   1.470 -		else
   1.471 -		{
   1.472 -			break;
   1.473 -		}
   1.474 -	}
   1.475 -
   1.476 -	display_last_refresh = gpGlobals->curtime;
   1.477 -}
   1.478 -
   1.479 -int CRadioDisplay::GetAmountRemaining()
   1.480 -{
   1.481 -	size_t amt = m_Title.size() + 1 + m_BufferText.size();
   1.482 -	if (amt >= 511)
   1.483 -	{
   1.484 -		return 0;
   1.485 -	}
   1.486 -	return (int)(511 - amt);
   1.487 -}
   1.488 -
   1.489 -void CRadioDisplay::DeleteThis()
   1.490 -{
   1.491 -	delete this;
   1.492 -}
   1.493 -
   1.494 -bool CRadioDisplay::SetSelectableKeys(unsigned int keymap)
   1.495 -{
   1.496 -	keys = (signed)keymap;
   1.497 -	return true;
   1.498 -}
   1.499 -
   1.500 -CRadioMenu::CRadioMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) : 
   1.501 -CBaseMenu(pHandler, &g_RadioMenuStyle, pOwner)
   1.502 -{
   1.503 -}
   1.504 -
   1.505 -bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
   1.506 -{
   1.507 -	return false;
   1.508 -}
   1.509 -
   1.510 -IMenuPanel *CRadioMenu::CreatePanel()
   1.511 -{
   1.512 -	return g_RadioMenuStyle.MakeRadioDisplay(this);
   1.513 -}
   1.514 -
   1.515 -bool CRadioMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
   1.516 -{
   1.517 -	return DisplayAtItem(client, time, 0, alt_handler);
   1.518 -}
   1.519 -
   1.520 -bool CRadioMenu::DisplayAtItem(int client,
   1.521 -							   unsigned int time,
   1.522 -							   unsigned int start_item,
   1.523 -							   IMenuHandler *alt_handler)
   1.524 -{
   1.525 -#if defined MENU_DEBUG
   1.526 -	g_Logger.LogMessage("[SM_MENU] CRadioMenu::Display(%p) (client %d) (time %d)",
   1.527 -		this,
   1.528 -		client,
   1.529 -		time);
   1.530 -#endif
   1.531 -	if (m_bCancelling)
   1.532 -	{
   1.533 -		return false;
   1.534 -	}
   1.535 -
   1.536 -	return g_RadioMenuStyle.DoClientMenu(client,
   1.537 -		this,
   1.538 -		start_item,
   1.539 -		alt_handler ? alt_handler : m_pHandler, 
   1.540 -		time);
   1.541 -}
   1.542 -
   1.543 -void CRadioMenu::Cancel_Finally()
   1.544 -{
   1.545 -	g_RadioMenuStyle.CancelMenu(this);
   1.546 -}
   1.547 -
   1.548 -const char *g_RadioNumTable[11] = 
   1.549 -{
   1.550 -	"0. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "0. "
   1.551 -};