-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathMenuStyle_Valve.cpp
445 lines (377 loc) · 11.1 KB
/
MenuStyle_Valve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* vim: set ts=4 sw=4 tw=99 noet :
* =============================================================================
* SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "sm_stringutil.h"
#include "PlayerManager.h"
#include "MenuStyle_Valve.h"
#include "PlayerManager.h"
#include "ConCmdManager.h"
SH_DECL_HOOK4_void(IServerPluginHelpers, CreateMessage, SH_NOATTRIB, false, edict_t *, DIALOG_TYPE, KeyValues *, IServerPluginCallbacks *);
ValveMenuStyle g_ValveMenuStyle;
extern const char *g_OptionNumTable[];
extern const char *g_OptionCmdTable[];
CallClass<IServerPluginHelpers> *g_pSPHCC = NULL;
ValveMenuStyle::ValveMenuStyle() : m_players(new CValveMenuPlayer[256+1])
{
}
CBaseMenuPlayer *ValveMenuStyle::GetMenuPlayer(int client)
{
return &m_players[client];
}
bool ValveMenuStyle::OnClientCommand(int client, const char *cmdname, const CCommand &cmd)
{
if (strcmp(cmdname, "sm_vmenuselect") == 0)
{
int key_press = atoi(cmd.Arg(1));
g_ValveMenuStyle.ClientPressedKey(client, key_press);
return true;
}
return false;
}
void ValveMenuStyle::OnSourceModAllInitialized()
{
g_Players.AddClientListener(this);
SH_ADD_HOOK(IServerPluginHelpers, CreateMessage, serverpluginhelpers, SH_MEMBER(this, &ValveMenuStyle::HookCreateMessage), false);
g_pSPHCC = SH_GET_CALLCLASS(serverpluginhelpers);
}
void ValveMenuStyle::OnSourceModShutdown()
{
SH_RELEASE_CALLCLASS(g_pSPHCC);
SH_REMOVE_HOOK(IServerPluginHelpers, CreateMessage, serverpluginhelpers, SH_MEMBER(this, &ValveMenuStyle::HookCreateMessage), false);
g_Players.RemoveClientListener(this);
}
void ValveMenuStyle::HookCreateMessage(edict_t *pEdict,
DIALOG_TYPE type,
KeyValues *kv,
IServerPluginCallbacks *plugin)
{
if (type != DIALOG_MENU)
{
return;
}
int client = IndexOfEdict(pEdict);
if (client < 1 || client > 256)
{
return;
}
CValveMenuPlayer *player = &m_players[client];
/* We don't care if the player is in a menu because, for all intents and purposes,
* the menu is completely private. Instead, we just figure out the level we'll need
* in order to override it.
*/
player->curPrioLevel = kv->GetInt("level", player->curPrioLevel);
/* Oh no! What happens if we got a menu that overwrites ours?! */
if (player->bInMenu)
{
/* Okay, let the external menu survive for now. It may live another
* day to avenge its grandfather, killed in the great Menu Interruption
* battle.
*/
_CancelClientMenu(client, MenuCancel_Interrupted, true);
}
}
IMenuPanel *ValveMenuStyle::CreatePanel()
{
return new CValveMenuDisplay();
}
IBaseMenu *ValveMenuStyle::CreateMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner)
{
return new CValveMenu(pHandler, pOwner);
}
const char *ValveMenuStyle::GetStyleName()
{
return "valve";
}
unsigned int ValveMenuStyle::GetMaxPageItems()
{
return 8;
}
void ValveMenuStyle::SendDisplay(int client, IMenuPanel *display)
{
m_players[client].curPrioLevel--;
CValveMenuDisplay *vDisplay = (CValveMenuDisplay *)display;
vDisplay->SendRawDisplay(client, m_players[client].curPrioLevel, m_players[client].menuHoldTime);
}
bool ValveMenuStyle::DoClientMenu(int client, IMenuPanel *menu, IMenuHandler *mh, unsigned int time)
{
if (vsp_interface == NULL)
{
return false;
}
return BaseMenuStyle::DoClientMenu(client, menu, mh, time);
}
bool ValveMenuStyle::DoClientMenu(int client, CBaseMenu *menu, unsigned int first_item, IMenuHandler *mh, unsigned int time)
{
if (vsp_interface == NULL)
{
mh->OnMenuStart(menu);
mh->OnMenuCancel(menu, client, MenuCancel_NoDisplay);
mh->OnMenuEnd(menu, MenuEnd_Cancelled);
return false;
}
return BaseMenuStyle::DoClientMenu(client, menu, first_item, mh, time);
}
unsigned int ValveMenuStyle::GetApproxMemUsage()
{
return sizeof(ValveMenuStyle) + (sizeof(CValveMenuPlayer) * 257);
}
CValveMenuDisplay::CValveMenuDisplay()
{
m_pKv = NULL;
Reset();
}
CValveMenuDisplay::CValveMenuDisplay(CValveMenu *pMenu)
{
m_pKv = NULL;
Reset();
m_pKv->SetColor("color", pMenu->m_IntroColor);
m_pKv->SetString("title", pMenu->m_IntroMsg);
}
void CValveMenuDisplay::DeleteThis()
{
delete this;
}
CValveMenuDisplay::~CValveMenuDisplay()
{
m_pKv->deleteThis();
}
IMenuStyle *CValveMenuDisplay::GetParentStyle()
{
return &g_ValveMenuStyle;
}
void CValveMenuDisplay::Reset()
{
if (m_pKv)
{
m_pKv->deleteThis();
}
m_pKv = new KeyValues("menu");
m_NextPos = 1;
m_TitleDrawn = false;
}
unsigned int CValveMenuDisplay::GetCurrentKey()
{
return m_NextPos;
}
bool CValveMenuDisplay::SetCurrentKey(unsigned int key)
{
if (key < m_NextPos || key > 9)
{
return false;
}
m_NextPos = key;
return true;
}
bool CValveMenuDisplay::SetExtOption(MenuOption option, const void *valuePtr)
{
if (option == MenuOption_IntroMessage)
{
m_pKv->SetString("title", (const char *)valuePtr);
return true;
} else if (option == MenuOption_IntroColor) {
int *array = (int *)valuePtr;
m_pKv->SetColor("color", Color(array[0], array[1], array[2], array[3]));
return true;
} else if (option == MenuOption_Priority) {
m_pKv->SetInt("level", *(int *)valuePtr);
return true;
}
return false;
}
bool CValveMenuDisplay::CanDrawItem(unsigned int drawFlags)
{
/**
* ITEMDRAW_RAWLINE - We can't draw random text, and this doesn't add a slot,
* so it's completely safe to ignore it.
* -----------------------------------------
*/
if (drawFlags & ITEMDRAW_RAWLINE)
{
return false;
}
/**
* Special cases, explained in DrawItem()
*/
if ((drawFlags & ITEMDRAW_NOTEXT)
|| (drawFlags & ITEMDRAW_SPACER))
{
return true;
}
/**
* We can't draw disabled text. We could bump the position, but we
* want DirectDraw() to find some actual items to display.
*/
if (drawFlags & ITEMDRAW_DISABLED)
{
return false;
}
return true;
}
unsigned int CValveMenuDisplay::DrawItem(const ItemDrawInfo &item)
{
if (m_NextPos > 9 || !CanDrawItem(item.style))
{
return 0;
}
/**
* For these cases we can't draw anything at all, but
* we can at least bump the position since we were explicitly asked to.
*/
if ((item.style & ITEMDRAW_NOTEXT)
|| (item.style & ITEMDRAW_SPACER))
{
return m_NextPos++;
}
char buffer[255];
ke::SafeSprintf(buffer, sizeof(buffer), "%d. %s", m_NextPos, item.display);
KeyValues *ki = m_pKv->FindKey(g_OptionNumTable[m_NextPos], true);
ki->SetString("command", g_OptionCmdTable[m_NextPos]);
ki->SetString("msg", buffer);
return m_NextPos++;
}
void CValveMenuDisplay::DrawTitle(const char *text, bool onlyIfEmpty/* =false */)
{
if (onlyIfEmpty && m_TitleDrawn)
{
return;
}
m_pKv->SetString("msg", text);
m_TitleDrawn = true;
}
bool CValveMenuDisplay::DrawRawLine(const char *rawline)
{
return false;
}
void CValveMenuDisplay::SendRawDisplay(int client, int priority, unsigned int time)
{
m_pKv->SetInt("level", priority);
m_pKv->SetInt("time", time ? time : 200);
SH_CALL(g_pSPHCC, &IServerPluginHelpers::CreateMessage)(
PEntityOfEntIndex(client),
DIALOG_MENU,
m_pKv,
vsp_interface);
}
bool CValveMenuDisplay::SendDisplay(int client, IMenuHandler *handler, unsigned int time)
{
return g_ValveMenuStyle.DoClientMenu(client, this, handler, time);
}
bool CValveMenuDisplay::SetSelectableKeys(unsigned int keymap)
{
return false;
}
int CValveMenuDisplay::GetAmountRemaining()
{
/* :TODO: this is a lie, but nothing really seems meaningful... */
return -1;
}
unsigned int CValveMenuDisplay::GetApproxMemUsage()
{
return sizeof(CValveMenuDisplay) + (sizeof(KeyValues) * m_NextPos * 10);
}
CValveMenu::CValveMenu(IMenuHandler *pHandler, IdentityToken_t *pOwner) :
CBaseMenu(pHandler, &g_ValveMenuStyle, pOwner),
m_IntroColor(255, 0, 0, 255)
{
strcpy(m_IntroMsg, "You have a menu, press ESC");
m_Pagination = 5;
}
void CValveMenu::Cancel_Finally()
{
g_ValveMenuStyle.CancelMenu(this);
}
bool CValveMenu::SetPagination(unsigned int itemsPerPage)
{
if (itemsPerPage > 5)
{
return false;
}
return CBaseMenu::SetPagination(itemsPerPage);
}
bool CValveMenu::SetExtOption(MenuOption option, const void *valuePtr)
{
if (option == MenuOption_IntroMessage)
{
ke::SafeStrcpy(m_IntroMsg, sizeof(m_IntroMsg), (const char *)valuePtr);
return true;
} else if (option == MenuOption_IntroColor) {
unsigned int *array = (unsigned int *)valuePtr;
m_IntroColor = Color(array[0], array[1], array[2], array[3]);
return true;
}
return false;
}
bool CValveMenu::Display(int client, unsigned int time, IMenuHandler *alt_handler)
{
return DisplayAtItem(client, time, 0, alt_handler);
}
bool CValveMenu::DisplayAtItem(int client,
unsigned int time,
unsigned int start_item,
IMenuHandler *alt_handler/* =NULL */)
{
if (m_bCancelling)
{
return false;
}
AutoHandleRooter ahr(this->GetHandle());
return g_ValveMenuStyle.DoClientMenu(client, this, start_item, alt_handler ? alt_handler : m_pHandler, time);
}
IMenuPanel *CValveMenu::CreatePanel()
{
return new CValveMenuDisplay(this);
}
void CValveMenu::SetMenuOptionFlags(unsigned int flags)
{
flags |= MENUFLAG_BUTTON_EXIT;
CBaseMenu::SetMenuOptionFlags(flags);
}
unsigned int CValveMenu::GetApproxMemUsage()
{
return sizeof(CValveMenu) + GetBaseMemUsage();
}
const char *g_OptionNumTable[] =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
};
const char *g_OptionCmdTable[] =
{
"sm_vmenuselect 0", /* INVALID! */
"sm_vmenuselect 1",
"sm_vmenuselect 2",
"sm_vmenuselect 3",
"sm_vmenuselect 4",
"sm_vmenuselect 5",
"sm_vmenuselect 6",
"sm_vmenuselect 7",
"sm_vmenuselect 8",
"sm_vmenuselect 9",
"sm_vmenuselect 10"
};