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