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