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