]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/menu.qc
re-allow main menu to play music when coming back to it
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / menu.qc
1 #include "menu.qh"
2
3 #include "item.qh"
4
5 #include "anim/animhost.qh"
6
7 #include "item/dialog.qh"
8 #include "item/listbox.qh"
9 #include "item/nexposee.qh"
10
11 #include "xonotic/commandbutton.qh"
12 #include "xonotic/mainwindow.qh"
13 #include "xonotic/serverlist.qh"
14 #include "xonotic/slider_resolution.qh"
15
16 .string controlledCvar;
17
18 #include "xonotic/util.qh"
19
20 #include <common/items/_mod.qh>
21 #include <common/weapons/_all.qh>
22 #include <common/mapinfo.qh>
23 #include <common/mutators/base.qh>
24
25 int mouseButtonsPressed;
26 vector menuMousePos;
27 int menuShiftState;
28 float menuPrevTime;
29 float menuAlpha;
30 float menuLogoAlpha;
31 float prevMenuAlpha;
32 bool menuInitialized;
33 int menuMouseMode;
34
35 // Used for having effects only execute once in main menu, not for every reload
36 // 0: never been in main menu before. 1: coming back to main menu. 2: in main menu.
37 int menuNotTheFirstFrame;
38 bool autocvar_menu_no_music_nor_welcome;
39
40 float conwidth_s, conheight_s;
41 float vidwidth_s, vidheight_s, vidpixelheight_s;
42 float realconwidth, realconheight;
43
44 void m_sync()
45 {
46         updateCompression();
47         vidwidth_s = vidheight_s = vidpixelheight_s = 0;  // Force updateConwidths on next draw
48
49         loadAllCvars(main);
50 }
51
52 void m_gamestatus()
53 {
54         gamestatus = 0;
55         if (isserver()) gamestatus |= GAME_ISSERVER;
56         if (clientstate() == CS_CONNECTED || isdemo()) gamestatus |= GAME_CONNECTED;
57         if (cvar("developer") > 0) gamestatus |= GAME_DEVELOPER;
58 }
59
60 void m_init()
61 {
62         bool restarting = false;
63         cvar_set("_menu_alpha", "0");
64         prvm_language = cvar_string("prvm_language");
65         if (prvm_language == "")
66         {
67                 prvm_language = "en";
68                 cvar_set("prvm_language", prvm_language);
69                 localcmd("\nmenu_restart\n");
70                 restarting = true;
71         }
72         prvm_language = strzone(prvm_language);
73         cvar_set("_menu_prvm_language", prvm_language);
74
75 #ifdef WATERMARK
76                 LOG_TRACEF("^4MQC Build information: ^1%s", WATERMARK);
77 #endif
78
79         // list all game dirs (TEST)
80         if (cvar("developer") > 0)
81         {
82                 for (int i = 0; ; ++i)
83                 {
84                         string s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
85                         if (!s) break;
86                         LOG_TRACE(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
87                 }
88         }
89
90         registercvar("_menu_cmd_closemenu_available", "0", 0);
91         cvar_set("_menu_cmd_closemenu_available", "1");
92
93         // needs to be done so early because of the constants they create
94         static_init();
95         static_init_late();
96         static_init_precache();
97
98         RegisterSLCategories();
99
100         float ddsload = cvar("r_texture_dds_load");
101         float texcomp = cvar("gl_texturecompression");
102         updateCompression();
103         if (ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression")) localcmd("\nr_restart\n");
104
105         if (!restarting)
106         {
107                 if (cvar("_menu_initialized"))  // always show menu after menu_restart
108                         m_display();
109                 else m_hide();
110                 cvar_set("_menu_initialized", "1");
111         }
112 }
113
114 const float MENU_ASPECT = 1280 / 1024;
115
116 void draw_reset_cropped()
117 {
118         draw_reset(conwidth, conheight, 0.5 * (realconwidth - conwidth), 0.5 * (realconheight - conheight));
119 }
120 void draw_reset_full()
121 {
122         draw_reset(realconwidth, realconheight, 0, 0);
123 }
124
125 void UpdateConWidthHeight(float w, float h, float p)
126 {
127         if (w != vidwidth_s || h != vidheight_s || p != vidpixelheight_s)
128         {
129                 if (updateConwidths(w, h, p)) localcmd(sprintf("\nexec %s\n", cvar_string("menu_font_cfg")));
130                 vidwidth_s = w;
131                 vidheight_s = h;
132                 vidpixelheight_s = p;
133         }
134         conwidth_s = conwidth;
135         conheight_s = conheight;
136         realconwidth = cvar("vid_conwidth");
137         realconheight = cvar("vid_conheight");
138         if (realconwidth / realconheight > MENU_ASPECT)
139         {
140                 // widescreen
141                 conwidth = realconheight * MENU_ASPECT;
142                 conheight = realconheight;
143         }
144         else
145         {
146                 // squarescreen
147                 conwidth = realconwidth;
148                 conheight = realconwidth / MENU_ASPECT;
149         }
150         if (main)
151         {
152                 if (conwidth_s != conwidth || conheight_s != conheight)
153                 {
154                         draw_reset_cropped();
155                         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
156                 }
157         }
158         else
159         {
160                 vidwidth_s = vidheight_s = vidpixelheight_s = 0;  // retry next frame
161         }
162 }
163
164 string m_goto_buffer;
165 void m_init_delayed()
166 {
167         draw_reset_cropped();
168
169         menuInitialized = false;
170         if (!preMenuInit()) return;
171         menuInitialized = true;
172
173         int fh = -1;
174         if (cvar_string("menu_skin") != "")
175         {
176                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
177                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
178         }
179         if (fh < 0 && cvar_defstring("menu_skin") != "")
180         {
181                 cvar_set("menu_skin", cvar_defstring("menu_skin"));
182                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
183                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
184         }
185         if (fh < 0)
186         {
187                 draw_currentSkin = "gfx/menu/default";
188                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
189         }
190         if (fh < 0) error("cannot load any menu skin\n");
191         draw_currentSkin = strzone(draw_currentSkin);
192         for (string s; (s = fgets(fh)); )
193         {
194                 // these two are handled by skinlist.qc
195                 if (substring(s, 0, 6) == "title ") continue;
196                 if (substring(s, 0, 7) == "author ") continue;
197                 int n = tokenize_console(s);
198                 if (n < 2) continue;
199                 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
200         }
201         fclose(fh);
202
203         int glob = search_begin(strcat(draw_currentSkin, "/*.tga"), true, true);
204         if (glob >= 0)
205         {
206                 for (int i = 0, n = search_getsize(glob); i < n; ++i)
207                         precache_pic(search_getfilename(glob, i));
208                 search_end(glob);
209         }
210
211         draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
212
213         anim = NEW(AnimHost);
214         main = NEW(MainWindow);
215         main.configureMainWindow(main);
216
217         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
218         main.focused = true;
219         menuShiftState = 0;
220         menuMousePos = '0.5 0.5 0';
221
222         m_sync();
223
224         if (m_goto_buffer)
225         {
226                 m_goto(m_goto_buffer);
227                 strfree(m_goto_buffer);
228         }
229
230         if (Menu_Active) m_display();  // delayed menu display
231
232         cvar_set("_menu_initialized", "2");
233 }
234
235 void m_keyup(float key, float ascii)
236 {
237         if (!menuInitialized) return;
238         if (!Menu_Active) return;
239         draw_reset_cropped();
240         main.keyUp(main, key, ascii, menuShiftState);
241         if (key >= K_MOUSE1 && key <= K_MOUSE3)
242         {
243                 --mouseButtonsPressed;
244                 if (!mouseButtonsPressed) main.mouseRelease(main, menuMousePos);
245                 if (mouseButtonsPressed < 0)
246                 {
247                         mouseButtonsPressed = 0;
248                         LOG_TRACE("Warning: released an already released button");
249                 }
250         }
251         if (key == K_ALT) menuShiftState &= ~S_ALT;
252         if (key == K_CTRL) menuShiftState &= ~S_CTRL;
253         if (key == K_SHIFT) menuShiftState &= ~S_SHIFT;
254 }
255
256 void m_keydown(float key, float ascii)
257 {
258         if (!menuInitialized) return;
259         if (!Menu_Active) return;
260
261         if (menuMouseMode && key >= K_MOUSE1 && key <= K_MOUSE3)
262         {
263                 // detect a click outside of the game window
264                 vector p = getmousepos();
265                 if (p.x < 0 || p.x > realconwidth || p.y < 0 || p.y > realconheight)
266                 {
267                         ++mouseButtonsPressed;
268                         return;
269                 }
270         }
271
272         if (keyGrabber)
273         {
274                 entity e = keyGrabber;
275                 keyGrabber = NULL;
276                 e.keyGrabbed(e, key, ascii);
277         }
278         else
279         {
280                 draw_reset_cropped();
281                 if (!mouseButtonsPressed && key >= K_MOUSE1 && key <= K_MOUSE3)
282                         main.mousePress(main, menuMousePos);
283                 if (!main.keyDown(main, key, ascii, menuShiftState))
284                 {
285                         // disable menu on unhandled ESC
286                         if (key == K_ESCAPE)
287                                 if (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))  // don't back out to console only
288                                         m_hide();
289                 }
290         }
291         if (key >= K_MOUSE1 && key <= K_MOUSE3)
292         {
293                 ++mouseButtonsPressed;
294                 if (mouseButtonsPressed > 10)
295                 {
296                         mouseButtonsPressed = 10;
297                         LOG_TRACE("Warning: pressed an already pressed button");
298                 }
299         }
300         if (key == K_ALT) menuShiftState |= S_ALT;
301         if (key == K_CTRL) menuShiftState |= S_CTRL;
302         if (key == K_SHIFT) menuShiftState |= S_SHIFT;
303 }
304
305 enum {
306         SCALEMODE_CROP,
307         SCALEMODE_LETTERBOX,
308         SCALEMODE_WIDTH,
309         SCALEMODE_HEIGHT,
310         SCALEMODE_STRETCH,
311 };
312 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
313 {
314         vector sz = draw_PictureSize(img);
315         bool width_is_larger = (sz.x * draw_scale.y >= sz.y * draw_scale.x);
316         vector isz_w = '1 0 0' + '0 1 0' * ((sz.y / sz.x) * (draw_scale.x / draw_scale.y));
317         vector isz_h = '0 1 0' + '1 0 0' * ((sz.x / sz.y) * (draw_scale.y / draw_scale.x));
318         vector isz;
319         switch (scalemode)
320         {
321                 default:
322                 case SCALEMODE_CROP:
323                         isz = (width_is_larger ? isz_h : isz_w);
324                         break;
325                 case SCALEMODE_LETTERBOX:
326                         isz = (width_is_larger ? isz_w : isz_h);
327                         break;
328                 case SCALEMODE_WIDTH:
329                         isz = isz_w;
330                         break;
331                 case SCALEMODE_HEIGHT:
332                         isz = isz_h;
333                         break;
334                 case SCALEMODE_STRETCH:
335                         isz = '1 1 0';
336                         break;
337         }
338         vector org = eX * (algn.x * (1 - isz.x)) + eY * (algn.y * (1 - isz.y));
339         draw_Picture(org, img, isz, '1 1 1', a);
340 }
341
342 void drawBackground(string img, float a, string algn, float force1)
343 {
344         if (main.mainNexposee.ModalController_state == 0) return;
345         vector v = '0 0 0';
346         int scalemode = SCALEMODE_CROP;
347         int len = strlen(algn);
348         for (int i = 0, l = 0; i < len; ++i)
349         {
350                 string c = substring(algn, i, 1);
351                 switch (c)
352                 {
353                         case "c":
354                                 scalemode = SCALEMODE_CROP;
355                                 goto nopic;
356                         case "l":
357                                 scalemode = SCALEMODE_LETTERBOX;
358                                 goto nopic;
359                         case "h":
360                                 scalemode = SCALEMODE_HEIGHT;
361                                 goto nopic;
362                         case "w":
363                                 scalemode = SCALEMODE_WIDTH;
364                                 goto nopic;
365                         case "s":
366                                 scalemode = SCALEMODE_STRETCH;
367                                 goto nopic;
368                         case "1": case "4": case "7":
369                                 v.x = 0.0;
370                                 break;
371                         case "2": case "5": case "8":
372                                 v.x = 0.5;
373                                 break;
374                         case "3": case "6": case "9":
375                                 v.x = 1.0;
376                                 break;
377                         default:
378                                 v.x = random();
379                                 break;
380                 }
381                 switch (c)
382                 {
383                         case "7": case "8": case "9":
384                                 v.y = 0.0;
385                                 break;
386                         case "4": case "5": case "6":
387                                 v.y = 0.5;
388                                 break;
389                         case "1": case "2": case "3":
390                                 v.y = 1.0;
391                                 break;
392                         default:
393                                 v.y = random();
394                                 break;
395                 }
396                 if (l == 0)
397                 {
398                         draw_Picture_Aligned(v, scalemode, img, a);
399                 }
400                 else if (force1)
401                 {
402                         // force all secondary layers to use alpha 1. Prevents ugly issues
403                         // with overlap. It's a flag because it cannot be used for the
404                         // ingame background
405                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l + 1)), 1);
406                 }
407                 else
408                 {
409                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l + 1)), a);
410                 }
411                 ++l;
412 LABEL(nopic)
413         }
414 }
415
416 int menu_tooltips;
417 int menu_tooltips_old;
418 vector menuTooltipAveragedMousePos;
419 entity menuTooltipItem;
420 vector menuTooltipOrigin;
421 vector menuTooltipSize;
422 float menuTooltipAlpha;
423 string menuTooltipText;
424 int menuTooltipState;  // 0: static, 1: fading in, 2: fading out, 3: forced fading out
425 bool m_testmousetooltipbox(vector pos)
426 {
427         return !(
428             (pos.x >= menuTooltipOrigin.x && pos.x < menuTooltipOrigin.x + menuTooltipSize.x)
429             && (pos.y >= menuTooltipOrigin.y && pos.y < menuTooltipOrigin.y + menuTooltipSize.y)
430                 );
431 }
432 bool m_testtooltipbox(vector tooltippos)
433 {
434         if (tooltippos.x < 0) return false;
435         if (tooltippos.y < 0) return false;
436         if (tooltippos.x + menuTooltipSize.x > 1) return false;
437         if (tooltippos.y + menuTooltipSize.y > 1) return false;
438         menuTooltipOrigin = tooltippos;
439         return true;
440 }
441 bool m_allocatetooltipbox(vector pos)
442 {
443         vector avoidplus;
444         avoidplus.x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
445         avoidplus.y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
446         avoidplus.z = 0;
447
448         vector avoidminus;
449         avoidminus.x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize.x;
450         avoidminus.y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize.y;
451         avoidminus.z = 0;
452
453         // bottom right
454         vector v = pos + avoidplus;
455         if (m_testtooltipbox(v)) return true;
456
457         // bottom center
458         v.x = pos.x - menuTooltipSize.x * 0.5;
459         if (m_testtooltipbox(v)) return true;
460
461         // bottom left
462         v.x = pos.x - avoidminus.x;
463         if (m_testtooltipbox(v)) return true;
464
465         // top left
466         v.y = pos.y - avoidminus.y;
467         if (m_testtooltipbox(v)) return true;
468
469         // top center
470         v.x = pos.x - menuTooltipSize.x * 0.5;
471         if (m_testtooltipbox(v)) return true;
472
473         // top right
474         v.x = pos.x + avoidplus.x;
475         if (m_testtooltipbox(v)) return true;
476
477         return false;
478 }
479 entity m_findtooltipitem(entity root, vector pos)
480 {
481         entity best = NULL;
482         for (entity it = root; it.instanceOfContainer; )
483         {
484                 while (it.instanceOfNexposee && it.focusedChild)
485                 {
486                         it = it.focusedChild;
487                         pos = globalToBox(pos, it.Container_origin, it.Container_size);
488                 }
489                 if (it.instanceOfNexposee)
490                 {
491                         it = it.itemFromPoint(it, pos);
492                         if (it.tooltip) best = it;
493                         else if (menu_tooltips == 2 && (it.controlledCvar || it.onClickCommand)) best = it;
494                         it = NULL;
495                 }
496                 else if (it.instanceOfModalController)
497                 {
498                         it = it.focusedChild;
499                 }
500                 else
501                 {
502                         it = it.itemFromPoint(it, pos);
503                 }
504                 if (!it) break;
505                 if (it.tooltip) best = it;
506                 else if (menu_tooltips == 2 && (it.controlledCvar || it.onClickCommand)) best = it;
507                 pos = globalToBox(pos, it.Container_origin, it.Container_size);
508         }
509
510         return best;
511 }
512 string gettooltip()
513 {
514         if (menu_tooltips == 2)
515         {
516                 string s;
517                 if (menuTooltipItem.controlledCvar)
518                 {
519                         string cvar_list = getCvarsMulti(menuTooltipItem);
520                         if (cvar_list)
521                                 cvar_list = strcat(menuTooltipItem.controlledCvar, " ", cvar_list);
522                         else
523                                 cvar_list = menuTooltipItem.controlledCvar;
524                         s = strcat("[", cvar_list, " \"", cvar_string(menuTooltipItem.controlledCvar), "\"]");
525                 }
526                 else if (menuTooltipItem.onClickCommand)
527                 {
528                         s = strcat("<", menuTooltipItem.onClickCommand, ">");
529                 }
530                 else
531                 {
532                         return menuTooltipItem.tooltip;
533                 }
534                 if (menuTooltipItem.tooltip) return strcat(menuTooltipItem.tooltip, " ", s);
535                 return s;
536         }
537         return menuTooltipItem.tooltip;
538 }
539 void m_tooltip(vector pos)
540 {
541         static string prev_tooltip;
542         entity it;
543         menu_tooltips = cvar("menu_tooltips");
544         if (!menu_tooltips)
545         {
546                 // don't return immediately, fade out the active tooltip first
547                 if (menuTooltipItem == NULL) return;
548                 it = NULL;
549                 menu_tooltips_old = menu_tooltips;
550         }
551         else
552         {
553                 float f = bound(0, frametime * 2, 1);
554                 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
555                 if (vdist(pos - menuTooltipAveragedMousePos, <, 0.01))
556                 {
557                         it = m_findtooltipitem(main, pos);
558
559                         if (it.instanceOfListBox && it.isScrolling(it)) it = NULL;
560
561                         if (it && prev_tooltip != it.tooltip)
562                         {
563                                 // fade out if tooltip of a certain item has changed
564                                 menuTooltipState = 3;
565                                 strcpy(prev_tooltip, it.tooltip);
566                         }
567                         else if (menuTooltipItem && !m_testmousetooltipbox(pos))
568                         {
569                                 menuTooltipState = 3;  // fade out if mouse touches it
570                         }
571                 }
572                 else
573                 {
574                         it = NULL;
575                 }
576         }
577         vector fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
578
579         // float menuTooltipState; // 0: static, 1: fading in, 2: fading out, 3: forced fading out
580         if (it != menuTooltipItem)
581         {
582                 switch (menuTooltipState)
583                 {
584                         case 0:
585                                 if (menuTooltipItem)
586                                 {
587                                         // another item: fade out first
588                                         menuTooltipState = 2;
589                                 }
590                                 else
591                                 {
592                                         // new item: fade in
593                                         menuTooltipState = 1;
594                                         menuTooltipItem = it;
595
596                                         menuTooltipOrigin.x = -1;  // unallocated
597
598                                         strcpy(menuTooltipText, gettooltip());
599
600                                         int i = 0;
601                                         float w = 0;
602                                         for (getWrappedLine_remaining = menuTooltipText; getWrappedLine_remaining && i <= 16; ++i)
603                                         {
604                                                 string s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
605                                                 if (i == 16)
606                                                         s = "...";
607                                                 float f = draw_TextWidth(s, false, fontsize);
608                                                 if (f > w) w = f;
609                                         }
610                                         menuTooltipSize.x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
611                                         menuTooltipSize.y = i * fontsize.y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
612                                         menuTooltipSize.z = 0;
613                                 }
614                                 break;
615                         case 1:
616                                 // changing item while fading in: fade out first
617                                 menuTooltipState = 2;
618                                 break;
619                         case 2:
620                                 // changing item while fading out: can't
621                                 break;
622                 }
623         }
624         else if (menuTooltipState == 2)  // re-fade in?
625         {
626                 menuTooltipState = 1;
627         }
628
629         switch (menuTooltipState)
630         {
631                 case 1:  // fade in
632                         menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
633                         if (menuTooltipAlpha == 1) menuTooltipState = 0;
634                         break;
635                 case 2:  // fade out
636                 case 3:  // forced fade out
637                         menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
638                         if (menuTooltipAlpha == 0)
639                         {
640                                 menuTooltipState = 0;
641                                 menuTooltipItem = NULL;
642                         }
643                         break;
644         }
645
646         if (menuTooltipItem == NULL)
647         {
648                 strfree(menuTooltipText);
649                 return;
650         }
651         else
652         {
653                 if (menu_tooltips != menu_tooltips_old)
654                 {
655                         if (menu_tooltips != 0 && menu_tooltips_old != 0) menuTooltipItem = NULL; // reload tooltip next frame
656                         menu_tooltips_old = menu_tooltips;
657                 }
658                 else if (menuTooltipOrigin.x < 0)                                             // unallocated?
659                 {
660                         m_allocatetooltipbox(pos);
661                 }
662                 if (menuTooltipOrigin.x >= 0)
663                 {
664                         // draw the tooltip!
665                         vector p = SKINBORDER_TOOLTIP;
666                         p.x *= 1 / conwidth;
667                         p.y *= 1 / conheight;
668                         draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
669                         p = menuTooltipOrigin;
670                         p.x += SKINMARGIN_TOOLTIP_x / conwidth;
671                         p.y += SKINMARGIN_TOOLTIP_y / conheight;
672                         int i = 0;
673                         for (getWrappedLine_remaining = menuTooltipText; getWrappedLine_remaining && i <= 16; ++i, p.y += fontsize.y)
674                         {
675                                 string s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
676                                 if (i == 16)
677                                         s = "...";
678                                 draw_Text(p, s, fontsize, SKINCOLOR_TOOLTIP, SKINALPHA_TOOLTIP * menuTooltipAlpha, false);
679                         }
680                 }
681         }
682 }
683
684 const int MIN_DISCONNECTION_TIME = 1;
685 bool autocvar_g_campaign;
686 void m_draw(float width, float height)
687 {
688         static float connected_time;
689         if (clientstate() == CS_DISCONNECTED)
690         {
691                 // avoid a bug where the main menu re-opens when changing maps
692                 // potentially exclusive to `map <mapname>` cmd?
693                 if (connected_time && time - connected_time > MIN_DISCONNECTION_TIME)
694                 {
695                         if (autocvar_g_campaign)
696                         {
697                                 // in the case player uses the disconnect command (in the console or with a key)
698                                 // reset g_campaign and update menu items to reflect cvar values that may have been restored after quiting the campaign
699                                 // see also LEAVEMATCH_CMD
700                                 cvar_set("g_campaign", "0");
701                                 m_sync();
702                         }
703
704                         // reload the menu so that disconnecting players don't
705                         // have to press ESC to open it again
706                         m_toggle(true);
707
708                         localcmd("\nmenu_cmd directmenu Welcome RESET\n");
709                         connected_time = 0;
710
711                         // reset main menu
712                         // FIXME?: find out if anything should be done to reset it more,
713                         // this is just a fix to make main menu music replay nicely
714                         menuNotTheFirstFrame = 1;
715                 }
716         }
717         else
718                 connected_time = time;
719
720         m_gamestatus();
721
722         execute_next_frame();
723
724         menuMouseMode = cvar("menu_mouse_absolute");
725
726         if (anim) anim.tickAll(anim);
727
728         UpdateConWidthHeight(width, height, cvar("vid_pixelheight"));
729
730         if (!menuInitialized)
731         {
732                 // TODO draw an info image about this situation
733                 m_init_delayed();
734                 return;
735         }
736
737         if (menuNotTheFirstFrame == 0) // only fade the menu in once ever
738                 menuLogoAlpha = -0.8;  // no idea why, but when I start this at zero, it jumps instead of fading FIXME
739
740         if (menuNotTheFirstFrame <= 1) // only once per menu reload
741         {
742                 if (Menu_Active && !autocvar_menu_no_music_nor_welcome)
743                 {
744                         localcmd("cd loop $menu_cdtrack\n");
745
746                         // TODO: enable this when we have a welcome sound
747                         // FIXME: change the file used according to the selected announcer
748                         // Only play the welcome announcement once, not on any menu reloads
749                         //if (menuNotTheFirstFrame == 0)
750                         //localcmd("play sound/announcer/default/welcome.wav\n");
751                 }
752
753                 menuNotTheFirstFrame = 2;
754         }
755
756         float t = gettime();
757         float realFrametime = frametime = min(0.2, t - menuPrevTime);
758         menuPrevTime = t;
759         time += frametime;
760
761         t = cvar("menu_slowmo");
762         if (t)
763         {
764                 frametime *= t;
765                 realFrametime *= t;
766         }
767         else
768         {
769                 t = 1;
770         }
771
772         if (Menu_Active)
773         {
774                 if (getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU)
775                     && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
776                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
777                 else m_hide();
778         }
779
780         if (cvar("cl_capturevideo")) frametime = t / cvar("cl_capturevideo_fps");  // make capturevideo work smoothly
781
782         prevMenuAlpha = menuAlpha;
783         if (Menu_Active)
784         {
785                 if (menuAlpha == 0 && menuLogoAlpha < 2)
786                 {
787                         menuLogoAlpha += 2 * frametime;
788                 }
789                 else
790                 {
791                         menuAlpha = min(1, menuAlpha + 5 * frametime);
792                         menuLogoAlpha = 2;
793                 }
794         }
795         else
796         {
797                 menuAlpha = max(0, menuAlpha - 5 * frametime);
798                 menuLogoAlpha = 2;
799         }
800
801         draw_reset_cropped();
802
803         if (!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
804         {
805                 if (menuLogoAlpha > 0)
806                 {
807                         draw_reset_full();
808                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
809                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, true);
810                         draw_reset_cropped();
811                         if (menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
812                         {
813                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
814                                 draw_drawMousePointer(menuMousePos);
815                                 draw_alpha = 1;
816                         }
817                 }
818         }
819         else if (SKINALPHA_BACKGROUND_INGAME)
820         {
821                 if (menuAlpha > 0)
822                 {
823                         draw_reset_full();
824                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME,
825                                 SKINALIGN_BACKGROUND_INGAME, false);
826                         draw_reset_cropped();
827                 }
828         }
829
830         if (menuAlpha != prevMenuAlpha) cvar_set("_menu_alpha", ftos(menuAlpha));
831
832         draw_reset_cropped();
833         preMenuDraw();
834         draw_reset_cropped();
835
836         if (menuAlpha <= 0)
837         {
838                 if (prevMenuAlpha > 0) main.initializeDialog(main, main.firstChild);
839                 draw_reset_cropped();
840                 postMenuDraw();
841                 return;
842         }
843
844         draw_alpha *= menuAlpha;
845
846         if (menuMouseMode)
847         {
848                 vector rawMousePos = getmousepos();
849                 vector newMouse = globalToBox(rawMousePos, draw_shift, draw_scale);
850                 if (rawMousePos != '0 0 0' && newMouse != menuMousePos)
851                 {
852                         menuMousePos = newMouse;
853                         if (mouseButtonsPressed) main.mouseDrag(main, menuMousePos);
854                         else main.mouseMove(main, menuMousePos);
855                 }
856         }
857         else if (frametime > 0)
858         {
859                 vector dMouse = getmousepos() * (frametime / realFrametime);  // for capturevideo
860                 if (dMouse != '0 0 0')
861                 {
862                         vector minpos = globalToBox('0 0 0', draw_shift, draw_scale);
863                         vector maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
864                         dMouse = globalToBoxSize(dMouse, draw_scale);
865                         menuMousePos += dMouse * cvar("menu_mouse_speed");
866                         menuMousePos.x = bound(minpos.x, menuMousePos.x, maxpos.x);
867                         menuMousePos.y = bound(minpos.y, menuMousePos.y, maxpos.y);
868                         if (mouseButtonsPressed) main.mouseDrag(main, menuMousePos);
869                         else main.mouseMove(main, menuMousePos);
870                 }
871         }
872         main.draw(main);
873
874         m_tooltip(menuMousePos);
875
876         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
877
878         draw_drawMousePointer(menuMousePos);
879
880         draw_reset_cropped();
881         postMenuDraw();
882
883         frametime = 0;
884         IL_ENDFRAME();
885 }
886
887 void m_display()
888 {
889         Menu_Active = true;
890         setkeydest(KEY_MENU);
891         setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
892
893         if (!menuInitialized) return;
894
895         if (mouseButtonsPressed) main.mouseRelease(main, menuMousePos);
896         mouseButtonsPressed = 0;
897
898         main.focusEnter(main);
899         main.showNotify(main);
900 }
901
902 void m_hide()
903 {
904         Menu_Active = false;
905         setkeydest(KEY_GAME);
906         setmousetarget(MT_CLIENT);
907
908         if (!menuInitialized) return;
909
910         main.focusLeave(main);
911         main.hideNotify(main);
912 }
913
914 void m_toggle(int mode)
915 {
916         if (Menu_Active)
917         {
918                 if (mode == 1) return;
919                 m_hide();
920         }
921         else
922         {
923                 if (mode == 0) return;
924                 m_display();
925         }
926 }
927
928 void Shutdown()
929 {
930         m_hide();
931         FOREACH_ENTITY_ORDERED(it.destroy, {
932                 if (it.classname == "vtbl") continue;
933                 it.destroy(it);
934         });
935         cvar_set("_menu_cmd_closemenu_available", "0");
936 }
937
938 void m_focus_item_chain(entity outermost, entity innermost)
939 {
940         if (innermost.parent != outermost) m_focus_item_chain(outermost, innermost.parent);
941         innermost.parent.setFocus(innermost.parent, innermost);
942 }
943
944 void m_activate_window(entity wnd)
945 {
946         entity par = wnd.parent;
947         if (par) m_activate_window(par);
948
949         if (par.instanceOfModalController)
950         {
951                 if (wnd.tabSelectingButton)
952                         // tabs
953                         TabButton_Click(wnd.tabSelectingButton, wnd);
954                 else
955                         // root
956                         par.initializeDialog(par, wnd);
957         }
958         else if (par.instanceOfNexposee)
959         {
960                 // nexposee (sorry for violating abstraction here)
961                 par.selectedChild = wnd;
962                 par.animationState = 1;
963                 Container_setFocus(par, NULL);
964         }
965         else if (par.instanceOfContainer)
966         {
967                 // other containers
968                 if (par.focused) par.setFocus(par, wnd);
969         }
970 }
971
972 void m_setpointerfocus(entity wnd)
973 {
974         if (!wnd.instanceOfContainer) return;
975         entity focus = wnd.preferredFocusedGrandChild(wnd);
976         if (!focus) return;
977         menuMousePos = focus.origin + 0.5 * focus.size;
978         menuMousePos.x *= 1 / conwidth;
979         menuMousePos.y *= 1 / conheight;
980         entity par = wnd.parent;
981         if (par.focused) par.setFocus(par, wnd);
982         if (wnd.focused) m_focus_item_chain(wnd, focus);
983 }
984
985 void m_goto(string itemname)
986 {
987         if (!menuInitialized)
988         {
989                 strcpy(m_goto_buffer, itemname);
990                 return;
991         }
992         if (itemname == "")  // this can be called by GameCommand
993         {
994                 if (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
995                 {
996                         m_hide();
997                         return;
998                 }
999                 itemname = "nexposee";
1000         }
1001
1002         if (itemname == "nexposee")
1003         {
1004                 // unlike 'togglemenu 1', this closes modal and root dialogs if opened
1005                 m_activate_window(main.mainNexposee);
1006                 m_display();
1007         }
1008         else
1009         {
1010                 entity e;
1011                 for (e = NULL; (e = find(e, name, itemname)); )
1012                         if (e.classname != "vtbl") break;
1013
1014                 if ((e) && (!e.requiresConnection || (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))))
1015                 {
1016                         if(!Menu_Active)
1017                                 e.hideMenuOnClose = true;
1018                         m_hide();
1019                         m_activate_window(e);
1020                         m_setpointerfocus(e);
1021                         m_display();
1022                 }
1023         }
1024 }
1025
1026 void m_play_focus_sound()
1027 {
1028         static float menuLastFocusSoundTime;
1029         if (cvar("menu_sounds") < 2) return;
1030         if (time - menuLastFocusSoundTime <= 0.25) return;
1031         localsound(MENU_SOUND_FOCUS);
1032         menuLastFocusSoundTime = time;
1033 }
1034
1035 void m_play_click_sound(string soundfile)
1036 {
1037         if (!cvar("menu_sounds")) return;
1038         localsound(soundfile);
1039 }