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