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