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