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