core/MenuStyle_Radio.cpp
branchsourcemod-1.1.0
changeset 1715 f9ecab642e25
parent 1541 e46ad208b9b4
     1.1 --- a/core/MenuStyle_Radio.cpp	Sun Nov 11 20:59:20 2007 +0000
     1.2 +++ b/core/MenuStyle_Radio.cpp	Fri Nov 23 05:10:33 2007 +0000
     1.3 @@ -42,9 +42,15 @@
     1.4  CRadioStyle g_RadioMenuStyle;
     1.5  int g_ShowMenuId = -1;
     1.6  bool g_bRadioInit = false;
     1.7 +unsigned int g_RadioMenuTimeout = 0;
     1.8  
     1.9 -CRadioStyle::CRadioStyle() : m_players(new CBaseMenuPlayer[256+1])
    1.10 +CRadioStyle::CRadioStyle()
    1.11  {
    1.12 +	m_players = new CRadioMenuPlayer[256+1];
    1.13 +	for (size_t i = 0; i < 256+1; i++)
    1.14 +	{
    1.15 +		m_players[i].Radio_SetIndex(i);
    1.16 +	}
    1.17  }
    1.18  
    1.19  void CRadioStyle::OnSourceModAllInitialized()
    1.20 @@ -73,6 +79,16 @@
    1.21  		return;
    1.22  	}
    1.23  
    1.24 +	const char *val = g_pGameConf->GetKeyValue("RadioMenuTimeout");
    1.25 +	if (val != NULL)
    1.26 +	{
    1.27 +		g_RadioMenuTimeout = atoi(val);
    1.28 +	}
    1.29 +	else
    1.30 +	{
    1.31 +		g_RadioMenuTimeout = 0;
    1.32 +	}
    1.33 +
    1.34  	g_Menus.AddStyle(this);
    1.35  	g_Menus.SetDefaultStyle(this);
    1.36  
    1.37 @@ -190,7 +206,9 @@
    1.38  	if (m_FreeDisplays.empty())
    1.39  	{
    1.40  		display = new CRadioDisplay();
    1.41 -	} else {
    1.42 +	}
    1.43 +	else
    1.44 +	{
    1.45  		display = m_FreeDisplays.front();
    1.46  		m_FreeDisplays.pop();
    1.47  		display->Reset();
    1.48 @@ -198,11 +216,51 @@
    1.49  	return display;
    1.50  }
    1.51  
    1.52 +IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
    1.53 +{
    1.54 +	CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
    1.55 +
    1.56 +	pPanel->DirectSet(str, keys);
    1.57 +
    1.58 +	return pPanel;
    1.59 +}
    1.60 +
    1.61  void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
    1.62  {
    1.63  	m_FreeDisplays.push(display);
    1.64  }
    1.65  
    1.66 +CRadioMenuPlayer *CRadioStyle::GetRadioMenuPlayer(int client)
    1.67 +{
    1.68 +	return &m_players[client];
    1.69 +}
    1.70 +
    1.71 +void CRadioStyle::ProcessWatchList()
    1.72 +{
    1.73 +	if (!g_RadioMenuTimeout)
    1.74 +	{
    1.75 +		BaseMenuStyle::ProcessWatchList();
    1.76 +		return;
    1.77 +	}
    1.78 +
    1.79 +	BaseMenuStyle::ProcessWatchList();
    1.80 +
    1.81 +	CRadioMenuPlayer *pPlayer;
    1.82 +	unsigned int max_clients = g_Players.GetMaxClients();
    1.83 +	for (unsigned int i = 1; i <= max_clients; i++)
    1.84 +	{
    1.85 +		pPlayer = GetRadioMenuPlayer(i);
    1.86 +		if (!pPlayer->bInMenu || pPlayer->bInExternMenu)
    1.87 +		{
    1.88 +			continue;
    1.89 +		}
    1.90 +		if (pPlayer->Radio_NeedsRefresh())
    1.91 +		{
    1.92 +			pPlayer->Radio_Refresh();
    1.93 +		}
    1.94 +	}
    1.95 +}
    1.96 +
    1.97  CRadioDisplay::CRadioDisplay()
    1.98  {
    1.99  	Reset();
   1.100 @@ -228,6 +286,13 @@
   1.101  	keys = 0;
   1.102  }
   1.103  
   1.104 +void CRadioDisplay::DirectSet(const char *str, int keymap)
   1.105 +{
   1.106 +	m_Title.clear();
   1.107 +	m_BufferText.assign(str);
   1.108 +	keys = keymap;
   1.109 +}
   1.110 +
   1.111  unsigned int CRadioDisplay::GetCurrentKey()
   1.112  {
   1.113  	return m_NextPos;
   1.114 @@ -326,17 +391,60 @@
   1.115  
   1.116  void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
   1.117  {
   1.118 -	char buffer[4096];
   1.119 -	size_t len;
   1.120 +	int _sel_keys = (keys == 0) ? (1<<9) : keys;
   1.121 +	CRadioMenuPlayer *pPlayer = g_RadioMenuStyle.GetRadioMenuPlayer(client);
   1.122 +	pPlayer->Radio_Init(_sel_keys, m_Title.c_str(), m_BufferText.c_str());
   1.123 +	pPlayer->Radio_Refresh();
   1.124 +}
   1.125  
   1.126 -	len = UTIL_Format(buffer, sizeof(buffer), "%s\n%s", m_Title.c_str(), m_BufferText.c_str());
   1.127 +void CRadioMenuPlayer::Radio_SetIndex(unsigned int index)
   1.128 +{
   1.129 +	m_index = index;
   1.130 +}
   1.131 +
   1.132 +bool CRadioMenuPlayer::Radio_NeedsRefresh()
   1.133 +{
   1.134 +	return (gpGlobals->curtime - display_last_refresh >= g_RadioMenuTimeout);
   1.135 +}
   1.136  
   1.137 -	cell_t players[1] = {client};
   1.138 +void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
   1.139 +{
   1.140 +	if (title[0] != '\0')
   1.141 +	{
   1.142 +		display_len = UTIL_Format(display_pkt, 
   1.143 +			sizeof(display_pkt), 
   1.144 +			"%s\n%s", 
   1.145 +			title,
   1.146 +			text);
   1.147 +	}
   1.148 +	else
   1.149 +	{
   1.150 +		display_len = UTIL_Format(display_pkt, 
   1.151 +			sizeof(display_pkt), 
   1.152 +			"%s", 
   1.153 +			text);
   1.154 +	}
   1.155 +	display_keys = keys;
   1.156 +}
   1.157  
   1.158 -	int _sel_keys = (keys == 0) ? (1<<9) : keys;
   1.159 +void CRadioMenuPlayer::Radio_Refresh()
   1.160 +{
   1.161 +	cell_t players[1] = {m_index};
   1.162 +	char *ptr = display_pkt;
   1.163 +	char save = 0;
   1.164 +	size_t len = display_len;
   1.165 +	unsigned int time;
   1.166  
   1.167 -	char *ptr = buffer;
   1.168 -	char save = 0;
   1.169 +	/* Compute the new time */
   1.170 +	if (menuHoldTime == 0)
   1.171 +	{
   1.172 +		time = 0;
   1.173 +	}
   1.174 +	else
   1.175 +	{
   1.176 +		time = menuHoldTime - (unsigned int)(gpGlobals->curtime - menuStartTime);
   1.177 +	}
   1.178 +
   1.179  	while (true)
   1.180  	{
   1.181  		if (len > 240)
   1.182 @@ -345,7 +453,7 @@
   1.183  			ptr[240] = '\0';
   1.184  		}
   1.185  		bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
   1.186 -		buffer->WriteWord(_sel_keys);
   1.187 +		buffer->WriteWord(display_keys);
   1.188  		buffer->WriteChar(time ? time : -1);
   1.189  		buffer->WriteByte( (len > 240) ? 1 : 0 );
   1.190  		buffer->WriteString(ptr);
   1.191 @@ -355,10 +463,14 @@
   1.192  			ptr[240] = save;
   1.193  			ptr = &ptr[240];
   1.194  			len -= 240;
   1.195 -		} else {
   1.196 +		}
   1.197 +		else
   1.198 +		{
   1.199  			break;
   1.200  		}
   1.201  	}
   1.202 +
   1.203 +	display_last_refresh = gpGlobals->curtime;
   1.204  }
   1.205  
   1.206  int CRadioDisplay::GetAmountRemaining()