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);
56 check_unacceptable_compiler_bugs();
59 printf(_("^4MQC Build information: ^1%s\n"), WATERMARK);
62 // list all game dirs (TEST)
69 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
72 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
76 // needs to be done so early because of the constants they create
77 CALL_ACCUMULATED_FUNCTION(RegisterWeapons);
78 CALL_ACCUMULATED_FUNCTION(RegisterGametypes);
80 RegisterSLCategories();
82 float ddsload = cvar("r_texture_dds_load");
83 float texcomp = cvar("gl_texturecompression");
85 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
86 localcmd("\nr_restart\n");
90 if(cvar("_menu_initialized")) // always show menu after menu_restart
94 cvar_set("_menu_initialized", "1");
97 //PlayerInfo_Details();
98 PlayerStats_PlayerDetail_CheckUpdate();
101 const float MENU_ASPECT = 1.25; // 1280x1024
103 void draw_reset_cropped()
105 draw_reset(conwidth, conheight, 0.5 * (realconwidth - conwidth), 0.5 * (realconheight - conheight));
107 void draw_reset_full()
109 draw_reset(realconwidth, realconheight, 0, 0);
112 void UpdateConWidthHeight(float w, float h, float p)
114 if (w != vidwidth_s || h != vidheight_s || p != vidpixelheight_s)
116 if (updateConwidths(w, h, p))
117 localcmd(sprintf("\nexec %s\n", cvar_string("menu_font_cfg")));
120 vidpixelheight_s = p;
122 conwidth_s = conwidth;
123 conheight_s = conheight;
124 realconwidth = cvar("vid_conwidth");
125 realconheight = cvar("vid_conheight");
126 if(realconwidth / realconheight > MENU_ASPECT)
129 conwidth = realconheight * MENU_ASPECT;
130 conheight = realconheight;
135 conwidth = realconwidth;
136 conheight = realconwidth / MENU_ASPECT;
140 if(conwidth_s != conwidth || conheight_s != conheight)
142 draw_reset_cropped();
143 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
148 vidwidth_s = vidheight_s = vidpixelheight_s = 0; // retry next frame
152 string m_goto_buffer;
153 void m_init_delayed()
155 float fh, glob, n, i;
158 draw_reset_cropped();
166 if(cvar_string("menu_skin") != "")
168 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
169 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
172 if(cvar_defstring("menu_skin") != "")
174 cvar_set("menu_skin", cvar_defstring("menu_skin"));
175 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
176 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
180 draw_currentSkin = "gfx/menu/default";
181 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
185 error("cannot load any menu skin\n");
187 draw_currentSkin = strzone(draw_currentSkin);
188 while((s = fgets(fh)))
190 // these two are handled by skinlist.qc
191 if(substring(s, 0, 6) == "title ")
193 if(substring(s, 0, 7) == "author ")
195 n = tokenize_console(s);
197 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
201 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
204 n = search_getsize(glob);
205 for(i = 0; i < n; ++i)
206 precache_pic(search_getfilename(glob, i));
210 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
213 anim = spawnAnimHost();
214 main = spawnMainWindow(); main.configureMainWindow(main);
217 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
220 menuMousePos = '0.5 0.5 0';
226 m_goto(m_goto_buffer);
227 strunzone(m_goto_buffer);
228 m_goto_buffer = string_null;
232 m_display(); // delayed menu display
235 void m_keyup (float key, float ascii)
241 draw_reset_cropped();
242 main.keyUp(main, key, ascii, menuShiftState);
243 if(key >= K_MOUSE1 && key <= K_MOUSE3)
245 --mouseButtonsPressed;
246 if(!mouseButtonsPressed)
247 main.mouseRelease(main, menuMousePos);
248 if(mouseButtonsPressed < 0)
250 mouseButtonsPressed = 0;
251 dprint("Warning: released an already released button\n");
254 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
255 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
256 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
259 void m_keydown(float key, float ascii)
267 if(key >= K_MOUSE1 && key <= K_MOUSE3)
269 // detect a click outside of the game window
270 vector p = getmousepos();
271 if(p_x < 0 || p_x > realconwidth || p_y < 0 || p_y > realconheight)
273 ++mouseButtonsPressed;
283 e.keyGrabbed(e, key, ascii);
287 draw_reset_cropped();
288 if(key >= K_MOUSE1 && key <= K_MOUSE3)
289 if(!mouseButtonsPressed)
290 main.mousePress(main, menuMousePos);
291 if(!main.keyDown(main, key, ascii, menuShiftState))
293 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
294 m_hide(); // disable menu on unhandled ESC
296 if(key >= K_MOUSE1 && key <= K_MOUSE3)
298 ++mouseButtonsPressed;
299 if(mouseButtonsPressed > 10)
301 mouseButtonsPressed = 10;
302 dprint("Warning: pressed an already pressed button\n");
305 if(key == K_ALT) menuShiftState |= S_ALT;
306 if(key == K_CTRL) menuShiftState |= S_CTRL;
307 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
310 const float SCALEMODE_CROP = 0;
311 const float SCALEMODE_LETTERBOX = 1;
312 const float SCALEMODE_WIDTH = 2;
313 const float SCALEMODE_HEIGHT = 3;
314 const float SCALEMODE_STRETCH = 4;
315 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
317 vector sz, org, isz, isz_w, isz_h;
318 float width_is_larger;
320 sz = draw_PictureSize(img);
321 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
322 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
323 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
332 isz = (width_is_larger ? isz_h : isz_w);
334 case SCALEMODE_LETTERBOX:
335 isz = (width_is_larger ? isz_w : isz_h);
337 case SCALEMODE_WIDTH:
340 case SCALEMODE_HEIGHT:
343 case SCALEMODE_STRETCH:
348 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
349 draw_Picture(org, img, isz, '1 1 1', a);
352 void drawBackground(string img, float a, string algn, float force1)
354 if(main.mainNexposee.ModalController_state == 0)
364 scalemode = SCALEMODE_CROP;
367 for(i = 0; i < strlen(algn); ++i)
369 c = substring(algn, i, 1);
372 case "c": scalemode = SCALEMODE_CROP; goto nopic;
373 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
374 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
375 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
376 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
377 case "1": case "4": case "7": v_x = 0.0; break;
378 case "2": case "5": case "8": v_x = 0.5; break;
379 case "3": case "6": case "9": v_x = 1.0; break;
380 default: v_x = random(); break;
384 case "7": case "8": case "9": v_y = 0.0; break;
385 case "4": case "5": case "6": v_y = 0.5; break;
386 case "1": case "2": case "3": v_y = 1.0; break;
387 default: v_y = random(); break;
390 draw_Picture_Aligned(v, scalemode, img, a);
392 // force all secondary layers to use alpha 1. Prevents ugly issues
393 // with overlap. It's a flag because it cannot be used for the
395 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
397 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
404 float menu_tooltips_old;
405 vector menuTooltipAveragedMousePos;
406 entity menuTooltipItem;
407 vector menuTooltipOrigin;
408 vector menuTooltipSize;
409 float menuTooltipAlpha;
410 string menuTooltipText;
411 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
412 float m_testmousetooltipbox(vector pos)
414 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
415 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
419 float m_testtooltipbox(vector tooltippos)
425 if(tooltippos_x + menuTooltipSize_x > 1)
427 if(tooltippos_y + menuTooltipSize_y > 1)
429 menuTooltipOrigin = tooltippos;
432 float m_allocatetooltipbox(vector pos)
434 vector avoidplus, avoidminus;
437 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
438 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
441 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
442 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
447 if(m_testtooltipbox(v))
451 v_x = pos_x - menuTooltipSize_x * 0.5;
452 if(m_testtooltipbox(v))
456 v_x = pos_x - avoidminus_x;
457 if(m_testtooltipbox(v))
461 v_y = pos_y - avoidminus_y;
462 if(m_testtooltipbox(v))
466 v_x = pos_x - menuTooltipSize_x * 0.5;
467 if(m_testtooltipbox(v))
471 v_x = pos_x + avoidplus_x;
472 if(m_testtooltipbox(v))
477 entity m_findtooltipitem(entity root, vector pos)
485 while(it.instanceOfContainer)
487 while(it.instanceOfNexposee && it.focusedChild)
489 it = it.focusedChild;
490 pos = globalToBox(pos, it.Container_origin, it.Container_size);
492 if(it.instanceOfNexposee)
494 it = it.itemFromPoint(it, pos);
497 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
501 else if(it.instanceOfModalController)
502 it = it.focusedChild;
504 it = it.itemFromPoint(it, pos);
509 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
511 pos = globalToBox(pos, it.Container_origin, it.Container_size);
518 if (menu_tooltips == 2)
521 if (menuTooltipItem.cvarName)
523 if (getCvarsMulti(menuTooltipItem))
524 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
526 s = strcat("[", menuTooltipItem.cvarName, "]");
528 else if (menuTooltipItem.onClickCommand)
529 s = strcat("<", menuTooltipItem.onClickCommand, ">");
531 return menuTooltipItem.tooltip;
532 if (menuTooltipItem.tooltip)
533 return strcat(menuTooltipItem.tooltip, " ", s);
536 return menuTooltipItem.tooltip;
538 void m_tooltip(vector pos)
545 menu_tooltips = cvar("menu_tooltips");
548 // don't return immediately, fade out the active tooltip first
549 if (menuTooltipItem == world)
552 menu_tooltips_old = menu_tooltips;
556 f = bound(0, frametime * 2, 1);
557 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
558 f = vlen(pos - menuTooltipAveragedMousePos);
560 it = m_findtooltipitem(main, pos);
564 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
566 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
567 if(it != menuTooltipItem)
569 switch(menuTooltipState)
574 // another item: fade out first
575 menuTooltipState = 2;
580 menuTooltipState = 1;
581 menuTooltipItem = it;
583 menuTooltipOrigin_x = -1; // unallocated
586 strunzone(menuTooltipText);
587 menuTooltipText = strzone(gettooltip());
591 getWrappedLine_remaining = menuTooltipText;
592 while(getWrappedLine_remaining)
594 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
596 f = draw_TextWidth(s, FALSE, fontsize);
600 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
601 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
602 menuTooltipSize_z = 0;
606 // changing item while fading in: fade out first
607 menuTooltipState = 2;
610 // changing item while fading out: can't
614 else if(menuTooltipState == 2) // re-fade in?
615 menuTooltipState = 1;
618 if(!m_testmousetooltipbox(pos))
619 menuTooltipState = 2; // fade out if mouse touches it
621 switch(menuTooltipState)
624 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
625 if(menuTooltipAlpha == 1)
626 menuTooltipState = 0;
629 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
630 if(menuTooltipAlpha == 0)
632 menuTooltipState = 0;
633 menuTooltipItem = world;
638 if(menuTooltipItem == world)
642 strunzone(menuTooltipText);
643 menuTooltipText = string_null;
649 if(menu_tooltips != menu_tooltips_old)
651 if (menu_tooltips != 0 && menu_tooltips_old != 0)
652 menuTooltipItem = world; // reload tooltip next frame
653 menu_tooltips_old = menu_tooltips;
655 else if(menuTooltipOrigin_x < 0) // unallocated?
656 m_allocatetooltipbox(pos);
658 if(menuTooltipOrigin_x >= 0)
661 p = SKINBORDER_TOOLTIP;
663 p_y *= 1 / conheight;
664 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
665 p = menuTooltipOrigin;
666 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
667 p_y += SKINMARGIN_TOOLTIP_y / conheight;
668 getWrappedLine_remaining = menuTooltipText;
669 while(getWrappedLine_remaining)
671 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
672 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
679 void m_draw(float width, float height)
686 execute_next_frame();
688 menuMouseMode = cvar("menu_mouse_absolute");
693 UpdateConWidthHeight(width, height, cvar("vid_pixelheight"));
697 // TODO draw an info image about this situation
701 if(!menuNotTheFirstFrame)
703 menuNotTheFirstFrame = 1;
705 if(!cvar("menu_video_played"))
707 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.wav\n");
708 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
710 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
711 localcmd("set menu_video_played 1\n");
715 realFrametime = frametime = min(0.2, t - menuPrevTime);
719 t = cvar("menu_slowmo");
730 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
731 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
736 if(cvar("cl_capturevideo"))
737 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
739 prevMenuAlpha = menuAlpha;
742 if(menuAlpha == 0 && menuLogoAlpha < 2)
744 menuLogoAlpha = menuLogoAlpha + frametime * 2;
748 menuAlpha = min(1, menuAlpha + frametime * 5);
754 menuAlpha = max(0, menuAlpha - frametime * 5);
758 draw_reset_cropped();
760 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
762 if(menuLogoAlpha > 0)
765 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
766 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
767 draw_reset_cropped();
768 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
770 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
771 draw_drawMousePointer(menuMousePos);
776 else if(SKINALPHA_BACKGROUND_INGAME)
781 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
782 draw_reset_cropped();
786 if(menuAlpha != prevMenuAlpha)
787 cvar_set("_menu_alpha", ftos(menuAlpha));
789 draw_reset_cropped();
791 draw_reset_cropped();
795 if(prevMenuAlpha > 0)
796 main.initializeDialog(main, main.firstChild);
797 draw_reset_cropped();
802 draw_alpha *= menuAlpha;
806 // do not update mouse position
807 // it prevents mouse jumping to '0 0 0' when menu is fading out
809 else if(menuMouseMode)
812 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
813 if(newMouse != '0 0 0')
814 if(newMouse != menuMousePos)
816 menuMousePos = newMouse;
817 if(mouseButtonsPressed)
818 main.mouseDrag(main, menuMousePos);
820 main.mouseMove(main, menuMousePos);
827 vector dMouse, minpos, maxpos;
828 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
829 if(dMouse != '0 0 0')
831 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
832 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
833 dMouse = globalToBoxSize(dMouse, draw_scale);
834 menuMousePos += dMouse * cvar("menu_mouse_speed");
835 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
836 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
837 if(mouseButtonsPressed)
838 main.mouseDrag(main, menuMousePos);
840 main.mouseMove(main, menuMousePos);
846 m_tooltip(menuMousePos);
848 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
850 draw_drawMousePointer(menuMousePos);
852 draw_reset_cropped();
861 setkeydest(KEY_MENU);
862 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
867 if(mouseButtonsPressed)
868 main.mouseRelease(main, menuMousePos);
869 mouseButtonsPressed = 0;
871 main.focusEnter(main);
872 main.showNotify(main);
878 setkeydest(KEY_GAME);
879 setmousetarget(MT_CLIENT);
884 main.focusLeave(main);
885 main.hideNotify(main);
888 void m_toggle(float mode)
909 for(e = NULL; (e = nextent(e)) != NULL; )
911 if(e.classname != "vtbl")
917 void m_focus_item_chain(entity outermost, entity innermost)
919 if(innermost.parent != outermost)
920 m_focus_item_chain(outermost, innermost.parent);
921 innermost.parent.setFocus(innermost.parent, innermost);
924 void m_activate_window(entity wnd)
929 m_activate_window(par);
931 if(par.instanceOfModalController)
933 if(wnd.tabSelectingButton)
935 TabButton_Click(wnd.tabSelectingButton, wnd);
938 par.initializeDialog(par, wnd);
940 else if(par.instanceOfNexposee)
942 // nexposee (sorry for violating abstraction here)
943 par.selectedChild = wnd;
944 par.animationState = 1;
945 Container_setFocus(par, NULL);
947 else if(par.instanceOfContainer)
951 par.setFocus(par, wnd);
955 void m_setpointerfocus(entity wnd)
957 if(wnd.instanceOfContainer)
959 entity focus = wnd.preferredFocusedGrandChild(wnd);
962 menuMousePos = focus.origin + 0.5 * focus.size;
963 menuMousePos_x *= 1 / conwidth;
964 menuMousePos_y *= 1 / conheight;
965 if(wnd.focused) // why does this never happen?
966 m_focus_item_chain(wnd, focus);
971 void m_goto(string itemname)
977 strunzone(m_goto_buffer);
978 m_goto_buffer = strzone(itemname);
981 if(itemname == "") // this can be called by GameCommand
983 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
989 m_activate_window(main.mainNexposee);
995 for(e = NULL; (e = find(e, name, itemname)); )
996 if(e.classname != "vtbl")
999 if((e) && (!e.requiresConnection || (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))))
1002 m_activate_window(e);
1003 m_setpointerfocus(e);