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