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