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