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 const float MENU_ASPECT = 1.25; // 1280x1024
95 const 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)
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);
681 execute_next_frame();
683 menuMouseMode = cvar("menu_mouse_absolute");
689 UpdateConWidthHeight();
693 // TODO draw an info image about this situation
697 if(!menuNotTheFirstFrame)
699 menuNotTheFirstFrame = 1;
701 if(!cvar("menu_video_played"))
703 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.wav\n");
704 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
706 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
707 localcmd("set menu_video_played 1\n");
711 realFrametime = frametime = min(0.2, t - menuPrevTime);
715 t = cvar("menu_slowmo");
726 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
727 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
732 if(cvar("cl_capturevideo"))
733 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
735 prevMenuAlpha = menuAlpha;
738 if(menuAlpha == 0 && menuLogoAlpha < 2)
740 menuLogoAlpha = menuLogoAlpha + frametime * 2;
744 menuAlpha = min(1, menuAlpha + frametime * 5);
750 menuAlpha = max(0, menuAlpha - frametime * 5);
754 draw_reset_cropped();
756 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
758 if(menuLogoAlpha > 0)
761 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
762 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
763 draw_reset_cropped();
764 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
766 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
767 draw_drawMousePointer(menuMousePos);
772 else if(SKINALPHA_BACKGROUND_INGAME)
777 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
778 draw_reset_cropped();
782 if(menuAlpha != prevMenuAlpha)
783 cvar_set("_menu_alpha", ftos(menuAlpha));
785 draw_reset_cropped();
787 draw_reset_cropped();
791 if(prevMenuAlpha > 0)
792 main.initializeDialog(main, main.firstChild);
793 draw_reset_cropped();
798 draw_alpha *= menuAlpha;
802 // do not update mouse position
803 // it prevents mouse jumping to '0 0 0' when menu is fading out
805 else if(menuMouseMode)
808 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
809 if(newMouse != '0 0 0')
810 if(newMouse != menuMousePos)
812 menuMousePos = newMouse;
813 if(mouseButtonsPressed)
814 main.mouseDrag(main, menuMousePos);
816 main.mouseMove(main, menuMousePos);
823 vector dMouse, minpos, maxpos;
824 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
825 if(dMouse != '0 0 0')
827 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
828 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
829 dMouse = globalToBoxSize(dMouse, draw_scale);
830 menuMousePos += dMouse * cvar("menu_mouse_speed");
831 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
832 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
833 if(mouseButtonsPressed)
834 main.mouseDrag(main, menuMousePos);
836 main.mouseMove(main, menuMousePos);
842 m_tooltip(menuMousePos);
844 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
846 draw_drawMousePointer(menuMousePos);
848 draw_reset_cropped();
857 setkeydest(KEY_MENU);
858 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
863 if(mouseButtonsPressed)
864 main.mouseRelease(main, menuMousePos);
865 mouseButtonsPressed = 0;
867 main.focusEnter(main);
868 main.showNotify(main);
874 setkeydest(KEY_GAME);
875 setmousetarget(MT_CLIENT);
880 main.focusLeave(main);
881 main.hideNotify(main);
884 void m_toggle(float mode)
905 for(e = NULL; (e = nextent(e)) != NULL; )
907 if(e.classname != "vtbl")
913 void m_focus_item_chain(entity outermost, entity innermost)
915 if(innermost.parent != outermost)
916 m_focus_item_chain(outermost, innermost.parent);
917 innermost.parent.setFocus(innermost.parent, innermost);
920 void m_activate_window(entity wnd)
925 m_activate_window(par);
927 if(par.instanceOfModalController)
929 if(wnd.tabSelectingButton)
931 TabButton_Click(wnd.tabSelectingButton, wnd);
934 par.initializeDialog(par, wnd);
936 else if(par.instanceOfNexposee)
938 // nexposee (sorry for violating abstraction here)
939 par.selectedChild = wnd;
940 par.animationState = 1;
941 Container_setFocus(par, NULL);
943 else if(par.instanceOfContainer)
947 par.setFocus(par, wnd);
951 void m_setpointerfocus(entity wnd)
953 if(wnd.instanceOfContainer)
955 entity focus = wnd.preferredFocusedGrandChild(wnd);
958 menuMousePos = focus.origin + 0.5 * focus.size;
959 menuMousePos_x *= 1 / conwidth;
960 menuMousePos_y *= 1 / conheight;
961 if(wnd.focused) // why does this never happen?
962 m_focus_item_chain(wnd, focus);
967 void m_goto(string itemname)
973 strunzone(m_goto_buffer);
974 m_goto_buffer = strzone(itemname);
977 if(itemname == "") // this can be called by GameCommand
979 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
985 m_activate_window(main.mainNexposee);
991 for(e = NULL; (e = find(e, name, itemname)); )
992 if(e.classname != "vtbl")
997 m_activate_window(e);
998 m_setpointerfocus(e);