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 gamestatus = gamestatus | GAME_ISSERVER;
32 if(clientstate() == CS_CONNECTED || isdemo())
33 gamestatus = gamestatus | GAME_CONNECTED;
35 gamestatus = gamestatus | GAME_DEVELOPER;
41 cvar_set("_menu_alpha", "0");
42 prvm_language = cvar_string("prvm_language");
43 if(prvm_language == "")
46 cvar_set("prvm_language", prvm_language);
47 localcmd("\nmenu_restart\n");
50 prvm_language = strzone(prvm_language);
51 cvar_set("_menu_prvm_language", prvm_language);
53 check_unacceptable_compiler_bugs();
56 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK));
59 // list all game dirs (TEST)
66 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
69 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
73 // needs to be done so early because of the constants they create
74 CALL_ACCUMULATED_FUNCTION(RegisterWeapons);
75 CALL_ACCUMULATED_FUNCTION(RegisterGametypes);
77 float ddsload = cvar("r_texture_dds_load");
78 float texcomp = cvar("gl_texturecompression");
80 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
81 localcmd("\nr_restart\n");
86 if(cvar("_menu_initialized")) // always show menu after menu_restart
90 cvar_set("_menu_initialized", "1");
94 float MENU_ASPECT = 1.25; // 1280x1024
95 float MENU_MINHEIGHT = 600;
96 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
97 void draw_reset_cropped()
99 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
101 void draw_reset_full()
103 draw_reset(realconwidth, realconheight, 0, 0);
105 void UpdateConWidthHeight()
107 conwidth_s = conwidth;
108 conheight_s = conheight;
109 realconwidth = cvar("vid_conwidth");
110 realconheight = cvar("vid_conheight");
111 if(realconwidth / realconheight > MENU_ASPECT)
114 conwidth = realconheight * MENU_ASPECT;
115 conheight = realconheight;
120 conwidth = realconwidth;
121 conheight = realconwidth / MENU_ASPECT;
123 screenconwidth = conwidth;
124 screenconheight = conheight;
125 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
127 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
128 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
130 if(conheight < MENU_MINHEIGHT)
132 conwidth *= MENU_MINHEIGHT / conheight;
133 conheight = MENU_MINHEIGHT;
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 string m_goto_buffer;
146 void m_init_delayed()
148 float fh, glob, n, i;
151 conwidth = conheight = -1;
152 UpdateConWidthHeight();
153 draw_reset_cropped();
161 if(cvar_string("menu_skin") != "")
163 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
164 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
167 if(cvar_defstring("menu_skin") != "")
169 cvar_set("menu_skin", cvar_defstring("menu_skin"));
170 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
171 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
175 draw_currentSkin = "gfx/menu/default";
176 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
180 error("cannot load any menu skin\n");
182 draw_currentSkin = strzone(draw_currentSkin);
183 while((s = fgets(fh)))
185 // these two are handled by skinlist.qc
186 if(substring(s, 0, 6) == "title ")
188 if(substring(s, 0, 7) == "author ")
190 n = tokenize_console(s);
192 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
196 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
199 n = search_getsize(glob);
200 for(i = 0; i < n; ++i)
201 precache_pic(search_getfilename(glob, i));
205 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
208 anim = spawnAnimHost();
209 main = spawnMainWindow(); main.configureMainWindow(main);
212 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
215 menuMousePos = '0.5 0.5 0';
221 m_goto(m_goto_buffer);
222 strunzone(m_goto_buffer);
223 m_goto_buffer = string_null;
227 m_display(); // delayed menu display
230 void m_keyup (float key, float ascii)
236 draw_reset_cropped();
237 main.keyUp(main, key, ascii, menuShiftState);
238 if(key >= K_MOUSE1 && key <= K_MOUSE3)
240 --mouseButtonsPressed;
241 if(!mouseButtonsPressed)
242 main.mouseRelease(main, menuMousePos);
243 if(mouseButtonsPressed < 0)
245 mouseButtonsPressed = 0;
246 dprint("Warning: released an already released button\n");
249 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
250 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
251 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
254 void m_keydown(float key, float ascii)
265 e.keyGrabbed(e, key, ascii);
269 draw_reset_cropped();
270 if(key >= K_MOUSE1 && key <= K_MOUSE3)
271 if(!mouseButtonsPressed)
272 main.mousePress(main, menuMousePos);
273 if(!main.keyDown(main, key, ascii, menuShiftState))
275 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
276 m_hide(); // disable menu on unhandled ESC
278 if(key >= K_MOUSE1 && key <= K_MOUSE3)
280 ++mouseButtonsPressed;
281 if(mouseButtonsPressed > 10)
283 mouseButtonsPressed = 10;
284 dprint("Warning: pressed an already pressed button\n");
287 if(key == K_ALT) menuShiftState |= S_ALT;
288 if(key == K_CTRL) menuShiftState |= S_CTRL;
289 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
292 float SCALEMODE_CROP = 0;
293 float SCALEMODE_LETTERBOX = 1;
294 float SCALEMODE_WIDTH = 2;
295 float SCALEMODE_HEIGHT = 3;
296 float SCALEMODE_STRETCH = 4;
297 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
299 vector sz, org, isz, isz_w, isz_h;
300 float width_is_larger;
302 sz = draw_PictureSize(img);
303 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
304 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
305 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
314 isz = (width_is_larger ? isz_h : isz_w);
316 case SCALEMODE_LETTERBOX:
317 isz = (width_is_larger ? isz_w : isz_h);
319 case SCALEMODE_WIDTH:
322 case SCALEMODE_HEIGHT:
325 case SCALEMODE_STRETCH:
330 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
331 draw_Picture(org, img, isz, '1 1 1', a);
334 void drawBackground(string img, float a, string algn, float force1)
336 if(main.mainNexposee.ModalController_state == 0)
346 scalemode = SCALEMODE_CROP;
349 for(i = 0; i < strlen(algn); ++i)
351 c = substring(algn, i, 1);
354 case "c": scalemode = SCALEMODE_CROP; goto nopic;
355 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
356 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
357 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
358 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
359 case "1": case "4": case "7": v_x = 0.0; break;
360 case "2": case "5": case "8": v_x = 0.5; break;
361 case "3": case "6": case "9": v_x = 1.0; break;
362 default: v_x = random(); break;
366 case "7": case "8": case "9": v_y = 0.0; break;
367 case "4": case "5": case "6": v_y = 0.5; break;
368 case "1": case "2": case "3": v_y = 1.0; break;
369 default: v_y = random(); break;
372 draw_Picture_Aligned(v, scalemode, img, a);
374 // force all secondary layers to use alpha 1. Prevents ugly issues
375 // with overlap. It's a flag because it cannot be used for the
377 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
379 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
386 float menu_tooltips_old;
387 vector menuTooltipAveragedMousePos;
388 entity menuTooltipItem;
389 vector menuTooltipOrigin;
390 vector menuTooltipSize;
391 float menuTooltipAlpha;
392 string menuTooltipText;
393 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
394 float m_testmousetooltipbox(vector pos)
396 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
397 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
401 float m_testtooltipbox(vector tooltippos)
407 if(tooltippos_x + menuTooltipSize_x > 1)
409 if(tooltippos_y + menuTooltipSize_y > 1)
411 menuTooltipOrigin = tooltippos;
414 float m_allocatetooltipbox(vector pos)
416 vector avoidplus, avoidminus;
419 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
420 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
423 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
424 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
429 if(m_testtooltipbox(v))
433 v_x = pos_x - menuTooltipSize_x * 0.5;
434 if(m_testtooltipbox(v))
438 v_x = pos_x - avoidminus_x;
439 if(m_testtooltipbox(v))
443 v_y = pos_y - avoidminus_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 + avoidplus_x;
454 if(m_testtooltipbox(v))
459 entity m_findtooltipitem(entity root, vector pos)
467 while(it.instanceOfContainer)
469 while(it.instanceOfNexposee && it.focusedChild)
471 it = it.focusedChild;
472 pos = globalToBox(pos, it.Container_origin, it.Container_size);
474 if(it.instanceOfNexposee)
476 it = it.itemFromPoint(it, pos);
479 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
483 else if(it.instanceOfModalController)
484 it = it.focusedChild;
486 it = it.itemFromPoint(it, pos);
491 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
493 pos = globalToBox(pos, it.Container_origin, it.Container_size);
500 if (menu_tooltips == 2)
503 if (menuTooltipItem.cvarName)
505 if (getCvarsMulti(menuTooltipItem))
506 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
508 s = strcat("[", menuTooltipItem.cvarName, "]");
510 else if (menuTooltipItem.onClickCommand)
511 s = strcat("<", menuTooltipItem.onClickCommand, ">");
513 return menuTooltipItem.tooltip;
514 if (menuTooltipItem.tooltip)
515 return strcat(menuTooltipItem.tooltip, " ", s);
518 return menuTooltipItem.tooltip;
520 void m_tooltip(vector pos)
527 menu_tooltips = cvar("menu_tooltips");
530 // don't return immediately, fade out the active tooltip first
531 if (menuTooltipItem == world)
534 menu_tooltips_old = menu_tooltips;
538 f = bound(0, frametime * 2, 1);
539 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
540 f = vlen(pos - menuTooltipAveragedMousePos);
542 it = m_findtooltipitem(main, pos);
546 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
548 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
549 if(it != menuTooltipItem)
551 switch(menuTooltipState)
556 // another item: fade out first
557 menuTooltipState = 2;
562 menuTooltipState = 1;
563 menuTooltipItem = it;
565 menuTooltipOrigin_x = -1; // unallocated
568 strunzone(menuTooltipText);
569 menuTooltipText = strzone(gettooltip());
573 getWrappedLine_remaining = menuTooltipText;
574 while(getWrappedLine_remaining)
576 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
578 f = draw_TextWidth(s, FALSE, fontsize);
582 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
583 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
584 menuTooltipSize_z = 0;
588 // changing item while fading in: fade out first
589 menuTooltipState = 2;
592 // changing item while fading out: can't
596 else if(menuTooltipState == 2) // re-fade in?
597 menuTooltipState = 1;
600 if(!m_testmousetooltipbox(pos))
601 menuTooltipState = 2; // fade out if mouse touches it
603 switch(menuTooltipState)
606 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
607 if(menuTooltipAlpha == 1)
608 menuTooltipState = 0;
611 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
612 if(menuTooltipAlpha == 0)
614 menuTooltipState = 0;
615 menuTooltipItem = world;
620 if(menuTooltipItem == world)
624 strunzone(menuTooltipText);
625 menuTooltipText = string_null;
631 if(menu_tooltips != menu_tooltips_old)
633 if (menu_tooltips != 0 && menu_tooltips_old != 0)
634 menuTooltipItem = world; // reload tooltip next frame
635 menu_tooltips_old = menu_tooltips;
637 else if(menuTooltipOrigin_x < 0) // unallocated?
638 m_allocatetooltipbox(pos);
640 if(menuTooltipOrigin_x >= 0)
643 p = SKINBORDER_TOOLTIP;
645 p_y *= 1 / conheight;
646 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
647 p = menuTooltipOrigin;
648 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
649 p_y += SKINMARGIN_TOOLTIP_y / conheight;
650 getWrappedLine_remaining = menuTooltipText;
651 while(getWrappedLine_remaining)
653 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
654 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
668 execute_next_frame();
670 menuMouseMode = cvar("menu_mouse_absolute");
676 UpdateConWidthHeight();
680 // TODO draw an info image about this situation
684 if(!menuNotTheFirstFrame)
686 menuNotTheFirstFrame = 1;
688 if(!cvar("menu_video_played"))
690 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
691 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
693 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
694 localcmd("set menu_video_played 1\n");
698 realFrametime = frametime = min(0.2, t - menuPrevTime);
702 t = cvar("menu_slowmo");
713 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
714 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
719 if(cvar("cl_capturevideo"))
720 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
722 prevMenuAlpha = menuAlpha;
725 if(menuAlpha == 0 && menuLogoAlpha < 2)
727 menuLogoAlpha = menuLogoAlpha + frametime * 2;
731 menuAlpha = min(1, menuAlpha + frametime * 5);
737 menuAlpha = max(0, menuAlpha - frametime * 5);
741 draw_reset_cropped();
743 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
745 if(menuLogoAlpha > 0)
748 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
749 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
750 draw_reset_cropped();
751 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
753 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
754 draw_drawMousePointer(menuMousePos);
759 else if(SKINALPHA_BACKGROUND_INGAME)
764 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
765 draw_reset_cropped();
769 if(menuAlpha != prevMenuAlpha)
770 cvar_set("_menu_alpha", ftos(menuAlpha));
772 draw_reset_cropped();
774 draw_reset_cropped();
778 if(prevMenuAlpha > 0)
779 main.initializeDialog(main, main.firstChild);
780 draw_reset_cropped();
785 draw_alpha *= menuAlpha;
790 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
791 if(newMouse != '0 0 0')
792 if(newMouse != menuMousePos)
794 menuMousePos = newMouse;
795 if(mouseButtonsPressed)
796 main.mouseDrag(main, menuMousePos);
798 main.mouseMove(main, menuMousePos);
805 vector dMouse, minpos, maxpos;
806 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
807 if(dMouse != '0 0 0')
809 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
810 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
811 dMouse = globalToBoxSize(dMouse, draw_scale);
812 menuMousePos += dMouse * cvar("menu_mouse_speed");
813 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
814 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
815 if(mouseButtonsPressed)
816 main.mouseDrag(main, menuMousePos);
818 main.mouseMove(main, menuMousePos);
824 m_tooltip(menuMousePos);
826 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
828 draw_drawMousePointer(menuMousePos);
830 draw_reset_cropped();
839 setkeydest(KEY_MENU);
840 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
845 if(mouseButtonsPressed)
846 main.mouseRelease(main, menuMousePos);
847 mouseButtonsPressed = 0;
849 main.focusEnter(main);
850 main.showNotify(main);
856 setkeydest(KEY_GAME);
857 setmousetarget(MT_CLIENT);
862 main.focusLeave(main);
863 main.hideNotify(main);
866 void m_toggle(float mode)
887 for(e = NULL; (e = nextent(e)) != NULL; )
889 if(e.classname != "vtbl")
895 void m_focus_item_chain(entity outermost, entity innermost)
897 if(innermost.parent != outermost)
898 m_focus_item_chain(outermost, innermost.parent);
899 innermost.parent.setFocus(innermost.parent, innermost);
902 void m_activate_window(entity wnd)
907 m_activate_window(par);
909 if(par.instanceOfModalController)
911 if(wnd.tabSelectingButton)
913 TabButton_Click(wnd.tabSelectingButton, wnd);
916 par.initializeDialog(par, wnd);
918 else if(par.instanceOfNexposee)
920 // nexposee (sorry for violating abstraction here)
921 par.selectedChild = wnd;
922 par.animationState = 1;
923 Container_setFocus(par, NULL);
925 else if(par.instanceOfContainer)
929 par.setFocus(par, wnd);
933 void m_setpointerfocus(entity wnd)
935 if(wnd.instanceOfContainer)
937 entity focus = wnd.preferredFocusedGrandChild(wnd);
940 menuMousePos = focus.origin + 0.5 * focus.size;
941 menuMousePos_x *= 1 / conwidth;
942 menuMousePos_y *= 1 / conheight;
943 if(wnd.focused) // why does this never happen?
944 m_focus_item_chain(wnd, focus);
949 void m_goto(string itemname)
955 strunzone(m_goto_buffer);
956 m_goto_buffer = strzone(itemname);
959 if(itemname == "") // this can be called by GameCommand
961 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
967 m_activate_window(main.mainNexposee);
973 for(e = NULL; (e = find(e, name, itemname)); )
974 if(e.classname != "vtbl")
979 m_activate_window(e);
980 m_setpointerfocus(e);