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