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