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