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