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