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