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