1 ///////////////////////////////////////////////
3 ///////////////////////
4 // This file belongs to dpmod/darkplaces
5 // AK contains all menu functions (especially the required ones)
6 ///////////////////////////////////////////////
8 float mouseButtonsPressed;
15 float menuInitialized;
16 float menuNotTheFirstFrame;
23 cvar_set("_menu_alpha", "0");
24 prvm_language = strzone(cvar_string("prvm_language"));
26 check_unacceptable_compiler_bugs();
29 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK()));
32 // list all game dirs (TEST)
39 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
42 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
47 float MENU_ASPECT = 1.25; // 1280x1024
48 float MENU_MINHEIGHT = 600;
49 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
50 void draw_reset_cropped()
52 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
54 void draw_reset_full()
56 draw_reset(realconwidth, realconheight, 0, 0);
58 void UpdateConWidthHeight()
60 conwidth_s = conwidth;
61 conheight_s = conheight;
62 realconwidth = cvar("vid_conwidth");
63 realconheight = cvar("vid_conheight");
64 if(realconwidth / realconheight > MENU_ASPECT)
67 conwidth = realconheight * MENU_ASPECT;
68 conheight = realconheight;
73 conwidth = realconwidth;
74 conheight = realconwidth / MENU_ASPECT;
76 screenconwidth = conwidth;
77 screenconheight = conheight;
78 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
80 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
81 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
83 if(conheight < MENU_MINHEIGHT)
85 conwidth *= MENU_MINHEIGHT / conheight;
86 conheight = MENU_MINHEIGHT;
90 if(conwidth_s != conwidth || conheight_s != conheight)
93 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
98 void() m_init_delayed =
100 float fh, glob, n, i;
103 conwidth = conheight = -1;
104 UpdateConWidthHeight();
105 draw_reset_cropped();
116 if(cvar_string("menu_skin") != "")
118 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
119 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
122 if(cvar_defstring("menu_skin") != "")
124 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
125 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
129 draw_currentSkin = "gfx/menu/default";
130 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
132 draw_currentSkin = strzone(draw_currentSkin);
133 while((s = fgets(fh)))
135 // these two are handled by skinlist.qc
136 if(substring(s, 0, 6) == "title ")
138 if(substring(s, 0, 7) == "author ")
140 n = tokenize_console(s);
142 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
146 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
149 n = search_getsize(glob);
150 for(i = 0; i < n; ++i)
151 precache_pic(search_getfilename(glob, i));
155 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
158 anim = spawnAnimHost();
159 main = spawnMainWindow(); main.configureMainWindow(main);
162 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
165 menuMousePos = '0.5 0.5 0';
168 m_display(); // delayed menu display
171 void(float key, float ascii) m_keyup =
177 draw_reset_cropped();
178 main.keyUp(main, key, ascii, menuShiftState);
179 if(key >= K_MOUSE1 && key <= K_MOUSE3)
181 --mouseButtonsPressed;
182 if(!mouseButtonsPressed)
183 main.mouseRelease(main, menuMousePos);
184 if(mouseButtonsPressed < 0)
186 mouseButtonsPressed = 0;
187 dprint("Warning: released an already released button\n");
190 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
191 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
192 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
195 void(float key, float ascii) m_keydown =
206 e.keyGrabbed(e, key, ascii);
210 draw_reset_cropped();
211 if(key >= K_MOUSE1 && key <= K_MOUSE3)
212 if(!mouseButtonsPressed)
213 main.mousePress(main, menuMousePos);
214 if(!main.keyDown(main, key, ascii, menuShiftState))
216 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
217 m_hide(); // disable menu on unhandled ESC
219 if(key >= K_MOUSE1 && key <= K_MOUSE3)
221 ++mouseButtonsPressed;
222 if(mouseButtonsPressed > 10)
224 mouseButtonsPressed = 10;
225 dprint("Warning: pressed an already pressed button\n");
228 if(key == K_ALT) menuShiftState |= S_ALT;
229 if(key == K_CTRL) menuShiftState |= S_CTRL;
230 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
233 float SCALEMODE_CROP = 0;
234 float SCALEMODE_LETTERBOX = 1;
235 float SCALEMODE_WIDTH = 2;
236 float SCALEMODE_HEIGHT = 3;
237 float SCALEMODE_STRETCH = 4;
238 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
240 vector sz, org, isz, isz_w, isz_h;
241 float width_is_larger;
243 sz = draw_PictureSize(img);
244 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
245 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
246 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
252 isz = (width_is_larger ? isz_h : isz_w);
254 case SCALEMODE_LETTERBOX:
255 isz = (width_is_larger ? isz_w : isz_h);
257 case SCALEMODE_WIDTH:
260 case SCALEMODE_HEIGHT:
263 case SCALEMODE_STRETCH:
268 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
269 draw_Picture(org, img, isz, '1 1 1', a);
272 void(string img, float a, string algn, float force1) drawBackground =
274 if(main.mainNexposee.ModalController_state == 0)
284 scalemode = SCALEMODE_CROP;
286 for(i = 0; i < strlen(algn); ++i)
288 c = substring(algn, i, 1);
291 case "c": scalemode = SCALEMODE_CROP; goto nopic;
292 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
293 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
294 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
295 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
296 case "1": case "4": case "7": v_x = 0.0; break;
297 case "2": case "5": case "8": v_x = 0.5; break;
298 case "3": case "6": case "9": v_x = 1.0; break;
299 default: v_x = random(); break;
303 case "7": case "8": case "9": v_y = 0.0; break;
304 case "4": case "5": case "6": v_y = 0.5; break;
305 case "1": case "2": case "3": v_y = 1.0; break;
306 default: v_y = random(); break;
309 draw_Picture_Aligned(v, scalemode, img, a);
311 // force all secondary layers to use alpha 1. Prevents ugly issues
312 // with overlap. It's a flag because it cannot be used for the
314 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
316 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
322 vector menuTooltipAveragedMousePos;
323 entity menuTooltipItem;
324 vector menuTooltipOrigin;
325 vector menuTooltipSize;
326 float menuTooltipAlpha;
327 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
328 float m_testmousetooltipbox(vector pos)
330 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
331 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
335 float m_testtooltipbox(vector tooltippos)
341 if(tooltippos_x + menuTooltipSize_x > 1)
343 if(tooltippos_y + menuTooltipSize_y > 1)
345 menuTooltipOrigin = tooltippos;
348 float m_allocatetooltipbox(vector pos)
350 vector avoidplus, avoidminus;
353 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
354 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
357 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
358 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
363 if(m_testtooltipbox(v))
367 v_x = pos_x - menuTooltipSize_x * 0.5;
368 if(m_testtooltipbox(v))
372 v_x = pos_x - avoidminus_x;
373 if(m_testtooltipbox(v))
377 v_y = pos_y - avoidminus_y;
378 if(m_testtooltipbox(v))
382 v_x = pos_x - menuTooltipSize_x * 0.5;
383 if(m_testtooltipbox(v))
387 v_x = pos_x + avoidplus_x;
388 if(m_testtooltipbox(v))
393 entity m_findtooltipitem(entity root, vector pos)
401 while(it.instanceOfContainer)
403 while(it.instanceOfNexposee && it.focusedChild)
405 it = it.focusedChild;
406 pos = globalToBox(pos, it.Container_origin, it.Container_size);
408 if(it.instanceOfNexposee)
410 it = it.itemFromPoint(it, pos);
415 else if(it.instanceOfModalController)
416 it = it.focusedChild;
418 it = it.itemFromPoint(it, pos);
423 pos = globalToBox(pos, it.Container_origin, it.Container_size);
428 void m_tooltip(vector pos)
435 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
437 f = bound(0, frametime * 2, 1);
438 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
439 f = vlen(pos - menuTooltipAveragedMousePos);
442 it = m_findtooltipitem(main, pos);
446 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
447 if(it != menuTooltipItem)
449 switch(menuTooltipState)
454 // another item: fade out first
455 menuTooltipState = 2;
460 menuTooltipState = 1;
461 menuTooltipItem = it;
463 menuTooltipOrigin_x = -1; // unallocated
466 getWrappedLine_remaining = it.tooltip;
467 while(getWrappedLine_remaining)
469 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
471 f = draw_TextWidth(s, FALSE, fontsize);
475 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
476 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
477 menuTooltipSize_z = 0;
481 // changing item while fading in: fade out first
482 menuTooltipState = 2;
485 // changing item while fading out: can't
489 else if(menuTooltipState == 2) // re-fade in?
490 menuTooltipState = 1;
493 if(!m_testmousetooltipbox(pos))
494 menuTooltipState = 2; // fade out if mouse touches it
496 switch(menuTooltipState)
499 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
500 if(menuTooltipAlpha == 1)
501 menuTooltipState = 0;
504 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
505 if(menuTooltipAlpha == 0)
507 menuTooltipState = 0;
508 menuTooltipItem = world;
515 if(menuTooltipOrigin_x < 0) // unallocated?
516 m_allocatetooltipbox(pos);
518 if(menuTooltipOrigin_x >= 0)
521 p = SKINBORDER_TOOLTIP;
523 p_y *= 1 / conheight;
524 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
525 p = menuTooltipOrigin;
526 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
527 p_y += SKINMARGIN_TOOLTIP_y / conheight;
528 getWrappedLine_remaining = menuTooltipItem.tooltip;
529 while(getWrappedLine_remaining)
531 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
532 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
544 menuMouseMode = cvar("menu_mouse_absolute");
550 UpdateConWidthHeight();
554 // TODO draw an info image about this situation
558 if(!menuNotTheFirstFrame)
560 menuNotTheFirstFrame = 1;
562 if(!cvar("menu_video_played"))
564 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
565 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
567 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
568 localcmd("set menu_video_played 1\n");
572 realFrametime = frametime = min(0.2, t - menuPrevTime);
576 t = cvar("menu_slowmo");
587 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
588 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
593 if(cvar("cl_capturevideo"))
594 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
598 gamestatus = gamestatus | GAME_ISSERVER;
599 if(clientstate() == CS_CONNECTED)
600 gamestatus = gamestatus | GAME_CONNECTED;
601 if(cvar("developer"))
602 gamestatus = gamestatus | GAME_DEVELOPER;
604 prevMenuAlpha = menuAlpha;
607 if(menuAlpha == 0 && menuLogoAlpha < 2)
609 menuLogoAlpha = menuLogoAlpha + frametime * 2;
613 menuAlpha = min(1, menuAlpha + frametime * 5);
619 menuAlpha = max(0, menuAlpha - frametime * 5);
623 draw_reset_cropped();
625 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
627 if(menuLogoAlpha > 0)
630 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
631 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
632 draw_reset_cropped();
633 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
635 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
636 draw_drawMousePointer(menuMousePos);
641 else if(SKINALPHA_BACKGROUND_INGAME)
646 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
647 draw_reset_cropped();
651 if(menuAlpha != prevMenuAlpha)
652 cvar_set("_menu_alpha", ftos(menuAlpha));
654 draw_reset_cropped();
656 draw_reset_cropped();
660 if(prevMenuAlpha > 0)
661 main.initializeDialog(main, main.firstChild);
662 draw_reset_cropped();
667 draw_alpha *= menuAlpha;
672 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
673 if(newMouse != '0 0 0')
674 if(newMouse != menuMousePos)
676 menuMousePos = newMouse;
677 if(mouseButtonsPressed)
678 main.mouseDrag(main, menuMousePos);
680 main.mouseMove(main, menuMousePos);
687 vector dMouse, minpos, maxpos;
688 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
689 if(dMouse != '0 0 0')
691 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
692 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
693 dMouse = globalToBoxSize(dMouse, draw_scale);
694 menuMousePos += dMouse * cvar("menu_mouse_speed");
695 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
696 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
697 if(mouseButtonsPressed)
698 main.mouseDrag(main, menuMousePos);
700 main.mouseMove(main, menuMousePos);
706 m_tooltip(menuMousePos);
708 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
710 draw_drawMousePointer(menuMousePos);
712 draw_reset_cropped();
721 setkeydest(KEY_MENU);
722 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
727 if(mouseButtonsPressed)
728 main.mouseRelease(main, menuMousePos);
729 mouseButtonsPressed = 0;
731 main.focusEnter(main);
732 main.showNotify(main);
738 setkeydest(KEY_GAME);
739 setmousetarget(MT_CLIENT);
744 main.focusLeave(main);
745 main.hideNotify(main);
761 for(e = NULL; (e = nextent(e)) != NULL; )
763 if(e.classname != "vtbl")
769 void m_focus_item_chain(entity outermost, entity innermost)
771 if(innermost.parent != outermost)
772 m_focus_item_chain(outermost, innermost.parent);
773 innermost.parent.setFocus(innermost.parent, innermost);
776 void m_activate_window(entity wnd)
781 m_activate_window(par);
783 if(par.instanceOfModalController)
785 if(wnd.tabSelectingButton)
787 TabButton_Click(wnd.tabSelectingButton, wnd);
790 par.initializeDialog(par, wnd);
792 else if(par.instanceOfNexposee)
794 // nexposee (sorry for violating abstraction here)
795 par.selectedChild = wnd;
796 par.animationState = 1;
797 Container_setFocus(par, NULL);
799 else if(par.instanceOfContainer)
803 par.setFocus(par, wnd);
807 void m_setpointerfocus(entity wnd)
809 if(wnd.instanceOfContainer)
811 entity focus = wnd.preferredFocusedGrandChild(wnd);
814 menuMousePos = focus.origin + 0.5 * focus.size;
815 menuMousePos_x *= 1 / conwidth;
816 menuMousePos_y *= 1 / conheight;
817 if(wnd.focused) // why does this never happen?
818 m_focus_item_chain(wnd, focus);
823 void(string itemname) m_goto =
828 if(itemname == "") // this can be called by GameCommand
830 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
834 m_activate_window(main.mainNexposee);
840 for(e = NULL; (e = findstring(e, name, itemname)); )
841 if(e.classname != "vtbl")
846 m_activate_window(e);
847 m_setpointerfocus(e);
853 void() m_goto_skin_selector =
857 // TODO add code to switch back to the skin selector (no idea how to do it now)
858 m_goto("skinselector");
861 void() m_goto_language_selector =
865 // TODO add code to switch back to the language selector (no idea how to do it now)
866 m_goto("languageselector");
869 void() m_goto_video_settings =
873 // TODO add code to switch back to the video settings (no idea how to do it now)
874 m_goto("videosettings");