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