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