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