-
Notifications
You must be signed in to change notification settings - Fork 596
/
menu.cpp
282 lines (232 loc) · 6.03 KB
/
menu.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
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// menu.cpp
//
// generic menu handler
//
#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>
#include <stdio.h>
#include "vgui_viewport.h"
#define MAX_MENU_STRING 512
char g_szMenuString[MAX_MENU_STRING];
char g_szPrelocalisedMenuString[MAX_MENU_STRING];
int KB_ConvertString( char *in, char **ppout );
DECLARE_MESSAGE( m_Menu, ShowMenu );
int CHudMenu :: Init( void )
{
gHUD.AddHudElem( this );
HOOK_MESSAGE( ShowMenu );
InitHUDData();
return 1;
}
void CHudMenu :: InitHUDData( void )
{
m_fMenuDisplayed = 0;
m_bitsValidSlots = 0;
Reset();
}
void CHudMenu :: Reset( void )
{
g_szPrelocalisedMenuString[0] = 0;
m_fWaitingForMore = FALSE;
}
int CHudMenu :: VidInit( void )
{
return 1;
}
/*=================================
ParseEscapeToken
Interprets the given escape token (backslash followed by a letter). The
first character of the token must be a backslash. The second character
specifies the operation to perform:
\w : White text (this is the default)
\d : Dim (gray) text
\y : Yellow text
\r : Red text
\R : Right-align (just for the remainder of the current line)
=================================*/
static int menu_r, menu_g, menu_b, menu_x, menu_ralign;
static inline const char* ParseEscapeToken( const char* token )
{
if ( *token != '\\' )
return token;
token++;
switch ( *token )
{
case '\0':
return token;
case 'w':
menu_r = 255;
menu_g = 255;
menu_b = 255;
break;
case 'd':
menu_r = 100;
menu_g = 100;
menu_b = 100;
break;
case 'y':
menu_r = 255;
menu_g = 210;
menu_b = 64;
break;
case 'r':
menu_r = 210;
menu_g = 24;
menu_b = 0;
break;
case 'R':
menu_x = ScreenWidth/2;
menu_ralign = TRUE;
break;
}
return ++token;
}
int CHudMenu :: Draw( float flTime )
{
// check for if menu is set to disappear
if ( m_flShutoffTime > 0 )
{
if ( m_flShutoffTime <= gHUD.m_flTime )
{ // times up, shutoff
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
return 1;
}
}
// don't draw the menu if the scoreboard is being shown
if ( gViewPort && gViewPort->IsScoreBoardVisible() )
return 1;
// draw the menu, along the left-hand side of the screen
// count the number of newlines
int nlc = 0;
int i;
for ( i = 0; i < MAX_MENU_STRING && g_szMenuString[i] != '\0'; i++ )
{
if ( g_szMenuString[i] == '\n' )
nlc++;
}
// center it
int y = (ScreenHeight/2) - ((nlc/2)*12) - 40; // make sure it is above the say text
menu_r = 255;
menu_g = 255;
menu_b = 255;
menu_x = 20;
menu_ralign = FALSE;
const char* sptr = g_szMenuString;
while ( *sptr != '\0' )
{
if ( *sptr == '\\' )
{
sptr = ParseEscapeToken( sptr );
}
else if ( *sptr == '\n' )
{
menu_ralign = FALSE;
menu_x = 20;
y += (12);
sptr++;
}
else
{
char menubuf[ 80 ];
const char *ptr = sptr;
while ( *sptr != '\0' && *sptr != '\n' && *sptr != '\\')
{
sptr++;
}
strncpy( menubuf, ptr, min( ( sptr - ptr), (int)sizeof( menubuf ) ));
menubuf[ min( ( sptr - ptr), (int)(sizeof( menubuf )-1) ) ] = '\0';
if ( menu_ralign )
{
// IMPORTANT: Right-to-left rendered text does not parse escape tokens!
menu_x = gHUD.DrawHudStringReverse( menu_x, y, 0, menubuf, menu_r, menu_g, menu_b );
}
else
{
menu_x = gHUD.DrawHudString( menu_x, y, 320, menubuf, menu_r, menu_g, menu_b );
}
}
}
return 1;
}
// selects an item from the menu
void CHudMenu :: SelectMenuItem( int menu_item )
{
// if menu_item is in a valid slot, send a menuselect command to the server
if ( (menu_item > 0) && (m_bitsValidSlots & (1 << (menu_item-1))) )
{
char szbuf[32];
sprintf( szbuf, "menuselect %d\n", menu_item );
ClientCmd( szbuf );
// remove the menu
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
}
}
// Message handler for ShowMenu message
// takes four values:
// short: a bitfield of keys that are valid input
// char : the duration, in seconds, the menu should stay up. -1 means is stays until something is chosen.
// byte : a boolean, TRUE if there is more string yet to be received before displaying the menu, FALSE if it's the last string
// string: menu string to display
// if this message is never received, then scores will simply be the combined totals of the players.
int CHudMenu :: MsgFunc_ShowMenu( const char *pszName, int iSize, void *pbuf )
{
char *temp = NULL;
BEGIN_READ( pbuf, iSize );
m_bitsValidSlots = READ_SHORT();
int DisplayTime = READ_CHAR();
int NeedMore = READ_BYTE();
if ( DisplayTime > 0 )
m_flShutoffTime = DisplayTime + gHUD.m_flTime;
else
m_flShutoffTime = -1;
if ( m_bitsValidSlots )
{
if ( !m_fWaitingForMore ) // this is the start of a new menu
{
strncpy( g_szPrelocalisedMenuString, READ_STRING(), MAX_MENU_STRING );
}
else
{ // append to the current menu string
strncat( g_szPrelocalisedMenuString, READ_STRING(), MAX_MENU_STRING - strlen(g_szPrelocalisedMenuString) );
}
g_szPrelocalisedMenuString[MAX_MENU_STRING-1] = 0; // ensure null termination (strncat/strncpy does not)
if ( !NeedMore )
{ // we have the whole string, so we can localise it now
strcpy( g_szMenuString, gHUD.m_TextMessage.BufferedLocaliseTextString( g_szPrelocalisedMenuString ) );
// Swap in characters
if ( KB_ConvertString( g_szMenuString, &temp ) )
{
strcpy( g_szMenuString, temp );
free( temp );
}
}
m_fMenuDisplayed = 1;
m_iFlags |= HUD_ACTIVE;
}
else
{
m_fMenuDisplayed = 0; // no valid slots means that the menu should be turned off
m_iFlags &= ~HUD_ACTIVE;
}
m_fWaitingForMore = NeedMore;
return 1;
}