core/MenuStyle_Radio.cpp
changeset 780 570fa34723bf
child 785 bca431afd754
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/MenuStyle_Radio.cpp	Sun May 13 05:51:30 2007 +0000
     1.3 @@ -0,0 +1,305 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.8 + * ===============================================================
     1.9 + *
    1.10 + * This file is not open source and may not be copied without explicit
    1.11 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.12 + * in whole or significant part.
    1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.14 + *
    1.15 + * Version: $Id$
    1.16 + */
    1.17 +
    1.18 +#include "MenuStyle_Radio.h"
    1.19 +#include "sm_stringutil.h"
    1.20 +#include "UserMessages.h"
    1.21 +#include "GameConfigs.h"
    1.22 +
    1.23 +extern const char *g_RadioNumTable[];
    1.24 +CRadioStyle g_RadioMenuStyle;
    1.25 +int g_ShowMenuId = -1;
    1.26 +
    1.27 +CRadioStyle::CRadioStyle() : m_players(new CBaseMenuPlayer[256+1])
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +void CRadioStyle::OnSourceModAllInitialized()
    1.32 +{
    1.33 +	const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
    1.34 +	if (!msg || msg[0] == '\0')
    1.35 +	{
    1.36 +		return;
    1.37 +	}
    1.38 +
    1.39 +	g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
    1.40 +
    1.41 +	if (!IsSupported())
    1.42 +	{
    1.43 +		return;
    1.44 +	}
    1.45 +
    1.46 +	g_Menus.AddStyle(this);
    1.47 +	g_Menus.SetDefaultStyle(this);
    1.48 +
    1.49 +	g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
    1.50 +}
    1.51 +
    1.52 +void CRadioStyle::OnSourceModShutdown()
    1.53 +{
    1.54 +	g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
    1.55 +}
    1.56 +
    1.57 +bool CRadioStyle::IsSupported()
    1.58 +{
    1.59 +	return (g_ShowMenuId != -1);
    1.60 +}
    1.61 +
    1.62 +bool CRadioStyle::OnClientCommand(int client)
    1.63 +{
    1.64 +	const char *cmd = engine->Cmd_Argv(0);
    1.65 +
    1.66 +	if (strcmp(cmd, "menuselect") == 0)
    1.67 +	{
    1.68 +		if (!m_players[client].bInMenu)
    1.69 +		{
    1.70 +			return false;
    1.71 +		}
    1.72 +		int arg = atoi(engine->Cmd_Argv(1));
    1.73 +		ClientPressedKey(client, arg);
    1.74 +	}
    1.75 +
    1.76 +	return false;
    1.77 +}
    1.78 +
    1.79 +static unsigned int g_last_holdtime = 0;
    1.80 +static unsigned int g_last_client_count = 0;
    1.81 +static int g_last_clients[256];
    1.82 +
    1.83 +void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
    1.84 +{
    1.85 +	int count = pFilter->GetRecipientCount();
    1.86 +	bf_read br(bf->GetBasePointer(), 2);
    1.87 +
    1.88 +	br.ReadWord();
    1.89 +	int c = br.ReadChar();
    1.90 +
    1.91 +	g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
    1.92 +
    1.93 +	for (int i=0; i<count; i++)
    1.94 +	{
    1.95 +		g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
    1.96 +	}
    1.97 +}
    1.98 +
    1.99 +void CRadioStyle::OnUserMessageSent(int msg_id)
   1.100 +{
   1.101 +	for (unsigned int i=0; i<g_last_client_count; i++)
   1.102 +	{
   1.103 +		int client = g_last_clients[i];
   1.104 +		if (m_players[client].bInMenu)
   1.105 +		{
   1.106 +			_CancelClientMenu(client, true);
   1.107 +		}
   1.108 +		m_players[client].bInExternMenu = true;
   1.109 +		m_players[client].menuHoldTime = g_last_holdtime;
   1.110 +	}
   1.111 +	g_last_client_count = 0;
   1.112 +}
   1.113 +
   1.114 +void CRadioStyle::SendDisplay(int client, IMenuDisplay *display)
   1.115 +{
   1.116 +	CRadioDisplay *rDisplay = (CRadioDisplay *)display;
   1.117 +	rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
   1.118 +}
   1.119 +
   1.120 +IMenuDisplay *CRadioStyle::CreateDisplay()
   1.121 +{
   1.122 +	return new CRadioDisplay();
   1.123 +}
   1.124 +
   1.125 +IBaseMenu *CRadioStyle::CreateMenu()
   1.126 +{
   1.127 +	return new CRadioMenu();
   1.128 +}
   1.129 +
   1.130 +unsigned int CRadioStyle::GetMaxPageItems()
   1.131 +{
   1.132 +	return 10;
   1.133 +}
   1.134 +
   1.135 +const char *CRadioStyle::GetStyleName()
   1.136 +{
   1.137 +	return "radio";
   1.138 +}
   1.139 +
   1.140 +CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
   1.141 +{
   1.142 +	return &m_players[client];
   1.143 +}
   1.144 +
   1.145 +CRadioDisplay::CRadioDisplay()
   1.146 +{
   1.147 +	Reset();
   1.148 +}
   1.149 +
   1.150 +CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
   1.151 +{
   1.152 +	Reset();
   1.153 +}
   1.154 +
   1.155 +bool CRadioDisplay::DrawRawLine(const char *rawline)
   1.156 +{
   1.157 +	m_BufferText.append(rawline);
   1.158 +	m_BufferText.append("\n");
   1.159 +	return true;
   1.160 +}
   1.161 +
   1.162 +void CRadioDisplay::Reset()
   1.163 +{
   1.164 +	m_BufferText.assign("");
   1.165 +	m_Title.assign("");
   1.166 +	m_NextPos = 1;
   1.167 +}
   1.168 +
   1.169 +bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
   1.170 +{
   1.171 +	return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
   1.172 +}
   1.173 +
   1.174 +bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
   1.175 +{
   1.176 +	return false;
   1.177 +}
   1.178 +
   1.179 +IMenuStyle *CRadioDisplay::GetParentStyle()
   1.180 +{
   1.181 +	return &g_RadioMenuStyle;
   1.182 +}
   1.183 +
   1.184 +void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
   1.185 +{
   1.186 +	if (m_Title.size() != 0 && onlyIfEmpty)
   1.187 +	{
   1.188 +		return;
   1.189 +	}
   1.190 +	m_Title.assign(text);
   1.191 +}
   1.192 +
   1.193 +unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
   1.194 +{
   1.195 +	if (m_NextPos > 10 || !CanDrawItem(item.style))
   1.196 +	{
   1.197 +		return 0;
   1.198 +	}
   1.199 +
   1.200 +	if (item.style & ITEMDRAW_RAWLINE)
   1.201 +	{
   1.202 +		if (item.style & ITEMDRAW_SPACER)
   1.203 +		{
   1.204 +			m_BufferText.append("\n");
   1.205 +		} else {
   1.206 +			m_BufferText.append(item.display);
   1.207 +			m_BufferText.append("\n");
   1.208 +		}
   1.209 +		return 0;
   1.210 +	} else if (item.style & ITEMDRAW_SPACER) {
   1.211 +		m_BufferText.append("\n");
   1.212 +		return m_NextPos++;
   1.213 +	}
   1.214 +
   1.215 +	if (item.style & ITEMDRAW_DISABLED)
   1.216 +	{
   1.217 +		m_BufferText.append(g_RadioNumTable[m_NextPos]);
   1.218 +		m_BufferText.append(". ");
   1.219 +		m_BufferText.append(item.display);
   1.220 +		m_BufferText.append("\n");
   1.221 +	} else {
   1.222 +		m_BufferText.append("->. ");
   1.223 +		m_BufferText.append(item.display);
   1.224 +		m_BufferText.append("\n");
   1.225 +		keys |= (1<<(m_NextPos-1));
   1.226 +	}
   1.227 +
   1.228 +	return m_NextPos++;
   1.229 +}
   1.230 +
   1.231 +bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
   1.232 +{
   1.233 +	if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
   1.234 +	{
   1.235 +		return false;
   1.236 +	}
   1.237 +
   1.238 +	return true;
   1.239 +}
   1.240 +
   1.241 +void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
   1.242 +{
   1.243 +	char buffer[4096];
   1.244 +	size_t len;
   1.245 +
   1.246 +	len = UTIL_Format(buffer, sizeof(buffer), "%s\n%s", m_Title.c_str(), m_BufferText.c_str());
   1.247 +
   1.248 +	cell_t players[1] = {client};
   1.249 +
   1.250 +	char *ptr = buffer;
   1.251 +	char save = 0;
   1.252 +	while (true)
   1.253 +	{
   1.254 +		if (len > 240)
   1.255 +		{
   1.256 +			save = ptr[240];
   1.257 +			ptr[240] = '\0';
   1.258 +		}
   1.259 +		bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, 0);
   1.260 +		buffer->WriteWord(keys);
   1.261 +		buffer->WriteChar(time ? time : -1);
   1.262 +		buffer->WriteByte( (len > 240) ? 1 : 0 );
   1.263 +		buffer->WriteString(ptr);
   1.264 +		g_UserMsgs.EndMessage();
   1.265 +		if (len > 240)
   1.266 +		{
   1.267 +			ptr[240] = save;
   1.268 +			ptr = &ptr[240];
   1.269 +			len -= 240;
   1.270 +		} else {
   1.271 +			break;
   1.272 +		}
   1.273 +	}
   1.274 +}
   1.275 +
   1.276 +void CRadioDisplay::DeleteThis()
   1.277 +{
   1.278 +	delete this;
   1.279 +}
   1.280 +
   1.281 +CRadioMenu::CRadioMenu() : CBaseMenu(&g_RadioMenuStyle)
   1.282 +{
   1.283 +}
   1.284 +
   1.285 +bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
   1.286 +{
   1.287 +	return false;
   1.288 +}
   1.289 +
   1.290 +IMenuDisplay *CRadioMenu::CreateDisplay()
   1.291 +{
   1.292 +	return new CRadioDisplay(this);
   1.293 +}
   1.294 +
   1.295 +bool CRadioMenu::Display(int client, IMenuHandler *handler, unsigned int time)
   1.296 +{
   1.297 +	return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
   1.298 +}
   1.299 +
   1.300 +void CRadioMenu::Cancel_Finally()
   1.301 +{
   1.302 +	g_RadioMenuStyle.CancelMenu(this);
   1.303 +}
   1.304 +
   1.305 +const char *g_RadioNumTable[11] = 
   1.306 +{
   1.307 +	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
   1.308 +};