]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/menu.qc
Merge remote branch 'refs/remotes/origin/terencehill/bot_vs_human_fix'
[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                         print(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                         print("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                         print("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         vector v;
274         float i, l;
275         string c;
276         float scalemode;
277
278         v_z = 0;
279
280         scalemode = SCALEMODE_CROP;
281
282         for(i = 0; i < strlen(algn); ++i)
283         {
284                 c = substring(algn, i, 1);
285                 switch(c)
286                 {
287                         case "c": scalemode = SCALEMODE_CROP; goto nopic;
288                         case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
289                         case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
290                         case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
291                         case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
292                         case "1": case "4": case "7": v_x = 0.0; break;
293                         case "2": case "5": case "8": v_x = 0.5; break;
294                         case "3": case "6": case "9": v_x = 1.0; break;
295                         default: v_x = random(); break;
296                 }
297                 switch(c)
298                 {
299                         case "7": case "8": case "9": v_y = 0.0; break;
300                         case "4": case "5": case "6": v_y = 0.5; break;
301                         case "1": case "2": case "3": v_y = 1.0; break;
302                         default: v_y = random(); break;
303                 }
304                 if(l == 0)
305                         draw_Picture_Aligned(v, scalemode, img, a);
306                 else if(force1)
307                         // force all secondary layers to use alpha 1. Prevents ugly issues
308                         // with overlap. It's a flag because it cannot be used for the
309                         // ingame background
310                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
311                 else
312                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
313                 ++l;
314 :nopic
315         }
316 }
317
318 vector menuTooltipAveragedMousePos;
319 entity menuTooltipItem;
320 vector menuTooltipOrigin;
321 vector menuTooltipSize;
322 float menuTooltipAlpha;
323 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
324 float m_testmousetooltipbox(vector pos)
325 {
326         if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
327         if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
328                 return FALSE;
329         return TRUE;
330 }
331 float m_testtooltipbox(vector tooltippos)
332 {
333         if(tooltippos_x < 0)
334                 return FALSE;
335         if(tooltippos_y < 0)
336                 return FALSE;
337         if(tooltippos_x + menuTooltipSize_x > 1)
338                 return FALSE;
339         if(tooltippos_y + menuTooltipSize_y > 1)
340                 return FALSE;
341         /*
342         menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
343         menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
344         menuTooltipOrigin_z = 0;
345         */
346         menuTooltipOrigin = tooltippos;
347         return TRUE;
348 }
349 float m_allocatetooltipbox(vector pos)
350 {
351         vector avoidplus, avoidminus;
352         vector v;
353
354         avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
355         avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
356         avoidplus_z = 0;
357
358         avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
359         avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
360         avoidminus_z = 0;
361
362         // bottom right
363         v = pos + avoidplus;
364         if(m_testtooltipbox(v))
365                 return TRUE;
366         
367         // bottom center
368         v_x = pos_x - menuTooltipSize_x * 0.5;
369         if(m_testtooltipbox(v))
370                 return TRUE;
371
372         // bottom left
373         v_x = pos_x - avoidminus_x;
374         if(m_testtooltipbox(v))
375                 return TRUE;
376
377         // top left
378         v_y = pos_y - avoidminus_y;
379         if(m_testtooltipbox(v))
380                 return TRUE;
381
382         // top center
383         v_x = pos_x - menuTooltipSize_x * 0.5;
384         if(m_testtooltipbox(v))
385                 return TRUE;
386         
387         // top right
388         v_x = pos_x + avoidplus_x;
389         if(m_testtooltipbox(v))
390                 return TRUE;
391         
392         return FALSE;
393 }
394 entity m_findtooltipitem(entity root, vector pos)
395 {
396         entity it;
397         entity best;
398
399         best = world;
400         it = root;
401
402         while(it.instanceOfContainer)
403         {
404                 while(it.instanceOfNexposee && it.focusedChild)
405                 {
406                         it = it.focusedChild;
407                         pos = globalToBox(pos, it.Container_origin, it.Container_size);
408                 }
409                 if(it.instanceOfNexposee)
410                 {
411                         it = it.itemFromPoint(it, pos);
412                         if(it.tooltip)
413                                 best = it;
414                         it = world;
415                 }
416                 else if(it.instanceOfModalController)
417                         it = it.focusedChild;
418                 else
419                         it = it.itemFromPoint(it, pos);
420                 if(!it)
421                         break;
422                 if(it.tooltip)
423                         best = it;
424                 pos = globalToBox(pos, it.Container_origin, it.Container_size);
425         }
426
427         return best;
428 }
429 void m_tooltip(vector pos)
430 {
431         float f, i, w;
432         entity it;
433         vector fontsize, p;
434         string s;
435
436         fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
437
438         f = bound(0, frametime * 2, 1);
439         menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
440         f = vlen(pos - menuTooltipAveragedMousePos);
441
442         if(f < 0.01)
443                 it = m_findtooltipitem(main, pos);
444         else    
445                 it = world;
446
447         // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
448         if(it != menuTooltipItem)
449         {
450                 switch(menuTooltipState)
451                 {
452                         case 0:
453                                 if(menuTooltipItem)
454                                 {
455                                         // another item: fade out first
456                                         menuTooltipState = 2;
457                                 }
458                                 else
459                                 {
460                                         // new item: fade in
461                                         menuTooltipState = 1;
462                                         menuTooltipItem = it;
463
464                                         menuTooltipOrigin_x = -1; // unallocated
465                                         i = 0;
466                                         w =  0;
467                                         getWrappedLine_remaining = it.tooltip;
468                                         while(getWrappedLine_remaining)
469                                         {
470                                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
471                                                 ++i;
472                                                 f = draw_TextWidth(s, FALSE, fontsize);
473                                                 if(f > w)
474                                                         w = f;
475                                         }
476                                         menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
477                                         menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
478                                         menuTooltipSize_z = 0;
479                                 }
480                                 break;
481                         case 1:
482                                 // changing item while fading in: fade out first
483                                 menuTooltipState = 2;
484                                 break;
485                         case 2:
486                                 // changing item while fading out: can't
487                                 break;
488                 }
489         }
490         else if(menuTooltipState == 2) // re-fade in?
491                 menuTooltipState = 1;
492
493         if(menuTooltipItem)
494                 if(!m_testmousetooltipbox(pos))
495                         menuTooltipState = 2; // fade out if mouse touches it
496
497         switch(menuTooltipState)
498         {
499                 case 1:
500                         menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
501                         if(menuTooltipAlpha == 1)
502                                 menuTooltipState = 0;
503                         break;
504                 case 2:
505                         menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
506                         if(menuTooltipAlpha == 0)
507                         {
508                                 menuTooltipState = 0;
509                                 menuTooltipItem = world;
510                         }
511                         break;
512         }
513
514         if(menuTooltipItem)
515         {
516                 if(menuTooltipOrigin_x < 0) // unallocated?
517                         m_allocatetooltipbox(pos);
518
519                 if(menuTooltipOrigin_x >= 0)
520                 {
521                         // draw the tooltip!
522                         p = SKINBORDER_TOOLTIP;
523                         p_x *= 1 / conwidth;
524                         p_y *= 1 / conheight;
525                         draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
526                         p = menuTooltipOrigin;
527                         p_x += SKINMARGIN_TOOLTIP_x / conwidth;
528                         p_y += SKINMARGIN_TOOLTIP_y / conheight;
529                         getWrappedLine_remaining = menuTooltipItem.tooltip;
530                         while(getWrappedLine_remaining)
531                         {
532                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
533                                 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
534                                 p_y += fontsize_y;
535                         }
536                 }
537         }
538 }
539
540 void() m_draw =
541 {
542         float t;
543         float realFrametime;
544
545         menuMouseMode = cvar("menu_mouse_absolute");
546
547         if (anim)
548                 anim.tickAll(anim);
549
550         if(main)
551                 UpdateConWidthHeight();
552
553         if(!menuInitialized)
554         {
555                 // TODO draw an info image about this situation
556                 m_init_delayed();
557                 return;
558         }
559         if(!menuNotTheFirstFrame)
560         {
561                 menuNotTheFirstFrame = 1;
562                 if(Menu_Active)
563                 if(!cvar("menu_video_played"))
564                 {
565                         localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
566                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
567                 }
568         }
569
570         t = gettime();
571         realFrametime = frametime = min(0.2, t - menuPrevTime);
572         menuPrevTime = t;
573         time += frametime;
574
575         t = cvar("menu_slowmo");
576         if(t)
577         {
578                 frametime *= t;
579                 realFrametime *= t;
580         }
581         else
582                 t = 1;
583
584         if(Menu_Active)
585         {
586                 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
587                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
588                 else
589                         m_hide();
590         }
591
592         if(cvar("cl_capturevideo"))
593                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
594
595         gamestatus = 0;
596         if(isserver())
597                 gamestatus = gamestatus | GAME_ISSERVER;
598         if(clientstate() == CS_CONNECTED)
599                 gamestatus = gamestatus | GAME_CONNECTED;
600         if(cvar("developer"))
601                 gamestatus = gamestatus | GAME_DEVELOPER;
602
603         prevMenuAlpha = menuAlpha;
604         if(Menu_Active)
605         {
606                 if(menuAlpha == 0 && menuLogoAlpha < 2)
607                 {
608                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
609                 }
610                 else
611                 {
612                         menuAlpha = min(1, menuAlpha + frametime * 5);
613                         menuLogoAlpha = 2;
614                 }
615         }
616         else
617         {
618                 menuAlpha = max(0, menuAlpha - frametime * 5);
619                 menuLogoAlpha = 2;
620         }
621
622         draw_reset_cropped();
623
624         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
625         {
626                 if(menuLogoAlpha > 0)
627                 {
628                         draw_reset_full();
629                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
630                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
631                         draw_reset_cropped();
632                         if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
633                         {
634                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
635                                 draw_drawMousePointer(menuMousePos);
636                                 draw_alpha = 1;
637                         }
638                 }
639         }
640         else if(SKINALPHA_BACKGROUND_INGAME)
641         {
642                 if(menuAlpha > 0)
643                 {
644                         draw_reset_full();
645                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
646                         draw_reset_cropped();
647                 }
648         }
649
650         if(menuAlpha != prevMenuAlpha)
651                 cvar_set("_menu_alpha", ftos(menuAlpha));
652
653         draw_reset_cropped();
654         preMenuDraw();
655         draw_reset_cropped();
656
657         if(menuAlpha <= 0)
658         {
659                 if(prevMenuAlpha > 0)
660                         main.initializeDialog(main, main.firstChild);
661                 draw_reset_cropped();
662                 postMenuDraw();
663                 return;
664         }
665
666         draw_alpha *= menuAlpha;
667
668         if(menuMouseMode)
669         {
670                 vector newMouse;
671                 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
672                 if(newMouse != '0 0 0')
673                         if(newMouse != menuMousePos)
674                         {
675                                 menuMousePos = newMouse;
676                                 if(mouseButtonsPressed)
677                                         main.mouseDrag(main, menuMousePos);
678                                 else
679                                         main.mouseMove(main, menuMousePos);
680                         }
681         }
682         else
683         {
684                 if(frametime > 0)
685                 {
686                         vector dMouse, minpos, maxpos;
687                         dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
688                         if(dMouse != '0 0 0')
689                         {
690                                 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
691                                 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
692                                 dMouse = globalToBoxSize(dMouse, draw_scale);
693                                 menuMousePos += dMouse * cvar("menu_mouse_speed");
694                                 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
695                                 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
696                                 if(mouseButtonsPressed)
697                                         main.mouseDrag(main, menuMousePos);
698                                 else
699                                         main.mouseMove(main, menuMousePos);
700                         }
701                 }
702         }
703         main.draw(main);
704
705         m_tooltip(menuMousePos);
706
707         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
708
709         draw_drawMousePointer(menuMousePos);
710
711         draw_reset_cropped();
712         postMenuDraw();
713
714         frametime = 0;
715 };
716
717 void() m_display =
718 {
719         Menu_Active = true;
720         setkeydest(KEY_MENU);
721         setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
722
723         if(!menuInitialized)
724                 return;
725
726         if(mouseButtonsPressed)
727                 main.mouseRelease(main, menuMousePos);
728         mouseButtonsPressed = 0;
729
730         main.focusEnter(main);
731         main.showNotify(main);
732 };
733
734 void() m_hide =
735 {
736         Menu_Active = false;
737         setkeydest(KEY_GAME);
738         setmousetarget(MT_CLIENT);
739
740         if(!menuInitialized)
741                 return;
742
743         main.focusLeave(main);
744         main.hideNotify(main);
745 };
746
747 void() m_toggle =
748 {
749         if(Menu_Active)
750                 m_hide();
751         else
752                 m_display();
753 };
754
755 void() m_shutdown =
756 {
757         entity e;
758
759         m_hide();
760         for(e = NULL; (e = nextent(e)) != NULL; )
761         {
762                 if(e.destroy)
763                         e.destroy(e);
764         }
765 };
766
767 void m_focus_item_chain(entity outermost, entity innermost)
768 {
769         if(innermost.parent != outermost)
770                 m_focus_item_chain(outermost, innermost.parent);
771         innermost.parent.setFocus(innermost.parent, innermost);
772 }
773
774 void m_activate_window(entity wnd)
775 {
776         entity par;
777         par = wnd.parent;
778         if(par)
779                 m_activate_window(par);
780
781         if(par.instanceOfModalController)
782         {
783                 if(wnd.tabSelectingButton)
784                         // tabs
785                         TabButton_Click(wnd.tabSelectingButton, wnd);
786                 else
787                         // root
788                         par.initializeDialog(par, wnd);
789         }
790         else if(par.instanceOfNexposee)
791         {
792                 // nexposee (sorry for violating abstraction here)
793                 par.selectedChild = wnd;
794                 par.animationState = 1;
795                 setFocusContainer(par, NULL);
796         }
797         else if(par.instanceOfContainer)
798         {
799                 // other containers
800                 if(par.focused)
801                         par.setFocus(par, wnd);
802         }
803 }
804
805 void m_setpointerfocus(entity wnd)
806 {
807         if(wnd.instanceOfContainer)
808         {
809                 entity focus = wnd.preferredFocusedGrandChild(wnd);
810                 if(focus)
811                 {
812                         menuMousePos = focus.origin + 0.5 * focus.size;
813                         menuMousePos_x *= 1 / conwidth;
814                         menuMousePos_y *= 1 / conheight;
815                         if(wnd.focused) // why does this never happen?
816                                 m_focus_item_chain(wnd, focus);
817                 }
818         }
819 }
820
821 void(string itemname) m_goto =
822 {
823         entity e;
824         if(!menuInitialized)
825                 return;
826         if(itemname == "") // this can be called by GameCommand
827         {
828                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
829                         m_hide();
830                 else
831                 {
832                         m_activate_window(main.mainNexposee);
833                         m_display();
834                 }
835         }
836         else
837         {
838                 e = findstring(NULL, name, itemname);
839                 if(e)
840                 {
841                         m_hide();
842                         m_activate_window(e);
843                         m_setpointerfocus(e);
844                         m_display();
845                 }
846         }
847 }
848
849 void() m_goto_skin_selector =
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("skinselector");
855 }
856
857 void() m_goto_video_settings =
858 {
859         if(!menuInitialized)
860                 return;
861         // TODO add code to switch back to the skin selector (no idea how to do it now)
862         m_goto("videosettings");
863 }