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