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