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, vidwidth_s, vidheight_s, realconwidth,
97 realconheight, screenconwidth, screenconheight;
98 void draw_reset_cropped()
100 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
102 void draw_reset_full()
104 draw_reset(realconwidth, realconheight, 0, 0);
107 void UpdateConWidthHeight(float w, float h)
109 if (w != vidwidth_s || h != vidheight_s)
116 conwidth_s = conwidth;
117 conheight_s = conheight;
118 realconwidth = cvar("vid_conwidth");
119 realconheight = cvar("vid_conheight");
120 if(realconwidth / realconheight > MENU_ASPECT)
123 conwidth = realconheight * MENU_ASPECT;
124 conheight = realconheight;
129 conwidth = realconwidth;
130 conheight = realconwidth / MENU_ASPECT;
132 screenconwidth = conwidth;
133 screenconheight = conheight;
134 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
136 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
137 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
139 if(conheight < MENU_MINHEIGHT)
141 conwidth *= MENU_MINHEIGHT / conheight;
142 conheight = MENU_MINHEIGHT;
146 if(conwidth_s != conwidth || conheight_s != conheight)
148 draw_reset_cropped();
149 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
154 string m_goto_buffer;
155 void m_init_delayed()
157 float fh, glob, n, i;
160 conwidth = conheight = -1;
161 UpdateConWidthHeight(cvar("vid_width"), cvar("vid_height"));
162 draw_reset_cropped();
170 if(cvar_string("menu_skin") != "")
172 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
173 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
176 if(cvar_defstring("menu_skin") != "")
178 cvar_set("menu_skin", cvar_defstring("menu_skin"));
179 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
180 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
184 draw_currentSkin = "gfx/menu/default";
185 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
189 error("cannot load any menu skin\n");
191 draw_currentSkin = strzone(draw_currentSkin);
192 while((s = fgets(fh)))
194 // these two are handled by skinlist.qc
195 if(substring(s, 0, 6) == "title ")
197 if(substring(s, 0, 7) == "author ")
199 n = tokenize_console(s);
201 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
205 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
208 n = search_getsize(glob);
209 for(i = 0; i < n; ++i)
210 precache_pic(search_getfilename(glob, i));
214 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
217 anim = spawnAnimHost();
218 main = spawnMainWindow(); main.configureMainWindow(main);
221 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
224 menuMousePos = '0.5 0.5 0';
230 m_goto(m_goto_buffer);
231 strunzone(m_goto_buffer);
232 m_goto_buffer = string_null;
236 m_display(); // delayed menu display
239 void m_keyup (float key, float ascii)
245 draw_reset_cropped();
246 main.keyUp(main, key, ascii, menuShiftState);
247 if(key >= K_MOUSE1 && key <= K_MOUSE3)
249 --mouseButtonsPressed;
250 if(!mouseButtonsPressed)
251 main.mouseRelease(main, menuMousePos);
252 if(mouseButtonsPressed < 0)
254 mouseButtonsPressed = 0;
255 dprint("Warning: released an already released button\n");
258 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
259 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
260 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
263 void m_keydown(float key, float ascii)
274 e.keyGrabbed(e, key, ascii);
278 draw_reset_cropped();
279 if(key >= K_MOUSE1 && key <= K_MOUSE3)
280 if(!mouseButtonsPressed)
281 main.mousePress(main, menuMousePos);
282 if(!main.keyDown(main, key, ascii, menuShiftState))
284 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
285 m_hide(); // disable menu on unhandled ESC
287 if(key >= K_MOUSE1 && key <= K_MOUSE3)
289 ++mouseButtonsPressed;
290 if(mouseButtonsPressed > 10)
292 mouseButtonsPressed = 10;
293 dprint("Warning: pressed an already pressed button\n");
296 if(key == K_ALT) menuShiftState |= S_ALT;
297 if(key == K_CTRL) menuShiftState |= S_CTRL;
298 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
301 float SCALEMODE_CROP = 0;
302 float SCALEMODE_LETTERBOX = 1;
303 float SCALEMODE_WIDTH = 2;
304 float SCALEMODE_HEIGHT = 3;
305 float SCALEMODE_STRETCH = 4;
306 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
308 vector sz, org, isz, isz_w, isz_h;
309 float width_is_larger;
311 sz = draw_PictureSize(img);
312 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
313 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
314 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
323 isz = (width_is_larger ? isz_h : isz_w);
325 case SCALEMODE_LETTERBOX:
326 isz = (width_is_larger ? isz_w : isz_h);
328 case SCALEMODE_WIDTH:
331 case SCALEMODE_HEIGHT:
334 case SCALEMODE_STRETCH:
339 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
340 draw_Picture(org, img, isz, '1 1 1', a);
343 void drawBackground(string img, float a, string algn, float force1)
345 if(main.mainNexposee.ModalController_state == 0)
355 scalemode = SCALEMODE_CROP;
358 for(i = 0; i < strlen(algn); ++i)
360 c = substring(algn, i, 1);
363 case "c": scalemode = SCALEMODE_CROP; goto nopic;
364 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
365 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
366 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
367 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
368 case "1": case "4": case "7": v_x = 0.0; break;
369 case "2": case "5": case "8": v_x = 0.5; break;
370 case "3": case "6": case "9": v_x = 1.0; break;
371 default: v_x = random(); break;
375 case "7": case "8": case "9": v_y = 0.0; break;
376 case "4": case "5": case "6": v_y = 0.5; break;
377 case "1": case "2": case "3": v_y = 1.0; break;
378 default: v_y = random(); break;
381 draw_Picture_Aligned(v, scalemode, img, a);
383 // force all secondary layers to use alpha 1. Prevents ugly issues
384 // with overlap. It's a flag because it cannot be used for the
386 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
388 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
395 float menu_tooltips_old;
396 vector menuTooltipAveragedMousePos;
397 entity menuTooltipItem;
398 vector menuTooltipOrigin;
399 vector menuTooltipSize;
400 float menuTooltipAlpha;
401 string menuTooltipText;
402 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
403 float m_testmousetooltipbox(vector pos)
405 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
406 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
410 float m_testtooltipbox(vector tooltippos)
416 if(tooltippos_x + menuTooltipSize_x > 1)
418 if(tooltippos_y + menuTooltipSize_y > 1)
420 menuTooltipOrigin = tooltippos;
423 float m_allocatetooltipbox(vector pos)
425 vector avoidplus, avoidminus;
428 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
429 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
432 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
433 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
438 if(m_testtooltipbox(v))
442 v_x = pos_x - menuTooltipSize_x * 0.5;
443 if(m_testtooltipbox(v))
447 v_x = pos_x - avoidminus_x;
448 if(m_testtooltipbox(v))
452 v_y = pos_y - avoidminus_y;
453 if(m_testtooltipbox(v))
457 v_x = pos_x - menuTooltipSize_x * 0.5;
458 if(m_testtooltipbox(v))
462 v_x = pos_x + avoidplus_x;
463 if(m_testtooltipbox(v))
468 entity m_findtooltipitem(entity root, vector pos)
476 while(it.instanceOfContainer)
478 while(it.instanceOfNexposee && it.focusedChild)
480 it = it.focusedChild;
481 pos = globalToBox(pos, it.Container_origin, it.Container_size);
483 if(it.instanceOfNexposee)
485 it = it.itemFromPoint(it, pos);
488 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
492 else if(it.instanceOfModalController)
493 it = it.focusedChild;
495 it = it.itemFromPoint(it, pos);
500 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
502 pos = globalToBox(pos, it.Container_origin, it.Container_size);
509 if (menu_tooltips == 2)
512 if (menuTooltipItem.cvarName)
514 if (getCvarsMulti(menuTooltipItem))
515 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
517 s = strcat("[", menuTooltipItem.cvarName, "]");
519 else if (menuTooltipItem.onClickCommand)
520 s = strcat("<", menuTooltipItem.onClickCommand, ">");
522 return menuTooltipItem.tooltip;
523 if (menuTooltipItem.tooltip)
524 return strcat(menuTooltipItem.tooltip, " ", s);
527 return menuTooltipItem.tooltip;
529 void m_tooltip(vector pos)
536 menu_tooltips = cvar("menu_tooltips");
539 // don't return immediately, fade out the active tooltip first
540 if (menuTooltipItem == world)
543 menu_tooltips_old = menu_tooltips;
547 f = bound(0, frametime * 2, 1);
548 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
549 f = vlen(pos - menuTooltipAveragedMousePos);
551 it = m_findtooltipitem(main, pos);
555 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
557 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
558 if(it != menuTooltipItem)
560 switch(menuTooltipState)
565 // another item: fade out first
566 menuTooltipState = 2;
571 menuTooltipState = 1;
572 menuTooltipItem = it;
574 menuTooltipOrigin_x = -1; // unallocated
577 strunzone(menuTooltipText);
578 menuTooltipText = strzone(gettooltip());
582 getWrappedLine_remaining = menuTooltipText;
583 while(getWrappedLine_remaining)
585 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
587 f = draw_TextWidth(s, FALSE, fontsize);
591 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
592 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
593 menuTooltipSize_z = 0;
597 // changing item while fading in: fade out first
598 menuTooltipState = 2;
601 // changing item while fading out: can't
605 else if(menuTooltipState == 2) // re-fade in?
606 menuTooltipState = 1;
609 if(!m_testmousetooltipbox(pos))
610 menuTooltipState = 2; // fade out if mouse touches it
612 switch(menuTooltipState)
615 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
616 if(menuTooltipAlpha == 1)
617 menuTooltipState = 0;
620 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
621 if(menuTooltipAlpha == 0)
623 menuTooltipState = 0;
624 menuTooltipItem = world;
629 if(menuTooltipItem == world)
633 strunzone(menuTooltipText);
634 menuTooltipText = string_null;
640 if(menu_tooltips != menu_tooltips_old)
642 if (menu_tooltips != 0 && menu_tooltips_old != 0)
643 menuTooltipItem = world; // reload tooltip next frame
644 menu_tooltips_old = menu_tooltips;
646 else if(menuTooltipOrigin_x < 0) // unallocated?
647 m_allocatetooltipbox(pos);
649 if(menuTooltipOrigin_x >= 0)
652 p = SKINBORDER_TOOLTIP;
654 p_y *= 1 / conheight;
655 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
656 p = menuTooltipOrigin;
657 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
658 p_y += SKINMARGIN_TOOLTIP_y / conheight;
659 getWrappedLine_remaining = menuTooltipText;
660 while(getWrappedLine_remaining)
662 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
663 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
670 void m_draw(float width, float height)
677 execute_next_frame();
679 menuMouseMode = cvar("menu_mouse_absolute");
686 #ifdef COMPAT_XON070_OLD_M_DRAW
687 UpdateConWidthHeight(width ? width : cvar("vid_width"), height ? height : cvar("vid_height"));
689 UpdateConWidthHeight(width, height);
695 // TODO draw an info image about this situation
699 if(!menuNotTheFirstFrame)
701 menuNotTheFirstFrame = 1;
703 if(!cvar("menu_video_played"))
705 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.wav\n");
706 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
708 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
709 localcmd("set menu_video_played 1\n");
713 realFrametime = frametime = min(0.2, t - menuPrevTime);
717 t = cvar("menu_slowmo");
728 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
729 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
734 if(cvar("cl_capturevideo"))
735 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
737 prevMenuAlpha = menuAlpha;
740 if(menuAlpha == 0 && menuLogoAlpha < 2)
742 menuLogoAlpha = menuLogoAlpha + frametime * 2;
746 menuAlpha = min(1, menuAlpha + frametime * 5);
752 menuAlpha = max(0, menuAlpha - frametime * 5);
756 draw_reset_cropped();
758 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
760 if(menuLogoAlpha > 0)
763 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
764 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
765 draw_reset_cropped();
766 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
768 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
769 draw_drawMousePointer(menuMousePos);
774 else if(SKINALPHA_BACKGROUND_INGAME)
779 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
780 draw_reset_cropped();
784 if(menuAlpha != prevMenuAlpha)
785 cvar_set("_menu_alpha", ftos(menuAlpha));
787 draw_reset_cropped();
789 draw_reset_cropped();
793 if(prevMenuAlpha > 0)
794 main.initializeDialog(main, main.firstChild);
795 draw_reset_cropped();
800 draw_alpha *= menuAlpha;
804 // do not update mouse position
805 // it prevents mouse jumping to '0 0 0' when menu is fading out
807 else if(menuMouseMode)
810 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
811 if(newMouse != '0 0 0')
812 if(newMouse != menuMousePos)
814 menuMousePos = newMouse;
815 if(mouseButtonsPressed)
816 main.mouseDrag(main, menuMousePos);
818 main.mouseMove(main, menuMousePos);
825 vector dMouse, minpos, maxpos;
826 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
827 if(dMouse != '0 0 0')
829 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
830 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
831 dMouse = globalToBoxSize(dMouse, draw_scale);
832 menuMousePos += dMouse * cvar("menu_mouse_speed");
833 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
834 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
835 if(mouseButtonsPressed)
836 main.mouseDrag(main, menuMousePos);
838 main.mouseMove(main, menuMousePos);
844 m_tooltip(menuMousePos);
846 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
848 draw_drawMousePointer(menuMousePos);
850 draw_reset_cropped();
859 setkeydest(KEY_MENU);
860 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
865 if(mouseButtonsPressed)
866 main.mouseRelease(main, menuMousePos);
867 mouseButtonsPressed = 0;
869 main.focusEnter(main);
870 main.showNotify(main);
876 setkeydest(KEY_GAME);
877 setmousetarget(MT_CLIENT);
882 main.focusLeave(main);
883 main.hideNotify(main);
886 void m_toggle(float mode)
907 for(e = NULL; (e = nextent(e)) != NULL; )
909 if(e.classname != "vtbl")
915 void m_focus_item_chain(entity outermost, entity innermost)
917 if(innermost.parent != outermost)
918 m_focus_item_chain(outermost, innermost.parent);
919 innermost.parent.setFocus(innermost.parent, innermost);
922 void m_activate_window(entity wnd)
927 m_activate_window(par);
929 if(par.instanceOfModalController)
931 if(wnd.tabSelectingButton)
933 TabButton_Click(wnd.tabSelectingButton, wnd);
936 par.initializeDialog(par, wnd);
938 else if(par.instanceOfNexposee)
940 // nexposee (sorry for violating abstraction here)
941 par.selectedChild = wnd;
942 par.animationState = 1;
943 Container_setFocus(par, NULL);
945 else if(par.instanceOfContainer)
949 par.setFocus(par, wnd);
953 void m_setpointerfocus(entity wnd)
955 if(wnd.instanceOfContainer)
957 entity focus = wnd.preferredFocusedGrandChild(wnd);
960 menuMousePos = focus.origin + 0.5 * focus.size;
961 menuMousePos_x *= 1 / conwidth;
962 menuMousePos_y *= 1 / conheight;
963 if(wnd.focused) // why does this never happen?
964 m_focus_item_chain(wnd, focus);
969 void m_goto(string itemname)
975 strunzone(m_goto_buffer);
976 m_goto_buffer = strzone(itemname);
979 if(itemname == "") // this can be called by GameCommand
981 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
987 m_activate_window(main.mainNexposee);
993 for(e = NULL; (e = find(e, name, itemname)); )
994 if(e.classname != "vtbl")
999 m_activate_window(e);
1000 m_setpointerfocus(e);