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 print(sprintf(_("^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 float ddsload = cvar("r_texture_dds_load");
81 float texcomp = cvar("gl_texturecompression");
83 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
84 localcmd("\nr_restart\n");
88 if(cvar("_menu_initialized")) // always show menu after menu_restart
92 cvar_set("_menu_initialized", "1");
96 const float MENU_ASPECT = 1.25; // 1280x1024
98 void draw_reset_cropped()
100 draw_reset(conwidth, conheight, 0.5 * (realconwidth - conwidth), 0.5 * (realconheight - conheight));
102 void draw_reset_full()
104 draw_reset(realconwidth, realconheight, 0, 0);
107 void UpdateConWidthHeight(float w, float h, float p)
109 if (w != vidwidth_s || h != vidheight_s || p != vidpixelheight_s)
111 if (updateConwidths(w, h, p))
112 localcmd(sprintf("\nexec %s\n", cvar_string("menu_font_cfg")));
115 vidpixelheight_s = p;
117 conwidth_s = conwidth;
118 conheight_s = conheight;
119 realconwidth = cvar("vid_conwidth");
120 realconheight = cvar("vid_conheight");
121 if(realconwidth / realconheight > MENU_ASPECT)
124 conwidth = realconheight * MENU_ASPECT;
125 conheight = realconheight;
130 conwidth = realconwidth;
131 conheight = realconwidth / MENU_ASPECT;
135 if(conwidth_s != conwidth || conheight_s != conheight)
137 draw_reset_cropped();
138 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
143 vidwidth_s = vidheight_s = vidpixelheight_s = 0; // retry next frame
147 string m_goto_buffer;
148 void m_init_delayed()
150 float fh, glob, n, i;
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)
262 if(key >= K_MOUSE1 && key <= K_MOUSE3)
264 // detect a click outside of the game window
265 vector p = getmousepos();
266 if(p_x < 0 || p_x > realconwidth || p_y < 0 || p_y > realconheight)
268 ++mouseButtonsPressed;
278 e.keyGrabbed(e, key, ascii);
282 draw_reset_cropped();
283 if(key >= K_MOUSE1 && key <= K_MOUSE3)
284 if(!mouseButtonsPressed)
285 main.mousePress(main, menuMousePos);
286 if(!main.keyDown(main, key, ascii, menuShiftState))
288 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
289 m_hide(); // disable menu on unhandled ESC
291 if(key >= K_MOUSE1 && key <= K_MOUSE3)
293 ++mouseButtonsPressed;
294 if(mouseButtonsPressed > 10)
296 mouseButtonsPressed = 10;
297 dprint("Warning: pressed an already pressed button\n");
300 if(key == K_ALT) menuShiftState |= S_ALT;
301 if(key == K_CTRL) menuShiftState |= S_CTRL;
302 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
305 const float SCALEMODE_CROP = 0;
306 const float SCALEMODE_LETTERBOX = 1;
307 const float SCALEMODE_WIDTH = 2;
308 const float SCALEMODE_HEIGHT = 3;
309 const float SCALEMODE_STRETCH = 4;
310 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
312 vector sz, org, isz, isz_w, isz_h;
313 float width_is_larger;
315 sz = draw_PictureSize(img);
316 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
317 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
318 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
327 isz = (width_is_larger ? isz_h : isz_w);
329 case SCALEMODE_LETTERBOX:
330 isz = (width_is_larger ? isz_w : isz_h);
332 case SCALEMODE_WIDTH:
335 case SCALEMODE_HEIGHT:
338 case SCALEMODE_STRETCH:
343 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
344 draw_Picture(org, img, isz, '1 1 1', a);
347 void drawBackground(string img, float a, string algn, float force1)
349 if(main.mainNexposee.ModalController_state == 0)
359 scalemode = SCALEMODE_CROP;
362 for(i = 0; i < strlen(algn); ++i)
364 c = substring(algn, i, 1);
367 case "c": scalemode = SCALEMODE_CROP; goto nopic;
368 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
369 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
370 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
371 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
372 case "1": case "4": case "7": v_x = 0.0; break;
373 case "2": case "5": case "8": v_x = 0.5; break;
374 case "3": case "6": case "9": v_x = 1.0; break;
375 default: v_x = random(); break;
379 case "7": case "8": case "9": v_y = 0.0; break;
380 case "4": case "5": case "6": v_y = 0.5; break;
381 case "1": case "2": case "3": v_y = 1.0; break;
382 default: v_y = random(); break;
385 draw_Picture_Aligned(v, scalemode, img, a);
387 // force all secondary layers to use alpha 1. Prevents ugly issues
388 // with overlap. It's a flag because it cannot be used for the
390 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
392 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
399 float menu_tooltips_old;
400 vector menuTooltipAveragedMousePos;
401 entity menuTooltipItem;
402 vector menuTooltipOrigin;
403 vector menuTooltipSize;
404 float menuTooltipAlpha;
405 string menuTooltipText;
406 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
407 float m_testmousetooltipbox(vector pos)
409 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
410 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
414 float m_testtooltipbox(vector tooltippos)
420 if(tooltippos_x + menuTooltipSize_x > 1)
422 if(tooltippos_y + menuTooltipSize_y > 1)
424 menuTooltipOrigin = tooltippos;
427 float m_allocatetooltipbox(vector pos)
429 vector avoidplus, avoidminus;
432 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
433 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
436 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
437 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
442 if(m_testtooltipbox(v))
446 v_x = pos_x - menuTooltipSize_x * 0.5;
447 if(m_testtooltipbox(v))
451 v_x = pos_x - avoidminus_x;
452 if(m_testtooltipbox(v))
456 v_y = pos_y - avoidminus_y;
457 if(m_testtooltipbox(v))
461 v_x = pos_x - menuTooltipSize_x * 0.5;
462 if(m_testtooltipbox(v))
466 v_x = pos_x + avoidplus_x;
467 if(m_testtooltipbox(v))
472 entity m_findtooltipitem(entity root, vector pos)
480 while(it.instanceOfContainer)
482 while(it.instanceOfNexposee && it.focusedChild)
484 it = it.focusedChild;
485 pos = globalToBox(pos, it.Container_origin, it.Container_size);
487 if(it.instanceOfNexposee)
489 it = it.itemFromPoint(it, pos);
492 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
496 else if(it.instanceOfModalController)
497 it = it.focusedChild;
499 it = it.itemFromPoint(it, pos);
504 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
506 pos = globalToBox(pos, it.Container_origin, it.Container_size);
513 if (menu_tooltips == 2)
516 if (menuTooltipItem.cvarName)
518 if (getCvarsMulti(menuTooltipItem))
519 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
521 s = strcat("[", menuTooltipItem.cvarName, "]");
523 else if (menuTooltipItem.onClickCommand)
524 s = strcat("<", menuTooltipItem.onClickCommand, ">");
526 return menuTooltipItem.tooltip;
527 if (menuTooltipItem.tooltip)
528 return strcat(menuTooltipItem.tooltip, " ", s);
531 return menuTooltipItem.tooltip;
533 void m_tooltip(vector pos)
540 menu_tooltips = cvar("menu_tooltips");
543 // don't return immediately, fade out the active tooltip first
544 if (menuTooltipItem == world)
547 menu_tooltips_old = menu_tooltips;
551 f = bound(0, frametime * 2, 1);
552 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
553 f = vlen(pos - menuTooltipAveragedMousePos);
555 it = m_findtooltipitem(main, pos);
559 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
561 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
562 if(it != menuTooltipItem)
564 switch(menuTooltipState)
569 // another item: fade out first
570 menuTooltipState = 2;
575 menuTooltipState = 1;
576 menuTooltipItem = it;
578 menuTooltipOrigin_x = -1; // unallocated
581 strunzone(menuTooltipText);
582 menuTooltipText = strzone(gettooltip());
586 getWrappedLine_remaining = menuTooltipText;
587 while(getWrappedLine_remaining)
589 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
591 f = draw_TextWidth(s, FALSE, fontsize);
595 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
596 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
597 menuTooltipSize_z = 0;
601 // changing item while fading in: fade out first
602 menuTooltipState = 2;
605 // changing item while fading out: can't
609 else if(menuTooltipState == 2) // re-fade in?
610 menuTooltipState = 1;
613 if(!m_testmousetooltipbox(pos))
614 menuTooltipState = 2; // fade out if mouse touches it
616 switch(menuTooltipState)
619 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
620 if(menuTooltipAlpha == 1)
621 menuTooltipState = 0;
624 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
625 if(menuTooltipAlpha == 0)
627 menuTooltipState = 0;
628 menuTooltipItem = world;
633 if(menuTooltipItem == world)
637 strunzone(menuTooltipText);
638 menuTooltipText = string_null;
644 if(menu_tooltips != menu_tooltips_old)
646 if (menu_tooltips != 0 && menu_tooltips_old != 0)
647 menuTooltipItem = world; // reload tooltip next frame
648 menu_tooltips_old = menu_tooltips;
650 else if(menuTooltipOrigin_x < 0) // unallocated?
651 m_allocatetooltipbox(pos);
653 if(menuTooltipOrigin_x >= 0)
656 p = SKINBORDER_TOOLTIP;
658 p_y *= 1 / conheight;
659 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
660 p = menuTooltipOrigin;
661 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
662 p_y += SKINMARGIN_TOOLTIP_y / conheight;
663 getWrappedLine_remaining = menuTooltipText;
664 while(getWrappedLine_remaining)
666 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
667 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
674 void m_draw(float width, float height)
681 execute_next_frame();
683 menuMouseMode = cvar("menu_mouse_absolute");
688 UpdateConWidthHeight(width, height, cvar("vid_pixelheight"));
692 // TODO draw an info image about this situation
696 if(!menuNotTheFirstFrame)
698 menuNotTheFirstFrame = 1;
700 if(!cvar("menu_video_played"))
702 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.wav\n");
703 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
705 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
706 localcmd("set menu_video_played 1\n");
710 realFrametime = frametime = min(0.2, t - menuPrevTime);
714 t = cvar("menu_slowmo");
725 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
726 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
731 if(cvar("cl_capturevideo"))
732 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
734 prevMenuAlpha = menuAlpha;
737 if(menuAlpha == 0 && menuLogoAlpha < 2)
739 menuLogoAlpha = menuLogoAlpha + frametime * 2;
743 menuAlpha = min(1, menuAlpha + frametime * 5);
749 menuAlpha = max(0, menuAlpha - frametime * 5);
753 draw_reset_cropped();
755 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
757 if(menuLogoAlpha > 0)
760 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
761 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
762 draw_reset_cropped();
763 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
765 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
766 draw_drawMousePointer(menuMousePos);
771 else if(SKINALPHA_BACKGROUND_INGAME)
776 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
777 draw_reset_cropped();
781 if(menuAlpha != prevMenuAlpha)
782 cvar_set("_menu_alpha", ftos(menuAlpha));
784 draw_reset_cropped();
786 draw_reset_cropped();
790 if(prevMenuAlpha > 0)
791 main.initializeDialog(main, main.firstChild);
792 draw_reset_cropped();
797 draw_alpha *= menuAlpha;
801 // do not update mouse position
802 // it prevents mouse jumping to '0 0 0' when menu is fading out
804 else if(menuMouseMode)
807 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
808 if(newMouse != '0 0 0')
809 if(newMouse != menuMousePos)
811 menuMousePos = newMouse;
812 if(mouseButtonsPressed)
813 main.mouseDrag(main, menuMousePos);
815 main.mouseMove(main, menuMousePos);
822 vector dMouse, minpos, maxpos;
823 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
824 if(dMouse != '0 0 0')
826 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
827 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
828 dMouse = globalToBoxSize(dMouse, draw_scale);
829 menuMousePos += dMouse * cvar("menu_mouse_speed");
830 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
831 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
832 if(mouseButtonsPressed)
833 main.mouseDrag(main, menuMousePos);
835 main.mouseMove(main, menuMousePos);
841 m_tooltip(menuMousePos);
843 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
845 draw_drawMousePointer(menuMousePos);
847 draw_reset_cropped();
856 setkeydest(KEY_MENU);
857 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
862 if(mouseButtonsPressed)
863 main.mouseRelease(main, menuMousePos);
864 mouseButtonsPressed = 0;
866 main.focusEnter(main);
867 main.showNotify(main);
873 setkeydest(KEY_GAME);
874 setmousetarget(MT_CLIENT);
879 main.focusLeave(main);
880 main.hideNotify(main);
883 void m_toggle(float mode)
904 for(e = NULL; (e = nextent(e)) != NULL; )
906 if(e.classname != "vtbl")
912 void m_focus_item_chain(entity outermost, entity innermost)
914 if(innermost.parent != outermost)
915 m_focus_item_chain(outermost, innermost.parent);
916 innermost.parent.setFocus(innermost.parent, innermost);
919 void m_activate_window(entity wnd)
924 m_activate_window(par);
926 if(par.instanceOfModalController)
928 if(wnd.tabSelectingButton)
930 TabButton_Click(wnd.tabSelectingButton, wnd);
933 par.initializeDialog(par, wnd);
935 else if(par.instanceOfNexposee)
937 // nexposee (sorry for violating abstraction here)
938 par.selectedChild = wnd;
939 par.animationState = 1;
940 Container_setFocus(par, NULL);
942 else if(par.instanceOfContainer)
946 par.setFocus(par, wnd);
950 void m_setpointerfocus(entity wnd)
952 if(wnd.instanceOfContainer)
954 entity focus = wnd.preferredFocusedGrandChild(wnd);
957 menuMousePos = focus.origin + 0.5 * focus.size;
958 menuMousePos_x *= 1 / conwidth;
959 menuMousePos_y *= 1 / conheight;
960 if(wnd.focused) // why does this never happen?
961 m_focus_item_chain(wnd, focus);
966 void m_goto(string itemname)
972 strunzone(m_goto_buffer);
973 m_goto_buffer = strzone(itemname);
976 if(itemname == "") // this can be called by GameCommand
978 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
984 m_activate_window(main.mainNexposee);
990 for(e = NULL; (e = find(e, name, itemname)); )
991 if(e.classname != "vtbl")
994 if((e) && (!e.requiresConnection || (gamestatus & (GAME_ISSERVER | GAME_CONNECTED))))
997 m_activate_window(e);
998 m_setpointerfocus(e);