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