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