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