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