-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp_menu.cpp
277 lines (215 loc) · 5.42 KB
/
p_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
// Copyright (c) ZeniMax Media Inc.
// Licensed under the GNU General Public License 2.0.
#include "g_local.h"
/*
============
P_Menu_Dirty
============
*/
void P_Menu_Dirty() {
for (auto player : active_clients())
if (player->client->menu) {
player->client->menudirty = true;
player->client->menutime = level.time;
}
}
// Note that the pmenu entries are duplicated
// this is so that a static set of pmenu entries can be used
// for multiple clients and changed without interference
// note that arg will be freed when the menu is closed, it must be allocated memory
menu_hnd_t *P_Menu_Open(gentity_t *ent, const menu_t *entries, int cur, int num, void *arg, UpdateFunc_t UpdateFunc) {
menu_hnd_t *hnd;
const menu_t *p;
size_t i;
if (!ent->client)
return nullptr;
if (ent->client->menu) {
gi.Com_Print("Warning: client already has a menu.\n");
if (!Vote_Menu_Active(ent))
P_Menu_Close(ent);
}
hnd = (menu_hnd_t *)gi.TagMalloc(sizeof(*hnd), TAG_LEVEL);
hnd->UpdateFunc = UpdateFunc;
hnd->arg = arg;
hnd->entries = (menu_t *)gi.TagMalloc(sizeof(menu_t) * num, TAG_LEVEL);
memcpy(hnd->entries, entries, sizeof(menu_t) * num);
// duplicate the strings since they may be from static memory
for (i = 0; i < num; i++)
Q_strlcpy(hnd->entries[i].text, entries[i].text, sizeof(entries[i].text));
hnd->num = num;
if (cur < 0 || !entries[cur].SelectFunc) {
for (i = 0, p = entries; i < num; i++, p++)
if (p->SelectFunc)
break;
} else
i = cur;
if (i >= num)
hnd->cur = -1;
else
hnd->cur = i;
ent->client->showscores = true;
ent->client->inmenu = true;
ent->client->menu = hnd;
ent->client->ps.stats[STAT_SHOW_STATUSBAR] = 0;
if (UpdateFunc)
UpdateFunc(ent);
P_Menu_Do_Update(ent);
gi.unicast(ent, true);
return hnd;
}
void P_Menu_Close(gentity_t *ent) {
menu_hnd_t *hnd;
if (!ent->client->menu)
return;
hnd = ent->client->menu;
gi.TagFree(hnd->entries);
if (hnd->arg)
gi.TagFree(hnd->arg);
gi.TagFree(hnd);
ent->client->menu = nullptr;
ent->client->showscores = false;
gentity_t *e = ent->client->follow_target ? ent->client->follow_target : ent;
ent->client->ps.stats[STAT_SHOW_STATUSBAR] = !ClientIsPlaying(e->client) ? 0 : 1;
}
// only use on pmenu's that have been called with P_Menu_Open
void P_Menu_UpdateEntry(menu_t *entry, const char *text, int align, SelectFunc_t SelectFunc) {
Q_strlcpy(entry->text, text, sizeof(entry->text));
entry->align = align;
entry->SelectFunc = SelectFunc;
}
#include "g_statusbar.h"
void P_Menu_Do_Update(gentity_t *ent) {
int i;
menu_t *p;
int x;
menu_hnd_t *hnd;
const char *t;
bool alt = false;
if (!ent->client->menu) {
gi.Com_Print("Warning: ent has no menu\n");
return;
}
hnd = ent->client->menu;
if (hnd->UpdateFunc)
hnd->UpdateFunc(ent);
statusbar_t sb;
sb.xv(32).yv(8).picn("inventory");
for (i = 0, p = hnd->entries; i < hnd->num; i++, p++) {
if (!*(p->text))
continue; // blank line
t = p->text;
if (*t == '*') {
alt = true;
t++;
}
sb.yv(32 + i * 8);
const char *loc_func = "loc_string";
if (p->align == MENU_ALIGN_CENTER) {
x = 0;
loc_func = "loc_cstring";
} else if (p->align == MENU_ALIGN_RIGHT) {
x = 260;
loc_func = "loc_rstring";
} else
x = 64;
sb.xv(x);
sb.sb << loc_func;
if (hnd->cur == i || alt)
sb.sb << '2';
sb.sb << " 1 \"" << t << "\" \"" << p->text_arg1 << "\" ";
if (hnd->cur == i) {
sb.xv(56);
sb.string2("\">\"");
}
alt = false;
}
gi.WriteByte(svc_layout);
gi.WriteString(sb.sb.str().c_str());
}
void P_Menu_Update(gentity_t *ent) {
if (!ent->client->menu) {
gi.Com_Print("Warning: ent has no menu\n");
return;
}
if (level.time - ent->client->menutime >= 1_sec) {
// been a second or more since last update, update now
P_Menu_Do_Update(ent);
gi.unicast(ent, true);
ent->client->menutime = level.time + 1_sec;
ent->client->menudirty = false;
}
ent->client->menutime = level.time;
ent->client->menudirty = true;
gi.local_sound(ent, CHAN_AUTO, gi.soundindex("misc/menu2.wav"), 1, ATTN_NONE, 0);
}
void P_Menu_Next(gentity_t *ent) {
menu_hnd_t *hnd;
int i;
menu_t *p;
if (!ent->client->menu) {
gi.Com_Print("Warning: ent has no menu\n");
return;
}
hnd = ent->client->menu;
if (hnd->cur < 0)
return; // no selectable entries
i = hnd->cur;
p = hnd->entries + hnd->cur;
do {
i++;
p++;
if (i == hnd->num) {
i = 0;
p = hnd->entries;
}
if (p->SelectFunc)
break;
} while (i != hnd->cur);
hnd->cur = i;
P_Menu_Update(ent);
}
void P_Menu_Prev(gentity_t *ent) {
menu_hnd_t *hnd;
int i;
menu_t *p;
if (!ent->client->menu) {
gi.Com_Print("Warning: ent has no menu\n");
return;
}
hnd = ent->client->menu;
if (hnd->cur < 0)
return; // no selectable entries
i = hnd->cur;
p = hnd->entries + hnd->cur;
do {
if (i == 0) {
i = hnd->num - 1;
p = hnd->entries + i;
} else {
i--;
p--;
}
if (p->SelectFunc)
break;
} while (i != hnd->cur);
hnd->cur = i;
P_Menu_Update(ent);
}
void P_Menu_Select(gentity_t *ent) {
menu_hnd_t *hnd;
menu_t *p;
if (!ent->client->menu) {
gi.Com_Print("Warning: ent has no menu\n");
return;
}
// no selecting during intermission
if (level.intermission_queued || level.intermission_time)
return;
hnd = ent->client->menu;
if (hnd->cur < 0)
return; // no selectable entries
p = hnd->entries + hnd->cur;
if (p->SelectFunc)
p->SelectFunc(ent, hnd);
//gi.local_sound(ent, CHAN_AUTO, gi.soundindex("misc/menu1.wav"), 1, ATTN_NONE, 0);
}