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