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;
19 float conwidth_s, conheight_s, vidwidth_s, vidheight_s, vidpixelheight_s,
20 realconwidth, realconheight;
25 vidwidth_s = vidheight_s = vidpixelheight_s = 0; // Force updateConwidths on next draw.
34 gamestatus = gamestatus | GAME_ISSERVER;
35 if(clientstate() == CS_CONNECTED || isdemo())
36 gamestatus = gamestatus | GAME_CONNECTED;
38 gamestatus = gamestatus | GAME_DEVELOPER;
44 cvar_set("_menu_alpha", "0");
45 prvm_language = cvar_string("prvm_language");
46 if(prvm_language == "")
49 cvar_set("prvm_language", prvm_language);
50 localcmd("\nmenu_restart\n");
53 prvm_language = strzone(prvm_language);
54 cvar_set("_menu_prvm_language", prvm_language);
57 dprintf("^4MQC Build information: ^1%s\n", WATERMARK);
60 // list all game dirs (TEST)
67 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
70 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
74 // needs to be done so early because of the constants they create
75 CALL_ACCUMULATED_FUNCTION(RegisterWeapons);
76 CALL_ACCUMULATED_FUNCTION(RegisterGametypes);
78 RegisterSLCategories();
80 float ddsload = cvar("r_texture_dds_load");
81 float texcomp = cvar("gl_texturecompression");
83 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
84 localcmd("\nr_restart\n");
88 if(cvar("_menu_initialized")) // always show menu after menu_restart
92 cvar_set("_menu_initialized", "1");
97 const float MENU_ASPECT = 1.25; // 1280x1024
99 void draw_reset_cropped()
101 draw_reset(conwidth, conheight, 0.5 * (realconwidth - conwidth), 0.5 * (realconheight - conheight));
103 void draw_reset_full()
105 draw_reset(realconwidth, realconheight, 0, 0);
108 void UpdateConWidthHeight(float w, float h, float p)
110 if (w != vidwidth_s || h != vidheight_s || p != vidpixelheight_s)
112 if (updateConwidths(w, h, p))
113 localcmd(sprintf("\nexec %s\n", cvar_string("menu_font_cfg")));
116 vidpixelheight_s = p;
118 conwidth_s = conwidth;
119 conheight_s = conheight;
120 realconwidth = cvar("vid_conwidth");
121 realconheight = cvar("vid_conheight");
122 if(realconwidth / realconheight > MENU_ASPECT)
125 conwidth = realconheight * MENU_ASPECT;
126 conheight = realconheight;
131 conwidth = realconwidth;
132 conheight = realconwidth / MENU_ASPECT;
136 if(conwidth_s != conwidth || conheight_s != conheight)
138 draw_reset_cropped();
139 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
144 vidwidth_s = vidheight_s = vidpixelheight_s = 0; // retry next frame
148 string m_goto_buffer;
149 void m_init_delayed()
151 float fh, glob, n, i;
154 draw_reset_cropped();
162 if(cvar_string("menu_skin") != "")
164 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
165 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
168 if(cvar_defstring("menu_skin") != "")
170 cvar_set("menu_skin", cvar_defstring("menu_skin"));
171 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
172 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
176 draw_currentSkin = "gfx/menu/default";
177 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
181 error("cannot load any menu skin\n");
183 draw_currentSkin = strzone(draw_currentSkin);
184 while((s = fgets(fh)))
186 // these two are handled by skinlist.qc
187 if(substring(s, 0, 6) == "title ")
189 if(substring(s, 0, 7) == "author ")
191 n = tokenize_console(s);
193 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
197 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), true, true);
200 n = search_getsize(glob);
201 for(i = 0; i < n; ++i)
202 precache_pic(search_getfilename(glob, i));
206 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
209 anim = spawnAnimHost();
210 main = spawnMainWindow(); main.configureMainWindow(main);
213 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
216 menuMousePos = '0.5 0.5 0';
222 m_goto(m_goto_buffer);
223 strunzone(m_goto_buffer);
224 m_goto_buffer = string_null;
228 m_display(); // delayed menu display
231 void m_keyup (float key, float ascii)
237 draw_reset_cropped();
238 main.keyUp(main, key, ascii, menuShiftState);
239 if(key >= K_MOUSE1 && key <= K_MOUSE3)
241 --mouseButtonsPressed;
242 if(!mouseButtonsPressed)
243 main.mouseRelease(main, menuMousePos);
244 if(mouseButtonsPressed < 0)
246 mouseButtonsPressed = 0;
247 dprint("Warning: released an already released button\n");
250 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
251 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
252 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
255 void m_keydown(float key, float ascii)
263 if(key >= K_MOUSE1 && key <= K_MOUSE3)
265 // detect a click outside of the game window
266 vector p = getmousepos();
267 if(p.x < 0 || p.x > realconwidth || p.y < 0 || p.y > realconheight)
269 ++mouseButtonsPressed;
279 e.keyGrabbed(e, key, ascii);
283 draw_reset_cropped();
284 if(key >= K_MOUSE1 && key <= K_MOUSE3)
285 if(!mouseButtonsPressed)
286 main.mousePress(main, menuMousePos);
287 if(!main.keyDown(main, key, ascii, menuShiftState))
289 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
290 m_hide(); // disable menu on unhandled ESC
292 if(key >= K_MOUSE1 && key <= K_MOUSE3)
294 ++mouseButtonsPressed;
295 if(mouseButtonsPressed > 10)
297 mouseButtonsPressed = 10;
298 dprint("Warning: pressed an already pressed button\n");
301 if(key == K_ALT) menuShiftState |= S_ALT;
302 if(key == K_CTRL) menuShiftState |= S_CTRL;
303 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
306 const float SCALEMODE_CROP = 0;
307 const float SCALEMODE_LETTERBOX = 1;
308 const float SCALEMODE_WIDTH = 2;
309 const float SCALEMODE_HEIGHT = 3;
310 const float SCALEMODE_STRETCH = 4;
311 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
313 vector sz, org, isz, isz_w, isz_h;
314 float width_is_larger;
316 sz = draw_PictureSize(img);
317 width_is_larger = (sz.x * draw_scale.y >= sz.y * draw_scale.x);
318 isz_w = '1 0 0' + '0 1 0' * ((sz.y / sz.x) * (draw_scale.x / draw_scale.y));
319 isz_h = '0 1 0' + '1 0 0' * ((sz.x / sz.y) * (draw_scale.y / draw_scale.x));
325 isz = (width_is_larger ? isz_h : isz_w);
327 case SCALEMODE_LETTERBOX:
328 isz = (width_is_larger ? isz_w : isz_h);
330 case SCALEMODE_WIDTH:
333 case SCALEMODE_HEIGHT:
336 case SCALEMODE_STRETCH:
341 org = eX * (algn.x * (1 - isz.x)) + eY * (algn.y * (1 - isz.y));
342 draw_Picture(org, img, isz, '1 1 1', a);
345 void drawBackground(string img, float a, string algn, float force1)
347 if(main.mainNexposee.ModalController_state == 0)
357 scalemode = SCALEMODE_CROP;
360 for(i = 0; i < strlen(algn); ++i)
362 c = substring(algn, i, 1);
365 case "c": scalemode = SCALEMODE_CROP; goto nopic;
366 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
367 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
368 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
369 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
370 case "1": case "4": case "7": v_x = 0.0; break;
371 case "2": case "5": case "8": v_x = 0.5; break;
372 case "3": case "6": case "9": v_x = 1.0; break;
373 default: v_x = random(); break;
377 case "7": case "8": case "9": v_y = 0.0; break;
378 case "4": case "5": case "6": v_y = 0.5; break;
379 case "1": case "2": case "3": v_y = 1.0; break;
380 default: v_y = random(); break;
383 draw_Picture_Aligned(v, scalemode, img, a);
385 // force all secondary layers to use alpha 1. Prevents ugly issues
386 // with overlap. It's a flag because it cannot be used for the
388 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
390 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
397 float menu_tooltips_old;
398 vector menuTooltipAveragedMousePos;
399 entity menuTooltipItem;
400 vector menuTooltipOrigin;
401 vector menuTooltipSize;
402 float menuTooltipAlpha;
403 string menuTooltipText;
404 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
405 float m_testmousetooltipbox(vector pos)
407 if(pos.x >= menuTooltipOrigin.x && pos.x < menuTooltipOrigin.x + menuTooltipSize.x)
408 if(pos.y >= menuTooltipOrigin.y && pos.y < menuTooltipOrigin.y + menuTooltipSize.y)
412 float m_testtooltipbox(vector tooltippos)
418 if(tooltippos.x + menuTooltipSize.x > 1)
420 if(tooltippos.y + menuTooltipSize.y > 1)
422 menuTooltipOrigin = tooltippos;
425 float m_allocatetooltipbox(vector pos)
427 vector avoidplus, avoidminus;
430 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
431 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
434 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize.x;
435 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize.y;
440 if(m_testtooltipbox(v))
444 v_x = pos.x - menuTooltipSize.x * 0.5;
445 if(m_testtooltipbox(v))
449 v_x = pos.x - avoidminus.x;
450 if(m_testtooltipbox(v))
454 v_y = pos.y - avoidminus.y;
455 if(m_testtooltipbox(v))
459 v_x = pos.x - menuTooltipSize.x * 0.5;
460 if(m_testtooltipbox(v))
464 v_x = pos.x + avoidplus.x;
465 if(m_testtooltipbox(v))
470 entity m_findtooltipitem(entity root, vector pos)
478 while(it.instanceOfContainer)
480 while(it.instanceOfNexposee && it.focusedChild)
482 it = it.focusedChild;
483 pos = globalToBox(pos, it.Container_origin, it.Container_size);
485 if(it.instanceOfNexposee)
487 it = it.itemFromPoint(it, pos);
490 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
494 else if(it.instanceOfModalController)
495 it = it.focusedChild;
497 it = it.itemFromPoint(it, pos);
502 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
504 pos = globalToBox(pos, it.Container_origin, it.Container_size);
511 if (menu_tooltips == 2)
514 if (menuTooltipItem.cvarName)
516 if (getCvarsMulti(menuTooltipItem))
517 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
519 s = strcat("[", menuTooltipItem.cvarName, "]");
521 else if (menuTooltipItem.onClickCommand)
522 s = strcat("<", menuTooltipItem.onClickCommand, ">");
524 return menuTooltipItem.tooltip;
525 if (menuTooltipItem.tooltip)
526 return strcat(menuTooltipItem.tooltip, " ", s);
529 return menuTooltipItem.tooltip;
531 void m_tooltip(vector pos)
538 menu_tooltips = cvar("menu_tooltips");
541 // don't return immediately, fade out the active tooltip first
542 if (menuTooltipItem == world)
545 menu_tooltips_old = menu_tooltips;
549 f = bound(0, frametime * 2, 1);
550 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
551 f = vlen(pos - menuTooltipAveragedMousePos);
553 it = m_findtooltipitem(main, pos);
557 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
559 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
560 if(it != menuTooltipItem)
562 switch(menuTooltipState)
567 // another item: fade out first
568 menuTooltipState = 2;
573 menuTooltipState = 1;
574 menuTooltipItem = it;
576 menuTooltipOrigin_x = -1; // unallocated
579 strunzone(menuTooltipText);
580 menuTooltipText = strzone(gettooltip());
584 getWrappedLine_remaining = menuTooltipText;
585 while(getWrappedLine_remaining)
587 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
589 f = draw_TextWidth(s, false, fontsize);
593 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
594 menuTooltipSize_y = i * fontsize.y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
595 menuTooltipSize_z = 0;
599 // changing item while fading in: fade out first
600 menuTooltipState = 2;
603 // changing item while fading out: can't
607 else if(menuTooltipState == 2) // re-fade in?
608 menuTooltipState = 1;
611 if(!m_testmousetooltipbox(pos))
612 menuTooltipState = 2; // fade out if mouse touches it
614 switch(menuTooltipState)
617 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
618 if(menuTooltipAlpha == 1)
619 menuTooltipState = 0;
622 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
623 if(menuTooltipAlpha == 0)
625 menuTooltipState = 0;
626 menuTooltipItem = world;
631 if(menuTooltipItem == world)
635 strunzone(menuTooltipText);
636 menuTooltipText = string_null;
642 if(menu_tooltips != menu_tooltips_old)
644 if (menu_tooltips != 0 && menu_tooltips_old != 0)
645 menuTooltipItem = world; // reload tooltip next frame
646 menu_tooltips_old = menu_tooltips;
648 else if(menuTooltipOrigin.x < 0) // unallocated?
649 m_allocatetooltipbox(pos);
651 if(menuTooltipOrigin.x >= 0)
654 p = SKINBORDER_TOOLTIP;
656 p.y *= 1 / conheight;
657 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
658 p = menuTooltipOrigin;
659 p.x += SKINMARGIN_TOOLTIP_x / conwidth;
660 p.y += SKINMARGIN_TOOLTIP_y / conheight;
661 getWrappedLine_remaining = menuTooltipText;
662 while(getWrappedLine_remaining)
664 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
665 draw_Text(p, s, fontsize, SKINCOLOR_TOOLTIP, SKINALPHA_TOOLTIP * menuTooltipAlpha, false);
672 void m_draw(float width, float height)
679 execute_next_frame();
681 menuMouseMode = cvar("menu_mouse_absolute");
686 UpdateConWidthHeight(width, height, cvar("vid_pixelheight"));
690 // TODO draw an info image about this situation
694 if(!menuNotTheFirstFrame)
696 menuNotTheFirstFrame = 1;
698 if(!cvar("menu_video_played"))
700 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.wav\n");
701 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
703 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
704 localcmd("set menu_video_played 1\n");
708 realFrametime = frametime = min(0.2, t - menuPrevTime);
712 t = cvar("menu_slowmo");
723 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
724 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
729 if(cvar("cl_capturevideo"))
730 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
732 prevMenuAlpha = menuAlpha;
735 if(menuAlpha == 0 && menuLogoAlpha < 2)
737 menuLogoAlpha = menuLogoAlpha + frametime * 2;
741 menuAlpha = min(1, menuAlpha + frametime * 5);
747 menuAlpha = max(0, menuAlpha - frametime * 5);
751 draw_reset_cropped();
753 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
755 if(menuLogoAlpha > 0)
758 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
759 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, true);
760 draw_reset_cropped();
761 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
763 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
764 draw_drawMousePointer(menuMousePos);
769 else if(SKINALPHA_BACKGROUND_INGAME)
774 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, false);
775 draw_reset_cropped();
779 if(menuAlpha != prevMenuAlpha)
780 cvar_set("_menu_alpha", ftos(menuAlpha));
782 draw_reset_cropped();
784 draw_reset_cropped();
788 if(prevMenuAlpha > 0)
789 main.initializeDialog(main, main.firstChild);
790 draw_reset_cropped();
795 draw_alpha *= menuAlpha;
799 // do not update mouse position
800 // it prevents mouse jumping to '0 0 0' when menu is fading out
802 else if(menuMouseMode)
805 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
806 if(newMouse != '0 0 0')
807 if(newMouse != menuMousePos)
809 menuMousePos = newMouse;
810 if(mouseButtonsPressed)
811 main.mouseDrag(main, menuMousePos);
813 main.mouseMove(main, menuMousePos);
820 vector dMouse, minpos, maxpos;
821 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
822 if(dMouse != '0 0 0')
824 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
825 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
826 dMouse = globalToBoxSize(dMouse, draw_scale);
827 menuMousePos += dMouse * cvar("menu_mouse_speed");
828 menuMousePos_x = bound(minpos.x, menuMousePos.x, maxpos.x);
829 menuMousePos_y = bound(minpos.y, menuMousePos.y, maxpos.y);
830 if(mouseButtonsPressed)
831 main.mouseDrag(main, menuMousePos);
833 main.mouseMove(main, menuMousePos);
839 m_tooltip(menuMousePos);
841 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
843 draw_drawMousePointer(menuMousePos);
845 draw_reset_cropped();
854 setkeydest(KEY_MENU);
855 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
860 if(mouseButtonsPressed)
861 main.mouseRelease(main, menuMousePos);
862 mouseButtonsPressed = 0;
864 main.focusEnter(main);
865 main.showNotify(main);
871 setkeydest(KEY_GAME);
872 setmousetarget(MT_CLIENT);
877 main.focusLeave(main);
878 main.hideNotify(main);
881 void m_toggle(float mode)
902 for(e = NULL; (e = nextent(e)) != NULL; )
904 if(e.classname != "vtbl")
910 void m_focus_item_chain(entity outermost, entity innermost)
912 if(innermost.parent != outermost)
913 m_focus_item_chain(outermost, innermost.parent);
914 innermost.parent.setFocus(innermost.parent, innermost);
917 void m_activate_window(entity wnd)
922 m_activate_window(par);
924 if(par.instanceOfModalController)
926 if(wnd.tabSelectingButton)
928 TabButton_Click(wnd.tabSelectingButton, wnd);
931 par.initializeDialog(par, wnd);
933 else if(par.instanceOfNexposee)
935 // nexposee (sorry for violating abstraction here)
936 par.selectedChild = wnd;
937 par.animationState = 1;
938 Container_setFocus(par, NULL);
940 else if(par.instanceOfContainer)
944 par.setFocus(par, wnd);
948 void m_setpointerfocus(entity wnd)
950 if(wnd.instanceOfContainer)
952 entity focus = wnd.preferredFocusedGrandChild(wnd);
955 menuMousePos = focus.origin + 0.5 * focus.size;
956 menuMousePos.x *= 1 / conwidth;
957 menuMousePos.y *= 1 / conheight;
958 if(wnd.focused) // why does this never happen?
959 m_focus_item_chain(wnd, focus);
964 void m_goto(string itemname)
970 strunzone(m_goto_buffer);
971 m_goto_buffer = strzone(itemname);
974 if(itemname == "") // this can be called by GameCommand
976 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
982 m_activate_window(main.mainNexposee);
988 for(e = NULL; (e = find(e, name, itemname)); )
989 if(e.classname != "vtbl")
992 if((e) && (!e.requiresConnection || (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))))
995 m_activate_window(e);
996 m_setpointerfocus(e);