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