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