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