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;
31 cvar_set("_menu_alpha", "0");
32 prvm_language = strzone(cvar_string("prvm_language"));
34 check_unacceptable_compiler_bugs();
37 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK()));
40 // list all game dirs (TEST)
47 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
50 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
55 float MENU_ASPECT = 1.25; // 1280x1024
56 float MENU_MINHEIGHT = 600;
57 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
58 void draw_reset_cropped()
60 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
62 void draw_reset_full()
64 draw_reset(realconwidth, realconheight, 0, 0);
66 void UpdateConWidthHeight()
68 conwidth_s = conwidth;
69 conheight_s = conheight;
70 realconwidth = cvar("vid_conwidth");
71 realconheight = cvar("vid_conheight");
72 if(realconwidth / realconheight > MENU_ASPECT)
75 conwidth = realconheight * MENU_ASPECT;
76 conheight = realconheight;
81 conwidth = realconwidth;
82 conheight = realconwidth / MENU_ASPECT;
84 screenconwidth = conwidth;
85 screenconheight = conheight;
86 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
88 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
89 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
91 if(conheight < MENU_MINHEIGHT)
93 conwidth *= MENU_MINHEIGHT / conheight;
94 conheight = MENU_MINHEIGHT;
98 if(conwidth_s != conwidth || conheight_s != conheight)
100 draw_reset_cropped();
101 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
106 void m_init_delayed()
108 float fh, glob, n, i;
111 conwidth = conheight = -1;
112 UpdateConWidthHeight();
113 draw_reset_cropped();
124 if(cvar_string("menu_skin") != "")
126 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
127 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
130 if(cvar_defstring("menu_skin") != "")
132 cvar_set("menu_skin", cvar_defstring("menu_skin"));
133 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
134 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
138 draw_currentSkin = "gfx/menu/default";
139 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
143 error("cannot load any menu skin\n");
145 draw_currentSkin = strzone(draw_currentSkin);
146 while((s = fgets(fh)))
148 // these two are handled by skinlist.qc
149 if(substring(s, 0, 6) == "title ")
151 if(substring(s, 0, 7) == "author ")
153 n = tokenize_console(s);
155 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
159 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
162 n = search_getsize(glob);
163 for(i = 0; i < n; ++i)
164 precache_pic(search_getfilename(glob, i));
168 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
171 anim = spawnAnimHost();
172 main = spawnMainWindow(); main.configureMainWindow(main);
175 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
178 menuMousePos = '0.5 0.5 0';
183 m_display(); // delayed menu display
186 void m_keyup (float key, float ascii)
192 draw_reset_cropped();
193 main.keyUp(main, key, ascii, menuShiftState);
194 if(key >= K_MOUSE1 && key <= K_MOUSE3)
196 --mouseButtonsPressed;
197 if(!mouseButtonsPressed)
198 main.mouseRelease(main, menuMousePos);
199 if(mouseButtonsPressed < 0)
201 mouseButtonsPressed = 0;
202 dprint("Warning: released an already released button\n");
205 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
206 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
207 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
210 void m_keydown(float key, float ascii)
221 e.keyGrabbed(e, key, ascii);
225 draw_reset_cropped();
226 if(key >= K_MOUSE1 && key <= K_MOUSE3)
227 if(!mouseButtonsPressed)
228 main.mousePress(main, menuMousePos);
229 if(!main.keyDown(main, key, ascii, menuShiftState))
231 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
232 m_hide(); // disable menu on unhandled ESC
234 if(key >= K_MOUSE1 && key <= K_MOUSE3)
236 ++mouseButtonsPressed;
237 if(mouseButtonsPressed > 10)
239 mouseButtonsPressed = 10;
240 dprint("Warning: pressed an already pressed button\n");
243 if(key == K_ALT) menuShiftState |= S_ALT;
244 if(key == K_CTRL) menuShiftState |= S_CTRL;
245 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
248 float SCALEMODE_CROP = 0;
249 float SCALEMODE_LETTERBOX = 1;
250 float SCALEMODE_WIDTH = 2;
251 float SCALEMODE_HEIGHT = 3;
252 float SCALEMODE_STRETCH = 4;
253 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
255 vector sz, org, isz, isz_w, isz_h;
256 float width_is_larger;
258 sz = draw_PictureSize(img);
259 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
260 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
261 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
267 isz = (width_is_larger ? isz_h : isz_w);
269 case SCALEMODE_LETTERBOX:
270 isz = (width_is_larger ? isz_w : isz_h);
272 case SCALEMODE_WIDTH:
275 case SCALEMODE_HEIGHT:
278 case SCALEMODE_STRETCH:
283 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
284 draw_Picture(org, img, isz, '1 1 1', a);
287 void drawBackground(string img, float a, string algn, float force1)
289 if(main.mainNexposee.ModalController_state == 0)
299 scalemode = SCALEMODE_CROP;
301 for(i = 0; i < strlen(algn); ++i)
303 c = substring(algn, i, 1);
306 case "c": scalemode = SCALEMODE_CROP; goto nopic;
307 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
308 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
309 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
310 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
311 case "1": case "4": case "7": v_x = 0.0; break;
312 case "2": case "5": case "8": v_x = 0.5; break;
313 case "3": case "6": case "9": v_x = 1.0; break;
314 default: v_x = random(); break;
318 case "7": case "8": case "9": v_y = 0.0; break;
319 case "4": case "5": case "6": v_y = 0.5; break;
320 case "1": case "2": case "3": v_y = 1.0; break;
321 default: v_y = random(); break;
324 draw_Picture_Aligned(v, scalemode, img, a);
326 // force all secondary layers to use alpha 1. Prevents ugly issues
327 // with overlap. It's a flag because it cannot be used for the
329 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
331 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
338 float menu_tooltips_old;
339 vector menuTooltipAveragedMousePos;
340 entity menuTooltipItem;
341 vector menuTooltipOrigin;
342 vector menuTooltipSize;
343 float menuTooltipAlpha;
344 string menuTooltipText;
345 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
346 float m_testmousetooltipbox(vector pos)
348 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
349 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
353 float m_testtooltipbox(vector tooltippos)
359 if(tooltippos_x + menuTooltipSize_x > 1)
361 if(tooltippos_y + menuTooltipSize_y > 1)
363 menuTooltipOrigin = tooltippos;
366 float m_allocatetooltipbox(vector pos)
368 vector avoidplus, avoidminus;
371 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
372 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
375 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
376 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
381 if(m_testtooltipbox(v))
385 v_x = pos_x - menuTooltipSize_x * 0.5;
386 if(m_testtooltipbox(v))
390 v_x = pos_x - avoidminus_x;
391 if(m_testtooltipbox(v))
395 v_y = pos_y - avoidminus_y;
396 if(m_testtooltipbox(v))
400 v_x = pos_x - menuTooltipSize_x * 0.5;
401 if(m_testtooltipbox(v))
405 v_x = pos_x + avoidplus_x;
406 if(m_testtooltipbox(v))
411 entity m_findtooltipitem(entity root, vector pos)
419 while(it.instanceOfContainer)
421 while(it.instanceOfNexposee && it.focusedChild)
423 it = it.focusedChild;
424 pos = globalToBox(pos, it.Container_origin, it.Container_size);
426 if(it.instanceOfNexposee)
428 it = it.itemFromPoint(it, pos);
431 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
435 else if(it.instanceOfModalController)
436 it = it.focusedChild;
438 it = it.itemFromPoint(it, pos);
443 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
445 pos = globalToBox(pos, it.Container_origin, it.Container_size);
452 if (menu_tooltips == 2)
455 if (menuTooltipItem.cvarName)
457 if (getCvarsMulti(menuTooltipItem))
458 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
460 s = strcat("[", menuTooltipItem.cvarName, "]");
462 else if (menuTooltipItem.onClickCommand)
463 s = strcat("<", menuTooltipItem.onClickCommand, ">");
465 return menuTooltipItem.tooltip;
466 if (menuTooltipItem.tooltip)
467 return strcat(menuTooltipItem.tooltip, " ", s);
470 return menuTooltipItem.tooltip;
472 void m_tooltip(vector pos)
479 menu_tooltips = cvar("menu_tooltips");
482 // don't return immediately, fade out the active tooltip first
483 if (menuTooltipItem == world)
486 menu_tooltips_old = menu_tooltips;
490 f = bound(0, frametime * 2, 1);
491 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
492 f = vlen(pos - menuTooltipAveragedMousePos);
494 it = m_findtooltipitem(main, pos);
498 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
500 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
501 if(it != menuTooltipItem)
503 switch(menuTooltipState)
508 // another item: fade out first
509 menuTooltipState = 2;
514 menuTooltipState = 1;
515 menuTooltipItem = it;
517 menuTooltipOrigin_x = -1; // unallocated
520 strunzone(menuTooltipText);
521 menuTooltipText = strzone(gettooltip());
525 getWrappedLine_remaining = menuTooltipText;
526 while(getWrappedLine_remaining)
528 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
530 f = draw_TextWidth(s, FALSE, fontsize);
534 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
535 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
536 menuTooltipSize_z = 0;
540 // changing item while fading in: fade out first
541 menuTooltipState = 2;
544 // changing item while fading out: can't
548 else if(menuTooltipState == 2) // re-fade in?
549 menuTooltipState = 1;
552 if(!m_testmousetooltipbox(pos))
553 menuTooltipState = 2; // fade out if mouse touches it
555 switch(menuTooltipState)
558 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
559 if(menuTooltipAlpha == 1)
560 menuTooltipState = 0;
563 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
564 if(menuTooltipAlpha == 0)
566 menuTooltipState = 0;
567 menuTooltipItem = world;
572 if(menuTooltipItem == world)
576 strunzone(menuTooltipText);
577 menuTooltipText = string_null;
583 if(menu_tooltips != menu_tooltips_old)
585 if (menu_tooltips != 0 && menu_tooltips_old != 0)
586 menuTooltipItem = world; // reload tooltip next frame
587 menu_tooltips_old = menu_tooltips;
589 else if(menuTooltipOrigin_x < 0) // unallocated?
590 m_allocatetooltipbox(pos);
592 if(menuTooltipOrigin_x >= 0)
595 p = SKINBORDER_TOOLTIP;
597 p_y *= 1 / conheight;
598 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
599 p = menuTooltipOrigin;
600 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
601 p_y += SKINMARGIN_TOOLTIP_y / conheight;
602 getWrappedLine_remaining = menuTooltipText;
603 while(getWrappedLine_remaining)
605 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
606 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
618 menuMouseMode = cvar("menu_mouse_absolute");
624 UpdateConWidthHeight();
628 // TODO draw an info image about this situation
632 if(!menuNotTheFirstFrame)
634 menuNotTheFirstFrame = 1;
636 if(!cvar("menu_video_played"))
638 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
639 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
641 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
642 localcmd("set menu_video_played 1\n");
646 realFrametime = frametime = min(0.2, t - menuPrevTime);
650 t = cvar("menu_slowmo");
661 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
662 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
667 if(cvar("cl_capturevideo"))
668 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
672 gamestatus = gamestatus | GAME_ISSERVER;
673 if(clientstate() == CS_CONNECTED)
674 gamestatus = gamestatus | GAME_CONNECTED;
675 if(cvar("developer"))
676 gamestatus = gamestatus | GAME_DEVELOPER;
678 prevMenuAlpha = menuAlpha;
681 if(menuAlpha == 0 && menuLogoAlpha < 2)
683 menuLogoAlpha = menuLogoAlpha + frametime * 2;
687 menuAlpha = min(1, menuAlpha + frametime * 5);
693 menuAlpha = max(0, menuAlpha - frametime * 5);
697 draw_reset_cropped();
699 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
701 if(menuLogoAlpha > 0)
704 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
705 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
706 draw_reset_cropped();
707 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
709 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
710 draw_drawMousePointer(menuMousePos);
715 else if(SKINALPHA_BACKGROUND_INGAME)
720 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
721 draw_reset_cropped();
725 if(menuAlpha != prevMenuAlpha)
726 cvar_set("_menu_alpha", ftos(menuAlpha));
728 draw_reset_cropped();
730 draw_reset_cropped();
734 if(prevMenuAlpha > 0)
735 main.initializeDialog(main, main.firstChild);
736 draw_reset_cropped();
741 draw_alpha *= menuAlpha;
746 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
747 if(newMouse != '0 0 0')
748 if(newMouse != menuMousePos)
750 menuMousePos = newMouse;
751 if(mouseButtonsPressed)
752 main.mouseDrag(main, menuMousePos);
754 main.mouseMove(main, menuMousePos);
761 vector dMouse, minpos, maxpos;
762 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
763 if(dMouse != '0 0 0')
765 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
766 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
767 dMouse = globalToBoxSize(dMouse, draw_scale);
768 menuMousePos += dMouse * cvar("menu_mouse_speed");
769 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
770 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
771 if(mouseButtonsPressed)
772 main.mouseDrag(main, menuMousePos);
774 main.mouseMove(main, menuMousePos);
780 m_tooltip(menuMousePos);
782 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
784 draw_drawMousePointer(menuMousePos);
786 draw_reset_cropped();
795 setkeydest(KEY_MENU);
796 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
801 if(mouseButtonsPressed)
802 main.mouseRelease(main, menuMousePos);
803 mouseButtonsPressed = 0;
805 main.focusEnter(main);
806 main.showNotify(main);
812 setkeydest(KEY_GAME);
813 setmousetarget(MT_CLIENT);
818 main.focusLeave(main);
819 main.hideNotify(main);
835 for(e = NULL; (e = nextent(e)) != NULL; )
837 if(e.classname != "vtbl")
843 void m_focus_item_chain(entity outermost, entity innermost)
845 if(innermost.parent != outermost)
846 m_focus_item_chain(outermost, innermost.parent);
847 innermost.parent.setFocus(innermost.parent, innermost);
850 void m_activate_window(entity wnd)
855 m_activate_window(par);
857 if(par.instanceOfModalController)
859 if(wnd.tabSelectingButton)
861 TabButton_Click(wnd.tabSelectingButton, wnd);
864 par.initializeDialog(par, wnd);
866 else if(par.instanceOfNexposee)
868 // nexposee (sorry for violating abstraction here)
869 par.selectedChild = wnd;
870 par.animationState = 1;
871 Container_setFocus(par, NULL);
873 else if(par.instanceOfContainer)
877 par.setFocus(par, wnd);
881 void m_setpointerfocus(entity wnd)
883 if(wnd.instanceOfContainer)
885 entity focus = wnd.preferredFocusedGrandChild(wnd);
888 menuMousePos = focus.origin + 0.5 * focus.size;
889 menuMousePos_x *= 1 / conwidth;
890 menuMousePos_y *= 1 / conheight;
891 if(wnd.focused) // why does this never happen?
892 m_focus_item_chain(wnd, focus);
897 void m_goto(string itemname)
902 if(itemname == "") // this can be called by GameCommand
904 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
908 m_activate_window(main.mainNexposee);
914 for(e = NULL; (e = findstring(e, name, itemname)); )
915 if(e.classname != "vtbl")
920 m_activate_window(e);
921 m_setpointerfocus(e);
927 void m_goto_skin_selector()
931 // TODO add code to switch back to the skin selector (no idea how to do it now)
932 m_goto("skinselector");
935 void m_goto_language_selector()
939 // TODO add code to switch back to the language selector (no idea how to do it now)
940 m_goto("languageselector");
943 void m_goto_video_settings()
947 // TODO add code to switch back to the video settings (no idea how to do it now)
948 m_goto("videosettings");