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