get rid of some dead code
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / menu.qc
1 ///////////////////////////////////////////////
2 // Menu Source File
3 ///////////////////////
4 // This file belongs to dpmod/darkplaces
5 // AK contains all menu functions (especially the required ones)
6 ///////////////////////////////////////////////
7
8 float mouseButtonsPressed;
9 vector menuMousePos;
10 float menuShiftState;
11 float menuPrevTime;
12 float menuAlpha;
13 float menuLogoAlpha;
14 float prevMenuAlpha;
15 float menuInitialized;
16 float menuNotTheFirstFrame;
17 float menuMouseMode;
18
19 void SUB_Null() { };
20
21 void() m_init =
22 {
23         cvar_set("_menu_alpha", "0");
24
25         check_unacceptable_compiler_bugs();
26
27 #ifdef WATERMARK
28         print("^4MQC Build information: ", WATERMARK(), "\n");
29 #endif
30
31         // list all game dirs (TEST)
32         if(cvar("developer"))
33         {
34                 float i;
35                 string s;
36                 for(i = 0; ; ++i)
37                 {
38                         s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
39                         if not(s)
40                                 break;
41                         dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
42                 }
43         }
44 }
45
46 float MENU_ASPECT = 1.25; // 1280x1024
47 float MENU_MINHEIGHT = 600;
48 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
49 void draw_reset_cropped()
50 {
51         draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
52 }
53 void draw_reset_full()
54 {
55         draw_reset(realconwidth, realconheight, 0, 0);
56 }
57 void UpdateConWidthHeight()
58 {
59         conwidth_s = conwidth;
60         conheight_s = conheight;
61         realconwidth = cvar("vid_conwidth");
62         realconheight = cvar("vid_conheight");
63         if(realconwidth / realconheight > MENU_ASPECT)
64         {
65                 // widescreen
66                 conwidth = realconheight * MENU_ASPECT;
67                 conheight = realconheight;
68         }
69         else
70         {
71                 // squarescreen
72                 conwidth = realconwidth;
73                 conheight = realconwidth / MENU_ASPECT;
74         }
75         screenconwidth = conwidth;
76         screenconheight = conheight;
77         if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
78         {
79                 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
80                 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
81         }
82         if(conheight < MENU_MINHEIGHT)
83         {
84                 conwidth *= MENU_MINHEIGHT / conheight;
85                 conheight = MENU_MINHEIGHT;
86         }
87         if(main)
88         {
89                 if(conwidth_s != conwidth || conheight_s != conheight)
90                 {
91                         draw_reset_cropped();
92                         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
93                 }
94         }
95 }
96
97 void() m_init_delayed =
98 {
99         float fh, glob, n, i;
100         string s;
101
102         conwidth = conheight = -1;
103         UpdateConWidthHeight();
104         draw_reset_cropped();
105
106         menuInitialized = 0;
107         if(!preMenuInit())
108                 return;
109         menuInitialized = 1;
110         GameCommand_Init();
111
112         RegisterWeapons();
113
114         fh = -1;
115         if(cvar_string("menu_skin") != "")
116         {
117                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
118                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
119         }
120         if(fh < 0)
121         if(cvar_defstring("menu_skin") != "")
122         {
123                 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
124                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
125         }
126         if(fh < 0)
127         {
128                 draw_currentSkin = "gfx/menu/default";
129                 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
130         }
131         draw_currentSkin = strzone(draw_currentSkin);
132         while((s = fgets(fh)))
133         {
134                 // these two are handled by skinlist.qc
135                 if(substring(s, 0, 6) == "title ")
136                         continue;
137                 if(substring(s, 0, 7) == "author ")
138                         continue;
139                 n = tokenize_console(s);
140                 if(n >= 2)
141                         Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
142         }
143         fclose(fh);
144
145         glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
146         if(glob >= 0)
147         {
148                 n = search_getsize(glob);
149                 for(i = 0; i < n; ++i)
150                         precache_pic(search_getfilename(glob, i));
151                 search_end(glob);
152         }
153
154         draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
155
156         loadTooltips();
157         anim = spawnAnimHost();
158         main = spawnMainWindow(); main.configureMainWindow(main);
159         unloadTooltips();
160
161         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
162         main.focused = 1;
163         menuShiftState = 0;
164         menuMousePos = '0.5 0.5 0';
165
166         if(Menu_Active)
167                 m_display(); // delayed menu display
168 };
169
170 void(float key, float ascii) m_keyup =
171 {
172         if(!menuInitialized)
173                 return;
174         if(!Menu_Active)
175                 return;
176         draw_reset_cropped();
177         main.keyUp(main, key, ascii, menuShiftState);
178         if(key >= K_MOUSE1 && key <= K_MOUSE3)
179         {
180                 --mouseButtonsPressed;
181                 if(!mouseButtonsPressed)
182                         main.mouseRelease(main, menuMousePos);
183                 if(mouseButtonsPressed < 0)
184                 {
185                         mouseButtonsPressed = 0;
186                         dprint("Warning: released an already released button\n");
187                 }
188         }
189         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
190         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
191         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
192 };
193
194 void(float key, float ascii) m_keydown =
195 {
196         if(!menuInitialized)
197                 return;
198         if(!Menu_Active)
199                 return;
200         if(keyGrabber)
201         {
202                 entity e;
203                 e = keyGrabber;
204                 keyGrabber = NULL;
205                 e.keyGrabbed(e, key, ascii);
206         }
207         else
208         {
209                 draw_reset_cropped();
210                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
211                         if(!mouseButtonsPressed)
212                                 main.mousePress(main, menuMousePos);
213                 if(!main.keyDown(main, key, ascii, menuShiftState))
214                         if(key == K_ESCAPE)
215                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
216                                         m_hide(); // disable menu on unhandled ESC
217         }
218         if(key >= K_MOUSE1 && key <= K_MOUSE3)
219         {
220                 ++mouseButtonsPressed;
221                 if(mouseButtonsPressed > 10)
222                 {
223                         mouseButtonsPressed = 10;
224                         dprint("Warning: pressed an already pressed button\n");
225                 }
226         }
227         if(key == K_ALT) menuShiftState |= S_ALT;
228         if(key == K_CTRL) menuShiftState |= S_CTRL;
229         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
230 };
231
232 float SCALEMODE_CROP = 0;
233 float SCALEMODE_LETTERBOX = 1;
234 float SCALEMODE_WIDTH = 2;
235 float SCALEMODE_HEIGHT = 3;
236 float SCALEMODE_STRETCH = 4;
237 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
238 {
239         vector sz, org, isz, isz_w, isz_h;
240         float width_is_larger;
241
242         sz = draw_PictureSize(img);
243         width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
244         isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y)); 
245         isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x)); 
246
247         switch(scalemode)
248         {
249                 default:
250                 case SCALEMODE_CROP:
251                         isz = (width_is_larger ? isz_h : isz_w);
252                         break;
253                 case SCALEMODE_LETTERBOX:
254                         isz = (width_is_larger ? isz_w : isz_h);
255                         break;
256                 case SCALEMODE_WIDTH:
257                         isz = isz_w;
258                         break;
259                 case SCALEMODE_HEIGHT:
260                         isz = isz_h;
261                         break;
262                 case SCALEMODE_STRETCH:
263                         isz = '1 1 0';
264                         break;
265         }
266
267         org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
268         draw_Picture(org, img, isz, '1 1 1', a);
269 }
270
271 void(string img, float a, string algn, float force1) drawBackground =
272 {
273         if(main.mainNexposee.ModalController_state == 0)
274                 return;
275
276         vector v;
277         float i, l;
278         string c;
279         float scalemode;
280
281         v_z = 0;
282
283         scalemode = SCALEMODE_CROP;
284
285         for(i = 0; i < strlen(algn); ++i)
286         {
287                 c = substring(algn, i, 1);
288                 switch(c)
289                 {
290                         case "c": scalemode = SCALEMODE_CROP; goto nopic;
291                         case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
292                         case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
293                         case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
294                         case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
295                         case "1": case "4": case "7": v_x = 0.0; break;
296                         case "2": case "5": case "8": v_x = 0.5; break;
297                         case "3": case "6": case "9": v_x = 1.0; break;
298                         default: v_x = random(); break;
299                 }
300                 switch(c)
301                 {
302                         case "7": case "8": case "9": v_y = 0.0; break;
303                         case "4": case "5": case "6": v_y = 0.5; break;
304                         case "1": case "2": case "3": v_y = 1.0; break;
305                         default: v_y = random(); break;
306                 }
307                 if(l == 0)
308                         draw_Picture_Aligned(v, scalemode, img, a);
309                 else if(force1)
310                         // force all secondary layers to use alpha 1. Prevents ugly issues
311                         // with overlap. It's a flag because it cannot be used for the
312                         // ingame background
313                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
314                 else
315                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
316                 ++l;
317 :nopic
318         }
319 }
320
321 vector menuTooltipAveragedMousePos;
322 entity menuTooltipItem;
323 vector menuTooltipOrigin;
324 vector menuTooltipSize;
325 float menuTooltipAlpha;
326 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
327 float m_testmousetooltipbox(vector pos)
328 {
329         if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
330         if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
331                 return FALSE;
332         return TRUE;
333 }
334 float m_testtooltipbox(vector tooltippos)
335 {
336         if(tooltippos_x < 0)
337                 return FALSE;
338         if(tooltippos_y < 0)
339                 return FALSE;
340         if(tooltippos_x + menuTooltipSize_x > 1)
341                 return FALSE;
342         if(tooltippos_y + menuTooltipSize_y > 1)
343                 return FALSE;
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, draw_TextWidth_WithoutColors);
469                                                 ++i;
470                                                 f = draw_TextWidth(s, FALSE, fontsize);
471                                                 if(f > w)
472                                                         w = f;
473                                         }
474                                         menuTooltipSize_x = w + 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, 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 (anim)
546                 anim.tickAll(anim);
547
548         if(main)
549                 UpdateConWidthHeight();
550
551         if(!menuInitialized)
552         {
553                 // TODO draw an info image about this situation
554                 m_init_delayed();
555                 return;
556         }
557         if(!menuNotTheFirstFrame)
558         {
559                 menuNotTheFirstFrame = 1;
560                 if(Menu_Active)
561                 if(!cvar("menu_video_played"))
562                 {
563                         localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
564                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
565                 }
566         }
567
568         t = gettime();
569         realFrametime = frametime = min(0.2, t - menuPrevTime);
570         menuPrevTime = t;
571         time += frametime;
572
573         t = cvar("menu_slowmo");
574         if(t)
575         {
576                 frametime *= t;
577                 realFrametime *= t;
578         }
579         else
580                 t = 1;
581
582         if(Menu_Active)
583         {
584                 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
585                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
586                 else
587                         m_hide();
588         }
589
590         if(cvar("cl_capturevideo"))
591                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
592
593         gamestatus = 0;
594         if(isserver())
595                 gamestatus = gamestatus | GAME_ISSERVER;
596         if(clientstate() == CS_CONNECTED)
597                 gamestatus = gamestatus | GAME_CONNECTED;
598         if(cvar("developer"))
599                 gamestatus = gamestatus | GAME_DEVELOPER;
600
601         prevMenuAlpha = menuAlpha;
602         if(Menu_Active)
603         {
604                 if(menuAlpha == 0 && menuLogoAlpha < 2)
605                 {
606                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
607                 }
608                 else
609                 {
610                         menuAlpha = min(1, menuAlpha + frametime * 5);
611                         menuLogoAlpha = 2;
612                 }
613         }
614         else
615         {
616                 menuAlpha = max(0, menuAlpha - frametime * 5);
617                 menuLogoAlpha = 2;
618         }
619
620         draw_reset_cropped();
621
622         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
623         {
624                 if(menuLogoAlpha > 0)
625                 {
626                         draw_reset_full();
627                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
628                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
629                         draw_reset_cropped();
630                         if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
631                         {
632                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
633                                 draw_drawMousePointer(menuMousePos);
634                                 draw_alpha = 1;
635                         }
636                 }
637         }
638         else if(SKINALPHA_BACKGROUND_INGAME)
639         {
640                 if(menuAlpha > 0)
641                 {
642                         draw_reset_full();
643                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
644                         draw_reset_cropped();
645                 }
646         }
647
648         if(menuAlpha != prevMenuAlpha)
649                 cvar_set("_menu_alpha", ftos(menuAlpha));
650
651         draw_reset_cropped();
652         preMenuDraw();
653         draw_reset_cropped();
654
655         if(menuAlpha <= 0)
656         {
657                 if(prevMenuAlpha > 0)
658                         main.initializeDialog(main, main.firstChild);
659                 draw_reset_cropped();
660                 postMenuDraw();
661                 return;
662         }
663
664         draw_alpha *= menuAlpha;
665
666         if(menuMouseMode)
667         {
668                 vector newMouse;
669                 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
670                 if(newMouse != '0 0 0')
671                         if(newMouse != menuMousePos)
672                         {
673                                 menuMousePos = newMouse;
674                                 if(mouseButtonsPressed)
675                                         main.mouseDrag(main, menuMousePos);
676                                 else
677                                         main.mouseMove(main, menuMousePos);
678                         }
679         }
680         else
681         {
682                 if(frametime > 0)
683                 {
684                         vector dMouse, minpos, maxpos;
685                         dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
686                         if(dMouse != '0 0 0')
687                         {
688                                 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
689                                 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
690                                 dMouse = globalToBoxSize(dMouse, draw_scale);
691                                 menuMousePos += dMouse * cvar("menu_mouse_speed");
692                                 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
693                                 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
694                                 if(mouseButtonsPressed)
695                                         main.mouseDrag(main, menuMousePos);
696                                 else
697                                         main.mouseMove(main, menuMousePos);
698                         }
699                 }
700         }
701         main.draw(main);
702
703         m_tooltip(menuMousePos);
704
705         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
706
707         draw_drawMousePointer(menuMousePos);
708
709         draw_reset_cropped();
710         postMenuDraw();
711
712         frametime = 0;
713 };
714
715 void() m_display =
716 {
717         Menu_Active = true;
718         setkeydest(KEY_MENU);
719         setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
720
721         if(!menuInitialized)
722                 return;
723
724         if(mouseButtonsPressed)
725                 main.mouseRelease(main, menuMousePos);
726         mouseButtonsPressed = 0;
727
728         main.focusEnter(main);
729         main.showNotify(main);
730 };
731
732 void() m_hide =
733 {
734         Menu_Active = false;
735         setkeydest(KEY_GAME);
736         setmousetarget(MT_CLIENT);
737
738         if(!menuInitialized)
739                 return;
740
741         main.focusLeave(main);
742         main.hideNotify(main);
743 };
744
745 void() m_toggle =
746 {
747         if(Menu_Active)
748                 m_hide();
749         else
750                 m_display();
751 };
752
753 void() m_shutdown =
754 {
755         entity e;
756
757         m_hide();
758         for(e = NULL; (e = nextent(e)) != NULL; )
759         {
760                 if(e.classname != "vtbl")
761                         if(e.destroy)
762                                 e.destroy(e);
763         }
764 };
765
766 void m_focus_item_chain(entity outermost, entity innermost)
767 {
768         if(innermost.parent != outermost)
769                 m_focus_item_chain(outermost, innermost.parent);
770         innermost.parent.setFocus(innermost.parent, innermost);
771 }
772
773 void m_activate_window(entity wnd)
774 {
775         entity par;
776         par = wnd.parent;
777         if(par)
778                 m_activate_window(par);
779
780         if(par.instanceOfModalController)
781         {
782                 if(wnd.tabSelectingButton)
783                         // tabs
784                         TabButton_Click(wnd.tabSelectingButton, wnd);
785                 else
786                         // root
787                         par.initializeDialog(par, wnd);
788         }
789         else if(par.instanceOfNexposee)
790         {
791                 // nexposee (sorry for violating abstraction here)
792                 par.selectedChild = wnd;
793                 par.animationState = 1;
794                 Container_setFocus(par, NULL);
795         }
796         else if(par.instanceOfContainer)
797         {
798                 // other containers
799                 if(par.focused)
800                         par.setFocus(par, wnd);
801         }
802 }
803
804 void m_setpointerfocus(entity wnd)
805 {
806         if(wnd.instanceOfContainer)
807         {
808                 entity focus = wnd.preferredFocusedGrandChild(wnd);
809                 if(focus)
810                 {
811                         menuMousePos = focus.origin + 0.5 * focus.size;
812                         menuMousePos_x *= 1 / conwidth;
813                         menuMousePos_y *= 1 / conheight;
814                         if(wnd.focused) // why does this never happen?
815                                 m_focus_item_chain(wnd, focus);
816                 }
817         }
818 }
819
820 void(string itemname) m_goto =
821 {
822         entity e;
823         if(!menuInitialized)
824                 return;
825         if(itemname == "") // this can be called by GameCommand
826         {
827                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
828                         m_hide();
829                 else
830                 {
831                         m_activate_window(main.mainNexposee);
832                         m_display();
833                 }
834         }
835         else
836         {
837                 e = findstring(NULL, name, itemname);
838                 if(e)
839                 {
840                         m_hide();
841                         m_activate_window(e);
842                         m_setpointerfocus(e);
843                         m_display();
844                 }
845         }
846 }
847
848 void() m_goto_skin_selector =
849 {
850         if(!menuInitialized)
851                 return;
852         // TODO add code to switch back to the skin selector (no idea how to do it now)
853         m_goto("skinselector");
854 }
855
856 void() m_goto_video_settings =
857 {
858         if(!menuInitialized)
859                 return;
860         // TODO add code to switch back to the skin selector (no idea how to do it now)
861         m_goto("videosettings");
862 }