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