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));
54 // needs to be done so early because of the constants they create
59 float MENU_ASPECT = 1.25; // 1280x1024
60 float MENU_MINHEIGHT = 600;
61 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
62 void draw_reset_cropped()
64 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
66 void draw_reset_full()
68 draw_reset(realconwidth, realconheight, 0, 0);
70 void UpdateConWidthHeight()
72 conwidth_s = conwidth;
73 conheight_s = conheight;
74 realconwidth = cvar("vid_conwidth");
75 realconheight = cvar("vid_conheight");
76 if(realconwidth / realconheight > MENU_ASPECT)
79 conwidth = realconheight * MENU_ASPECT;
80 conheight = realconheight;
85 conwidth = realconwidth;
86 conheight = realconwidth / MENU_ASPECT;
88 screenconwidth = conwidth;
89 screenconheight = conheight;
90 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
92 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
93 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
95 if(conheight < MENU_MINHEIGHT)
97 conwidth *= MENU_MINHEIGHT / conheight;
98 conheight = MENU_MINHEIGHT;
102 if(conwidth_s != conwidth || conheight_s != conheight)
104 draw_reset_cropped();
105 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
110 void m_init_delayed()
112 float fh, glob, n, i;
115 conwidth = conheight = -1;
116 UpdateConWidthHeight();
117 draw_reset_cropped();
125 if(cvar_string("menu_skin") != "")
127 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
128 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
131 if(cvar_defstring("menu_skin") != "")
133 cvar_set("menu_skin", cvar_defstring("menu_skin"));
134 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
135 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
139 draw_currentSkin = "gfx/menu/default";
140 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
144 error("cannot load any menu skin\n");
146 draw_currentSkin = strzone(draw_currentSkin);
147 while((s = fgets(fh)))
149 // these two are handled by skinlist.qc
150 if(substring(s, 0, 6) == "title ")
152 if(substring(s, 0, 7) == "author ")
154 n = tokenize_console(s);
156 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
160 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
163 n = search_getsize(glob);
164 for(i = 0; i < n; ++i)
165 precache_pic(search_getfilename(glob, i));
169 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
172 anim = spawnAnimHost();
173 main = spawnMainWindow(); main.configureMainWindow(main);
176 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
179 menuMousePos = '0.5 0.5 0';
184 m_display(); // delayed menu display
187 void m_keyup (float key, float ascii)
193 draw_reset_cropped();
194 main.keyUp(main, key, ascii, menuShiftState);
195 if(key >= K_MOUSE1 && key <= K_MOUSE3)
197 --mouseButtonsPressed;
198 if(!mouseButtonsPressed)
199 main.mouseRelease(main, menuMousePos);
200 if(mouseButtonsPressed < 0)
202 mouseButtonsPressed = 0;
203 dprint("Warning: released an already released button\n");
206 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
207 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
208 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
211 void m_keydown(float key, float ascii)
222 e.keyGrabbed(e, key, ascii);
226 draw_reset_cropped();
227 if(key >= K_MOUSE1 && key <= K_MOUSE3)
228 if(!mouseButtonsPressed)
229 main.mousePress(main, menuMousePos);
230 if(!main.keyDown(main, key, ascii, menuShiftState))
232 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
233 m_hide(); // disable menu on unhandled ESC
235 if(key >= K_MOUSE1 && key <= K_MOUSE3)
237 ++mouseButtonsPressed;
238 if(mouseButtonsPressed > 10)
240 mouseButtonsPressed = 10;
241 dprint("Warning: pressed an already pressed button\n");
244 if(key == K_ALT) menuShiftState |= S_ALT;
245 if(key == K_CTRL) menuShiftState |= S_CTRL;
246 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
249 float SCALEMODE_CROP = 0;
250 float SCALEMODE_LETTERBOX = 1;
251 float SCALEMODE_WIDTH = 2;
252 float SCALEMODE_HEIGHT = 3;
253 float SCALEMODE_STRETCH = 4;
254 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
256 vector sz, org, isz, isz_w, isz_h;
257 float width_is_larger;
259 sz = draw_PictureSize(img);
260 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
261 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
262 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
268 isz = (width_is_larger ? isz_h : isz_w);
270 case SCALEMODE_LETTERBOX:
271 isz = (width_is_larger ? isz_w : isz_h);
273 case SCALEMODE_WIDTH:
276 case SCALEMODE_HEIGHT:
279 case SCALEMODE_STRETCH:
284 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
285 draw_Picture(org, img, isz, '1 1 1', a);
288 void drawBackground(string img, float a, string algn, float force1)
290 if(main.mainNexposee.ModalController_state == 0)
300 scalemode = SCALEMODE_CROP;
302 for(i = 0; i < strlen(algn); ++i)
304 c = substring(algn, i, 1);
307 case "c": scalemode = SCALEMODE_CROP; goto nopic;
308 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
309 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
310 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
311 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
312 case "1": case "4": case "7": v_x = 0.0; break;
313 case "2": case "5": case "8": v_x = 0.5; break;
314 case "3": case "6": case "9": v_x = 1.0; break;
315 default: v_x = random(); break;
319 case "7": case "8": case "9": v_y = 0.0; break;
320 case "4": case "5": case "6": v_y = 0.5; break;
321 case "1": case "2": case "3": v_y = 1.0; break;
322 default: v_y = random(); break;
325 draw_Picture_Aligned(v, scalemode, img, a);
327 // force all secondary layers to use alpha 1. Prevents ugly issues
328 // with overlap. It's a flag because it cannot be used for the
330 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
332 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
339 float menu_tooltips_old;
340 vector menuTooltipAveragedMousePos;
341 entity menuTooltipItem;
342 vector menuTooltipOrigin;
343 vector menuTooltipSize;
344 float menuTooltipAlpha;
345 string menuTooltipText;
346 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
347 float m_testmousetooltipbox(vector pos)
349 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
350 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
354 float m_testtooltipbox(vector tooltippos)
360 if(tooltippos_x + menuTooltipSize_x > 1)
362 if(tooltippos_y + menuTooltipSize_y > 1)
364 menuTooltipOrigin = tooltippos;
367 float m_allocatetooltipbox(vector pos)
369 vector avoidplus, avoidminus;
372 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
373 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
376 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
377 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
382 if(m_testtooltipbox(v))
386 v_x = pos_x - menuTooltipSize_x * 0.5;
387 if(m_testtooltipbox(v))
391 v_x = pos_x - avoidminus_x;
392 if(m_testtooltipbox(v))
396 v_y = pos_y - avoidminus_y;
397 if(m_testtooltipbox(v))
401 v_x = pos_x - menuTooltipSize_x * 0.5;
402 if(m_testtooltipbox(v))
406 v_x = pos_x + avoidplus_x;
407 if(m_testtooltipbox(v))
412 entity m_findtooltipitem(entity root, vector pos)
420 while(it.instanceOfContainer)
422 while(it.instanceOfNexposee && it.focusedChild)
424 it = it.focusedChild;
425 pos = globalToBox(pos, it.Container_origin, it.Container_size);
427 if(it.instanceOfNexposee)
429 it = it.itemFromPoint(it, pos);
432 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
436 else if(it.instanceOfModalController)
437 it = it.focusedChild;
439 it = it.itemFromPoint(it, pos);
444 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
446 pos = globalToBox(pos, it.Container_origin, it.Container_size);
453 if (menu_tooltips == 2)
456 if (menuTooltipItem.cvarName)
458 if (getCvarsMulti(menuTooltipItem))
459 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
461 s = strcat("[", menuTooltipItem.cvarName, "]");
463 else if (menuTooltipItem.onClickCommand)
464 s = strcat("<", menuTooltipItem.onClickCommand, ">");
466 return menuTooltipItem.tooltip;
467 if (menuTooltipItem.tooltip)
468 return strcat(menuTooltipItem.tooltip, " ", s);
471 return menuTooltipItem.tooltip;
473 void m_tooltip(vector pos)
480 menu_tooltips = cvar("menu_tooltips");
483 // don't return immediately, fade out the active tooltip first
484 if (menuTooltipItem == world)
487 menu_tooltips_old = menu_tooltips;
491 f = bound(0, frametime * 2, 1);
492 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
493 f = vlen(pos - menuTooltipAveragedMousePos);
495 it = m_findtooltipitem(main, pos);
499 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
501 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
502 if(it != menuTooltipItem)
504 switch(menuTooltipState)
509 // another item: fade out first
510 menuTooltipState = 2;
515 menuTooltipState = 1;
516 menuTooltipItem = it;
518 menuTooltipOrigin_x = -1; // unallocated
521 strunzone(menuTooltipText);
522 menuTooltipText = strzone(gettooltip());
526 getWrappedLine_remaining = menuTooltipText;
527 while(getWrappedLine_remaining)
529 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
531 f = draw_TextWidth(s, FALSE, fontsize);
535 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
536 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
537 menuTooltipSize_z = 0;
541 // changing item while fading in: fade out first
542 menuTooltipState = 2;
545 // changing item while fading out: can't
549 else if(menuTooltipState == 2) // re-fade in?
550 menuTooltipState = 1;
553 if(!m_testmousetooltipbox(pos))
554 menuTooltipState = 2; // fade out if mouse touches it
556 switch(menuTooltipState)
559 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
560 if(menuTooltipAlpha == 1)
561 menuTooltipState = 0;
564 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
565 if(menuTooltipAlpha == 0)
567 menuTooltipState = 0;
568 menuTooltipItem = world;
573 if(menuTooltipItem == world)
577 strunzone(menuTooltipText);
578 menuTooltipText = string_null;
584 if(menu_tooltips != menu_tooltips_old)
586 if (menu_tooltips != 0 && menu_tooltips_old != 0)
587 menuTooltipItem = world; // reload tooltip next frame
588 menu_tooltips_old = menu_tooltips;
590 else if(menuTooltipOrigin_x < 0) // unallocated?
591 m_allocatetooltipbox(pos);
593 if(menuTooltipOrigin_x >= 0)
596 p = SKINBORDER_TOOLTIP;
598 p_y *= 1 / conheight;
599 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
600 p = menuTooltipOrigin;
601 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
602 p_y += SKINMARGIN_TOOLTIP_y / conheight;
603 getWrappedLine_remaining = menuTooltipText;
604 while(getWrappedLine_remaining)
606 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
607 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
619 menuMouseMode = cvar("menu_mouse_absolute");
625 UpdateConWidthHeight();
629 // TODO draw an info image about this situation
633 if(!menuNotTheFirstFrame)
635 menuNotTheFirstFrame = 1;
637 if(!cvar("menu_video_played"))
639 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
640 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
642 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
643 localcmd("set menu_video_played 1\n");
647 realFrametime = frametime = min(0.2, t - menuPrevTime);
651 t = cvar("menu_slowmo");
662 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
663 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
668 if(cvar("cl_capturevideo"))
669 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
673 gamestatus = gamestatus | GAME_ISSERVER;
674 if(clientstate() == CS_CONNECTED)
675 gamestatus = gamestatus | GAME_CONNECTED;
676 if(cvar("developer"))
677 gamestatus = gamestatus | GAME_DEVELOPER;
679 prevMenuAlpha = menuAlpha;
682 if(menuAlpha == 0 && menuLogoAlpha < 2)
684 menuLogoAlpha = menuLogoAlpha + frametime * 2;
688 menuAlpha = min(1, menuAlpha + frametime * 5);
694 menuAlpha = max(0, menuAlpha - frametime * 5);
698 draw_reset_cropped();
700 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
702 if(menuLogoAlpha > 0)
705 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
706 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
707 draw_reset_cropped();
708 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
710 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
711 draw_drawMousePointer(menuMousePos);
716 else if(SKINALPHA_BACKGROUND_INGAME)
721 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
722 draw_reset_cropped();
726 if(menuAlpha != prevMenuAlpha)
727 cvar_set("_menu_alpha", ftos(menuAlpha));
729 draw_reset_cropped();
731 draw_reset_cropped();
735 if(prevMenuAlpha > 0)
736 main.initializeDialog(main, main.firstChild);
737 draw_reset_cropped();
742 draw_alpha *= menuAlpha;
747 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
748 if(newMouse != '0 0 0')
749 if(newMouse != menuMousePos)
751 menuMousePos = newMouse;
752 if(mouseButtonsPressed)
753 main.mouseDrag(main, menuMousePos);
755 main.mouseMove(main, menuMousePos);
762 vector dMouse, minpos, maxpos;
763 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
764 if(dMouse != '0 0 0')
766 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
767 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
768 dMouse = globalToBoxSize(dMouse, draw_scale);
769 menuMousePos += dMouse * cvar("menu_mouse_speed");
770 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
771 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
772 if(mouseButtonsPressed)
773 main.mouseDrag(main, menuMousePos);
775 main.mouseMove(main, menuMousePos);
781 m_tooltip(menuMousePos);
783 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
785 draw_drawMousePointer(menuMousePos);
787 draw_reset_cropped();
796 setkeydest(KEY_MENU);
797 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
802 if(mouseButtonsPressed)
803 main.mouseRelease(main, menuMousePos);
804 mouseButtonsPressed = 0;
806 main.focusEnter(main);
807 main.showNotify(main);
813 setkeydest(KEY_GAME);
814 setmousetarget(MT_CLIENT);
819 main.focusLeave(main);
820 main.hideNotify(main);
823 void m_toggle(float mode)
844 for(e = NULL; (e = nextent(e)) != NULL; )
846 if(e.classname != "vtbl")
852 void m_focus_item_chain(entity outermost, entity innermost)
854 if(innermost.parent != outermost)
855 m_focus_item_chain(outermost, innermost.parent);
856 innermost.parent.setFocus(innermost.parent, innermost);
859 void m_activate_window(entity wnd)
864 m_activate_window(par);
866 if(par.instanceOfModalController)
868 if(wnd.tabSelectingButton)
870 TabButton_Click(wnd.tabSelectingButton, wnd);
873 par.initializeDialog(par, wnd);
875 else if(par.instanceOfNexposee)
877 // nexposee (sorry for violating abstraction here)
878 par.selectedChild = wnd;
879 par.animationState = 1;
880 Container_setFocus(par, NULL);
882 else if(par.instanceOfContainer)
886 par.setFocus(par, wnd);
890 void m_setpointerfocus(entity wnd)
892 if(wnd.instanceOfContainer)
894 entity focus = wnd.preferredFocusedGrandChild(wnd);
897 menuMousePos = focus.origin + 0.5 * focus.size;
898 menuMousePos_x *= 1 / conwidth;
899 menuMousePos_y *= 1 / conheight;
900 if(wnd.focused) // why does this never happen?
901 m_focus_item_chain(wnd, focus);
906 void m_goto(string itemname)
911 if(itemname == "") // this can be called by GameCommand
913 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
917 m_activate_window(main.mainNexposee);
923 for(e = NULL; (e = find(e, name, itemname)); )
924 if(e.classname != "vtbl")
929 m_activate_window(e);
930 m_setpointerfocus(e);
936 void m_goto_skin_selector()
940 // TODO add code to switch back to the skin selector (no idea how to do it now)
941 m_goto("skinselector");
944 void m_goto_language_selector()
948 // TODO add code to switch back to the language selector (no idea how to do it now)
949 m_goto("languageselector");
952 void m_goto_video_settings()
956 // TODO add code to switch back to the video settings (no idea how to do it now)
957 m_goto("videosettings");