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
58 float ddsload = cvar("r_texture_dds_load");
59 float texcomp = cvar("gl_texturecompression");
61 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
62 localcmd("\nr_restart\n");
66 float MENU_ASPECT = 1.25; // 1280x1024
67 float MENU_MINHEIGHT = 600;
68 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
69 void draw_reset_cropped()
71 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
73 void draw_reset_full()
75 draw_reset(realconwidth, realconheight, 0, 0);
77 void UpdateConWidthHeight()
79 conwidth_s = conwidth;
80 conheight_s = conheight;
81 realconwidth = cvar("vid_conwidth");
82 realconheight = cvar("vid_conheight");
83 if(realconwidth / realconheight > MENU_ASPECT)
86 conwidth = realconheight * MENU_ASPECT;
87 conheight = realconheight;
92 conwidth = realconwidth;
93 conheight = realconwidth / MENU_ASPECT;
95 screenconwidth = conwidth;
96 screenconheight = conheight;
97 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
99 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
100 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
102 if(conheight < MENU_MINHEIGHT)
104 conwidth *= MENU_MINHEIGHT / conheight;
105 conheight = MENU_MINHEIGHT;
109 if(conwidth_s != conwidth || conheight_s != conheight)
111 draw_reset_cropped();
112 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
117 void m_init_delayed()
119 float fh, glob, n, i;
122 conwidth = conheight = -1;
123 UpdateConWidthHeight();
124 draw_reset_cropped();
132 if(cvar_string("menu_skin") != "")
134 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
135 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
138 if(cvar_defstring("menu_skin") != "")
140 cvar_set("menu_skin", cvar_defstring("menu_skin"));
141 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
142 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
146 draw_currentSkin = "gfx/menu/default";
147 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
151 error("cannot load any menu skin\n");
153 draw_currentSkin = strzone(draw_currentSkin);
154 while((s = fgets(fh)))
156 // these two are handled by skinlist.qc
157 if(substring(s, 0, 6) == "title ")
159 if(substring(s, 0, 7) == "author ")
161 n = tokenize_console(s);
163 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
167 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
170 n = search_getsize(glob);
171 for(i = 0; i < n; ++i)
172 precache_pic(search_getfilename(glob, i));
176 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
179 anim = spawnAnimHost();
180 main = spawnMainWindow(); main.configureMainWindow(main);
183 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
186 menuMousePos = '0.5 0.5 0';
191 m_display(); // delayed menu display
194 void m_keyup (float key, float ascii)
200 draw_reset_cropped();
201 main.keyUp(main, key, ascii, menuShiftState);
202 if(key >= K_MOUSE1 && key <= K_MOUSE3)
204 --mouseButtonsPressed;
205 if(!mouseButtonsPressed)
206 main.mouseRelease(main, menuMousePos);
207 if(mouseButtonsPressed < 0)
209 mouseButtonsPressed = 0;
210 dprint("Warning: released an already released button\n");
213 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
214 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
215 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
218 void m_keydown(float key, float ascii)
229 e.keyGrabbed(e, key, ascii);
233 draw_reset_cropped();
234 if(key >= K_MOUSE1 && key <= K_MOUSE3)
235 if(!mouseButtonsPressed)
236 main.mousePress(main, menuMousePos);
237 if(!main.keyDown(main, key, ascii, menuShiftState))
239 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
240 m_hide(); // disable menu on unhandled ESC
242 if(key >= K_MOUSE1 && key <= K_MOUSE3)
244 ++mouseButtonsPressed;
245 if(mouseButtonsPressed > 10)
247 mouseButtonsPressed = 10;
248 dprint("Warning: pressed an already pressed button\n");
251 if(key == K_ALT) menuShiftState |= S_ALT;
252 if(key == K_CTRL) menuShiftState |= S_CTRL;
253 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
256 float SCALEMODE_CROP = 0;
257 float SCALEMODE_LETTERBOX = 1;
258 float SCALEMODE_WIDTH = 2;
259 float SCALEMODE_HEIGHT = 3;
260 float SCALEMODE_STRETCH = 4;
261 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
263 vector sz, org, isz, isz_w, isz_h;
264 float width_is_larger;
266 sz = draw_PictureSize(img);
267 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
268 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
269 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
275 isz = (width_is_larger ? isz_h : isz_w);
277 case SCALEMODE_LETTERBOX:
278 isz = (width_is_larger ? isz_w : isz_h);
280 case SCALEMODE_WIDTH:
283 case SCALEMODE_HEIGHT:
286 case SCALEMODE_STRETCH:
291 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
292 draw_Picture(org, img, isz, '1 1 1', a);
295 void drawBackground(string img, float a, string algn, float force1)
297 if(main.mainNexposee.ModalController_state == 0)
307 scalemode = SCALEMODE_CROP;
309 for(i = 0; i < strlen(algn); ++i)
311 c = substring(algn, i, 1);
314 case "c": scalemode = SCALEMODE_CROP; goto nopic;
315 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
316 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
317 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
318 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
319 case "1": case "4": case "7": v_x = 0.0; break;
320 case "2": case "5": case "8": v_x = 0.5; break;
321 case "3": case "6": case "9": v_x = 1.0; break;
322 default: v_x = random(); break;
326 case "7": case "8": case "9": v_y = 0.0; break;
327 case "4": case "5": case "6": v_y = 0.5; break;
328 case "1": case "2": case "3": v_y = 1.0; break;
329 default: v_y = random(); break;
332 draw_Picture_Aligned(v, scalemode, img, a);
334 // force all secondary layers to use alpha 1. Prevents ugly issues
335 // with overlap. It's a flag because it cannot be used for the
337 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
339 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
346 float menu_tooltips_old;
347 vector menuTooltipAveragedMousePos;
348 entity menuTooltipItem;
349 vector menuTooltipOrigin;
350 vector menuTooltipSize;
351 float menuTooltipAlpha;
352 string menuTooltipText;
353 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
354 float m_testmousetooltipbox(vector pos)
356 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
357 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
361 float m_testtooltipbox(vector tooltippos)
367 if(tooltippos_x + menuTooltipSize_x > 1)
369 if(tooltippos_y + menuTooltipSize_y > 1)
371 menuTooltipOrigin = tooltippos;
374 float m_allocatetooltipbox(vector pos)
376 vector avoidplus, avoidminus;
379 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
380 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
383 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
384 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
389 if(m_testtooltipbox(v))
393 v_x = pos_x - menuTooltipSize_x * 0.5;
394 if(m_testtooltipbox(v))
398 v_x = pos_x - avoidminus_x;
399 if(m_testtooltipbox(v))
403 v_y = pos_y - avoidminus_y;
404 if(m_testtooltipbox(v))
408 v_x = pos_x - menuTooltipSize_x * 0.5;
409 if(m_testtooltipbox(v))
413 v_x = pos_x + avoidplus_x;
414 if(m_testtooltipbox(v))
419 entity m_findtooltipitem(entity root, vector pos)
427 while(it.instanceOfContainer)
429 while(it.instanceOfNexposee && it.focusedChild)
431 it = it.focusedChild;
432 pos = globalToBox(pos, it.Container_origin, it.Container_size);
434 if(it.instanceOfNexposee)
436 it = it.itemFromPoint(it, pos);
439 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
443 else if(it.instanceOfModalController)
444 it = it.focusedChild;
446 it = it.itemFromPoint(it, pos);
451 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
453 pos = globalToBox(pos, it.Container_origin, it.Container_size);
460 if (menu_tooltips == 2)
463 if (menuTooltipItem.cvarName)
465 if (getCvarsMulti(menuTooltipItem))
466 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
468 s = strcat("[", menuTooltipItem.cvarName, "]");
470 else if (menuTooltipItem.onClickCommand)
471 s = strcat("<", menuTooltipItem.onClickCommand, ">");
473 return menuTooltipItem.tooltip;
474 if (menuTooltipItem.tooltip)
475 return strcat(menuTooltipItem.tooltip, " ", s);
478 return menuTooltipItem.tooltip;
480 void m_tooltip(vector pos)
487 menu_tooltips = cvar("menu_tooltips");
490 // don't return immediately, fade out the active tooltip first
491 if (menuTooltipItem == world)
494 menu_tooltips_old = menu_tooltips;
498 f = bound(0, frametime * 2, 1);
499 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
500 f = vlen(pos - menuTooltipAveragedMousePos);
502 it = m_findtooltipitem(main, pos);
506 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
508 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
509 if(it != menuTooltipItem)
511 switch(menuTooltipState)
516 // another item: fade out first
517 menuTooltipState = 2;
522 menuTooltipState = 1;
523 menuTooltipItem = it;
525 menuTooltipOrigin_x = -1; // unallocated
528 strunzone(menuTooltipText);
529 menuTooltipText = strzone(gettooltip());
533 getWrappedLine_remaining = menuTooltipText;
534 while(getWrappedLine_remaining)
536 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
538 f = draw_TextWidth(s, FALSE, fontsize);
542 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
543 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
544 menuTooltipSize_z = 0;
548 // changing item while fading in: fade out first
549 menuTooltipState = 2;
552 // changing item while fading out: can't
556 else if(menuTooltipState == 2) // re-fade in?
557 menuTooltipState = 1;
560 if(!m_testmousetooltipbox(pos))
561 menuTooltipState = 2; // fade out if mouse touches it
563 switch(menuTooltipState)
566 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
567 if(menuTooltipAlpha == 1)
568 menuTooltipState = 0;
571 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
572 if(menuTooltipAlpha == 0)
574 menuTooltipState = 0;
575 menuTooltipItem = world;
580 if(menuTooltipItem == world)
584 strunzone(menuTooltipText);
585 menuTooltipText = string_null;
591 if(menu_tooltips != menu_tooltips_old)
593 if (menu_tooltips != 0 && menu_tooltips_old != 0)
594 menuTooltipItem = world; // reload tooltip next frame
595 menu_tooltips_old = menu_tooltips;
597 else if(menuTooltipOrigin_x < 0) // unallocated?
598 m_allocatetooltipbox(pos);
600 if(menuTooltipOrigin_x >= 0)
603 p = SKINBORDER_TOOLTIP;
605 p_y *= 1 / conheight;
606 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
607 p = menuTooltipOrigin;
608 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
609 p_y += SKINMARGIN_TOOLTIP_y / conheight;
610 getWrappedLine_remaining = menuTooltipText;
611 while(getWrappedLine_remaining)
613 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
614 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
626 execute_next_frame();
628 menuMouseMode = cvar("menu_mouse_absolute");
634 UpdateConWidthHeight();
638 // TODO draw an info image about this situation
642 if(!menuNotTheFirstFrame)
644 menuNotTheFirstFrame = 1;
646 if(!cvar("menu_video_played"))
648 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
649 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
651 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
652 localcmd("set menu_video_played 1\n");
656 realFrametime = frametime = min(0.2, t - menuPrevTime);
660 t = cvar("menu_slowmo");
671 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
672 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
677 if(cvar("cl_capturevideo"))
678 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
682 gamestatus = gamestatus | GAME_ISSERVER;
683 if(clientstate() == CS_CONNECTED)
684 gamestatus = gamestatus | GAME_CONNECTED;
685 if(cvar("developer"))
686 gamestatus = gamestatus | GAME_DEVELOPER;
688 prevMenuAlpha = menuAlpha;
691 if(menuAlpha == 0 && menuLogoAlpha < 2)
693 menuLogoAlpha = menuLogoAlpha + frametime * 2;
697 menuAlpha = min(1, menuAlpha + frametime * 5);
703 menuAlpha = max(0, menuAlpha - frametime * 5);
707 draw_reset_cropped();
709 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
711 if(menuLogoAlpha > 0)
714 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
715 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
716 draw_reset_cropped();
717 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
719 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
720 draw_drawMousePointer(menuMousePos);
725 else if(SKINALPHA_BACKGROUND_INGAME)
730 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
731 draw_reset_cropped();
735 if(menuAlpha != prevMenuAlpha)
736 cvar_set("_menu_alpha", ftos(menuAlpha));
738 draw_reset_cropped();
740 draw_reset_cropped();
744 if(prevMenuAlpha > 0)
745 main.initializeDialog(main, main.firstChild);
746 draw_reset_cropped();
751 draw_alpha *= menuAlpha;
756 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
757 if(newMouse != '0 0 0')
758 if(newMouse != menuMousePos)
760 menuMousePos = newMouse;
761 if(mouseButtonsPressed)
762 main.mouseDrag(main, menuMousePos);
764 main.mouseMove(main, menuMousePos);
771 vector dMouse, minpos, maxpos;
772 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
773 if(dMouse != '0 0 0')
775 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
776 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
777 dMouse = globalToBoxSize(dMouse, draw_scale);
778 menuMousePos += dMouse * cvar("menu_mouse_speed");
779 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
780 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
781 if(mouseButtonsPressed)
782 main.mouseDrag(main, menuMousePos);
784 main.mouseMove(main, menuMousePos);
790 m_tooltip(menuMousePos);
792 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
794 draw_drawMousePointer(menuMousePos);
796 draw_reset_cropped();
805 setkeydest(KEY_MENU);
806 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
811 if(mouseButtonsPressed)
812 main.mouseRelease(main, menuMousePos);
813 mouseButtonsPressed = 0;
815 main.focusEnter(main);
816 main.showNotify(main);
822 setkeydest(KEY_GAME);
823 setmousetarget(MT_CLIENT);
828 main.focusLeave(main);
829 main.hideNotify(main);
832 void m_toggle(float mode)
853 for(e = NULL; (e = nextent(e)) != NULL; )
855 if(e.classname != "vtbl")
861 void m_focus_item_chain(entity outermost, entity innermost)
863 if(innermost.parent != outermost)
864 m_focus_item_chain(outermost, innermost.parent);
865 innermost.parent.setFocus(innermost.parent, innermost);
868 void m_activate_window(entity wnd)
873 m_activate_window(par);
875 if(par.instanceOfModalController)
877 if(wnd.tabSelectingButton)
879 TabButton_Click(wnd.tabSelectingButton, wnd);
882 par.initializeDialog(par, wnd);
884 else if(par.instanceOfNexposee)
886 // nexposee (sorry for violating abstraction here)
887 par.selectedChild = wnd;
888 par.animationState = 1;
889 Container_setFocus(par, NULL);
891 else if(par.instanceOfContainer)
895 par.setFocus(par, wnd);
899 void m_setpointerfocus(entity wnd)
901 if(wnd.instanceOfContainer)
903 entity focus = wnd.preferredFocusedGrandChild(wnd);
906 menuMousePos = focus.origin + 0.5 * focus.size;
907 menuMousePos_x *= 1 / conwidth;
908 menuMousePos_y *= 1 / conheight;
909 if(wnd.focused) // why does this never happen?
910 m_focus_item_chain(wnd, focus);
915 void m_goto(string itemname)
920 if(itemname == "") // this can be called by GameCommand
922 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
926 m_activate_window(main.mainNexposee);
932 for(e = NULL; (e = find(e, name, itemname)); )
933 if(e.classname != "vtbl")
938 m_activate_window(e);
939 m_setpointerfocus(e);
945 void m_goto_skin_selector()
949 // TODO add code to switch back to the skin selector (no idea how to do it now)
950 m_goto("skinselector");
953 void m_goto_language_selector()
957 // TODO add code to switch back to the language selector (no idea how to do it now)
958 m_goto("languageselector");
961 void m_goto_video_settings()
965 // TODO add code to switch back to the video settings (no idea how to do it now)
966 m_goto("videosettings");