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