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