005a25d5c61eb58633df6771686146373135ccf8
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / quickmenu.qc
1 #include "quickmenu.qh"
2
3 #include <client/autocvars.qh>
4 #include <client/defs.qh>
5 #include <client/miscfunctions.qh>
6 #include <common/ent_cs.qh>
7 #include <common/minigames/cl_minigames.qh>
8 #include <client/hud/_mod.qh>
9 #include <client/mapvoting.qh>
10
11 // QuickMenu (#23)
12
13 // QUICKMENU_MAXLINES must be <= 10
14 const int QUICKMENU_MAXLINES = 10;
15 // visible entries are loaded from QuickMenu_Buffer into QuickMenu_Page_* arrays
16 string QuickMenu_Page_Command[QUICKMENU_MAXLINES];
17 string QuickMenu_Page_Description[QUICKMENU_MAXLINES];
18 int QuickMenu_Page_Command_Type[QUICKMENU_MAXLINES];
19 int QuickMenu_Page_Entries;
20 int QuickMenu_Page;
21 int QuickMenu_Page_ActivatedEntry = -1;
22 bool QuickMenu_Page_ActivatedEntry_Close;
23 float QuickMenu_Page_ActivatedEntry_Time;
24 bool QuickMenu_IsLastPage;
25 // all the entries are loaded into QuickMenu_Buffer
26 // each entry (submenu or command) is composed of 2 entries
27 const int QUICKMENU_MAXENTRIES = 256;
28 const int QUICKMENU_BUFFER_MAXENTRIES = 2 * QUICKMENU_MAXENTRIES;
29 int QuickMenu_Buffer = -1;
30 int QuickMenu_Buffer_Size;
31 int QuickMenu_Buffer_Index;
32 string QuickMenu_CurrentSubMenu;
33 float QuickMenu_TimeOut;
34
35 // QuickMenu_Buffer are labeled with these tags
36 #define QM_TAG_TITLE "T"
37 #define QM_TAG_SUBMENU "S"
38 #define QM_TAG_COMMAND "C"
39 #define QM_TAG_PLCOMMAND "P"
40
41 #define QuickMenu_Buffer_Set(tag, string) bufstr_set(QuickMenu_Buffer, QuickMenu_Buffer_Size, strcat(tag, string))
42 #define QuickMenu_Buffer_Get() bufstr_get(QuickMenu_Buffer, QuickMenu_Buffer_Index)
43
44 // if s1 is not empty s will be displayed as command otherwise as submenu
45 void QuickMenu_Page_LoadEntry(int i, string s, string s1)
46 {
47         TC(int, i);
48         //LOG_INFOF("^xc80 entry %d: %s, %s\n", i, s, s1);
49         strcpy(QuickMenu_Page_Description[i], s);
50         strcpy(QuickMenu_Page_Command[i], s1);
51 }
52
53 void QuickMenu_Page_ClearEntry(int i)
54 {
55         TC(int, i);
56         strfree(QuickMenu_Page_Description[i]);
57         strfree(QuickMenu_Page_Command[i]);
58         QuickMenu_Page_Command_Type[i] = 0;
59 }
60
61 bool HUD_QuickMenu_Forbidden()
62 {
63         return (mv_active
64                 || (hud_configure_prev && hud_configure_prev != -1)
65                 || HUD_MinigameMenu_IsOpened()
66                 || (QuickMenu_TimeOut && time > QuickMenu_TimeOut));
67 }
68
69 // returns true if succeded, false otherwise
70 bool QuickMenu_Open(string mode, string submenu, string file)
71 {
72         QuickMenu_TimeOut = 0;
73         if (HUD_QuickMenu_Forbidden())
74                 return false;
75
76         int fh = -1;
77         string s;
78
79         if(mode == "")
80         {
81                 if(file == "" || file == "0")
82                         mode = "default";
83                 else
84                         mode = "file";
85         }
86
87         if(mode == "file")
88         {
89                 if(file == "" || file == "0")
90                         LOG_INFO("No file name is set in hud_panel_quickmenu_file, loading default quickmenu");
91                 else
92                 {
93                         fh = fopen(file, FILE_READ);
94                         if(fh < 0)
95                                 LOG_INFOF("Couldn't open file \"%s\", loading default quickmenu", file);
96                 }
97                 if(fh < 0)
98                         mode = "default";
99         }
100
101         if(mode == "default")
102         {
103                 QuickMenu_Buffer = buf_create();
104                 if(QuickMenu_Buffer < 0)
105                         return false;
106
107                 QuickMenu_Default(submenu);
108         }
109         else if(mode == "file")
110         {
111                 QuickMenu_Buffer = buf_create();
112                 if(QuickMenu_Buffer < 0)
113                 {
114                         fclose(fh);
115                         return false;
116                 }
117
118                 QuickMenu_Buffer_Size = 0;
119                 while((s = fgets(fh)) && QuickMenu_Buffer_Size < QUICKMENU_BUFFER_MAXENTRIES)
120                 {
121                         // first skip invalid entries, so we don't check them anymore
122                         int argc;
123                         argc = tokenize_console(s);
124                         if(argc == 0 || argv(0) == "")
125                                 continue;
126                         if(argc == 1)
127                                 QuickMenu_Buffer_Set(QM_TAG_SUBMENU, argv(0));
128                         else if(argc == 2)
129                         {
130                                 if(argv(1) == "")
131                                         continue;
132                                 QuickMenu_Buffer_Set(QM_TAG_TITLE, argv(0));
133                                 ++QuickMenu_Buffer_Size;
134                                 QuickMenu_Buffer_Set(QM_TAG_COMMAND, argv(1));
135                         }
136                         else if(argc == 3)
137                         {
138                                 // check for special keywords
139                                 float teamplayers = 0, without_me = 0;
140                                 switch(argv(2))
141                                 {
142                                         case "ALLPLAYERS_BUT_ME":               without_me = 1; // fall through
143                                         case "ALLPLAYERS":                              teamplayers = 0; break;
144                                         case "OWNTEAMPLAYERS_BUT_ME":   without_me = 1; // fall through
145                                         case "OWNTEAMPLAYERS":                  teamplayers = 1; break;
146                                         case "ENEMYTEAMPLAYERS":                teamplayers = 2; break;
147                                         default: continue;
148                                 }
149
150                                 if(QuickMenu_Buffer_Size + 3 < QUICKMENU_BUFFER_MAXENTRIES)
151                                 {
152                                         QuickMenu_Buffer_Set(QM_TAG_SUBMENU, argv(0));
153                                         ++QuickMenu_Buffer_Size;
154                                         QuickMenu_Buffer_Set(QM_TAG_TITLE, strcat(ftos(teamplayers), ftos(without_me))); // put PLCOMMAND arguments in the title string
155                                         ++QuickMenu_Buffer_Size;
156                                         QuickMenu_Buffer_Set(QM_TAG_PLCOMMAND, argv(1));
157                                         ++QuickMenu_Buffer_Size;
158                                         QuickMenu_Buffer_Set(QM_TAG_SUBMENU, argv(0));
159                                 }
160                         }
161                         ++QuickMenu_Buffer_Size;
162                 }
163                 fclose(fh);
164         }
165         else
166         {
167                 LOG_WARNF("Unrecognized mode %s", mode);
168                 return false;
169         }
170
171         if (QuickMenu_Buffer_Size <= 0)
172         {
173                 buf_del(QuickMenu_Buffer);
174                 QuickMenu_Buffer = -1;
175                 return false;
176         }
177
178         if(mode == "file")
179                 QuickMenu_Page_Load(submenu, 0);
180         else
181                 QuickMenu_Page_Load("", 0);
182
183         hudShiftState = 0;
184
185         QuickMenu_TimeOut = ((autocvar_hud_panel_quickmenu_time > 0) ? time + autocvar_hud_panel_quickmenu_time : 0);
186         return true;
187 }
188
189 void QuickMenu_Buffer_Close()
190 {
191         if (QuickMenu_Buffer >= 0)
192         {
193                 buf_del(QuickMenu_Buffer);
194                 QuickMenu_Buffer = -1;
195                 QuickMenu_Buffer_Size = 0;
196         }
197 }
198
199 void QuickMenu_Close()
200 {
201         strfree(QuickMenu_CurrentSubMenu);
202         int i;
203         for (i = 0; i < QUICKMENU_MAXLINES; ++i)
204                 QuickMenu_Page_ClearEntry(i);
205         QuickMenu_Page_Entries = 0;
206         mouseClicked = 0;
207         prevMouseClicked = 0;
208         QuickMenu_Buffer_Close();
209 }
210
211 // It assumes submenu open tag is already detected
212 void QuickMenu_skip_submenu(string submenu)
213 {
214         string z_submenu = string_null; strcpy(z_submenu, submenu);
215         for(++QuickMenu_Buffer_Index ; QuickMenu_Buffer_Index < QuickMenu_Buffer_Size; ++QuickMenu_Buffer_Index)
216         {
217                 string s = QuickMenu_Buffer_Get();
218                 if(substring(s, 0, 1) != QM_TAG_SUBMENU)
219                         continue;
220                 if(substring(s, 1, -1) == z_submenu) // submenu end
221                         break;
222                 QuickMenu_skip_submenu(substring(s, 1, -1));
223         }
224         strfree(z_submenu);
225 }
226
227 bool QuickMenu_IsOpened()
228 {
229         return (QuickMenu_Page_Entries > 0);
230 }
231
232 bool HUD_Quickmenu_PlayerListEntries_Create(string cmd, int teamplayers, bool without_me)
233 {
234         TC(int, teamplayers); TC(bool, without_me);
235         int i;
236         for(i = 0; i < QUICKMENU_MAXLINES; ++i)
237                 QuickMenu_Page_ClearEntry(i);
238         QuickMenu_Buffer_Close();
239
240         QuickMenu_Buffer = buf_create();
241         if(QuickMenu_Buffer < 0)
242                 return false;
243
244         HUD_Quickmenu_PlayerListEntries(cmd, teamplayers, without_me);
245
246         if(QuickMenu_Buffer_Size <= 0)
247         {
248                 buf_del(QuickMenu_Buffer);
249                 QuickMenu_Buffer = -1;
250                 return false;
251         }
252         return true;
253 }
254
255 // new_page 0 means page 0, new_page != 0 means next page
256 int QuickMenu_Buffer_Index_Prev;
257 bool QuickMenu_Page_Load(string target_submenu, bool new_page)
258 {
259         TC(bool, new_page);
260         string s = string_null, cmd = string_null, z_submenu;
261
262         QuickMenu_Page_ActivatedEntry = -1;
263         if (new_page == 0)
264                 QuickMenu_Page = 0;
265         else
266                 ++QuickMenu_Page;
267
268         z_submenu = strzone(target_submenu);
269         strcpy(QuickMenu_CurrentSubMenu, z_submenu);
270
271         QuickMenu_IsLastPage = true;
272         QuickMenu_Page_Entries = 0;
273
274         QuickMenu_Buffer_Index = 0;
275         if (z_submenu != "")
276         {
277                 // skip everything until the submenu open tag is found
278                 for( ; QuickMenu_Buffer_Index < QuickMenu_Buffer_Size; ++QuickMenu_Buffer_Index)
279                 {
280                         s = QuickMenu_Buffer_Get();
281                         if(substring(s, 0, 1) == QM_TAG_SUBMENU && substring(s, 1, -1) == z_submenu)
282                         {
283                                 //LOG_INFOF("^3 beginning of %s\n", z_submenu);
284                                 ++QuickMenu_Buffer_Index;
285                                 break; // target_submenu found!
286                         }
287                         //LOG_INFOF("^1 skipping %s\n", s);
288                 }
289                 if(QuickMenu_Buffer_Index == QuickMenu_Buffer_Size)
290                         LOG_WARNF("Couldn't find submenu \"%s\"", z_submenu);
291         }
292
293         // only the last page can contain up to QUICKMENU_MAXLINES entries
294         // the other ones contain only (QUICKMENU_MAXLINES - 2) entries
295         // so that the panel can show an empty row and "Continue..."
296         float first_entry = QuickMenu_Page * (QUICKMENU_MAXLINES - 2);
297         int entry_num = 0; // counts entries in target_submenu
298         for( ; QuickMenu_Buffer_Index < QuickMenu_Buffer_Size; ++QuickMenu_Buffer_Index)
299         {
300                 s = QuickMenu_Buffer_Get();
301
302                 if(z_submenu != "" && substring(s, 1, -1) == z_submenu)
303                 {
304                         //LOG_INFOF("^3 end of %s\n", z_submenu);
305                         break;
306                 }
307
308                 if(entry_num >= first_entry)
309                 {
310                         ++QuickMenu_Page_Entries;
311                         if(QuickMenu_Page_Entries == QUICKMENU_MAXLINES - 2)
312                                 QuickMenu_Buffer_Index_Prev = QuickMenu_Buffer_Index;
313                         else if(QuickMenu_Page_Entries == QUICKMENU_MAXLINES)
314                         {
315                                 QuickMenu_Page_ClearEntry(QUICKMENU_MAXLINES - 1);
316                                 QuickMenu_Buffer_Index = QuickMenu_Buffer_Index_Prev;
317                                 QuickMenu_IsLastPage = false;
318                                 break;
319                         }
320                 }
321
322                 // NOTE: entries are loaded starting from 1, not from 0
323                 if(substring(s, 0, 1) == QM_TAG_SUBMENU)
324                 {
325                         if(entry_num >= first_entry)
326                                 QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, substring(s, 1, -1), "");
327                         QuickMenu_skip_submenu(substring(s, 1, -1));
328                 }
329                 else if(substring(s, 0, 1) == QM_TAG_TITLE)
330                 {
331                         ++QuickMenu_Buffer_Index;
332                         if(entry_num >= first_entry)
333                         {
334                                 cmd = QuickMenu_Buffer_Get();
335                                 string command_code = substring(cmd, 0, 1);
336                                 if(command_code == QM_TAG_COMMAND)
337                                         cmd = substring(cmd, 1, -1);
338                                 else if(command_code == QM_TAG_PLCOMMAND)
339                                 {
340                                         // throw away the current quickmenu buffer and load a new one
341                                         cmd = substring(cmd, 1, -1);
342                                         strunzone(z_submenu);
343                                         if(HUD_Quickmenu_PlayerListEntries_Create(cmd, stof(substring(s, 1, 1)), stof(substring(s, 2, 1))))
344                                                 return QuickMenu_Page_Load("", 0);
345                                         QuickMenu_Close();
346                                         return false;
347                                 }
348
349                                 tokenize_console(cmd);
350                                 QuickMenu_Page_Command_Type[QuickMenu_Page_Entries] = (argv(1) && argv(0) == "toggle");
351
352                                 QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, substring(s, 1, -1), cmd);
353                         }
354
355                 }
356
357                 ++entry_num;
358         }
359         strunzone(z_submenu);
360         if (QuickMenu_Page_Entries == 0)
361         {
362                 QuickMenu_Close();
363                 return false;
364         }
365         QuickMenu_TimeOut = ((autocvar_hud_panel_quickmenu_time > 0) ? time + autocvar_hud_panel_quickmenu_time : 0);
366         return true;
367 }
368
369 bool QuickMenu_ActionForNumber(int num)
370 {
371         TC(int, num);
372         if (!QuickMenu_IsLastPage)
373         {
374                 if (num < 0 || num >= QUICKMENU_MAXLINES)
375                         return false;
376                 if (num == QUICKMENU_MAXLINES - 1)
377                         return false;
378                 if (num == 0)
379                 {
380                         QuickMenu_Page_Load(QuickMenu_CurrentSubMenu, +1);
381                         return false;
382                 }
383         } else if (num <= 0 || num > QuickMenu_Page_Entries)
384                 return false;
385
386         if (QuickMenu_Page_Command[num] != "")
387         {
388                 QuickMenu_Page_ActivatedEntry_Time = time + 0.1;
389                 localcmd(strcat("\n", QuickMenu_Page_Command[num], "\n"));
390                 QuickMenu_TimeOut = ((autocvar_hud_panel_quickmenu_time > 0) ? time + autocvar_hud_panel_quickmenu_time : 0);
391                 return true;
392         }
393         if (QuickMenu_Page_Description[num] != "")
394                 QuickMenu_Page_Load(QuickMenu_Page_Description[num], 0);
395         return false;
396 }
397
398 void QuickMenu_Page_ActiveEntry(int entry_num)
399 {
400         TC(int, entry_num);
401         QuickMenu_Page_ActivatedEntry = entry_num;
402         if(QuickMenu_Page_Command[QuickMenu_Page_ActivatedEntry])
403         {
404                 bool f = QuickMenu_ActionForNumber(QuickMenu_Page_ActivatedEntry);
405                 // toggle commands don't close the quickmenu
406                 if(QuickMenu_Page_Command_Type[QuickMenu_Page_ActivatedEntry] == 1)
407                         QuickMenu_Page_ActivatedEntry_Close = false;
408                 else
409                         QuickMenu_Page_ActivatedEntry_Close = (f && !(hudShiftState & S_CTRL));
410         }
411         else
412                 QuickMenu_Page_ActivatedEntry_Close = (!(hudShiftState & S_CTRL));
413 }
414
415 bool QuickMenu_InputEvent(int bInputType, float nPrimary, float nSecondary)
416 {
417         TC(int, bInputType);
418         // we only care for keyboard events
419         if(bInputType == 2)
420                 return false;
421
422         if(!QuickMenu_IsOpened() || autocvar__hud_configure || mv_active)
423                 return false;
424
425         if(bInputType == 3)
426         {
427                 mousepos.x = nPrimary;
428                 mousepos.y = nSecondary;
429                 return true;
430         }
431
432         // allow console bind to work
433         string con_keys = findkeysforcommand("toggleconsole", 0);
434         int keys = tokenize(con_keys); // findkeysforcommand returns data for this
435         bool hit_con_bind = false;
436         int i;
437         for (i = 0; i < keys; ++i)
438         {
439                 if(nPrimary == stof(argv(i)))
440                         hit_con_bind = true;
441         }
442
443         if(bInputType == 0) {
444                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
445                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
446                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
447         }
448         else if(bInputType == 1) {
449                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
450                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
451                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
452         }
453
454         if(nPrimary == K_ESCAPE)
455         {
456                 if (bInputType == 1)
457                         return true;
458                 QuickMenu_Close();
459         }
460         else if(nPrimary >= '0' && nPrimary <= '9')
461         {
462                 if (bInputType == 1)
463                         return true;
464                 QuickMenu_Page_ActiveEntry(stof(chr2str(nPrimary)));
465         }
466         if(nPrimary == K_MOUSE1)
467         {
468                 if(bInputType == 0) // key pressed
469                         mouseClicked |= S_MOUSE1;
470                 else if(bInputType == 1) // key released
471                         mouseClicked -= (mouseClicked & S_MOUSE1);
472         }
473         else if(nPrimary == K_MOUSE2)
474         {
475                 if(bInputType == 0) // key pressed
476                         mouseClicked |= S_MOUSE2;
477                 else if(bInputType == 1) // key released
478                         mouseClicked -= (mouseClicked & S_MOUSE2);
479         }
480         else if(hit_con_bind)
481                 return false;
482
483         return true;
484 }
485
486 void QuickMenu_Mouse()
487 {
488         if(mv_active) return;
489
490         if(!mouseClicked)
491         if(prevMouseClicked & S_MOUSE2)
492         {
493                 QuickMenu_Close();
494                 return;
495         }
496
497         panel = HUD_PANEL(QUICKMENU);
498         HUD_Panel_LoadCvars();
499
500         if(panel_bg_padding)
501         {
502                 panel_pos += '1 1 0' * panel_bg_padding;
503                 panel_size -= '2 2 0' * panel_bg_padding;
504         }
505
506         float first_entry_pos, entries_height;
507         vector fontsize;
508         fontsize = '1 1 0' * (panel_size.y / QUICKMENU_MAXLINES);
509         first_entry_pos = panel_pos.y + ((QUICKMENU_MAXLINES - QuickMenu_Page_Entries) * fontsize.y) / 2;
510         entries_height = panel_size.y - ((QUICKMENU_MAXLINES - QuickMenu_Page_Entries) * fontsize.y);
511
512         if (mousepos.x >= panel_pos.x && mousepos.y >= first_entry_pos && mousepos.x <= panel_pos.x + panel_size.x && mousepos.y <= first_entry_pos + entries_height)
513         {
514                 int entry_num = min(QuickMenu_Page_Entries - 1, floor((mousepos.y - first_entry_pos) / fontsize.y));
515                 if (QuickMenu_IsLastPage || entry_num != QUICKMENU_MAXLINES - 2)
516                 {
517                         if(!mouseClicked && (prevMouseClicked & S_MOUSE1))
518                                 QuickMenu_Page_ActiveEntry((entry_num < QUICKMENU_MAXLINES - 1) ? entry_num + 1 : 0);
519
520                         if (time > QuickMenu_Page_ActivatedEntry_Time)
521                         {
522                                 vector entry_pos = panel_pos;
523                                 entry_pos.y = first_entry_pos + entry_num * fontsize.y;
524                                 vector color;
525                                 if (mouseClicked & S_MOUSE1)
526                                         color = '0.5 1 0.5';
527                                 else if (hudShiftState & S_CTRL)
528                                         color = '1 1 0.3';
529                                 else
530                                         color = '1 1 1';
531                                 drawfill(entry_pos, vec2(panel_size.x, fontsize.y), color, .2, DRAWFLAG_NORMAL);
532                         }
533                 }
534         }
535 }
536
537 void HUD_Quickmenu_DrawEntry(vector pos, string desc, string option, vector fontsize)
538 {
539         string entry;
540         float offset;
541         float desc_width = panel_size.x;
542         if(option)
543         {
544                 string pic = strcat(hud_skin_path, "/", option);
545                 if(precache_pic(pic) == "")
546                         pic = strcat("gfx/hud/default/", option);
547                 vector option_size = '1 1 0' * fontsize.y * 0.8;
548                 desc_width -= option_size.x;
549                 drawpic(pos + vec2(desc_width, (fontsize.y - option_size.y) / 2), pic, option_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE);
550                 desc_width -= fontsize.x / 4;
551         }
552         entry = textShortenToWidth(desc, desc_width, fontsize, stringwidth_colors);
553         if (autocvar_hud_panel_quickmenu_align > 0)
554         {
555                 float real_desc_width = stringwidth_colors(entry, fontsize);
556                 offset = (desc_width - real_desc_width) * min(autocvar_hud_panel_quickmenu_align, 1);
557
558                 if(option)
559                 {
560                         // when there's enough room align description regardless the checkbox
561                         float extra_offset = (panel_size.x - desc_width) * min(autocvar_hud_panel_quickmenu_align, 1);
562                         if(offset + real_desc_width + extra_offset < desc_width)
563                                 offset += extra_offset;
564                         else
565                                 offset = max(0, desc_width - real_desc_width);
566                 }
567                 drawcolorcodedstring(pos + eX * offset, entry, fontsize, panel_fg_alpha, DRAWFLAG_ADDITIVE);
568         }
569         else
570                 drawcolorcodedstring(pos, entry, fontsize, panel_fg_alpha, DRAWFLAG_ADDITIVE);
571 }
572
573 void HUD_QuickMenu()
574 {
575         if(!autocvar__hud_configure)
576         {
577                 if (!hud_draw_maximized || !QuickMenu_IsOpened())
578                         return;
579
580                 if (HUD_QuickMenu_Forbidden())
581                 {
582                         QuickMenu_Close();
583                         return;
584                 }
585         }
586         else
587         {
588                 if(!QuickMenu_IsOpened())
589                 {
590                         QuickMenu_Page_Entries = 1;
591                         QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, sprintf(_("Submenu%d"), QuickMenu_Page_Entries), "");
592                         ++QuickMenu_Page_Entries;
593                         QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, sprintf(_("Submenu%d"), QuickMenu_Page_Entries), "");
594                         ++QuickMenu_Page_Entries;
595                         // although real command doesn't matter here, it must not be empty
596                         // otherwise the entry is displayed like a submenu
597                         for (; QuickMenu_Page_Entries < QUICKMENU_MAXLINES - 1; ++QuickMenu_Page_Entries)
598                                 QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, sprintf(_("Command%d"), QuickMenu_Page_Entries), "-");
599                         ++QuickMenu_Page_Entries;
600                         QuickMenu_Page_ClearEntry(QuickMenu_Page_Entries);
601                         QuickMenu_IsLastPage = false;
602                 }
603         }
604
605         HUD_Panel_LoadCvars();
606
607         HUD_Scale_Disable();
608         HUD_Panel_DrawBg();
609
610         if(panel_bg_padding)
611         {
612                 panel_pos += '1 1 0' * panel_bg_padding;
613                 panel_size -= '2 2 0' * panel_bg_padding;
614         }
615
616         int i;
617         vector fontsize;
618         string color;
619         fontsize = '1 1 0' * (panel_size.y / QUICKMENU_MAXLINES);
620
621         if (!QuickMenu_IsLastPage)
622         {
623                 color = "^5";
624                 HUD_Quickmenu_DrawEntry(panel_pos + eY * (panel_size.y - fontsize.y), sprintf("%d: %s%s", 0, color, _("Continue...")), string_null, fontsize);
625         }
626         else
627                 panel_pos.y += ((QUICKMENU_MAXLINES - QuickMenu_Page_Entries) * fontsize.y) / 2;
628
629         for (i = 1; i <= QuickMenu_Page_Entries; ++i) {
630                 if (QuickMenu_Page_Description[i] == "")
631                         break;
632                 string option = string_null;
633                 if (QuickMenu_Page_Command[i] == "")
634                         color = "^4";
635                 else
636                 {
637                         color = "^3";
638                         if(QuickMenu_Page_Command_Type[i] == 1) // toggle command
639                         {
640                                 int end = strstrofs(QuickMenu_Page_Command[i], ";", 0);
641                                 if(end < 0)
642                                         tokenize_console(QuickMenu_Page_Command[i]);
643                                 else
644                                         tokenize_console(substring(QuickMenu_Page_Command[i], 0, end));
645
646                                 if(argv(1) && argv(0) == "toggle")
647                                 {
648                                         // "enable feature xxx" "toggle xxx" (or "toggle xxx 1 0")
649                                         // "disable feature xxx" "toggle xxx 0 1"
650                                         float ON_value = 1, OFF_value = 0;
651                                         if(argv(2))
652                                                 ON_value = stof(argv(2));
653
654                                         if(argv(3))
655                                                 OFF_value = stof(argv(3));
656                                         else
657                                                 OFF_value = !ON_value;
658
659                                         float value = cvar(argv(1));
660                                         if(value == ON_value)
661                                                 option = "checkbox_checked";
662                                         else if(value == OFF_value)
663                                                 option = "checkbox_empty";
664                                         else
665                                                 option = "checkbox_undefined";
666                                 }
667                         }
668                 }
669                 HUD_Quickmenu_DrawEntry(panel_pos, sprintf("%d: %s%s", i, color, QuickMenu_Page_Description[i]), option, fontsize);
670
671                 if (time < QuickMenu_Page_ActivatedEntry_Time && QuickMenu_Page_ActivatedEntry == i)
672                         drawfill(panel_pos, vec2(panel_size.x, fontsize.y), '0.5 1 0.5', .2, DRAWFLAG_NORMAL);
673
674                 panel_pos.y += fontsize.y;
675         }
676
677         if(QuickMenu_Page_ActivatedEntry >= 0 && time >= QuickMenu_Page_ActivatedEntry_Time)
678         {
679                 if(!QuickMenu_Page_Command[QuickMenu_Page_ActivatedEntry])
680                 {
681                         bool f = QuickMenu_ActionForNumber(QuickMenu_Page_ActivatedEntry);
682                         if(f && QuickMenu_Page_ActivatedEntry_Close)
683                                 QuickMenu_Close();
684                 }
685                 else if(QuickMenu_Page_ActivatedEntry_Close)
686                         QuickMenu_Close();
687                 QuickMenu_Page_ActivatedEntry = -1;
688                 QuickMenu_Page_ActivatedEntry_Time = 0;
689         }
690 }
691
692
693 #define QUICKMENU_SMENU(submenu,eng_submenu) { \
694         if(target_submenu == eng_submenu && target_submenu_found) \
695                 return; /* target_submenu entries are now loaded, exit */ \
696         if(QuickMenu_Buffer_Size < QUICKMENU_BUFFER_MAXENTRIES) \
697                 QuickMenu_Buffer_Set(QM_TAG_SUBMENU, submenu); \
698         ++QuickMenu_Buffer_Size; \
699         if(target_submenu == eng_submenu && !target_submenu_found) { \
700                 QuickMenu_Buffer_Size = 0; /* enable load of next entries */ \
701                 target_submenu_found = true; \
702         } \
703 }
704
705 #define QUICKMENU_ENTRY(title,command) { \
706         if(QuickMenu_Buffer_Size + 1 < QUICKMENU_BUFFER_MAXENTRIES) \
707         { \
708                 QuickMenu_Buffer_Set(QM_TAG_TITLE, title); \
709                 ++QuickMenu_Buffer_Size; \
710                 QuickMenu_Buffer_Set(QM_TAG_COMMAND, command); \
711         } \
712         ++QuickMenu_Buffer_Size; \
713 }
714
715 #define QUICKMENU_SMENU_PL(submenu,eng_submenu,command,teamplayers,without_me) { \
716         if(QuickMenu_Buffer_Size + 3 < QUICKMENU_BUFFER_MAXENTRIES) {\
717                 QUICKMENU_SMENU(submenu,eng_submenu) \
718                 QuickMenu_Buffer_Set(QM_TAG_TITLE, strcat(ftos(teamplayers), ftos(without_me))); \
719                 ++QuickMenu_Buffer_Size; \
720                 QuickMenu_Buffer_Set(QM_TAG_PLCOMMAND, command); \
721                 ++QuickMenu_Buffer_Size; \
722                 QUICKMENU_SMENU(submenu,eng_submenu) \
723         } \
724 }
725
726
727
728 // useful to Translate a string inside the Command
729 #define QUICKMENU_ENTRY_TC(title,command,text,translated_text) {\
730         if(prvm_language == "en") { \
731                 tc_title = title; \
732                 tc_cmd = sprintf(command, text); \
733         } \
734         else if(!autocvar_hud_panel_quickmenu_translatecommands || translated_text == text) { \
735                 tc_title = strcat("(en)", title); \
736                 tc_cmd = sprintf(command, text); \
737         } \
738         else { \
739                 tc_title = strcat("(", prvm_language, ")", title); \
740                 tc_cmd = sprintf(command, translated_text); \
741         } \
742         QUICKMENU_ENTRY(tc_title, tc_cmd) \
743 }
744
745 void HUD_Quickmenu_PlayerListEntries(string cmd, int teamplayers, bool without_me)
746 {
747         TC(int, teamplayers); TC(bool, without_me);
748         entity pl;
749         if(teamplayers && !team_count)
750                 return;
751
752         for(pl = players.sort_next; pl; pl = pl.sort_next)
753         {
754                 if(teamplayers == 1 && (pl.team != myteam || pl.team == NUM_SPECTATOR)) // only own team players
755                         continue;
756                 if(teamplayers == 2 && (pl.team == myteam || pl.team == NUM_SPECTATOR)) // only enemy team players
757                         continue;
758                 if(without_me && pl.sv_entnum == player_localnum)
759                         continue;
760                 QUICKMENU_ENTRY(entcs_GetName(pl.sv_entnum), sprintf(cmd, entcs_GetName(pl.sv_entnum)))
761         }
762
763         return;
764 }
765
766
767 // Specifying target_submenu, this function only loads entries inside target_submenu
768 // NOTE: alternatively we could have loaded the whole default quickmenu and
769 // then called QuickMenu_Page_Load(target_submenu, 0);
770 // but this sytem is more reliable since we can always refer to target_submenu
771 // with the English title even if a translation is active
772 void QuickMenu_Default(string target_submenu)
773 {
774         bool target_submenu_found = false;
775         if(target_submenu != "")
776                 QuickMenu_Buffer_Size = QUICKMENU_BUFFER_MAXENTRIES; // forbids load of next entries until target_submenu
777
778         string tc_title;
779         string tc_cmd;
780
781         QUICKMENU_SMENU(_("Chat"), "Chat")
782                 QUICKMENU_SMENU_PL(CTX(_("QMCMD^Send public message to")), "Send public message to", "commandmode say %s:^7", 0, 1)
783                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^nice one")), "say %s", ":-) / nice one", CTX(_("QMCMD^:-) / nice one")))
784                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^good game")), "say %s", "good game", CTX(_("QMCMD^good game")))
785                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^hi / good luck")), "say %s", "hi / good luck and have fun", CTX(_("QMCMD^hi / good luck and have fun")))
786                 if(prvm_language != "en")
787                 QUICKMENU_ENTRY(CTX(_("QMCMD^Send in English")), "toggle hud_panel_quickmenu_translatecommands 0 1; quickmenu; wait; quickmenu default Chat")
788         QUICKMENU_SMENU(_("Chat"), "Chat")
789
790         if(teamplay)
791         {
792         QUICKMENU_SMENU(CTX(_("QMCMD^Team chat")), "Team chat")
793                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^quad soon")), "say_team %s", "quad soon", CTX(_("QMCMD^quad soon")))
794                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^free item, icon")), "say_team %s; g_waypointsprite_team_here_p", "free item %x^7 (l:%y^7)", CTX(_("QMCMD^free item %x^7 (l:%y^7)")))
795                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^took item, icon")), "say_team %s; g_waypointsprite_team_here", "took item (l:%l^7)", CTX(_("QMCMD^took item (l:%l^7)")))
796                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^negative")), "say_team %s", "negative", CTX(_("QMCMD^negative")))
797                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^positive")), "say_team %s", "positive", CTX(_("QMCMD^positive")))
798                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^need help, icon")), "say_team %s; g_waypointsprite_team_helpme; cmd voice needhelp", "need help (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)", CTX(_("QMCMD^need help (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)")))
799                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^enemy seen, icon")), "say_team %s; g_waypointsprite_team_danger_p; cmd voice incoming", "enemy seen (l:%y^7)", CTX(_("QMCMD^enemy seen (l:%y^7)")))
800                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^flag seen, icon")), "say_team %s; g_waypointsprite_team_here_p; cmd voice seenflag", "flag seen (l:%y^7)", CTX(_("QMCMD^flag seen (l:%y^7)")))
801                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^defending, icon")), "say_team %s; g_waypointsprite_team_here", "defending (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)", CTX(_("QMCMD^defending (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)")))
802                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^roaming, icon")), "say_team %s; g_waypointsprite_team_here", "roaming (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)", CTX(_("QMCMD^roaming (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)")))
803                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^attacking, icon")), "say_team %s; g_waypointsprite_team_here", "attacking (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)", CTX(_("QMCMD^attacking (l:%l^7) (h:%h^7 a:%a^7 w:%w^7)")))
804                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^killed flagcarrier, icon")), "say_team %s; g_waypointsprite_team_here_p", "killed flagcarrier (l:%y^7)", CTX(_("QMCMD^killed flagcarrier (l:%y^7)")))
805                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^dropped flag, icon")), "say_team %s; g_waypointsprite_team_here_d", "dropped flag (l:%d^7)", CTX(_("QMCMD^dropped flag (l:%d^7)")))
806                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^drop weapon, icon")), "say_team %s; g_waypointsprite_team_here; wait; dropweapon", "dropped gun %w^7 (l:%l^7)", CTX(_("QMCMD^dropped weapon %w^7 (l:%l^7)")))
807                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^drop flag/key, icon")), "say_team %s; g_waypointsprite_team_here; wait; use", "dropped flag/key %w^7 (l:%l^7)", CTX(_("QMCMD^dropped flag/key %w^7 (l:%l^7)")))
808         QUICKMENU_SMENU(CTX(_("QMCMD^Team chat")), "Team chat")
809         }
810
811         QUICKMENU_SMENU_PL(CTX(_("QMCMD^Send private message to")), "Send private message to", "commandmode tell \"%s^7\"", 0, 1)
812
813         QUICKMENU_SMENU(CTX(_("QMCMD^Settings")), "Settings")
814                 QUICKMENU_SMENU(CTX(_("QMCMD^View/HUD settings")), "View/HUD settings")
815                         QUICKMENU_ENTRY(CTX(_("QMCMD^3rd person view")), "toggle chase_active")
816                         QUICKMENU_ENTRY(CTX(_("QMCMD^Player models like mine")), "toggle cl_forceplayermodels")
817                         QUICKMENU_ENTRY(CTX(_("QMCMD^Names above players")), "toggle hud_shownames")
818                         QUICKMENU_ENTRY(CTX(_("QMCMD^Crosshair per weapon")), "toggle crosshair_per_weapon")
819                         QUICKMENU_ENTRY(CTX(_("QMCMD^FPS")), "toggle hud_panel_engineinfo")
820                         QUICKMENU_ENTRY(CTX(_("QMCMD^Net graph")), "toggle shownetgraph")
821                 QUICKMENU_SMENU(CTX(_("QMCMD^View/HUD settings")), "View/HUD settings")
822
823                 QUICKMENU_SMENU(CTX(_("QMCMD^Sound settings")), "Sound settings")
824                         QUICKMENU_ENTRY(CTX(_("QMCMD^Hit sound")), "toggle cl_hitsound")
825                         QUICKMENU_ENTRY(CTX(_("QMCMD^Chat sound")), "toggle con_chatsound")
826                 QUICKMENU_SMENU(CTX(_("QMCMD^Sound settings")), "Sound settings")
827
828                 if(spectatee_status > 0)
829                 {
830                 QUICKMENU_SMENU(CTX(_("QMCMD^Spectator camera")), "Spectator camera")
831                         QUICKMENU_ENTRY(CTX(_("QMCMD^1st person")), "chase_active 0; -use")
832                         QUICKMENU_ENTRY(CTX(_("QMCMD^3rd person around player")), "chase_active 1; +use")
833                         QUICKMENU_ENTRY(CTX(_("QMCMD^3rd person behind")), "chase_active 1; -use")
834                 QUICKMENU_SMENU(CTX(_("QMCMD^Spectator camera")), "Spectator camera")
835                 }
836
837                 if(spectatee_status == -1)
838                 {
839                 QUICKMENU_SMENU(CTX(_("QMCMD^Observer camera")), "Observer camera")
840                         QUICKMENU_ENTRY(CTX(_("QMCMD^Increase speed")), "weapnext")
841                         QUICKMENU_ENTRY(CTX(_("QMCMD^Decrease speed")), "weapprev")
842                         QUICKMENU_ENTRY(CTX(_("QMCMD^Wall collision")), "toggle cl_clippedspectating")
843                 QUICKMENU_SMENU(CTX(_("QMCMD^Observer camera")), "Observer camera")
844                 }
845
846                 QUICKMENU_ENTRY(CTX(_("QMCMD^Fullscreen")), "toggle vid_fullscreen; vid_restart")
847         QUICKMENU_SMENU(CTX(_("QMCMD^Settings")), "Settings")
848
849         QUICKMENU_SMENU(CTX(_("QMCMD^Call a vote")), "Call a vote")
850                 QUICKMENU_ENTRY(CTX(_("QMCMD^Restart the map")), "vcall restart")
851                 QUICKMENU_ENTRY(CTX(_("QMCMD^End match")), "vcall endmatch")
852                 if(STAT(TIMELIMIT) > 0)
853                 {
854                 QUICKMENU_ENTRY(CTX(_("QMCMD^Reduce match time")), "vcall reducematchtime")
855                 QUICKMENU_ENTRY(CTX(_("QMCMD^Extend match time")), "vcall extendmatchtime")
856                 }
857                 if(teamplay)
858                 QUICKMENU_ENTRY(CTX(_("QMCMD^Shuffle teams")), "vcall shuffleteams")
859         QUICKMENU_SMENU(CTX(_("QMCMD^Call a vote")), "Call a vote")
860
861         if(spectatee_status != 0)
862         {
863         QUICKMENU_SMENU_PL(CTX(_("QMCMD^Spectate a player")), "Spectate a player", "spectate \"%s^7\"", 0, 1)
864         }
865
866         if(target_submenu != "" && !target_submenu_found)
867         {
868                 LOG_INFOF("Couldn't find submenu \"%s\"", target_submenu);
869                 if(prvm_language != "en")
870                         LOG_INFOF("^3Warning: submenu title must be in English", target_submenu);
871                 QuickMenu_Buffer_Size = 0;
872         }
873 }
874 #undef QUICKMENU_SMENU
875 #undef QUICKMENU_ENTRY
876 #undef QUICKMENU_ENTRY_TC