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