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