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