3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "MenuStyle_Radio.h"
16 #include "sm_stringutil.h"
17 #include "UserMessages.h"
18 #include "GameConfigs.h"
20 extern const char *g_RadioNumTable[];
21 CRadioStyle g_RadioMenuStyle;
22 int g_ShowMenuId = -1;
23 bool g_bRadioInit = false;
25 CRadioStyle::CRadioStyle() : m_players(new CBaseMenuPlayer[256+1])
29 void CRadioStyle::OnSourceModLevelChange(const char *mapName)
37 const char *msg = g_pGameConf->GetKeyValue("HudRadioMenuMsg");
38 if (!msg || msg[0] == '\0')
43 g_ShowMenuId = g_UserMsgs.GetMessageIndex(msg);
50 g_Menus.AddStyle(this);
51 g_Menus.SetDefaultStyle(this);
53 g_UserMsgs.HookUserMessage(g_ShowMenuId, this, false);
56 void CRadioStyle::OnSourceModShutdown()
58 g_UserMsgs.UnhookUserMessage(g_ShowMenuId, this, false);
61 bool CRadioStyle::IsSupported()
63 return (g_ShowMenuId != -1);
66 bool CRadioStyle::OnClientCommand(int client)
68 const char *cmd = engine->Cmd_Argv(0);
70 if (strcmp(cmd, "menuselect") == 0)
72 if (!m_players[client].bInMenu)
77 int arg = atoi(engine->Cmd_Argv(1));
78 ClientPressedKey(client, arg);
85 static unsigned int g_last_holdtime = 0;
86 static unsigned int g_last_client_count = 0;
87 static int g_last_clients[256];
89 void CRadioStyle::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
91 int count = pFilter->GetRecipientCount();
92 bf_read br(bf->GetBasePointer(), 2);
95 int c = br.ReadChar();
97 g_last_holdtime = (c == -1) ? 0 : (unsigned)c;
99 for (int i=0; i<count; i++)
101 g_last_clients[g_last_client_count++] = pFilter->GetRecipientIndex(i);
105 void CRadioStyle::OnUserMessageSent(int msg_id)
107 for (unsigned int i=0; i<g_last_client_count; i++)
109 int client = g_last_clients[i];
110 if (m_players[client].bInMenu)
112 _CancelClientMenu(client, true);
114 m_players[client].bInExternMenu = true;
115 m_players[client].menuHoldTime = g_last_holdtime;
117 g_last_client_count = 0;
120 void CRadioStyle::SendDisplay(int client, IMenuPanel *display)
122 CRadioDisplay *rDisplay = (CRadioDisplay *)display;
123 rDisplay->SendRawDisplay(client, m_players[client].menuHoldTime);
126 IMenuPanel *CRadioStyle::CreatePanel()
128 return new CRadioDisplay();
131 IBaseMenu *CRadioStyle::CreateMenu()
133 return new CRadioMenu();
136 unsigned int CRadioStyle::GetMaxPageItems()
141 const char *CRadioStyle::GetStyleName()
146 CBaseMenuPlayer *CRadioStyle::GetMenuPlayer(int client)
148 return &m_players[client];
151 CRadioDisplay::CRadioDisplay()
156 CRadioDisplay::CRadioDisplay(CRadioMenu *menu)
161 bool CRadioDisplay::DrawRawLine(const char *rawline)
163 m_BufferText.append(rawline);
164 m_BufferText.append("\n");
168 void CRadioDisplay::Reset()
170 m_BufferText.assign("");
176 bool CRadioDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
178 return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
181 bool CRadioDisplay::SetExtOption(MenuOption option, const void *valuePtr)
186 IMenuStyle *CRadioDisplay::GetParentStyle()
188 return &g_RadioMenuStyle;
191 void CRadioDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
193 if (m_Title.size() != 0 && onlyIfEmpty)
197 m_Title.assign(text);
200 unsigned int CRadioDisplay::DrawItem(const ItemDrawInfo &item)
202 if (m_NextPos > 10 || !CanDrawItem(item.style))
207 if (item.style & ITEMDRAW_RAWLINE)
209 if (item.style & ITEMDRAW_SPACER)
211 m_BufferText.append(" \n");
213 m_BufferText.append(item.display);
214 m_BufferText.append("\n");
217 } else if (item.style & ITEMDRAW_SPACER) {
218 m_BufferText.append(" \n");
220 } else if (item.style & ITEMDRAW_NOTEXT) {
224 if (item.style & ITEMDRAW_DISABLED)
226 m_BufferText.append(g_RadioNumTable[m_NextPos]);
227 m_BufferText.append(item.display);
228 m_BufferText.append("\n");
230 m_BufferText.append("->");
231 m_BufferText.append(g_RadioNumTable[m_NextPos]);
232 m_BufferText.append(item.display);
233 m_BufferText.append("\n");
234 keys |= (1<<(m_NextPos-1));
240 bool CRadioDisplay::CanDrawItem(unsigned int drawFlags)
242 if ((drawFlags & ITEMDRAW_IGNORE) == ITEMDRAW_IGNORE)
247 if ((drawFlags & ITEMDRAW_DISABLED) && (drawFlags & ITEMDRAW_CONTROL))
255 void CRadioDisplay::SendRawDisplay(int client, unsigned int time)
260 len = UTIL_Format(buffer, sizeof(buffer), "%s\n%s", m_Title.c_str(), m_BufferText.c_str());
262 cell_t players[1] = {client};
273 bf_write *buffer = g_UserMsgs.StartMessage(g_ShowMenuId, players, 1, 0);
274 buffer->WriteWord(keys);
275 buffer->WriteChar(time ? time : -1);
276 buffer->WriteByte( (len > 240) ? 1 : 0 );
277 buffer->WriteString(ptr);
278 g_UserMsgs.EndMessage();
290 void CRadioDisplay::DeleteThis()
295 CRadioMenu::CRadioMenu() : CBaseMenu(&g_RadioMenuStyle)
299 bool CRadioMenu::SetExtOption(MenuOption option, const void *valuePtr)
304 IMenuPanel *CRadioMenu::CreatePanel()
306 return new CRadioDisplay(this);
309 bool CRadioMenu::Display(int client, IMenuHandler *handler, unsigned int time)
311 return g_RadioMenuStyle.DoClientMenu(client, this, handler, time);
314 void CRadioMenu::Cancel_Finally()
316 g_RadioMenuStyle.CancelMenu(this);
319 const char *g_RadioNumTable[11] =
321 "0. ", "1. ", "2. ", "3. ", "4. ", "5. ", "6. ", "7. ", "8. ", "9. ", "0. "