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