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