Another try at fixing build.
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 "MenuStyle_Radio.h"
33 #include "sm_stringutil.h"
34 #include "UserMessages.h"
35 #include <IGameConfigs.h>
36 #include "PlayerManager.h"
37 #if defined MENU_DEBUG
40 #include "logic_bridge.h"
42 extern const char *g_RadioNumTable[];
43 CRadioStyle g_RadioMenuStyle;
44 int g_ShowMenuId = -1;
45 bool g_bRadioInit = false;
46 unsigned int g_RadioMenuTimeout = 0;
48 CRadioStyle::CRadioStyle()
50 m_players = new CRadioMenuPlayer[256+1];
51 for (size_t i = 0; i < 256+1; i++)
53 m_players[i].Radio_SetIndex(i);
57 void CRadioStyle::OnSourceModAllInitialized()
59 g_Players.AddClientListener(this);
62 void CRadioStyle::OnSourceModLevelChange(const char *mapName)
70 const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
71 if (!msg || msg[0] == '\0')
76 g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
83 const char *val = g_pGameConf->GetKeyValue("RadioMenuTimeout");
86 g_RadioMenuTimeout = atoi(val);
90 g_RadioMenuTimeout = 0;
93 g_Menus.AddStyle(this);
94 g_Menus.SetDefaultStyle(this);
96 g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
99 void CRadioStyle::OnSourceModShutdown()
101 g_Players.RemoveClientListener(this);
102 g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
104 while (!m_FreeDisplays.empty())
106 delete m_FreeDisplays.front();
107 m_FreeDisplays.pop();
111 bool CRadioStyle::IsSupported()
113 return (g_ShowMenuId != -1);
116 bool CRadioStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
118 if (strcmp(cmdname, "menuselect") == 0)
120 if (!m_players[client].bInMenu)
122 m_players[client].bInExternMenu = false;
126 int arg = atoi(cmd.Arg(1));
127 ClientPressedKey(client, arg);
134 static unsigned int g_last_holdtime = 0;
135 static unsigned int g_last_client_count = 0;
136 static int g_last_clients[256];
138 void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
140 int count = pFilter->GetRecipientCount();
141 bf_read br(bf->GetBasePointer(), 3);
144 int c = br.ReadChar();
146 g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
148 for (int i=0; i<count; i++)
150 g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
154 void CRadioStyle::OnUserMessageSent(int msg_id)
156 for (unsigned int i=0; i<g_last_client_count; i++)
158 int client = g_last_clients[i];
159 #if defined MENU_DEBUG
160 g_Logger.LogMessage("[SM_MENU] CRadioStyle got ShowMenu (client %d) (bInMenu %d)",
162 m_players[client].bInExternMenu);
164 if (m_players[client].bInMenu)
166 _CancelClientMenu(client, MenuCancel_Interrupted, true);
168 m_players[client].bInExternMenu = true;
169 m_players[client].menuHoldTime = g_last_holdtime;
171 g_last_client_count = 0;
174 void CRadioStyle::SendDisplay(int client, IMenuPanel *display)
176 CRadioDisplay *rDisplay = (CRadioDisplay *)display;
177 rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
180 IMenuPanel *CRadioStyle::CreatePanel()
182 return g_RadioMenuStyle.MakeRadioDisplay();
185 IBaseMenu *CRadioStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
187 return new CRadioMenu(pHandler, pOwner);
190 unsigned int CRadioStyle::GetMaxPageItems()
195 const char *CRadioStyle::GetStyleName()
200 CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
202 return &m_players[client];
205 CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
207 CRadioDisplay *display;
208 if (m_FreeDisplays.empty())
210 display = new CRadioDisplay();
214 display = m_FreeDisplays.front();
215 m_FreeDisplays.pop();
221 IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
223 CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
225 pPanel->DirectSet(str, keys);
230 void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
232 m_FreeDisplays.push(display);
235 CRadioMenuPlayer *CRadioStyle::GetRadioMenuPlayer(int client)
237 return &m_players[client];
240 void CRadioStyle::ProcessWatchList()
242 if (!g_RadioMenuTimeout)
244 BaseMenuStyle::ProcessWatchList();
248 BaseMenuStyle::ProcessWatchList();
250 CRadioMenuPlayer *pPlayer;
251 unsigned int max_clients = g_Players.GetMaxClients();
252 for (unsigned int i = 1; i <= max_clients; i++)
254 pPlayer = GetRadioMenuPlayer(i);
255 if (!pPlayer->bInMenu || pPlayer->bInExternMenu)
259 if (pPlayer->Radio_NeedsRefresh())
261 pPlayer->Radio_Refresh();
266 unsigned int CRadioStyle::GetApproxMemUsage()
268 return sizeof(CRadioStyle) + (sizeof(CRadioMenuPlayer) * 257);
271 CRadioDisplay::CRadioDisplay()
276 CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
281 bool CRadioDisplay::DrawRawLine(const char *rawline)
283 m_BufferText.append(rawline);
284 m_BufferText.append("\n");
288 void CRadioDisplay::Reset()
290 m_BufferText.assign("");
296 void CRadioDisplay::DirectSet(const char *str, int keymap)
299 m_BufferText.assign(str);
303 unsigned int CRadioDisplay::GetCurrentKey()
308 bool CRadioDisplay::SetCurrentKey(unsigned int key)
310 if (key < m_NextPos || m_NextPos > 10)
320 bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
322 return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
325 bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
330 IMenuStyle *CRadioDisplay::GetParentStyle()
332 return &g_RadioMenuStyle;
335 void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
337 if (m_Title.size() != 0 && onlyIfEmpty)
341 m_Title.assign(text);
344 unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
346 if (m_NextPos > 10 || !CanDrawItem(item.style))
351 if (item.style & ITEMDRAW_RAWLINE)
353 if (item.style & ITEMDRAW_SPACER)
355 m_BufferText.append(" \n");
357 m_BufferText.append(item.display);
358 m_BufferText.append("\n");
361 } else if (item.style & ITEMDRAW_SPACER) {
362 m_BufferText.append(" \n");
364 } else if (item.style & ITEMDRAW_NOTEXT) {
368 if (item.style & ITEMDRAW_DISABLED)
370 m_BufferText.append(g_RadioNumTable[m_NextPos]);
371 m_BufferText.append(item.display);
372 m_BufferText.append("\n");
374 m_BufferText.append("->");
375 m_BufferText.append(g_RadioNumTable[m_NextPos]);
376 m_BufferText.append(item.display);
377 m_BufferText.append("\n");
378 keys |= (1<<(m_NextPos-1));
384 bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
386 if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
391 if ((drawFlags & ITEMDRAW_DISABLED) && (drawFlags & ITEMDRAW_CONTROL))
399 void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
401 int _sel_keys = (keys == 0) ? (1<<9) : keys;
402 CRadioMenuPlayer *pPlayer = g_RadioMenuStyle.GetRadioMenuPlayer(client);
403 pPlayer->Radio_Init(_sel_keys, m_Title.c_str(), m_BufferText.c_str());
404 pPlayer->Radio_Refresh();
407 void CRadioMenuPlayer::Radio_SetIndex(unsigned int index)
412 bool CRadioMenuPlayer::Radio_NeedsRefresh()
414 return (gpGlobals->curtime - display_last_refresh >= g_RadioMenuTimeout);
417 void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
419 if (title[0] != '\0')
421 display_len = UTIL_Format(display_pkt,
429 display_len = UTIL_Format(display_pkt,
437 void CRadioMenuPlayer::Radio_Refresh()
439 cell_t players[1] = {m_index};
440 char *ptr = display_pkt;
442 size_t len = display_len;
445 /* Compute the new time */
446 if (menuHoldTime == 0)
452 time = menuHoldTime - (unsigned int)(gpGlobals->curtime - menuStartTime);
462 bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
463 buffer->WriteWord(display_keys);
464 buffer->WriteChar(time ? time : -1);
465 buffer->WriteByte( (len > 240) ? 1 : 0 );
466 buffer->WriteString(ptr);
467 g_UserMsgs.EndMessage();
480 display_last_refresh = gpGlobals->curtime;
483 int CRadioDisplay::GetAmountRemaining()
485 size_t amt = m_Title.size() + 1 + m_BufferText.size();
490 return (int)(511 - amt);
493 void CRadioDisplay::DeleteThis()
498 bool CRadioDisplay::SetSelectableKeys(unsigned int keymap)
500 keys = (signed)keymap;
504 unsigned int CRadioDisplay::GetApproxMemUsage()
506 return sizeof(CRadioDisplay)
507 + m_BufferText.size()
511 CRadioMenu::CRadioMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
512 CBaseMenu(pHandler, &g_RadioMenuStyle, pOwner)
516 bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
521 IMenuPanel *CRadioMenu::CreatePanel()
523 return g_RadioMenuStyle.MakeRadioDisplay(this);
526 bool CRadioMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
528 return DisplayAtItem(client, time, 0, alt_handler);
531 bool CRadioMenu::DisplayAtItem(int client,
533 unsigned int start_item,
534 IMenuHandler *alt_handler)
536 #if defined MENU_DEBUG
537 g_Logger.LogMessage("[SM_MENU] CRadioMenu::Display(%p) (client %d) (time %d)",
547 return g_RadioMenuStyle.DoClientMenu(client,
550 alt_handler ? alt_handler : m_pHandler,
554 void CRadioMenu::Cancel_Finally()
556 g_RadioMenuStyle.CancelMenu(this);
559 unsigned int CRadioMenu::GetApproxMemUsage()
561 return sizeof(CRadioMenu) + GetBaseMemUsage();
564 const char *g_RadioNumTable[11] =
566 "0. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "0. "