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