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