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