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