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