]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/menu.qc
Merge remote-tracking branch 'origin/zykure/playerstats' into samual/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 void m_sync()
20 {
21         updateCompression();
22         updateConwidths();
23
24         loadAllCvars(main);
25 }
26
27 void m_gamestatus()
28 {
29         gamestatus = 0;
30         if(isserver())
31                 gamestatus = gamestatus | GAME_ISSERVER;
32         if(clientstate() == CS_CONNECTED || isdemo())
33                 gamestatus = gamestatus | GAME_CONNECTED;
34         if(cvar("developer"))
35                 gamestatus = gamestatus | GAME_DEVELOPER;
36 }
37
38 void m_init()
39 {
40         float restarting = 0;
41         cvar_set("_menu_alpha", "0");
42         prvm_language = cvar_string("prvm_language");
43         if(prvm_language == "")
44         {
45                 prvm_language = "en";
46                 cvar_set("prvm_language", prvm_language);
47                 localcmd("\nmenu_restart\n");
48                 restarting = 1;
49         }
50         prvm_language = strzone(prvm_language);
51         cvar_set("_menu_prvm_language", prvm_language);
52
53         check_unacceptable_compiler_bugs();
54
55 #ifdef WATERMARK
56         print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK));
57 #endif
58
59         // list all game dirs (TEST)
60         if(cvar("developer"))
61         {
62                 float i;
63                 string s;
64                 for(i = 0; ; ++i)
65                 {
66                         s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
67                         if not(s)
68                                 break;
69                         dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
70                 }
71         }
72
73         // needs to be done so early because of the constants they create
74         CALL_ACCUMULATED_FUNCTION(RegisterWeapons);
75         CALL_ACCUMULATED_FUNCTION(RegisterGametypes);
76
77         float ddsload = cvar("r_texture_dds_load");
78         float texcomp = cvar("gl_texturecompression");
79         updateCompression();
80         if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
81                 localcmd("\nr_restart\n");
82         initConwidths();
83
84         if(!restarting)
85         {
86                 if(cvar("_menu_initialized")) // always show menu after menu_restart
87                         m_display();
88                 else
89                         m_hide();
90                 cvar_set("_menu_initialized", "1");
91         }
92
93         PlayerInfo_Details();
94 }
95
96 float MENU_ASPECT = 1.25; // 1280x1024
97 float MENU_MINHEIGHT = 600;
98 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
99 void draw_reset_cropped()
100 {
101         draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
102 }
103 void draw_reset_full()
104 {
105         draw_reset(realconwidth, realconheight, 0, 0);
106 }
107 void UpdateConWidthHeight()
108 {
109         conwidth_s = conwidth;
110         conheight_s = conheight;
111         realconwidth = cvar("vid_conwidth");
112         realconheight = cvar("vid_conheight");
113         if(realconwidth / realconheight > MENU_ASPECT)
114         {
115                 // widescreen
116                 conwidth = realconheight * MENU_ASPECT;
117                 conheight = realconheight;
118         }
119         else
120         {
121                 // squarescreen
122                 conwidth = realconwidth;
123                 conheight = realconwidth / MENU_ASPECT;
124         }
125         screenconwidth = conwidth;
126         screenconheight = conheight;
127         if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
128         {
129                 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
130                 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
131         }
132         if(conheight < MENU_MINHEIGHT)
133         {
134                 conwidth *= MENU_MINHEIGHT / conheight;
135                 conheight = MENU_MINHEIGHT;
136         }
137         if(main)
138         {
139                 if(conwidth_s != conwidth || conheight_s != conheight)
140                 {
141                         draw_reset_cropped();
142                         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
143                 }
144         }
145 }
146
147 string m_goto_buffer;
148 void m_init_delayed()
149 {
150         float fh, glob, n, i;
151         string s;
152
153         conwidth = conheight = -1;
154         UpdateConWidthHeight();
155         draw_reset_cropped();
156
157         menuInitialized = 0;
158         if(!preMenuInit())
159                 return;
160         menuInitialized = 1;
161
162         fh = -1;
163         if(cvar_string("menu_skin") != "")
164         {
165                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
166                 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
167         }
168         if(fh < 0)
169         if(cvar_defstring("menu_skin") != "")
170         {
171                 cvar_set("menu_skin", cvar_defstring("menu_skin"));
172                 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
173                 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
174         }
175         if(fh < 0)
176         {
177                 draw_currentSkin = "gfx/menu/default";
178                 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
179         }
180         if(fh < 0)
181         {
182                 error("cannot load any menu skin\n");
183         }
184         draw_currentSkin = strzone(draw_currentSkin);
185         while((s = fgets(fh)))
186         {
187                 // these two are handled by skinlist.qc
188                 if(substring(s, 0, 6) == "title ")
189                         continue;
190                 if(substring(s, 0, 7) == "author ")
191                         continue;
192                 n = tokenize_console(s);
193                 if(n >= 2)
194                         Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
195         }
196         fclose(fh);
197
198         glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
199         if(glob >= 0)
200         {
201                 n = search_getsize(glob);
202                 for(i = 0; i < n; ++i)
203                         precache_pic(search_getfilename(glob, i));
204                 search_end(glob);
205         }
206
207         draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
208
209         loadTooltips();
210         anim = spawnAnimHost();
211         main = spawnMainWindow(); main.configureMainWindow(main);
212         unloadTooltips();
213
214         main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
215         main.focused = 1;
216         menuShiftState = 0;
217         menuMousePos = '0.5 0.5 0';
218
219         m_sync();
220
221         if(m_goto_buffer)
222         {
223                 m_goto(m_goto_buffer);
224                 strunzone(m_goto_buffer);
225                 m_goto_buffer = string_null;
226         }
227
228         if(Menu_Active)
229                 m_display(); // delayed menu display
230 }
231
232 void m_keyup (float key, float ascii)
233 {
234         if(!menuInitialized)
235                 return;
236         if(!Menu_Active)
237                 return;
238         draw_reset_cropped();
239         main.keyUp(main, key, ascii, menuShiftState);
240         if(key >= K_MOUSE1 && key <= K_MOUSE3)
241         {
242                 --mouseButtonsPressed;
243                 if(!mouseButtonsPressed)
244                         main.mouseRelease(main, menuMousePos);
245                 if(mouseButtonsPressed < 0)
246                 {
247                         mouseButtonsPressed = 0;
248                         dprint("Warning: released an already released button\n");
249                 }
250         }
251         if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
252         if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
253         if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
254 }
255
256 void m_keydown(float key, float ascii)
257 {
258         if(!menuInitialized)
259                 return;
260         if(!Menu_Active)
261                 return;
262
263         if(menuMouseMode)
264         if(key >= K_MOUSE1 && key <= K_MOUSE3)
265         {
266                 // detect a click outside of the game window
267                 vector p = getmousepos();
268                 if(p_x < 0 || p_x > realconwidth || p_y < 0 || p_y > realconheight)
269                 {
270                         ++mouseButtonsPressed;
271                         return;
272                 }
273         }
274
275         if(keyGrabber)
276         {
277                 entity e;
278                 e = keyGrabber;
279                 keyGrabber = NULL;
280                 e.keyGrabbed(e, key, ascii);
281         }
282         else
283         {
284                 draw_reset_cropped();
285                 if(key >= K_MOUSE1 && key <= K_MOUSE3)
286                         if(!mouseButtonsPressed)
287                                 main.mousePress(main, menuMousePos);
288                 if(!main.keyDown(main, key, ascii, menuShiftState))
289                         if(key == K_ESCAPE)
290                                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
291                                         m_hide(); // disable menu on unhandled ESC
292         }
293         if(key >= K_MOUSE1 && key <= K_MOUSE3)
294         {
295                 ++mouseButtonsPressed;
296                 if(mouseButtonsPressed > 10)
297                 {
298                         mouseButtonsPressed = 10;
299                         dprint("Warning: pressed an already pressed button\n");
300                 }
301         }
302         if(key == K_ALT) menuShiftState |= S_ALT;
303         if(key == K_CTRL) menuShiftState |= S_CTRL;
304         if(key == K_SHIFT) menuShiftState |= S_SHIFT;
305 }
306
307 float SCALEMODE_CROP = 0;
308 float SCALEMODE_LETTERBOX = 1;
309 float SCALEMODE_WIDTH = 2;
310 float SCALEMODE_HEIGHT = 3;
311 float SCALEMODE_STRETCH = 4;
312 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
313 {
314         vector sz, org, isz, isz_w, isz_h;
315         float width_is_larger;
316
317         sz = draw_PictureSize(img);
318         width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
319         isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
320         isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
321
322 #ifdef GMQCC
323         isz = '0 0 0';
324 #endif
325         switch(scalemode)
326         {
327                 default:
328                 case SCALEMODE_CROP:
329                         isz = (width_is_larger ? isz_h : isz_w);
330                         break;
331                 case SCALEMODE_LETTERBOX:
332                         isz = (width_is_larger ? isz_w : isz_h);
333                         break;
334                 case SCALEMODE_WIDTH:
335                         isz = isz_w;
336                         break;
337                 case SCALEMODE_HEIGHT:
338                         isz = isz_h;
339                         break;
340                 case SCALEMODE_STRETCH:
341                         isz = '1 1 0';
342                         break;
343         }
344
345         org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
346         draw_Picture(org, img, isz, '1 1 1', a);
347 }
348
349 void drawBackground(string img, float a, string algn, float force1)
350 {
351         if(main.mainNexposee.ModalController_state == 0)
352                 return;
353
354         vector v;
355         float i, l;
356         string c;
357         float scalemode;
358
359         v_z = 0;
360
361         scalemode = SCALEMODE_CROP;
362
363         l = 0;
364         for(i = 0; i < strlen(algn); ++i)
365         {
366                 c = substring(algn, i, 1);
367                 switch(c)
368                 {
369                         case "c": scalemode = SCALEMODE_CROP; goto nopic;
370                         case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
371                         case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
372                         case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
373                         case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
374                         case "1": case "4": case "7": v_x = 0.0; break;
375                         case "2": case "5": case "8": v_x = 0.5; break;
376                         case "3": case "6": case "9": v_x = 1.0; break;
377                         default: v_x = random(); break;
378                 }
379                 switch(c)
380                 {
381                         case "7": case "8": case "9": v_y = 0.0; break;
382                         case "4": case "5": case "6": v_y = 0.5; break;
383                         case "1": case "2": case "3": v_y = 1.0; break;
384                         default: v_y = random(); break;
385                 }
386                 if(l == 0)
387                         draw_Picture_Aligned(v, scalemode, img, a);
388                 else if(force1)
389                         // force all secondary layers to use alpha 1. Prevents ugly issues
390                         // with overlap. It's a flag because it cannot be used for the
391                         // ingame background
392                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
393                 else
394                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
395                 ++l;
396 :nopic
397         }
398 }
399
400 float menu_tooltips;
401 float menu_tooltips_old;
402 vector menuTooltipAveragedMousePos;
403 entity menuTooltipItem;
404 vector menuTooltipOrigin;
405 vector menuTooltipSize;
406 float menuTooltipAlpha;
407 string menuTooltipText;
408 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
409 float m_testmousetooltipbox(vector pos)
410 {
411         if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
412         if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
413                 return FALSE;
414         return TRUE;
415 }
416 float m_testtooltipbox(vector tooltippos)
417 {
418         if(tooltippos_x < 0)
419                 return FALSE;
420         if(tooltippos_y < 0)
421                 return FALSE;
422         if(tooltippos_x + menuTooltipSize_x > 1)
423                 return FALSE;
424         if(tooltippos_y + menuTooltipSize_y > 1)
425                 return FALSE;
426         menuTooltipOrigin = tooltippos;
427         return TRUE;
428 }
429 float m_allocatetooltipbox(vector pos)
430 {
431         vector avoidplus, avoidminus;
432         vector v;
433
434         avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
435         avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
436         avoidplus_z = 0;
437
438         avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
439         avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
440         avoidminus_z = 0;
441
442         // bottom right
443         v = pos + avoidplus;
444         if(m_testtooltipbox(v))
445                 return TRUE;
446
447         // bottom center
448         v_x = pos_x - menuTooltipSize_x * 0.5;
449         if(m_testtooltipbox(v))
450                 return TRUE;
451
452         // bottom left
453         v_x = pos_x - avoidminus_x;
454         if(m_testtooltipbox(v))
455                 return TRUE;
456
457         // top left
458         v_y = pos_y - avoidminus_y;
459         if(m_testtooltipbox(v))
460                 return TRUE;
461
462         // top center
463         v_x = pos_x - menuTooltipSize_x * 0.5;
464         if(m_testtooltipbox(v))
465                 return TRUE;
466
467         // top right
468         v_x = pos_x + avoidplus_x;
469         if(m_testtooltipbox(v))
470                 return TRUE;
471
472         return FALSE;
473 }
474 entity m_findtooltipitem(entity root, vector pos)
475 {
476         entity it;
477         entity best;
478
479         best = world;
480         it = root;
481
482         while(it.instanceOfContainer)
483         {
484                 while(it.instanceOfNexposee && it.focusedChild)
485                 {
486                         it = it.focusedChild;
487                         pos = globalToBox(pos, it.Container_origin, it.Container_size);
488                 }
489                 if(it.instanceOfNexposee)
490                 {
491                         it = it.itemFromPoint(it, pos);
492                         if(it.tooltip)
493                                 best = it;
494                         else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
495                                 best = it;
496                         it = world;
497                 }
498                 else if(it.instanceOfModalController)
499                         it = it.focusedChild;
500                 else
501                         it = it.itemFromPoint(it, pos);
502                 if(!it)
503                         break;
504                 if(it.tooltip)
505                         best = it;
506                 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
507                         best = it;
508                 pos = globalToBox(pos, it.Container_origin, it.Container_size);
509         }
510
511         return best;
512 }
513 string gettooltip()
514 {
515         if (menu_tooltips == 2)
516         {
517                 string s;
518                 if (menuTooltipItem.cvarName)
519                 {
520                         if (getCvarsMulti(menuTooltipItem))
521                                 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
522                         else
523                                 s = strcat("[", menuTooltipItem.cvarName, "]");
524                 }
525                 else if (menuTooltipItem.onClickCommand)
526                         s = strcat("<", menuTooltipItem.onClickCommand, ">");
527                 else
528                         return menuTooltipItem.tooltip;
529                 if (menuTooltipItem.tooltip)
530                         return strcat(menuTooltipItem.tooltip, " ", s);
531                 return s;
532         }
533         return menuTooltipItem.tooltip;
534 }
535 void m_tooltip(vector pos)
536 {
537         float f, i, w;
538         entity it;
539         vector fontsize, p;
540         string s;
541
542         menu_tooltips = cvar("menu_tooltips");
543         if (!menu_tooltips)
544         {
545                 // don't return immediately, fade out the active tooltip first
546                 if (menuTooltipItem == world)
547                         return;
548                 it = world;
549                 menu_tooltips_old = menu_tooltips;
550         }
551         else
552         {
553                 f = bound(0, frametime * 2, 1);
554                 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
555                 f = vlen(pos - menuTooltipAveragedMousePos);
556                 if(f < 0.01)
557                         it = m_findtooltipitem(main, pos);
558                 else
559                         it = world;
560         }
561         fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
562
563         // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
564         if(it != menuTooltipItem)
565         {
566                 switch(menuTooltipState)
567                 {
568                         case 0:
569                                 if(menuTooltipItem)
570                                 {
571                                         // another item: fade out first
572                                         menuTooltipState = 2;
573                                 }
574                                 else
575                                 {
576                                         // new item: fade in
577                                         menuTooltipState = 1;
578                                         menuTooltipItem = it;
579
580                                         menuTooltipOrigin_x = -1; // unallocated
581
582                                         if (menuTooltipText)
583                                                 strunzone(menuTooltipText);
584                                         menuTooltipText = strzone(gettooltip());
585
586                                         i = 0;
587                                         w = 0;
588                                         getWrappedLine_remaining = menuTooltipText;
589                                         while(getWrappedLine_remaining)
590                                         {
591                                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
592                                                 ++i;
593                                                 f = draw_TextWidth(s, FALSE, fontsize);
594                                                 if(f > w)
595                                                         w = f;
596                                         }
597                                         menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
598                                         menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
599                                         menuTooltipSize_z = 0;
600                                 }
601                                 break;
602                         case 1:
603                                 // changing item while fading in: fade out first
604                                 menuTooltipState = 2;
605                                 break;
606                         case 2:
607                                 // changing item while fading out: can't
608                                 break;
609                 }
610         }
611         else if(menuTooltipState == 2) // re-fade in?
612                 menuTooltipState = 1;
613
614         if(menuTooltipItem)
615                 if(!m_testmousetooltipbox(pos))
616                         menuTooltipState = 2; // fade out if mouse touches it
617
618         switch(menuTooltipState)
619         {
620                 case 1:
621                         menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
622                         if(menuTooltipAlpha == 1)
623                                 menuTooltipState = 0;
624                         break;
625                 case 2:
626                         menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
627                         if(menuTooltipAlpha == 0)
628                         {
629                                 menuTooltipState = 0;
630                                 menuTooltipItem = world;
631                         }
632                         break;
633         }
634
635         if(menuTooltipItem == world)
636         {
637                 if (menuTooltipText)
638                 {
639                         strunzone(menuTooltipText);
640                         menuTooltipText = string_null;
641                 }
642                 return;
643         }
644         else
645         {
646                 if(menu_tooltips != menu_tooltips_old)
647                 {
648                         if (menu_tooltips != 0 && menu_tooltips_old != 0)
649                                 menuTooltipItem = world; // reload tooltip next frame
650                         menu_tooltips_old = menu_tooltips;
651                 }
652                 else if(menuTooltipOrigin_x < 0) // unallocated?
653                         m_allocatetooltipbox(pos);
654
655                 if(menuTooltipOrigin_x >= 0)
656                 {
657                         // draw the tooltip!
658                         p = SKINBORDER_TOOLTIP;
659                         p_x *= 1 / conwidth;
660                         p_y *= 1 / conheight;
661                         draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
662                         p = menuTooltipOrigin;
663                         p_x += SKINMARGIN_TOOLTIP_x / conwidth;
664                         p_y += SKINMARGIN_TOOLTIP_y / conheight;
665                         getWrappedLine_remaining = menuTooltipText;
666                         while(getWrappedLine_remaining)
667                         {
668                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
669                                 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
670                                 p_y += fontsize_y;
671                         }
672                 }
673         }
674 }
675
676 void m_draw()
677 {
678         float t;
679         float realFrametime;
680
681         m_gamestatus();
682
683         execute_next_frame();
684
685         menuMouseMode = cvar("menu_mouse_absolute");
686
687         if (anim)
688                 anim.tickAll(anim);
689
690         if(main)
691                 UpdateConWidthHeight();
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                 if(e)
997                 {
998                         m_hide();
999                         m_activate_window(e);
1000                         m_setpointerfocus(e);
1001                         m_display();
1002                 }
1003         }
1004 }