01a4a2cdb06ea8462f80aff21c09a020eb836ed4
[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         // we must unset the player's buttons, as they aren't released elsewhere
197         localcmd("-fire\n");
198         localcmd("-fire2\n");
199         localcmd("-use\n");
200         localcmd("-hook\n");
201         localcmd("-jump\n");
202
203         QuickMenu_TimeOut_Set();
204         return true;
205 }
206
207 void QuickMenu_Buffer_Close()
208 {
209         if (QuickMenu_Buffer >= 0)
210         {
211                 buf_del(QuickMenu_Buffer);
212                 QuickMenu_Buffer = -1;
213                 QuickMenu_Buffer_Size = 0;
214         }
215 }
216
217 void QuickMenu_Close()
218 {
219         strfree(QuickMenu_CurrentSubMenu);
220         int i;
221         for (i = 0; i < QUICKMENU_MAXLINES; ++i)
222                 QuickMenu_Page_ClearEntry(i);
223         QuickMenu_Page_Entries = 0;
224         mouseClicked = 0;
225         prevMouseClicked = 0;
226         QuickMenu_Buffer_Close();
227 }
228
229 // It assumes submenu open tag is already detected
230 void QuickMenu_skip_submenu(string submenu)
231 {
232         string z_submenu = string_null; strcpy(z_submenu, submenu);
233         for(++QuickMenu_Buffer_Index ; QuickMenu_Buffer_Index < QuickMenu_Buffer_Size; ++QuickMenu_Buffer_Index)
234         {
235                 string s = QuickMenu_Buffer_Get();
236                 if(substring(s, 0, 1) != QM_TAG_SUBMENU)
237                         continue;
238                 if(substring(s, 1, -1) == z_submenu) // submenu end
239                         break;
240                 QuickMenu_skip_submenu(substring(s, 1, -1));
241         }
242         strfree(z_submenu);
243 }
244
245 bool QuickMenu_IsOpened()
246 {
247         return (QuickMenu_Page_Entries > 0);
248 }
249
250 bool HUD_Quickmenu_PlayerListEntries_Create(string cmd, int teamplayers, bool without_me)
251 {
252         TC(int, teamplayers); TC(bool, without_me);
253         int i;
254         for(i = 0; i < QUICKMENU_MAXLINES; ++i)
255                 QuickMenu_Page_ClearEntry(i);
256         QuickMenu_Buffer_Close();
257
258         QuickMenu_Buffer = buf_create();
259         if(QuickMenu_Buffer < 0)
260                 return false;
261
262         HUD_Quickmenu_PlayerListEntries(cmd, teamplayers, without_me);
263
264         if(QuickMenu_Buffer_Size <= 0)
265         {
266                 buf_del(QuickMenu_Buffer);
267                 QuickMenu_Buffer = -1;
268                 return false;
269         }
270         return true;
271 }
272
273 // new_page 0 means page 0, new_page != 0 means next page
274 int QuickMenu_Buffer_Index_Prev;
275 bool QuickMenu_Page_Load(string target_submenu, bool new_page)
276 {
277         TC(bool, new_page);
278         string s = string_null, cmd = string_null, z_submenu;
279
280         QuickMenu_Page_ActivatedEntry = -1;
281         if (new_page == 0)
282                 QuickMenu_Page = 0;
283         else
284                 ++QuickMenu_Page;
285
286         z_submenu = strzone(target_submenu);
287         strcpy(QuickMenu_CurrentSubMenu, z_submenu);
288
289         QuickMenu_IsLastPage = true;
290         QuickMenu_Page_Entries = 0;
291
292         QuickMenu_Buffer_Index = 0;
293         if (z_submenu != "")
294         {
295                 // skip everything until the submenu open tag is found
296                 for( ; QuickMenu_Buffer_Index < QuickMenu_Buffer_Size; ++QuickMenu_Buffer_Index)
297                 {
298                         s = QuickMenu_Buffer_Get();
299                         if(substring(s, 0, 1) == QM_TAG_SUBMENU && substring(s, 1, -1) == z_submenu)
300                         {
301                                 //LOG_INFOF("^3 beginning of %s\n", z_submenu);
302                                 ++QuickMenu_Buffer_Index;
303                                 break; // target_submenu found!
304                         }
305                         //LOG_INFOF("^1 skipping %s\n", s);
306                 }
307                 if(QuickMenu_Buffer_Index == QuickMenu_Buffer_Size)
308                         LOG_WARNF("Couldn't find submenu \"%s\"", z_submenu);
309         }
310
311         // only the last page can contain up to QUICKMENU_MAXLINES entries
312         // the other ones contain only (QUICKMENU_MAXLINES - 2) entries
313         // so that the panel can show an empty row and "Continue..."
314         float first_entry = QuickMenu_Page * (QUICKMENU_MAXLINES - 2);
315         int entry_num = 0; // counts entries in target_submenu
316         for( ; QuickMenu_Buffer_Index < QuickMenu_Buffer_Size; ++QuickMenu_Buffer_Index)
317         {
318                 s = QuickMenu_Buffer_Get();
319
320                 if(z_submenu != "" && substring(s, 1, -1) == z_submenu)
321                 {
322                         //LOG_INFOF("^3 end of %s\n", z_submenu);
323                         break;
324                 }
325
326                 if(entry_num >= first_entry)
327                 {
328                         ++QuickMenu_Page_Entries;
329                         if(QuickMenu_Page_Entries == QUICKMENU_MAXLINES - 2)
330                                 QuickMenu_Buffer_Index_Prev = QuickMenu_Buffer_Index;
331                         else if(QuickMenu_Page_Entries == QUICKMENU_MAXLINES)
332                         {
333                                 QuickMenu_Page_ClearEntry(QUICKMENU_MAXLINES - 1);
334                                 QuickMenu_Buffer_Index = QuickMenu_Buffer_Index_Prev;
335                                 QuickMenu_IsLastPage = false;
336                                 break;
337                         }
338                 }
339
340                 // NOTE: entries are loaded starting from 1, not from 0
341                 if(substring(s, 0, 1) == QM_TAG_SUBMENU)
342                 {
343                         if(entry_num >= first_entry)
344                                 QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, substring(s, 1, -1), "");
345                         QuickMenu_skip_submenu(substring(s, 1, -1));
346                 }
347                 else if(substring(s, 0, 1) == QM_TAG_TITLE)
348                 {
349                         ++QuickMenu_Buffer_Index;
350                         if(entry_num >= first_entry)
351                         {
352                                 cmd = QuickMenu_Buffer_Get();
353                                 string command_code = substring(cmd, 0, 1);
354                                 if(command_code == QM_TAG_COMMAND)
355                                         cmd = substring(cmd, 1, -1);
356                                 else if(command_code == QM_TAG_PLCOMMAND)
357                                 {
358                                         // throw away the current quickmenu buffer and load a new one
359                                         cmd = substring(cmd, 1, -1);
360                                         strunzone(z_submenu);
361                                         if(HUD_Quickmenu_PlayerListEntries_Create(cmd, stof(substring(s, 1, 1)), stof(substring(s, 2, 1))))
362                                                 return QuickMenu_Page_Load("", 0);
363                                         QuickMenu_Close();
364                                         return false;
365                                 }
366
367                                 tokenize_console(cmd);
368                                 QuickMenu_Page_Command_Type[QuickMenu_Page_Entries] = (argv(1) && argv(0) == "toggle");
369
370                                 QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, substring(s, 1, -1), cmd);
371                         }
372
373                 }
374
375                 ++entry_num;
376         }
377         strunzone(z_submenu);
378         if (QuickMenu_Page_Entries == 0)
379         {
380                 QuickMenu_Close();
381                 return false;
382         }
383         QuickMenu_TimeOut_Set();
384         return true;
385 }
386
387 bool QuickMenu_ActionForNumber(int num)
388 {
389         TC(int, num);
390         if (!QuickMenu_IsLastPage)
391         {
392                 if (num < 0 || num >= QUICKMENU_MAXLINES)
393                         return false;
394                 if (num == QUICKMENU_MAXLINES - 1)
395                         return false;
396                 if (num == 0)
397                 {
398                         QuickMenu_Page_Load(QuickMenu_CurrentSubMenu, +1);
399                         return false;
400                 }
401         } else if (num <= 0 || num > QuickMenu_Page_Entries)
402                 return false;
403
404         if (QuickMenu_Page_Command[num] != "")
405         {
406                 QuickMenu_Page_ActivatedEntry_Time = time + 0.1;
407                 localcmd(strcat("\n", QuickMenu_Page_Command[num], "\n"));
408                 QuickMenu_TimeOut_Set();
409                 return true;
410         }
411         if (QuickMenu_Page_Description[num] != "")
412                 QuickMenu_Page_Load(QuickMenu_Page_Description[num], 0);
413         return false;
414 }
415
416 void QuickMenu_Page_ActiveEntry(int entry_num)
417 {
418         TC(int, entry_num);
419         QuickMenu_Page_ActivatedEntry = entry_num;
420         if(QuickMenu_Page_Command[QuickMenu_Page_ActivatedEntry])
421         {
422                 bool f = QuickMenu_ActionForNumber(QuickMenu_Page_ActivatedEntry);
423                 // toggle commands don't close the quickmenu
424                 if(QuickMenu_Page_Command_Type[QuickMenu_Page_ActivatedEntry] == 1)
425                         QuickMenu_Page_ActivatedEntry_Close = false;
426                 else
427                         QuickMenu_Page_ActivatedEntry_Close = (f && !(hudShiftState & S_CTRL));
428         }
429         else
430                 QuickMenu_Page_ActivatedEntry_Close = (!(hudShiftState & S_CTRL));
431 }
432
433 bool QuickMenu_InputEvent(int bInputType, float nPrimary, float nSecondary)
434 {
435         TC(int, bInputType);
436
437         if(!QuickMenu_IsOpened() || autocvar__hud_configure || mv_active)
438                 return false;
439
440         if(bInputType == 3)
441         {
442                 mousepos.x = nPrimary;
443                 mousepos.y = nSecondary;
444                 return true;
445         }
446
447         if(bInputType == 2)
448                 return false;
449
450         // at this point bInputType can only be 0 or 1 (key pressed or released)
451         bool key_pressed = (bInputType == 0);
452
453         int hudShiftState_prev = hudShiftState;
454         int mouseClicked_prev = mouseClicked;
455         if(key_pressed)
456         {
457                 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
458                 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
459                 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
460                 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
461                 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
462         }
463         else
464         {
465                 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
466                 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
467                 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
468                 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
469                 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
470         }
471
472         if(nPrimary == K_ESCAPE && key_pressed)
473         {
474                 QuickMenu_Close();
475         }
476         else if(nPrimary >= '0' && nPrimary <= '9' && key_pressed)
477         {
478                 QuickMenu_Page_ActiveEntry(stof(chr2str(nPrimary)));
479         }
480         else if (hudShiftState_prev == hudShiftState && mouseClicked_prev == mouseClicked)
481         {
482                 // allow console bind to work
483                 string con_keys = findkeysforcommand("toggleconsole", 0);
484                 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
485                 for (int i = 0; i < keys; ++i)
486                 {
487                         if(nPrimary == stof(argv(i)))
488                                 return false; // hit console bind
489                 }
490                 if (key_pressed)
491                         QuickMenu_Close();
492                 return false;
493         }
494
495         return true;
496 }
497
498 int entry_num_prev = 0;
499 void QuickMenu_Mouse()
500 {
501         if(mv_active) return;
502
503         if(!mouseClicked)
504         if(prevMouseClicked & S_MOUSE2)
505         {
506                 QuickMenu_Close();
507                 return;
508         }
509
510         panel = HUD_PANEL(QUICKMENU);
511         HUD_Panel_LoadCvars();
512
513         if(panel_bg_padding)
514         {
515                 panel_pos += '1 1 0' * panel_bg_padding;
516                 panel_size -= '2 2 0' * panel_bg_padding;
517         }
518
519         float first_entry_pos, entries_height;
520         vector fontsize;
521         fontsize = '1 1 0' * (panel_size.y / QUICKMENU_MAXLINES);
522         first_entry_pos = panel_pos.y + ((QUICKMENU_MAXLINES - QuickMenu_Page_Entries) * fontsize.y) / 2;
523         entries_height = panel_size.y - ((QUICKMENU_MAXLINES - QuickMenu_Page_Entries) * fontsize.y);
524
525         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)
526         {
527                 int entry_num = min(QuickMenu_Page_Entries - 1, floor((mousepos.y - first_entry_pos) / fontsize.y));
528                 if (entry_num != entry_num_prev)
529                 {
530                         QuickMenu_TimeOut_Set();
531                         entry_num_prev = entry_num;
532                 }
533                 if (QuickMenu_IsLastPage || entry_num != QUICKMENU_MAXLINES - 2)
534                 {
535                         if(!mouseClicked && (prevMouseClicked & S_MOUSE1))
536                                 QuickMenu_Page_ActiveEntry((entry_num < QUICKMENU_MAXLINES - 1) ? entry_num + 1 : 0);
537
538                         if (time > QuickMenu_Page_ActivatedEntry_Time)
539                         {
540                                 vector entry_pos = panel_pos;
541                                 entry_pos.y = first_entry_pos + entry_num * fontsize.y;
542                                 vector color;
543                                 if (mouseClicked & S_MOUSE1)
544                                         color = '0.5 1 0.5';
545                                 else if (hudShiftState & S_CTRL)
546                                         color = '1 1 0.3';
547                                 else
548                                         color = '1 1 1';
549                                 drawfill(entry_pos, vec2(panel_size.x, fontsize.y), color, .2, DRAWFLAG_NORMAL);
550                         }
551                 }
552         }
553 }
554
555 void HUD_Quickmenu_DrawEntry(vector pos, string desc, string option, vector fontsize)
556 {
557         string entry;
558         float offset;
559         float desc_width = panel_size.x;
560         if(option)
561         {
562                 string pic = strcat(hud_skin_path, "/", option);
563                 if(precache_pic(pic) == "")
564                         pic = strcat("gfx/hud/default/", option);
565                 vector option_size = '1 1 0' * fontsize.y * 0.8;
566                 desc_width -= option_size.x;
567                 drawpic(pos + vec2(desc_width, (fontsize.y - option_size.y) / 2), pic, option_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE);
568                 desc_width -= fontsize.x / 4;
569         }
570         entry = textShortenToWidth(desc, desc_width, fontsize, stringwidth_colors);
571         if (autocvar_hud_panel_quickmenu_align > 0)
572         {
573                 float real_desc_width = stringwidth_colors(entry, fontsize);
574                 offset = (desc_width - real_desc_width) * min(autocvar_hud_panel_quickmenu_align, 1);
575
576                 if(option)
577                 {
578                         // when there's enough room align description regardless the checkbox
579                         float extra_offset = (panel_size.x - desc_width) * min(autocvar_hud_panel_quickmenu_align, 1);
580                         if(offset + real_desc_width + extra_offset < desc_width)
581                                 offset += extra_offset;
582                         else
583                                 offset = max(0, desc_width - real_desc_width);
584                 }
585                 drawcolorcodedstring(pos + eX * offset, entry, fontsize, panel_fg_alpha, DRAWFLAG_ADDITIVE);
586         }
587         else
588                 drawcolorcodedstring(pos, entry, fontsize, panel_fg_alpha, DRAWFLAG_ADDITIVE);
589 }
590
591 void HUD_QuickMenu()
592 {
593         if(!autocvar__hud_configure)
594         {
595                 if (!hud_draw_maximized || !QuickMenu_IsOpened())
596                         return;
597
598                 if (HUD_QuickMenu_Forbidden())
599                 {
600                         QuickMenu_Close();
601                         return;
602                 }
603         }
604         else
605         {
606                 if(!QuickMenu_IsOpened())
607                 {
608                         QuickMenu_Page_Entries = 1;
609                         QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, sprintf(_("Submenu%d"), QuickMenu_Page_Entries), "");
610                         ++QuickMenu_Page_Entries;
611                         QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, sprintf(_("Submenu%d"), QuickMenu_Page_Entries), "");
612                         ++QuickMenu_Page_Entries;
613                         // although real command doesn't matter here, it must not be empty
614                         // otherwise the entry is displayed like a submenu
615                         for (; QuickMenu_Page_Entries < QUICKMENU_MAXLINES - 1; ++QuickMenu_Page_Entries)
616                                 QuickMenu_Page_LoadEntry(QuickMenu_Page_Entries, sprintf(_("Command%d"), QuickMenu_Page_Entries), "-");
617                         ++QuickMenu_Page_Entries;
618                         QuickMenu_Page_ClearEntry(QuickMenu_Page_Entries);
619                         QuickMenu_IsLastPage = false;
620                 }
621         }
622
623         HUD_Panel_LoadCvars();
624
625         HUD_Scale_Disable();
626         HUD_Panel_DrawBg();
627
628         if(panel_bg_padding)
629         {
630                 panel_pos += '1 1 0' * panel_bg_padding;
631                 panel_size -= '2 2 0' * panel_bg_padding;
632         }
633
634         int i;
635         vector fontsize;
636         string color;
637         fontsize = '1 1 0' * (panel_size.y / QUICKMENU_MAXLINES);
638
639         if (!QuickMenu_IsLastPage)
640         {
641                 color = "^5";
642                 HUD_Quickmenu_DrawEntry(panel_pos + eY * (panel_size.y - fontsize.y), sprintf("%d: %s%s", 0, color, _("Continue...")), string_null, fontsize);
643         }
644         else
645                 panel_pos.y += ((QUICKMENU_MAXLINES - QuickMenu_Page_Entries) * fontsize.y) / 2;
646
647         for (i = 1; i <= QuickMenu_Page_Entries; ++i) {
648                 if (QuickMenu_Page_Description[i] == "")
649                         break;
650                 string option = string_null;
651                 if (QuickMenu_Page_Command[i] == "")
652                         color = "^4";
653                 else
654                 {
655                         color = "^3";
656                         if(QuickMenu_Page_Command_Type[i] == 1) // toggle command
657                         {
658                                 int end = strstrofs(QuickMenu_Page_Command[i], ";", 0);
659                                 if(end < 0)
660                                         tokenize_console(QuickMenu_Page_Command[i]);
661                                 else
662                                         tokenize_console(substring(QuickMenu_Page_Command[i], 0, end));
663
664                                 if(argv(1) && argv(0) == "toggle")
665                                 {
666                                         // "enable feature xxx" "toggle xxx" (or "toggle xxx 1 0")
667                                         // "disable feature xxx" "toggle xxx 0 1"
668                                         float ON_value = 1, OFF_value = 0;
669                                         if(argv(2))
670                                                 ON_value = stof(argv(2));
671
672                                         if(argv(3))
673                                                 OFF_value = stof(argv(3));
674                                         else
675                                                 OFF_value = !ON_value;
676
677                                         float value = cvar(argv(1));
678                                         if(value == ON_value)
679                                                 option = "checkbox_checked";
680                                         else if(value == OFF_value)
681                                                 option = "checkbox_empty";
682                                         else
683                                                 option = "checkbox_undefined";
684                                 }
685                         }
686                 }
687                 HUD_Quickmenu_DrawEntry(panel_pos, sprintf("%d: %s%s", i, color, QuickMenu_Page_Description[i]), option, fontsize);
688
689                 if (time < QuickMenu_Page_ActivatedEntry_Time && QuickMenu_Page_ActivatedEntry == i)
690                         drawfill(panel_pos, vec2(panel_size.x, fontsize.y), '0.5 1 0.5', .2, DRAWFLAG_NORMAL);
691
692                 panel_pos.y += fontsize.y;
693         }
694
695         if(QuickMenu_Page_ActivatedEntry >= 0 && time >= QuickMenu_Page_ActivatedEntry_Time)
696         {
697                 if(!QuickMenu_Page_Command[QuickMenu_Page_ActivatedEntry])
698                 {
699                         bool f = QuickMenu_ActionForNumber(QuickMenu_Page_ActivatedEntry);
700                         if(f && QuickMenu_Page_ActivatedEntry_Close)
701                                 QuickMenu_Close();
702                 }
703                 else if(QuickMenu_Page_ActivatedEntry_Close)
704                         QuickMenu_Close();
705                 QuickMenu_Page_ActivatedEntry = -1;
706                 QuickMenu_Page_ActivatedEntry_Time = 0;
707         }
708 }
709
710
711 #define QUICKMENU_SMENU(submenu,eng_submenu) { \
712         if(target_submenu == eng_submenu && target_submenu_found) \
713                 return; /* target_submenu entries are now loaded, exit */ \
714         if(QuickMenu_Buffer_Size < QUICKMENU_BUFFER_MAXENTRIES) \
715                 QuickMenu_Buffer_Set(QM_TAG_SUBMENU, submenu); \
716         ++QuickMenu_Buffer_Size; \
717         if(target_submenu == eng_submenu && !target_submenu_found) { \
718                 QuickMenu_Buffer_Size = 0; /* enable load of next entries */ \
719                 target_submenu_found = true; \
720         } \
721 }
722
723 #define QUICKMENU_ENTRY(title,command) { \
724         if(QuickMenu_Buffer_Size + 1 < QUICKMENU_BUFFER_MAXENTRIES) \
725         { \
726                 QuickMenu_Buffer_Set(QM_TAG_TITLE, title); \
727                 ++QuickMenu_Buffer_Size; \
728                 QuickMenu_Buffer_Set(QM_TAG_COMMAND, command); \
729         } \
730         ++QuickMenu_Buffer_Size; \
731 }
732
733 #define QUICKMENU_SMENU_PL(submenu,eng_submenu,command,teamplayers,without_me) { \
734         if(QuickMenu_Buffer_Size + 3 < QUICKMENU_BUFFER_MAXENTRIES) {\
735                 QUICKMENU_SMENU(submenu,eng_submenu) \
736                 QuickMenu_Buffer_Set(QM_TAG_TITLE, strcat(ftos(teamplayers), ftos(without_me))); \
737                 ++QuickMenu_Buffer_Size; \
738                 QuickMenu_Buffer_Set(QM_TAG_PLCOMMAND, command); \
739                 ++QuickMenu_Buffer_Size; \
740                 QUICKMENU_SMENU(submenu,eng_submenu) \
741         } \
742 }
743
744
745
746 // useful to Translate a string inside the Command
747 #define QUICKMENU_ENTRY_TC(title,command,text,translated_text) {\
748         if(prvm_language == "en") { \
749                 tc_title = title; \
750                 tc_cmd = sprintf(command, text); \
751         } \
752         else if(!autocvar_hud_panel_quickmenu_translatecommands || translated_text == text) { \
753                 tc_title = strcat("(en)", title); \
754                 tc_cmd = sprintf(command, text); \
755         } \
756         else { \
757                 tc_title = strcat("(", prvm_language, ")", title); \
758                 tc_cmd = sprintf(command, translated_text); \
759         } \
760         QUICKMENU_ENTRY(tc_title, tc_cmd) \
761 }
762
763 void HUD_Quickmenu_PlayerListEntries(string cmd, int teamplayers, bool without_me)
764 {
765         TC(int, teamplayers); TC(bool, without_me);
766         entity pl;
767         if(teamplayers && !team_count)
768                 return;
769
770         Scoreboard_UpdatePlayerTeams();
771         for(pl = players.sort_next; pl; pl = pl.sort_next)
772         {
773                 if(teamplayers == 1 && (pl.team != myteam || pl.team == NUM_SPECTATOR)) // only own team players
774                         continue;
775                 if(teamplayers == 2 && (pl.team == myteam || pl.team == NUM_SPECTATOR)) // only enemy team players
776                         continue;
777                 if(without_me && pl.sv_entnum == player_localnum)
778                         continue;
779                 QUICKMENU_ENTRY(strcat("^7", entcs_GetName(pl.sv_entnum)), sprintf(cmd, entcs_GetName(pl.sv_entnum)))
780         }
781
782         return;
783 }
784
785
786 // Specifying target_submenu, this function only loads entries inside target_submenu
787 // NOTE: alternatively we could have loaded the whole default quickmenu and
788 // then called QuickMenu_Page_Load(target_submenu, 0);
789 // but this sytem is more reliable since we can always refer to target_submenu
790 // with the English title even if a translation is active
791 void QuickMenu_Default(string target_submenu)
792 {
793         bool target_submenu_found = false;
794         if(target_submenu != "")
795                 QuickMenu_Buffer_Size = QUICKMENU_BUFFER_MAXENTRIES; // forbids load of next entries until target_submenu
796
797         string tc_title;
798         string tc_cmd;
799
800         QUICKMENU_SMENU(_("Chat"), "Chat")
801                 QUICKMENU_SMENU_PL(CTX(_("QMCMD^Send public message to")), "Send public message to", "commandmode say %s:^7", 0, 1)
802                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^nice one")), "say %s", ":-) / nice one", CTX(_("QMCMD^:-) / nice one")))
803                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^good game")), "say %s", "good game", CTX(_("QMCMD^good game")))
804                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^hi / good luck")), "say %s", "hi / good luck and have fun", CTX(_("QMCMD^hi / good luck and have fun")))
805                 if(prvm_language != "en")
806                 QUICKMENU_ENTRY(CTX(_("QMCMD^Send in English")), "toggle hud_panel_quickmenu_translatecommands 0 1; quickmenu; wait; quickmenu default Chat")
807         QUICKMENU_SMENU(_("Chat"), "Chat")
808
809         if(teamplay)
810         {
811         QUICKMENU_SMENU(CTX(_("QMCMD^Team chat")), "Team chat")
812                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^strength soon")), "say_team %s", "strength soon", CTX(_("QMCMD^strength soon")))
813                 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)")))
814                 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)")))
815                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^negative")), "say_team %s", "negative", CTX(_("QMCMD^negative")))
816                 QUICKMENU_ENTRY_TC(CTX(_("QMCMD^positive")), "say_team %s", "positive", CTX(_("QMCMD^positive")))
817                 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)")))
818                 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)")))
819                 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)")))
820                 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)")))
821                 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)")))
822                 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)")))
823                 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)")))
824                 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)")))
825                 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)")))
826                 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)")))
827         QUICKMENU_SMENU(CTX(_("QMCMD^Team chat")), "Team chat")
828         }
829
830         QUICKMENU_SMENU_PL(CTX(_("QMCMD^Send private message to")), "Send private message to", "commandmode tell \"%s^7\"", 0, 1)
831
832         QUICKMENU_SMENU(CTX(_("QMCMD^Settings")), "Settings")
833                 QUICKMENU_SMENU(CTX(_("QMCMD^View/HUD settings")), "View/HUD settings")
834                         QUICKMENU_ENTRY(CTX(_("QMCMD^3rd person view")), "toggle chase_active")
835                         QUICKMENU_ENTRY(CTX(_("QMCMD^Player models like mine")), "toggle cl_forceplayermodels")
836                         QUICKMENU_ENTRY(CTX(_("QMCMD^Names above players")), "toggle hud_shownames")
837                         QUICKMENU_ENTRY(CTX(_("QMCMD^Crosshair per weapon")), "toggle crosshair_per_weapon")
838                         QUICKMENU_ENTRY(CTX(_("QMCMD^FPS")), "toggle hud_panel_engineinfo")
839                         QUICKMENU_ENTRY(CTX(_("QMCMD^Net graph")), "toggle shownetgraph")
840                 QUICKMENU_SMENU(CTX(_("QMCMD^View/HUD settings")), "View/HUD settings")
841
842                 QUICKMENU_SMENU(CTX(_("QMCMD^Sound settings")), "Sound settings")
843                         QUICKMENU_ENTRY(CTX(_("QMCMD^Hit sound")), "toggle cl_hitsound")
844                         QUICKMENU_ENTRY(CTX(_("QMCMD^Chat sound")), "toggle con_chatsound")
845                 QUICKMENU_SMENU(CTX(_("QMCMD^Sound settings")), "Sound settings")
846
847                 if(spectatee_status > 0)
848                 {
849                 QUICKMENU_ENTRY(CTX(_("QMCMD^Change spectator camera")), "dropweapon")
850                 }
851
852                 if(spectatee_status == -1)
853                 {
854                 QUICKMENU_SMENU(CTX(_("QMCMD^Observer camera")), "Observer camera")
855                         QUICKMENU_ENTRY(CTX(_("QMCMD^Increase speed")), "weapnext")
856                         QUICKMENU_ENTRY(CTX(_("QMCMD^Decrease speed")), "weapprev")
857                         QUICKMENU_ENTRY(CTX(_("QMCMD^Wall collision")), "toggle cl_clippedspectating")
858                 QUICKMENU_SMENU(CTX(_("QMCMD^Observer camera")), "Observer camera")
859                 }
860
861                 QUICKMENU_ENTRY(CTX(_("QMCMD^Fullscreen")), "toggle vid_fullscreen; vid_restart")
862         QUICKMENU_SMENU(CTX(_("QMCMD^Settings")), "Settings")
863
864         QUICKMENU_SMENU(CTX(_("QMCMD^Call a vote")), "Call a vote")
865                 QUICKMENU_ENTRY(CTX(_("QMCMD^Restart the map")), "vcall restart")
866                 QUICKMENU_ENTRY(CTX(_("QMCMD^End match")), "vcall endmatch")
867                 if(STAT(TIMELIMIT) > 0)
868                 {
869                 QUICKMENU_ENTRY(CTX(_("QMCMD^Reduce match time")), "vcall reducematchtime")
870                 QUICKMENU_ENTRY(CTX(_("QMCMD^Extend match time")), "vcall extendmatchtime")
871                 }
872                 if(teamplay)
873                 QUICKMENU_ENTRY(CTX(_("QMCMD^Shuffle teams")), "vcall shuffleteams")
874         QUICKMENU_SMENU(CTX(_("QMCMD^Call a vote")), "Call a vote")
875
876         if(spectatee_status != 0)
877         {
878         QUICKMENU_SMENU_PL(CTX(_("QMCMD^Spectate a player")), "Spectate a player", "spectate \"%s^7\"", 0, 1)
879         }
880
881         if(target_submenu != "" && !target_submenu_found)
882         {
883                 LOG_INFOF("Couldn't find submenu \"%s\"", target_submenu);
884                 if(prvm_language != "en")
885                         LOG_INFO("^3Warning: submenu title must be in English");
886                 QuickMenu_Buffer_Size = 0;
887         }
888 }
889 #undef QUICKMENU_SMENU
890 #undef QUICKMENU_ENTRY
891 #undef QUICKMENU_ENTRY_TC