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