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