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