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