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;
49 #define MAX_PAGINATION_OPTIONS 3
51 #define MAX_MENUSLOT_KEYS 10
53 static unsigned int s_RadioMaxPageItems = MAX_MENUSLOT_KEYS;
56 CRadioStyle::CRadioStyle()
58 m_players = new CRadioMenuPlayer[256+1];
59 for (size_t i = 0; i < 256+1; i++)
61 m_players[i].Radio_SetIndex(i);
65 void CRadioStyle::OnSourceModAllInitialized()
67 g_Players.AddClientListener(this);
70 void CRadioStyle::OnSourceModLevelChange(const char *mapName)
78 const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
79 if (!msg || msg[0] == '\0')
84 g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
91 const char *val = g_pGameConf->GetKeyValue("RadioMenuTimeout");
94 g_RadioMenuTimeout = atoi(val);
98 g_RadioMenuTimeout = 0;
101 const char *items = g_pGameConf->GetKeyValue("RadioMenuMaxPageItems");
104 int value = atoi(items);
106 // Only override the mostly-safe default if it's a sane value
107 if (value > MAX_PAGINATION_OPTIONS && value <= MAX_MENUSLOT_KEYS)
109 s_RadioMaxPageItems = value;
113 g_Menus.AddStyle(this);
114 g_Menus.SetDefaultStyle(this);
116 g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
119 void CRadioStyle::OnSourceModShutdown()
121 g_Players.RemoveClientListener(this);
122 g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
124 while (!m_FreeDisplays.empty())
126 delete m_FreeDisplays.front();
127 m_FreeDisplays.pop();
131 bool CRadioStyle::IsSupported()
133 return (g_ShowMenuId != -1);
136 bool CRadioStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
138 if (strcmp(cmdname, "menuselect") == 0)
140 if (!m_players[client].bInMenu)
142 m_players[client].bInExternMenu = false;
146 int arg = atoi(cmd.Arg(1));
147 ClientPressedKey(client, arg);
154 static unsigned int g_last_holdtime = 0;
155 static unsigned int g_last_client_count = 0;
156 static int g_last_clients[256];
158 void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
160 int count = pFilter->GetRecipientCount();
161 bf_read br(bf->GetBasePointer(), 3);
164 int c = br.ReadChar();
166 g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
168 for (int i=0; i<count; i++)
170 g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
174 void CRadioStyle::OnUserMessageSent(int msg_id)
176 for (unsigned int i=0; i<g_last_client_count; i++)
178 int client = g_last_clients[i];
179 #if defined MENU_DEBUG
180 g_Logger.LogMessage("[SM_MENU] CRadioStyle got ShowMenu (client %d) (bInMenu %d)",
182 m_players[client].bInExternMenu);
184 if (m_players[client].bInMenu)
186 _CancelClientMenu(client, MenuCancel_Interrupted, true);
188 m_players[client].bInExternMenu = true;
189 m_players[client].menuHoldTime = g_last_holdtime;
191 g_last_client_count = 0;
194 void CRadioStyle::SendDisplay(int client, IMenuPanel *display)
196 CRadioDisplay *rDisplay = (CRadioDisplay *)display;
197 rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
200 IMenuPanel *CRadioStyle::CreatePanel()
202 return g_RadioMenuStyle.MakeRadioDisplay();
205 IBaseMenu *CRadioStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
207 return new CRadioMenu(pHandler, pOwner);
210 unsigned int CRadioStyle::GetMaxPageItems()
212 return s_RadioMaxPageItems;
215 const char *CRadioStyle::GetStyleName()
220 CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
222 return &m_players[client];
225 CRadioDisplay *CRadioStyle::MakeRadioDisplay(CRadioMenu *menu)
227 CRadioDisplay *display;
228 if (m_FreeDisplays.empty())
230 display = new CRadioDisplay();
234 display = m_FreeDisplays.front();
235 m_FreeDisplays.pop();
241 IMenuPanel *CRadioStyle::MakeRadioDisplay(const char *str, int keys)
243 CRadioDisplay *pPanel = MakeRadioDisplay(NULL);
245 pPanel->DirectSet(str, keys);
250 void CRadioStyle::FreeRadioDisplay(CRadioDisplay *display)
252 m_FreeDisplays.push(display);
255 CRadioMenuPlayer *CRadioStyle::GetRadioMenuPlayer(int client)
257 return &m_players[client];
260 void CRadioStyle::ProcessWatchList()
262 if (!g_RadioMenuTimeout)
264 BaseMenuStyle::ProcessWatchList();
268 BaseMenuStyle::ProcessWatchList();
270 CRadioMenuPlayer *pPlayer;
271 unsigned int max_clients = g_Players.GetMaxClients();
272 for (unsigned int i = 1; i <= max_clients; i++)
274 pPlayer = GetRadioMenuPlayer(i);
275 if (!pPlayer->bInMenu || pPlayer->bInExternMenu)
279 if (pPlayer->Radio_NeedsRefresh())
281 pPlayer->Radio_Refresh();
286 unsigned int CRadioStyle::GetApproxMemUsage()
288 return sizeof(CRadioStyle) + (sizeof(CRadioMenuPlayer) * 257);
291 CRadioDisplay::CRadioDisplay()
296 CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
301 bool CRadioDisplay::DrawRawLine(const char *rawline)
303 m_BufferText.append(rawline);
304 m_BufferText.append("\n");
308 void CRadioDisplay::Reset()
310 m_BufferText.assign("");
316 void CRadioDisplay::DirectSet(const char *str, int keymap)
319 m_BufferText.assign(str);
323 unsigned int CRadioDisplay::GetCurrentKey()
328 bool CRadioDisplay::SetCurrentKey(unsigned int key)
330 if (key < m_NextPos || m_NextPos > s_RadioMaxPageItems)
340 bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
342 return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
345 bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
350 IMenuStyle *CRadioDisplay::GetParentStyle()
352 return &g_RadioMenuStyle;
355 void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
357 if (m_Title.size() != 0 && onlyIfEmpty)
361 m_Title.assign(text);
364 unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
366 if (m_NextPos > s_RadioMaxPageItems || !CanDrawItem(item.style))
371 if (item.style & ITEMDRAW_RAWLINE)
373 if (item.style & ITEMDRAW_SPACER)
375 m_BufferText.append(" \n");
377 m_BufferText.append(item.display);
378 m_BufferText.append("\n");
381 } else if (item.style & ITEMDRAW_SPACER) {
382 m_BufferText.append(" \n");
384 } else if (item.style & ITEMDRAW_NOTEXT) {
388 if (item.style & ITEMDRAW_DISABLED)
390 m_BufferText.append(g_RadioNumTable[m_NextPos]);
391 m_BufferText.append(item.display);
392 m_BufferText.append("\n");
394 m_BufferText.append("->");
395 m_BufferText.append(g_RadioNumTable[m_NextPos]);
396 m_BufferText.append(item.display);
397 m_BufferText.append("\n");
398 keys |= (1<<(m_NextPos-1));
404 bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
406 if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
411 if ((drawFlags & ITEMDRAW_DISABLED) && (drawFlags & ITEMDRAW_CONTROL))
419 void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
421 int _sel_keys = (keys == 0) ? (1<<9) : keys;
422 CRadioMenuPlayer *pPlayer = g_RadioMenuStyle.GetRadioMenuPlayer(client);
423 pPlayer->Radio_Init(_sel_keys, m_Title.c_str(), m_BufferText.c_str());
424 pPlayer->Radio_Refresh();
427 void CRadioMenuPlayer::Radio_SetIndex(unsigned int index)
432 bool CRadioMenuPlayer::Radio_NeedsRefresh()
434 return (gpGlobals->curtime - display_last_refresh >= g_RadioMenuTimeout);
437 void CRadioMenuPlayer::Radio_Init(int keys, const char *title, const char *text)
439 if (title[0] != '\0')
441 display_len = UTIL_Format(display_pkt,
449 display_len = UTIL_Format(display_pkt,
457 void CRadioMenuPlayer::Radio_Refresh()
459 cell_t players[1] = {m_index};
460 char *ptr = display_pkt;
462 size_t len = display_len;
465 /* Compute the new time */
466 if (menuHoldTime == 0)
472 time = menuHoldTime - (unsigned int)(gpGlobals->curtime - menuStartTime);
482 bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, USERMSG_BLOCKHOOKS);
483 buffer->WriteWord(display_keys);
484 buffer->WriteChar(time ? time : -1);
485 buffer->WriteByte( (len > 240) ? 1 : 0 );
486 buffer->WriteString(ptr);
487 g_UserMsgs.EndMessage();
500 display_last_refresh = gpGlobals->curtime;
503 int CRadioDisplay::GetAmountRemaining()
505 size_t amt = m_Title.size() + 1 + m_BufferText.size();
510 return (int)(511 - amt);
513 void CRadioDisplay::DeleteThis()
518 bool CRadioDisplay::SetSelectableKeys(unsigned int keymap)
520 keys = (signed)keymap;
524 unsigned int CRadioDisplay::GetApproxMemUsage()
526 return sizeof(CRadioDisplay)
527 + m_BufferText.size()
531 CRadioMenu::CRadioMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
532 CBaseMenu(pHandler, &g_RadioMenuStyle, pOwner)
534 m_Pagination = s_RadioMaxPageItems - MAX_PAGINATION_OPTIONS;
537 bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
542 IMenuPanel *CRadioMenu::CreatePanel()
544 return g_RadioMenuStyle.MakeRadioDisplay(this);
547 bool CRadioMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
549 return DisplayAtItem(client, time, 0, alt_handler);
552 bool CRadioMenu::DisplayAtItem(int client,
554 unsigned int start_item,
555 IMenuHandler *alt_handler)
557 #if defined MENU_DEBUG
558 g_Logger.LogMessage("[SM_MENU] CRadioMenu::Display(%p) (client %d) (time %d)",
568 return g_RadioMenuStyle.DoClientMenu(client,
571 alt_handler ? alt_handler : m_pHandler,
575 bool CRadioMenu::SetPagination(unsigned int itemsPerPage)
577 const unsigned int maxPerPage = s_RadioMaxPageItems - MAX_PAGINATION_OPTIONS;
578 if (itemsPerPage > maxPerPage)
583 return CBaseMenu::SetPagination(itemsPerPage);
586 void CRadioMenu::Cancel_Finally()
588 g_RadioMenuStyle.CancelMenu(this);
591 unsigned int CRadioMenu::GetApproxMemUsage()
593 return sizeof(CRadioMenu) + GetBaseMemUsage();
596 const char *g_RadioNumTable[11] =
598 "0. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "0. "