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