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");
98 const float MENU_ASPECT = 1.25; // 1280x1024
100 void draw_reset_cropped()
102 draw_reset(conwidth, conheight, 0.5 * (realconwidth - conwidth), 0.5 * (realconheight - conheight));
104 void draw_reset_full()
106 draw_reset(realconwidth, realconheight, 0, 0);
109 void UpdateConWidthHeight(float w, float h, float p)
111 if (w != vidwidth_s || h != vidheight_s || p != vidpixelheight_s)
113 if (updateConwidths(w, h, p))
114 localcmd(sprintf("\nexec %s\n", cvar_string("menu_font_cfg")));
117 vidpixelheight_s = p;
119 conwidth_s = conwidth;
120 conheight_s = conheight;
121 realconwidth = cvar("vid_conwidth");
122 realconheight = cvar("vid_conheight");
123 if(realconwidth / realconheight > MENU_ASPECT)
126 conwidth = realconheight * MENU_ASPECT;
127 conheight = realconheight;
132 conwidth = realconwidth;
133 conheight = realconwidth / MENU_ASPECT;
137 if(conwidth_s != conwidth || conheight_s != conheight)
139 draw_reset_cropped();
140 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
145 vidwidth_s = vidheight_s = vidpixelheight_s = 0; // retry next frame
149 string m_goto_buffer;
150 void m_init_delayed()
152 float fh, glob, n, i;
155 draw_reset_cropped();
163 if(cvar_string("menu_skin") != "")
165 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
166 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
169 if(cvar_defstring("menu_skin") != "")
171 cvar_set("menu_skin", cvar_defstring("menu_skin"));
172 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
173 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
177 draw_currentSkin = "gfx/menu/default";
178 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
182 error("cannot load any menu skin\n");
184 draw_currentSkin = strzone(draw_currentSkin);
185 while((s = fgets(fh)))
187 // these two are handled by skinlist.qc
188 if(substring(s, 0, 6) == "title ")
190 if(substring(s, 0, 7) == "author ")
192 n = tokenize_console(s);
194 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
198 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
201 n = search_getsize(glob);
202 for(i = 0; i < n; ++i)
203 precache_pic(search_getfilename(glob, i));
207 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
210 anim = spawnAnimHost();
211 main = spawnMainWindow(); main.configureMainWindow(main);
214 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
217 menuMousePos = '0.5 0.5 0';
223 m_goto(m_goto_buffer);
224 strunzone(m_goto_buffer);
225 m_goto_buffer = string_null;
229 m_display(); // delayed menu display
232 void m_keyup (float key, float ascii)
238 draw_reset_cropped();
239 main.keyUp(main, key, ascii, menuShiftState);
240 if(key >= K_MOUSE1 && key <= K_MOUSE3)
242 --mouseButtonsPressed;
243 if(!mouseButtonsPressed)
244 main.mouseRelease(main, menuMousePos);
245 if(mouseButtonsPressed < 0)
247 mouseButtonsPressed = 0;
248 dprint("Warning: released an already released button\n");
251 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
252 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
253 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
256 void m_keydown(float key, float ascii)
264 if(key >= K_MOUSE1 && key <= K_MOUSE3)
266 // detect a click outside of the game window
267 vector p = getmousepos();
268 if(p_x < 0 || p_x > realconwidth || p_y < 0 || p_y > realconheight)
270 ++mouseButtonsPressed;
280 e.keyGrabbed(e, key, ascii);
284 draw_reset_cropped();
285 if(key >= K_MOUSE1 && key <= K_MOUSE3)
286 if(!mouseButtonsPressed)
287 main.mousePress(main, menuMousePos);
288 if(!main.keyDown(main, key, ascii, menuShiftState))
290 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
291 m_hide(); // disable menu on unhandled ESC
293 if(key >= K_MOUSE1 && key <= K_MOUSE3)
295 ++mouseButtonsPressed;
296 if(mouseButtonsPressed > 10)
298 mouseButtonsPressed = 10;
299 dprint("Warning: pressed an already pressed button\n");
302 if(key == K_ALT) menuShiftState |= S_ALT;
303 if(key == K_CTRL) menuShiftState |= S_CTRL;
304 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
307 const float SCALEMODE_CROP = 0;
308 const float SCALEMODE_LETTERBOX = 1;
309 const float SCALEMODE_WIDTH = 2;
310 const float SCALEMODE_HEIGHT = 3;
311 const float SCALEMODE_STRETCH = 4;
312 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
314 vector sz, org, isz, isz_w, isz_h;
315 float width_is_larger;
317 sz = draw_PictureSize(img);
318 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
319 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
320 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
329 isz = (width_is_larger ? isz_h : isz_w);
331 case SCALEMODE_LETTERBOX:
332 isz = (width_is_larger ? isz_w : isz_h);
334 case SCALEMODE_WIDTH:
337 case SCALEMODE_HEIGHT:
340 case SCALEMODE_STRETCH:
345 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
346 draw_Picture(org, img, isz, '1 1 1', a);
349 void drawBackground(string img, float a, string algn, float force1)
351 if(main.mainNexposee.ModalController_state == 0)
361 scalemode = SCALEMODE_CROP;
364 for(i = 0; i < strlen(algn); ++i)
366 c = substring(algn, i, 1);
369 case "c": scalemode = SCALEMODE_CROP; goto nopic;
370 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
371 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
372 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
373 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
374 case "1": case "4": case "7": v_x = 0.0; break;
375 case "2": case "5": case "8": v_x = 0.5; break;
376 case "3": case "6": case "9": v_x = 1.0; break;
377 default: v_x = random(); break;
381 case "7": case "8": case "9": v_y = 0.0; break;
382 case "4": case "5": case "6": v_y = 0.5; break;
383 case "1": case "2": case "3": v_y = 1.0; break;
384 default: v_y = random(); break;
387 draw_Picture_Aligned(v, scalemode, img, a);
389 // force all secondary layers to use alpha 1. Prevents ugly issues
390 // with overlap. It's a flag because it cannot be used for the
392 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
394 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
401 float menu_tooltips_old;
402 vector menuTooltipAveragedMousePos;
403 entity menuTooltipItem;
404 vector menuTooltipOrigin;
405 vector menuTooltipSize;
406 float menuTooltipAlpha;
407 string menuTooltipText;
408 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
409 float m_testmousetooltipbox(vector pos)
411 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
412 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
416 float m_testtooltipbox(vector tooltippos)
422 if(tooltippos_x + menuTooltipSize_x > 1)
424 if(tooltippos_y + menuTooltipSize_y > 1)
426 menuTooltipOrigin = tooltippos;
429 float m_allocatetooltipbox(vector pos)
431 vector avoidplus, avoidminus;
434 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
435 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
438 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
439 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
444 if(m_testtooltipbox(v))
448 v_x = pos_x - menuTooltipSize_x * 0.5;
449 if(m_testtooltipbox(v))
453 v_x = pos_x - avoidminus_x;
454 if(m_testtooltipbox(v))
458 v_y = pos_y - avoidminus_y;
459 if(m_testtooltipbox(v))
463 v_x = pos_x - menuTooltipSize_x * 0.5;
464 if(m_testtooltipbox(v))
468 v_x = pos_x + avoidplus_x;
469 if(m_testtooltipbox(v))
474 entity m_findtooltipitem(entity root, vector pos)
482 while(it.instanceOfContainer)
484 while(it.instanceOfNexposee && it.focusedChild)
486 it = it.focusedChild;
487 pos = globalToBox(pos, it.Container_origin, it.Container_size);
489 if(it.instanceOfNexposee)
491 it = it.itemFromPoint(it, pos);
494 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
498 else if(it.instanceOfModalController)
499 it = it.focusedChild;
501 it = it.itemFromPoint(it, pos);
506 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
508 pos = globalToBox(pos, it.Container_origin, it.Container_size);
515 if (menu_tooltips == 2)
518 if (menuTooltipItem.cvarName)
520 if (getCvarsMulti(menuTooltipItem))
521 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
523 s = strcat("[", menuTooltipItem.cvarName, "]");
525 else if (menuTooltipItem.onClickCommand)
526 s = strcat("<", menuTooltipItem.onClickCommand, ">");
528 return menuTooltipItem.tooltip;
529 if (menuTooltipItem.tooltip)
530 return strcat(menuTooltipItem.tooltip, " ", s);
533 return menuTooltipItem.tooltip;
535 void m_tooltip(vector pos)
542 menu_tooltips = cvar("menu_tooltips");
545 // don't return immediately, fade out the active tooltip first
546 if (menuTooltipItem == world)
549 menu_tooltips_old = menu_tooltips;
553 f = bound(0, frametime * 2, 1);
554 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
555 f = vlen(pos - menuTooltipAveragedMousePos);
557 it = m_findtooltipitem(main, pos);
561 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
563 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
564 if(it != menuTooltipItem)
566 switch(menuTooltipState)
571 // another item: fade out first
572 menuTooltipState = 2;
577 menuTooltipState = 1;
578 menuTooltipItem = it;
580 menuTooltipOrigin_x = -1; // unallocated
583 strunzone(menuTooltipText);
584 menuTooltipText = strzone(gettooltip());
588 getWrappedLine_remaining = menuTooltipText;
589 while(getWrappedLine_remaining)
591 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
593 f = draw_TextWidth(s, FALSE, fontsize);
597 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
598 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
599 menuTooltipSize_z = 0;
603 // changing item while fading in: fade out first
604 menuTooltipState = 2;
607 // changing item while fading out: can't
611 else if(menuTooltipState == 2) // re-fade in?
612 menuTooltipState = 1;
615 if(!m_testmousetooltipbox(pos))
616 menuTooltipState = 2; // fade out if mouse touches it
618 switch(menuTooltipState)
621 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
622 if(menuTooltipAlpha == 1)
623 menuTooltipState = 0;
626 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
627 if(menuTooltipAlpha == 0)
629 menuTooltipState = 0;
630 menuTooltipItem = world;
635 if(menuTooltipItem == world)
639 strunzone(menuTooltipText);
640 menuTooltipText = string_null;
646 if(menu_tooltips != menu_tooltips_old)
648 if (menu_tooltips != 0 && menu_tooltips_old != 0)
649 menuTooltipItem = world; // reload tooltip next frame
650 menu_tooltips_old = menu_tooltips;
652 else if(menuTooltipOrigin_x < 0) // unallocated?
653 m_allocatetooltipbox(pos);
655 if(menuTooltipOrigin_x >= 0)
658 p = SKINBORDER_TOOLTIP;
660 p_y *= 1 / conheight;
661 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
662 p = menuTooltipOrigin;
663 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
664 p_y += SKINMARGIN_TOOLTIP_y / conheight;
665 getWrappedLine_remaining = menuTooltipText;
666 while(getWrappedLine_remaining)
668 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
669 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
676 void m_draw(float width, float height)
683 execute_next_frame();
685 menuMouseMode = cvar("menu_mouse_absolute");
690 UpdateConWidthHeight(width, height, cvar("vid_pixelheight"));
694 // TODO draw an info image about this situation
698 if(!menuNotTheFirstFrame)
700 menuNotTheFirstFrame = 1;
702 if(!cvar("menu_video_played"))
704 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.wav\n");
705 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
707 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
708 localcmd("set menu_video_played 1\n");
712 realFrametime = frametime = min(0.2, t - menuPrevTime);
716 t = cvar("menu_slowmo");
727 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
728 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
733 if(cvar("cl_capturevideo"))
734 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
736 prevMenuAlpha = menuAlpha;
739 if(menuAlpha == 0 && menuLogoAlpha < 2)
741 menuLogoAlpha = menuLogoAlpha + frametime * 2;
745 menuAlpha = min(1, menuAlpha + frametime * 5);
751 menuAlpha = max(0, menuAlpha - frametime * 5);
755 draw_reset_cropped();
757 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
759 if(menuLogoAlpha > 0)
762 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
763 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
764 draw_reset_cropped();
765 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
767 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
768 draw_drawMousePointer(menuMousePos);
773 else if(SKINALPHA_BACKGROUND_INGAME)
778 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
779 draw_reset_cropped();
783 if(menuAlpha != prevMenuAlpha)
784 cvar_set("_menu_alpha", ftos(menuAlpha));
786 draw_reset_cropped();
788 draw_reset_cropped();
792 if(prevMenuAlpha > 0)
793 main.initializeDialog(main, main.firstChild);
794 draw_reset_cropped();
799 draw_alpha *= menuAlpha;
803 // do not update mouse position
804 // it prevents mouse jumping to '0 0 0' when menu is fading out
806 else if(menuMouseMode)
809 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
810 if(newMouse != '0 0 0')
811 if(newMouse != menuMousePos)
813 menuMousePos = newMouse;
814 if(mouseButtonsPressed)
815 main.mouseDrag(main, menuMousePos);
817 main.mouseMove(main, menuMousePos);
824 vector dMouse, minpos, maxpos;
825 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
826 if(dMouse != '0 0 0')
828 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
829 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
830 dMouse = globalToBoxSize(dMouse, draw_scale);
831 menuMousePos += dMouse * cvar("menu_mouse_speed");
832 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
833 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
834 if(mouseButtonsPressed)
835 main.mouseDrag(main, menuMousePos);
837 main.mouseMove(main, menuMousePos);
843 m_tooltip(menuMousePos);
845 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
847 draw_drawMousePointer(menuMousePos);
849 draw_reset_cropped();
858 setkeydest(KEY_MENU);
859 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
864 if(mouseButtonsPressed)
865 main.mouseRelease(main, menuMousePos);
866 mouseButtonsPressed = 0;
868 main.focusEnter(main);
869 main.showNotify(main);
875 setkeydest(KEY_GAME);
876 setmousetarget(MT_CLIENT);
881 main.focusLeave(main);
882 main.hideNotify(main);
885 void m_toggle(float mode)
906 for(e = NULL; (e = nextent(e)) != NULL; )
908 if(e.classname != "vtbl")
914 void m_focus_item_chain(entity outermost, entity innermost)
916 if(innermost.parent != outermost)
917 m_focus_item_chain(outermost, innermost.parent);
918 innermost.parent.setFocus(innermost.parent, innermost);
921 void m_activate_window(entity wnd)
926 m_activate_window(par);
928 if(par.instanceOfModalController)
930 if(wnd.tabSelectingButton)
932 TabButton_Click(wnd.tabSelectingButton, wnd);
935 par.initializeDialog(par, wnd);
937 else if(par.instanceOfNexposee)
939 // nexposee (sorry for violating abstraction here)
940 par.selectedChild = wnd;
941 par.animationState = 1;
942 Container_setFocus(par, NULL);
944 else if(par.instanceOfContainer)
948 par.setFocus(par, wnd);
952 void m_setpointerfocus(entity wnd)
954 if(wnd.instanceOfContainer)
956 entity focus = wnd.preferredFocusedGrandChild(wnd);
959 menuMousePos = focus.origin + 0.5 * focus.size;
960 menuMousePos_x *= 1 / conwidth;
961 menuMousePos_y *= 1 / conheight;
962 if(wnd.focused) // why does this never happen?
963 m_focus_item_chain(wnd, focus);
968 void m_goto(string itemname)
974 strunzone(m_goto_buffer);
975 m_goto_buffer = strzone(itemname);
978 if(itemname == "") // this can be called by GameCommand
980 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
986 m_activate_window(main.mainNexposee);
992 for(e = NULL; (e = find(e, name, itemname)); )
993 if(e.classname != "vtbl")
996 if((e) && (!e.requiresConnection || (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))))
999 m_activate_window(e);
1000 m_setpointerfocus(e);