]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/menu.qc
Merge remote-tracking branch 'origin/master' into terencehill/cursormode
[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         switch(scalemode)
310         {
311                 default:
312                 case SCALEMODE_CROP:
313                         isz = (width_is_larger ? isz_h : isz_w);
314                         break;
315                 case SCALEMODE_LETTERBOX:
316                         isz = (width_is_larger ? isz_w : isz_h);
317                         break;
318                 case SCALEMODE_WIDTH:
319                         isz = isz_w;
320                         break;
321                 case SCALEMODE_HEIGHT:
322                         isz = isz_h;
323                         break;
324                 case SCALEMODE_STRETCH:
325                         isz = '1 1 0';
326                         break;
327         }
328
329         org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
330         draw_Picture(org, img, isz, '1 1 1', a);
331 }
332
333 void drawBackground(string img, float a, string algn, float force1)
334 {
335         if(main.mainNexposee.ModalController_state == 0)
336                 return;
337
338         vector v;
339         float i, l;
340         string c;
341         float scalemode;
342
343         v_z = 0;
344
345         scalemode = SCALEMODE_CROP;
346
347         l = 0;
348         for(i = 0; i < strlen(algn); ++i)
349         {
350                 c = substring(algn, i, 1);
351                 switch(c)
352                 {
353                         case "c": scalemode = SCALEMODE_CROP; goto nopic;
354                         case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
355                         case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
356                         case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
357                         case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
358                         case "1": case "4": case "7": v_x = 0.0; break;
359                         case "2": case "5": case "8": v_x = 0.5; break;
360                         case "3": case "6": case "9": v_x = 1.0; break;
361                         default: v_x = random(); break;
362                 }
363                 switch(c)
364                 {
365                         case "7": case "8": case "9": v_y = 0.0; break;
366                         case "4": case "5": case "6": v_y = 0.5; break;
367                         case "1": case "2": case "3": v_y = 1.0; break;
368                         default: v_y = random(); break;
369                 }
370                 if(l == 0)
371                         draw_Picture_Aligned(v, scalemode, img, a);
372                 else if(force1)
373                         // force all secondary layers to use alpha 1. Prevents ugly issues
374                         // with overlap. It's a flag because it cannot be used for the
375                         // ingame background
376                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
377                 else
378                         draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
379                 ++l;
380 :nopic
381         }
382 }
383
384 float menu_tooltips;
385 float menu_tooltips_old;
386 vector menuTooltipAveragedMousePos;
387 entity menuTooltipItem;
388 vector menuTooltipOrigin;
389 vector menuTooltipSize;
390 float menuTooltipAlpha;
391 string menuTooltipText;
392 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
393 float m_testmousetooltipbox(vector pos)
394 {
395         if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
396         if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
397                 return FALSE;
398         return TRUE;
399 }
400 float m_testtooltipbox(vector tooltippos)
401 {
402         if(tooltippos_x < 0)
403                 return FALSE;
404         if(tooltippos_y < 0)
405                 return FALSE;
406         if(tooltippos_x + menuTooltipSize_x > 1)
407                 return FALSE;
408         if(tooltippos_y + menuTooltipSize_y > 1)
409                 return FALSE;
410         menuTooltipOrigin = tooltippos;
411         return TRUE;
412 }
413 float m_allocatetooltipbox(vector pos)
414 {
415         vector avoidplus, avoidminus;
416         vector v;
417
418         avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
419         avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
420         avoidplus_z = 0;
421
422         avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
423         avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
424         avoidminus_z = 0;
425
426         // bottom right
427         v = pos + avoidplus;
428         if(m_testtooltipbox(v))
429                 return TRUE;
430         
431         // bottom center
432         v_x = pos_x - menuTooltipSize_x * 0.5;
433         if(m_testtooltipbox(v))
434                 return TRUE;
435
436         // bottom left
437         v_x = pos_x - avoidminus_x;
438         if(m_testtooltipbox(v))
439                 return TRUE;
440
441         // top left
442         v_y = pos_y - avoidminus_y;
443         if(m_testtooltipbox(v))
444                 return TRUE;
445
446         // top center
447         v_x = pos_x - menuTooltipSize_x * 0.5;
448         if(m_testtooltipbox(v))
449                 return TRUE;
450         
451         // top right
452         v_x = pos_x + avoidplus_x;
453         if(m_testtooltipbox(v))
454                 return TRUE;
455         
456         return FALSE;
457 }
458 entity m_findtooltipitem(entity root, vector pos)
459 {
460         entity it;
461         entity best;
462
463         best = world;
464         it = root;
465
466         while(it.instanceOfContainer)
467         {
468                 while(it.instanceOfNexposee && it.focusedChild)
469                 {
470                         it = it.focusedChild;
471                         pos = globalToBox(pos, it.Container_origin, it.Container_size);
472                 }
473                 if(it.instanceOfNexposee)
474                 {
475                         it = it.itemFromPoint(it, pos);
476                         if(it.tooltip)
477                                 best = it;
478                         else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
479                                 best = it;
480                         it = world;
481                 }
482                 else if(it.instanceOfModalController)
483                         it = it.focusedChild;
484                 else
485                         it = it.itemFromPoint(it, pos);
486                 if(!it)
487                         break;
488                 if(it.tooltip)
489                         best = it;
490                 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
491                         best = it;
492                 pos = globalToBox(pos, it.Container_origin, it.Container_size);
493         }
494
495         return best;
496 }
497 string gettooltip()
498 {
499         if (menu_tooltips == 2)
500         {
501                 string s;
502                 if (menuTooltipItem.cvarName)
503                 {
504                         if (getCvarsMulti(menuTooltipItem))
505                                 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
506                         else
507                                 s = strcat("[", menuTooltipItem.cvarName, "]");
508                 }
509                 else if (menuTooltipItem.onClickCommand)
510                         s = strcat("<", menuTooltipItem.onClickCommand, ">");
511                 else
512                         return menuTooltipItem.tooltip;
513                 if (menuTooltipItem.tooltip)
514                         return strcat(menuTooltipItem.tooltip, " ", s);
515                 return s;
516         }
517         return menuTooltipItem.tooltip;
518 }
519 void m_tooltip(vector pos)
520 {
521         float f, i, w;
522         entity it;
523         vector fontsize, p;
524         string s;
525
526         menu_tooltips = cvar("menu_tooltips");
527         if (!menu_tooltips)
528         {
529                 // don't return immediately, fade out the active tooltip first
530                 if (menuTooltipItem == world)
531                         return;
532                 it = world;
533                 menu_tooltips_old = menu_tooltips;
534         }
535         else
536         {
537                 f = bound(0, frametime * 2, 1);
538                 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
539                 f = vlen(pos - menuTooltipAveragedMousePos);
540                 if(f < 0.01)
541                         it = m_findtooltipitem(main, pos);
542                 else
543                         it = world;
544         }
545         fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
546
547         // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
548         if(it != menuTooltipItem)
549         {
550                 switch(menuTooltipState)
551                 {
552                         case 0:
553                                 if(menuTooltipItem)
554                                 {
555                                         // another item: fade out first
556                                         menuTooltipState = 2;
557                                 }
558                                 else
559                                 {
560                                         // new item: fade in
561                                         menuTooltipState = 1;
562                                         menuTooltipItem = it;
563
564                                         menuTooltipOrigin_x = -1; // unallocated
565
566                                         if (menuTooltipText)
567                                                 strunzone(menuTooltipText);
568                                         menuTooltipText = strzone(gettooltip());
569
570                                         i = 0;
571                                         w = 0;
572                                         getWrappedLine_remaining = menuTooltipText;
573                                         while(getWrappedLine_remaining)
574                                         {
575                                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
576                                                 ++i;
577                                                 f = draw_TextWidth(s, FALSE, fontsize);
578                                                 if(f > w)
579                                                         w = f;
580                                         }
581                                         menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
582                                         menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
583                                         menuTooltipSize_z = 0;
584                                 }
585                                 break;
586                         case 1:
587                                 // changing item while fading in: fade out first
588                                 menuTooltipState = 2;
589                                 break;
590                         case 2:
591                                 // changing item while fading out: can't
592                                 break;
593                 }
594         }
595         else if(menuTooltipState == 2) // re-fade in?
596                 menuTooltipState = 1;
597
598         if(menuTooltipItem)
599                 if(!m_testmousetooltipbox(pos))
600                         menuTooltipState = 2; // fade out if mouse touches it
601
602         switch(menuTooltipState)
603         {
604                 case 1:
605                         menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
606                         if(menuTooltipAlpha == 1)
607                                 menuTooltipState = 0;
608                         break;
609                 case 2:
610                         menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
611                         if(menuTooltipAlpha == 0)
612                         {
613                                 menuTooltipState = 0;
614                                 menuTooltipItem = world;
615                         }
616                         break;
617         }
618
619         if(menuTooltipItem == world)
620         {
621                 if (menuTooltipText)
622                 {
623                         strunzone(menuTooltipText);
624                         menuTooltipText = string_null;
625                 }
626                 return;
627         }
628         else
629         {
630                 if(menu_tooltips != menu_tooltips_old)
631                 {
632                         if (menu_tooltips != 0 && menu_tooltips_old != 0)
633                                 menuTooltipItem = world; // reload tooltip next frame
634                         menu_tooltips_old = menu_tooltips;
635                 }
636                 else if(menuTooltipOrigin_x < 0) // unallocated?
637                         m_allocatetooltipbox(pos);
638
639                 if(menuTooltipOrigin_x >= 0)
640                 {
641                         // draw the tooltip!
642                         p = SKINBORDER_TOOLTIP;
643                         p_x *= 1 / conwidth;
644                         p_y *= 1 / conheight;
645                         draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
646                         p = menuTooltipOrigin;
647                         p_x += SKINMARGIN_TOOLTIP_x / conwidth;
648                         p_y += SKINMARGIN_TOOLTIP_y / conheight;
649                         getWrappedLine_remaining = menuTooltipText;
650                         while(getWrappedLine_remaining)
651                         {
652                                 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
653                                 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
654                                 p_y += fontsize_y;
655                         }
656                 }
657         }
658 }
659
660 void m_draw()
661 {
662         float t;
663         float realFrametime;
664
665         m_gamestatus();
666
667         execute_next_frame();
668
669         menuMouseMode = cvar("menu_mouse_absolute");
670
671         if (anim)
672                 anim.tickAll(anim);
673
674         if(main)
675                 UpdateConWidthHeight();
676
677         if(!menuInitialized)
678         {
679                 // TODO draw an info image about this situation
680                 m_init_delayed();
681                 return;
682         }
683         if(!menuNotTheFirstFrame)
684         {
685                 menuNotTheFirstFrame = 1;
686                 if(Menu_Active)
687                 if(!cvar("menu_video_played"))
688                 {
689                         localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
690                         menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
691                 }
692                 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
693                 localcmd("set menu_video_played 1\n");
694         }
695
696         t = gettime();
697         realFrametime = frametime = min(0.2, t - menuPrevTime);
698         menuPrevTime = t;
699         time += frametime;
700
701         t = cvar("menu_slowmo");
702         if(t)
703         {
704                 frametime *= t;
705                 realFrametime *= t;
706         }
707         else
708                 t = 1;
709
710         if(Menu_Active)
711         {
712                 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
713                         setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
714                 else
715                         m_hide();
716         }
717
718         if(cvar("cl_capturevideo"))
719                 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
720
721         prevMenuAlpha = menuAlpha;
722         if(Menu_Active)
723         {
724                 if(menuAlpha == 0 && menuLogoAlpha < 2)
725                 {
726                         menuLogoAlpha = menuLogoAlpha + frametime * 2;
727                 }
728                 else
729                 {
730                         menuAlpha = min(1, menuAlpha + frametime * 5);
731                         menuLogoAlpha = 2;
732                 }
733         }
734         else
735         {
736                 menuAlpha = max(0, menuAlpha - frametime * 5);
737                 menuLogoAlpha = 2;
738         }
739
740         draw_reset_cropped();
741
742         if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
743         {
744                 if(menuLogoAlpha > 0)
745                 {
746                         draw_reset_full();
747                         draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
748                         drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
749                         draw_reset_cropped();
750                         if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
751                         {
752                                 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
753                                 draw_drawMousePointer(menuMousePos);
754                                 draw_alpha = 1;
755                         }
756                 }
757         }
758         else if(SKINALPHA_BACKGROUND_INGAME)
759         {
760                 if(menuAlpha > 0)
761                 {
762                         draw_reset_full();
763                         drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
764                         draw_reset_cropped();
765                 }
766         }
767
768         if(menuAlpha != prevMenuAlpha)
769                 cvar_set("_menu_alpha", ftos(menuAlpha));
770
771         draw_reset_cropped();
772         preMenuDraw();
773         draw_reset_cropped();
774
775         if(menuAlpha <= 0)
776         {
777                 if(prevMenuAlpha > 0)
778                         main.initializeDialog(main, main.firstChild);
779                 draw_reset_cropped();
780                 postMenuDraw();
781                 return;
782         }
783
784         draw_alpha *= menuAlpha;
785
786         if(menuMouseMode)
787         {
788                 vector newMouse;
789                 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
790                 if(newMouse != '0 0 0')
791                         if(newMouse != menuMousePos)
792                         {
793                                 menuMousePos = newMouse;
794                                 if(mouseButtonsPressed)
795                                         main.mouseDrag(main, menuMousePos);
796                                 else
797                                         main.mouseMove(main, menuMousePos);
798                         }
799         }
800         else
801         {
802                 if(frametime > 0)
803                 {
804                         vector dMouse, minpos, maxpos;
805                         dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
806                         if(dMouse != '0 0 0')
807                         {
808                                 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
809                                 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
810                                 dMouse = globalToBoxSize(dMouse, draw_scale);
811                                 menuMousePos += dMouse * cvar("menu_mouse_speed");
812                                 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
813                                 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
814                                 if(mouseButtonsPressed)
815                                         main.mouseDrag(main, menuMousePos);
816                                 else
817                                         main.mouseMove(main, menuMousePos);
818                         }
819                 }
820         }
821         main.draw(main);
822
823         m_tooltip(menuMousePos);
824
825         draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
826
827         draw_drawMousePointer(menuMousePos);
828
829         draw_reset_cropped();
830         postMenuDraw();
831
832         frametime = 0;
833 }
834
835 void m_display()
836 {
837         Menu_Active = true;
838         setkeydest(KEY_MENU);
839         setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
840
841         if(!menuInitialized)
842                 return;
843
844         if(mouseButtonsPressed)
845                 main.mouseRelease(main, menuMousePos);
846         mouseButtonsPressed = 0;
847
848         main.focusEnter(main);
849         main.showNotify(main);
850 }
851
852 void m_hide()
853 {
854         Menu_Active = false;
855         setkeydest(KEY_GAME);
856         setmousetarget(MT_CLIENT);
857
858         if(!menuInitialized)
859                 return;
860
861         main.focusLeave(main);
862         main.hideNotify(main);
863 }
864
865 void m_toggle(float mode)
866 {
867         if(Menu_Active)
868         {
869                 if (mode == 1)
870                         return;
871                 m_hide();
872         }
873         else
874         {
875                 if (mode == 0)
876                         return;
877                 m_display();
878         }
879 }
880
881 void Shutdown()
882 {
883         entity e;
884
885         m_hide();
886         for(e = NULL; (e = nextent(e)) != NULL; )
887         {
888                 if(e.classname != "vtbl")
889                         if(e.destroy)
890                                 e.destroy(e);
891         }
892 }
893
894 void m_focus_item_chain(entity outermost, entity innermost)
895 {
896         if(innermost.parent != outermost)
897                 m_focus_item_chain(outermost, innermost.parent);
898         innermost.parent.setFocus(innermost.parent, innermost);
899 }
900
901 void m_activate_window(entity wnd)
902 {
903         entity par;
904         par = wnd.parent;
905         if(par)
906                 m_activate_window(par);
907
908         if(par.instanceOfModalController)
909         {
910                 if(wnd.tabSelectingButton)
911                         // tabs
912                         TabButton_Click(wnd.tabSelectingButton, wnd);
913                 else
914                         // root
915                         par.initializeDialog(par, wnd);
916         }
917         else if(par.instanceOfNexposee)
918         {
919                 // nexposee (sorry for violating abstraction here)
920                 par.selectedChild = wnd;
921                 par.animationState = 1;
922                 Container_setFocus(par, NULL);
923         }
924         else if(par.instanceOfContainer)
925         {
926                 // other containers
927                 if(par.focused)
928                         par.setFocus(par, wnd);
929         }
930 }
931
932 void m_setpointerfocus(entity wnd)
933 {
934         if(wnd.instanceOfContainer)
935         {
936                 entity focus = wnd.preferredFocusedGrandChild(wnd);
937                 if(focus)
938                 {
939                         menuMousePos = focus.origin + 0.5 * focus.size;
940                         menuMousePos_x *= 1 / conwidth;
941                         menuMousePos_y *= 1 / conheight;
942                         if(wnd.focused) // why does this never happen?
943                                 m_focus_item_chain(wnd, focus);
944                 }
945         }
946 }
947
948 void m_goto(string itemname)
949 {
950         entity e;
951         if(!menuInitialized)
952         {
953                 if(m_goto_buffer)
954                         strunzone(m_goto_buffer);
955                 m_goto_buffer = strzone(itemname);
956                 return;
957         }
958         if(itemname == "") // this can be called by GameCommand
959         {
960                 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
961                 {
962                         m_hide();
963                 }
964                 else
965                 {
966                         m_activate_window(main.mainNexposee);
967                         m_display();
968                 }
969         }
970         else
971         {
972                 for(e = NULL; (e = find(e, name, itemname)); )
973                         if(e.classname != "vtbl")
974                                 break;
975                 if(e)
976                 {
977                         m_hide();
978                         m_activate_window(e);
979                         m_setpointerfocus(e);
980                         m_display();
981                 }
982         }
983 }