]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/menu.qc
WIP of a simple general purpose animation framework for the menu. uncomment those...
[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         anim = spawnAnimHost();
154         main = spawnMainWindow(); main.configureMainWindow(main);
155         unloadTooltips();
156
157         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
158         main.focused = 1;
159         menuShiftState = 0;
160         menuMousePos = '0.5 0.5 0';
161
162         if(Menu_Active)
163                 m_display(); // delayed menu display
164 };
165
166 void(float key, float ascii) m_keyup =
167 {
168         if(!menuInitialized)
169                 return;
170         if(!Menu_Active)
171                 return;
172         draw_reset_cropped();
173         main.keyUp(main, key, ascii, menuShiftState);
174         if(key >= K_MOUSE1 && key <= K_MOUSE3)
175         {
176                 --mouseButtonsPressed;
177                 if(!mouseButtonsPressed)
178                         main.mouseRelease(main, menuMousePos);
179                 if(mouseButtonsPressed < 0)
180                 {
181                         mouseButtonsPressed = 0;
182                         print("Warning: released an already released button\n");
183                 }
184         }
185         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
186         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
187         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
188 };
189
190 void(float key, float ascii) m_keydown =
191 {
192         if(!menuInitialized)
193                 return;
194         if(!Menu_Active)
195                 return;
196         if(keyGrabber)
197         {
198                 entity e;
199                 e = keyGrabber;
200                 keyGrabber = NULL;
201                 e.keyGrabbed(e, key, ascii);
202         }
203         else
204         {
205                 draw_reset_cropped();
206                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
207                         if(!mouseButtonsPressed)
208                                 main.mousePress(main, menuMousePos);
209                 if(!main.keyDown(main, key, ascii, menuShiftState))
210                         if(key == K_ESCAPE)
211                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
212                                         m_hide(); // disable menu on unhandled ESC
213         }
214         if(key >= K_MOUSE1 && key <= K_MOUSE3)
215         {
216                 ++mouseButtonsPressed;
217                 if(mouseButtonsPressed > 10)
218                 {
219                         mouseButtonsPressed = 10;
220                         print("Warning: pressed an already pressed button\n");
221                 }
222         }
223         if(key == K_ALT) menuShiftState |= S_ALT;
224         if(key == K_CTRL) menuShiftState |= S_CTRL;
225         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
226 };
227
228 float SCALEMODE_CROP = 0;
229 float SCALEMODE_LETTERBOX = 1;
230 float SCALEMODE_WIDTH = 2;
231 float SCALEMODE_HEIGHT = 3;
232 float SCALEMODE_STRETCH = 4;
233 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
234 {
235         vector sz, org, isz, isz_w, isz_h;
236         float width_is_larger;
237
238         sz = draw_PictureSize(img);
239         width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
240         isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y)); 
241         isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x)); 
242
243         switch(scalemode)
244         {
245                 default:
246                 case SCALEMODE_CROP:
247                         isz = (width_is_larger ? isz_h : isz_w);
248                         break;
249                 case SCALEMODE_LETTERBOX:
250                         isz = (width_is_larger ? isz_w : isz_h);
251                         break;
252                 case SCALEMODE_WIDTH:
253                         isz = isz_w;
254                         break;
255                 case SCALEMODE_HEIGHT:
256                         isz = isz_h;
257                         break;
258                 case SCALEMODE_STRETCH:
259                         isz = '1 1 0';
260                         break;
261         }
262
263         org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
264         draw_Picture(org, img, isz, '1 1 1', a);
265 }
266
267 void(string img, float a, string algn, float force1) drawBackground =
268 {
269         vector v;
270         float i, l;
271         string c;
272         float scalemode;
273
274         v_z = 0;
275
276         scalemode = SCALEMODE_CROP;
277
278         for(i = 0; i < strlen(algn); ++i)
279         {
280                 c = substring(algn, i, 1);
281                 switch(c)
282                 {
283                         case "c": scalemode = SCALEMODE_CROP; goto nopic;
284                         case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
285                         case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
286                         case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
287                         case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
288                         case "1": case "4": case "7": v_x = 0.0; break;
289                         case "2": case "5": case "8": v_x = 0.5; break;
290                         case "3": case "6": case "9": v_x = 1.0; break;
291                         default: v_x = random(); break;
292                 }
293                 switch(c)
294                 {
295                         case "7": case "8": case "9": v_y = 0.0; break;
296                         case "4": case "5": case "6": v_y = 0.5; break;
297                         case "1": case "2": case "3": v_y = 1.0; break;
298                         default: v_y = random(); break;
299                 }
300                 if(l == 0)
301                         draw_Picture_Aligned(v, scalemode, img, a);
302                 else if(force1)
303                         // force all secondary layers to use alpha 1. Prevents ugly issues
304                         // with overlap. It's a flag because it cannot be used for the
305                         // ingame background
306                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
307                 else
308                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
309                 ++l;
310 :nopic
311         }
312 }
313
314 vector menuTooltipAveragedMousePos;
315 entity menuTooltipItem;
316 vector menuTooltipOrigin;
317 vector menuTooltipSize;
318 float menuTooltipAlpha;
319 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
320 float m_testmousetooltipbox(vector pos)
321 {
322         if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
323         if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
324                 return FALSE;
325         return TRUE;
326 }
327 float m_testtooltipbox(vector tooltippos)
328 {
329         if(tooltippos_x < 0)
330                 return FALSE;
331         if(tooltippos_y < 0)
332                 return FALSE;
333         if(tooltippos_x + menuTooltipSize_x > 1)
334                 return FALSE;
335         if(tooltippos_y + menuTooltipSize_y > 1)
336                 return FALSE;
337         /*
338         menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
339         menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
340         menuTooltipOrigin_z = 0;
341         */
342         menuTooltipOrigin = tooltippos;
343         return TRUE;
344 }
345 float m_allocatetooltipbox(vector pos)
346 {
347         vector avoidplus, avoidminus;
348         vector v;
349
350         avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
351         avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
352         avoidplus_z = 0;
353
354         avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
355         avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
356         avoidminus_z = 0;
357
358         // bottom right
359         v = pos + avoidplus;
360         if(m_testtooltipbox(v))
361                 return TRUE;
362         
363         // bottom center
364         v_x = pos_x - menuTooltipSize_x * 0.5;
365         if(m_testtooltipbox(v))
366                 return TRUE;
367
368         // bottom left
369         v_x = pos_x - avoidminus_x;
370         if(m_testtooltipbox(v))
371                 return TRUE;
372
373         // top left
374         v_y = pos_y - avoidminus_y;
375         if(m_testtooltipbox(v))
376                 return TRUE;
377
378         // top center
379         v_x = pos_x - menuTooltipSize_x * 0.5;
380         if(m_testtooltipbox(v))
381                 return TRUE;
382         
383         // top right
384         v_x = pos_x + avoidplus_x;
385         if(m_testtooltipbox(v))
386                 return TRUE;
387         
388         return FALSE;
389 }
390 entity m_findtooltipitem(entity root, vector pos)
391 {
392         entity it;
393         entity best;
394
395         best = world;
396         it = root;
397
398         while(it.instanceOfContainer)
399         {
400                 while(it.instanceOfNexposee && it.focusedChild)
401                 {
402                         it = it.focusedChild;
403                         pos = globalToBox(pos, it.Container_origin, it.Container_size);
404                 }
405                 if(it.instanceOfNexposee)
406                 {
407                         it = it.itemFromPoint(it, pos);
408                         if(it.tooltip)
409                                 best = it;
410                         it = world;
411                 }
412                 else if(it.instanceOfModalController)
413                         it = it.focusedChild;
414                 else
415                         it = it.itemFromPoint(it, pos);
416                 if(!it)
417                         break;
418                 if(it.tooltip)
419                         best = it;
420                 pos = globalToBox(pos, it.Container_origin, it.Container_size);
421         }
422
423         return best;
424 }
425 void m_tooltip(vector pos)
426 {
427         float f, i, w;
428         entity it;
429         vector fontsize, p;
430         string s;
431
432         fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
433
434         f = bound(0, frametime * 2, 1);
435         menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
436         f = vlen(pos - menuTooltipAveragedMousePos);
437
438         if(f < 0.01)
439                 it = m_findtooltipitem(main, pos);
440         else    
441                 it = world;
442
443         // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
444         if(it != menuTooltipItem)
445         {
446                 switch(menuTooltipState)
447                 {
448                         case 0:
449                                 if(menuTooltipItem)
450                                 {
451                                         // another item: fade out first
452                                         menuTooltipState = 2;
453                                 }
454                                 else
455                                 {
456                                         // new item: fade in
457                                         menuTooltipState = 1;
458                                         menuTooltipItem = it;
459
460                                         menuTooltipOrigin_x = -1; // unallocated
461                                         i = 0;
462                                         w =  0;
463                                         getWrappedLine_remaining = it.tooltip;
464                                         while(getWrappedLine_remaining)
465                                         {
466                                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
467                                                 ++i;
468                                                 f = draw_TextWidth(s, FALSE, fontsize);
469                                                 if(f > w)
470                                                         w = f;
471                                         }
472                                         menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
473                                         menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
474                                         menuTooltipSize_z = 0;
475                                 }
476                                 break;
477                         case 1:
478                                 // changing item while fading in: fade out first
479                                 menuTooltipState = 2;
480                                 break;
481                         case 2:
482                                 // changing item while fading out: can't
483                                 break;
484                 }
485         }
486         else if(menuTooltipState == 2) // re-fade in?
487                 menuTooltipState = 1;
488
489         if(menuTooltipItem)
490                 if(!m_testmousetooltipbox(pos))
491                         menuTooltipState = 2; // fade out if mouse touches it
492
493         switch(menuTooltipState)
494         {
495                 case 1:
496                         menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
497                         if(menuTooltipAlpha == 1)
498                                 menuTooltipState = 0;
499                         break;
500                 case 2:
501                         menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
502                         if(menuTooltipAlpha == 0)
503                         {
504                                 menuTooltipState = 0;
505                                 menuTooltipItem = world;
506                         }
507                         break;
508         }
509
510         if(menuTooltipItem)
511         {
512                 if(menuTooltipOrigin_x < 0) // unallocated?
513                         m_allocatetooltipbox(pos);
514
515                 if(menuTooltipOrigin_x >= 0)
516                 {
517                         // draw the tooltip!
518                         p = SKINBORDER_TOOLTIP;
519                         p_x *= 1 / conwidth;
520                         p_y *= 1 / conheight;
521                         draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
522                         p = menuTooltipOrigin;
523                         p_x += SKINMARGIN_TOOLTIP_x / conwidth;
524                         p_y += SKINMARGIN_TOOLTIP_y / conheight;
525                         getWrappedLine_remaining = menuTooltipItem.tooltip;
526                         while(getWrappedLine_remaining)
527                         {
528                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
529                                 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
530                                 p_y += fontsize_y;
531                         }
532                 }
533         }
534 }
535
536 void() m_draw =
537 {
538         float t;
539         float realFrametime;
540
541         menuMouseMode = cvar("menu_mouse_absolute");
542
543         if (anim)
544                 anim.tickAll(anim);
545
546         if(main)
547                 UpdateConWidthHeight();
548
549         if(!menuInitialized)
550         {
551                 // TODO draw an info image about this situation
552                 m_init_delayed();
553                 return;
554         }
555         if(!menuNotTheFirstFrame)
556         {
557                 menuNotTheFirstFrame = 1;
558                 if(Menu_Active)
559                 if(!cvar("menu_video_played"))
560                 {
561                         localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
562                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
563                 }
564         }
565
566         t = gettime();
567         realFrametime = frametime = min(0.2, t - menuPrevTime);
568         menuPrevTime = t;
569         time += frametime;
570
571         t = cvar("menu_slowmo");
572         if(t)
573         {
574                 frametime *= t;
575                 realFrametime *= t;
576         }
577         else
578                 t = 1;
579
580         if(Menu_Active)
581         {
582                 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
583                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
584                 else
585                         m_hide();
586         }
587
588         if(cvar("cl_capturevideo"))
589                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
590
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 }