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 = cvar_string("prvm_language");
33 if(prvm_language == "")
36 cvar_set("prvm_language", prvm_language);
37 localcmd("\nmenu_restart\n");
39 prvm_language = strzone(prvm_language);
40 cvar_set("_menu_prvm_language", prvm_language);
42 check_unacceptable_compiler_bugs();
45 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK()));
48 // list all game dirs (TEST)
55 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
58 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
62 // needs to be done so early because of the constants they create
66 float ddsload = cvar("r_texture_dds_load");
67 float texcomp = cvar("gl_texturecompression");
69 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
70 localcmd("\nr_restart\n");
76 float MENU_ASPECT = 1.25; // 1280x1024
77 float MENU_MINHEIGHT = 600;
78 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
79 void draw_reset_cropped()
81 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
83 void draw_reset_full()
85 draw_reset(realconwidth, realconheight, 0, 0);
87 void UpdateConWidthHeight()
89 conwidth_s = conwidth;
90 conheight_s = conheight;
91 realconwidth = cvar("vid_conwidth");
92 realconheight = cvar("vid_conheight");
93 if(realconwidth / realconheight > MENU_ASPECT)
96 conwidth = realconheight * MENU_ASPECT;
97 conheight = realconheight;
102 conwidth = realconwidth;
103 conheight = realconwidth / MENU_ASPECT;
105 screenconwidth = conwidth;
106 screenconheight = conheight;
107 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
109 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
110 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
112 if(conheight < MENU_MINHEIGHT)
114 conwidth *= MENU_MINHEIGHT / conheight;
115 conheight = MENU_MINHEIGHT;
119 if(conwidth_s != conwidth || conheight_s != conheight)
121 draw_reset_cropped();
122 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
127 string m_goto_buffer;
128 void m_init_delayed()
130 float fh, glob, n, i;
133 conwidth = conheight = -1;
134 UpdateConWidthHeight();
135 draw_reset_cropped();
143 if(cvar_string("menu_skin") != "")
145 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
146 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
149 if(cvar_defstring("menu_skin") != "")
151 cvar_set("menu_skin", cvar_defstring("menu_skin"));
152 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
153 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
157 draw_currentSkin = "gfx/menu/default";
158 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
162 error("cannot load any menu skin\n");
164 draw_currentSkin = strzone(draw_currentSkin);
165 while((s = fgets(fh)))
167 // these two are handled by skinlist.qc
168 if(substring(s, 0, 6) == "title ")
170 if(substring(s, 0, 7) == "author ")
172 n = tokenize_console(s);
174 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
178 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
181 n = search_getsize(glob);
182 for(i = 0; i < n; ++i)
183 precache_pic(search_getfilename(glob, i));
187 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
190 anim = spawnAnimHost();
191 main = spawnMainWindow(); main.configureMainWindow(main);
194 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
197 menuMousePos = '0.5 0.5 0';
203 m_goto(m_goto_buffer);
204 strunzone(m_goto_buffer);
205 m_goto_buffer = string_null;
209 m_display(); // delayed menu display
212 void m_keyup (float key, float ascii)
218 draw_reset_cropped();
219 main.keyUp(main, key, ascii, menuShiftState);
220 if(key >= K_MOUSE1 && key <= K_MOUSE3)
222 --mouseButtonsPressed;
223 if(!mouseButtonsPressed)
224 main.mouseRelease(main, menuMousePos);
225 if(mouseButtonsPressed < 0)
227 mouseButtonsPressed = 0;
228 dprint("Warning: released an already released button\n");
231 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
232 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
233 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
236 void m_keydown(float key, float ascii)
247 e.keyGrabbed(e, key, ascii);
251 draw_reset_cropped();
252 if(key >= K_MOUSE1 && key <= K_MOUSE3)
253 if(!mouseButtonsPressed)
254 main.mousePress(main, menuMousePos);
255 if(!main.keyDown(main, key, ascii, menuShiftState))
257 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
258 m_hide(); // disable menu on unhandled ESC
260 if(key >= K_MOUSE1 && key <= K_MOUSE3)
262 ++mouseButtonsPressed;
263 if(mouseButtonsPressed > 10)
265 mouseButtonsPressed = 10;
266 dprint("Warning: pressed an already pressed button\n");
269 if(key == K_ALT) menuShiftState |= S_ALT;
270 if(key == K_CTRL) menuShiftState |= S_CTRL;
271 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
274 float SCALEMODE_CROP = 0;
275 float SCALEMODE_LETTERBOX = 1;
276 float SCALEMODE_WIDTH = 2;
277 float SCALEMODE_HEIGHT = 3;
278 float SCALEMODE_STRETCH = 4;
279 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
281 vector sz, org, isz, isz_w, isz_h;
282 float width_is_larger;
284 sz = draw_PictureSize(img);
285 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
286 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
287 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
293 isz = (width_is_larger ? isz_h : isz_w);
295 case SCALEMODE_LETTERBOX:
296 isz = (width_is_larger ? isz_w : isz_h);
298 case SCALEMODE_WIDTH:
301 case SCALEMODE_HEIGHT:
304 case SCALEMODE_STRETCH:
309 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
310 draw_Picture(org, img, isz, '1 1 1', a);
313 void drawBackground(string img, float a, string algn, float force1)
315 if(main.mainNexposee.ModalController_state == 0)
325 scalemode = SCALEMODE_CROP;
327 for(i = 0; i < strlen(algn); ++i)
329 c = substring(algn, i, 1);
332 case "c": scalemode = SCALEMODE_CROP; goto nopic;
333 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
334 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
335 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
336 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
337 case "1": case "4": case "7": v_x = 0.0; break;
338 case "2": case "5": case "8": v_x = 0.5; break;
339 case "3": case "6": case "9": v_x = 1.0; break;
340 default: v_x = random(); break;
344 case "7": case "8": case "9": v_y = 0.0; break;
345 case "4": case "5": case "6": v_y = 0.5; break;
346 case "1": case "2": case "3": v_y = 1.0; break;
347 default: v_y = random(); break;
350 draw_Picture_Aligned(v, scalemode, img, a);
352 // force all secondary layers to use alpha 1. Prevents ugly issues
353 // with overlap. It's a flag because it cannot be used for the
355 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
357 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
364 float menu_tooltips_old;
365 vector menuTooltipAveragedMousePos;
366 entity menuTooltipItem;
367 vector menuTooltipOrigin;
368 vector menuTooltipSize;
369 float menuTooltipAlpha;
370 string menuTooltipText;
371 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
372 float m_testmousetooltipbox(vector pos)
374 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
375 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
379 float m_testtooltipbox(vector tooltippos)
385 if(tooltippos_x + menuTooltipSize_x > 1)
387 if(tooltippos_y + menuTooltipSize_y > 1)
389 menuTooltipOrigin = tooltippos;
392 float m_allocatetooltipbox(vector pos)
394 vector avoidplus, avoidminus;
397 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
398 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
401 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
402 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
407 if(m_testtooltipbox(v))
411 v_x = pos_x - menuTooltipSize_x * 0.5;
412 if(m_testtooltipbox(v))
416 v_x = pos_x - avoidminus_x;
417 if(m_testtooltipbox(v))
421 v_y = pos_y - avoidminus_y;
422 if(m_testtooltipbox(v))
426 v_x = pos_x - menuTooltipSize_x * 0.5;
427 if(m_testtooltipbox(v))
431 v_x = pos_x + avoidplus_x;
432 if(m_testtooltipbox(v))
437 entity m_findtooltipitem(entity root, vector pos)
445 while(it.instanceOfContainer)
447 while(it.instanceOfNexposee && it.focusedChild)
449 it = it.focusedChild;
450 pos = globalToBox(pos, it.Container_origin, it.Container_size);
452 if(it.instanceOfNexposee)
454 it = it.itemFromPoint(it, pos);
457 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
461 else if(it.instanceOfModalController)
462 it = it.focusedChild;
464 it = it.itemFromPoint(it, pos);
469 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
471 pos = globalToBox(pos, it.Container_origin, it.Container_size);
478 if (menu_tooltips == 2)
481 if (menuTooltipItem.cvarName)
483 if (getCvarsMulti(menuTooltipItem))
484 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
486 s = strcat("[", menuTooltipItem.cvarName, "]");
488 else if (menuTooltipItem.onClickCommand)
489 s = strcat("<", menuTooltipItem.onClickCommand, ">");
491 return menuTooltipItem.tooltip;
492 if (menuTooltipItem.tooltip)
493 return strcat(menuTooltipItem.tooltip, " ", s);
496 return menuTooltipItem.tooltip;
498 void m_tooltip(vector pos)
505 menu_tooltips = cvar("menu_tooltips");
508 // don't return immediately, fade out the active tooltip first
509 if (menuTooltipItem == world)
512 menu_tooltips_old = menu_tooltips;
516 f = bound(0, frametime * 2, 1);
517 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
518 f = vlen(pos - menuTooltipAveragedMousePos);
520 it = m_findtooltipitem(main, pos);
524 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
526 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
527 if(it != menuTooltipItem)
529 switch(menuTooltipState)
534 // another item: fade out first
535 menuTooltipState = 2;
540 menuTooltipState = 1;
541 menuTooltipItem = it;
543 menuTooltipOrigin_x = -1; // unallocated
546 strunzone(menuTooltipText);
547 menuTooltipText = strzone(gettooltip());
551 getWrappedLine_remaining = menuTooltipText;
552 while(getWrappedLine_remaining)
554 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
556 f = draw_TextWidth(s, FALSE, fontsize);
560 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
561 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
562 menuTooltipSize_z = 0;
566 // changing item while fading in: fade out first
567 menuTooltipState = 2;
570 // changing item while fading out: can't
574 else if(menuTooltipState == 2) // re-fade in?
575 menuTooltipState = 1;
578 if(!m_testmousetooltipbox(pos))
579 menuTooltipState = 2; // fade out if mouse touches it
581 switch(menuTooltipState)
584 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
585 if(menuTooltipAlpha == 1)
586 menuTooltipState = 0;
589 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
590 if(menuTooltipAlpha == 0)
592 menuTooltipState = 0;
593 menuTooltipItem = world;
598 if(menuTooltipItem == world)
602 strunzone(menuTooltipText);
603 menuTooltipText = string_null;
609 if(menu_tooltips != menu_tooltips_old)
611 if (menu_tooltips != 0 && menu_tooltips_old != 0)
612 menuTooltipItem = world; // reload tooltip next frame
613 menu_tooltips_old = menu_tooltips;
615 else if(menuTooltipOrigin_x < 0) // unallocated?
616 m_allocatetooltipbox(pos);
618 if(menuTooltipOrigin_x >= 0)
621 p = SKINBORDER_TOOLTIP;
623 p_y *= 1 / conheight;
624 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
625 p = menuTooltipOrigin;
626 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
627 p_y += SKINMARGIN_TOOLTIP_y / conheight;
628 getWrappedLine_remaining = menuTooltipText;
629 while(getWrappedLine_remaining)
631 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
632 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
644 execute_next_frame();
646 menuMouseMode = cvar("menu_mouse_absolute");
652 UpdateConWidthHeight();
656 // TODO draw an info image about this situation
660 if(!menuNotTheFirstFrame)
662 menuNotTheFirstFrame = 1;
664 if(!cvar("menu_video_played"))
666 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
667 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
669 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
670 localcmd("set menu_video_played 1\n");
674 realFrametime = frametime = min(0.2, t - menuPrevTime);
678 t = cvar("menu_slowmo");
689 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
690 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
695 if(cvar("cl_capturevideo"))
696 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
700 gamestatus = gamestatus | GAME_ISSERVER;
701 if(clientstate() == CS_CONNECTED)
702 gamestatus = gamestatus | GAME_CONNECTED;
703 if(cvar("developer"))
704 gamestatus = gamestatus | GAME_DEVELOPER;
706 prevMenuAlpha = menuAlpha;
709 if(menuAlpha == 0 && menuLogoAlpha < 2)
711 menuLogoAlpha = menuLogoAlpha + frametime * 2;
715 menuAlpha = min(1, menuAlpha + frametime * 5);
721 menuAlpha = max(0, menuAlpha - frametime * 5);
725 draw_reset_cropped();
727 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
729 if(menuLogoAlpha > 0)
732 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
733 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
734 draw_reset_cropped();
735 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
737 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
738 draw_drawMousePointer(menuMousePos);
743 else if(SKINALPHA_BACKGROUND_INGAME)
748 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
749 draw_reset_cropped();
753 if(menuAlpha != prevMenuAlpha)
754 cvar_set("_menu_alpha", ftos(menuAlpha));
756 draw_reset_cropped();
758 draw_reset_cropped();
762 if(prevMenuAlpha > 0)
763 main.initializeDialog(main, main.firstChild);
764 draw_reset_cropped();
769 draw_alpha *= menuAlpha;
774 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
775 if(newMouse != '0 0 0')
776 if(newMouse != menuMousePos)
778 menuMousePos = newMouse;
779 if(mouseButtonsPressed)
780 main.mouseDrag(main, menuMousePos);
782 main.mouseMove(main, menuMousePos);
789 vector dMouse, minpos, maxpos;
790 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
791 if(dMouse != '0 0 0')
793 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
794 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
795 dMouse = globalToBoxSize(dMouse, draw_scale);
796 menuMousePos += dMouse * cvar("menu_mouse_speed");
797 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
798 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
799 if(mouseButtonsPressed)
800 main.mouseDrag(main, menuMousePos);
802 main.mouseMove(main, menuMousePos);
808 m_tooltip(menuMousePos);
810 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
812 draw_drawMousePointer(menuMousePos);
814 draw_reset_cropped();
823 setkeydest(KEY_MENU);
824 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
829 if(mouseButtonsPressed)
830 main.mouseRelease(main, menuMousePos);
831 mouseButtonsPressed = 0;
833 main.focusEnter(main);
834 main.showNotify(main);
840 setkeydest(KEY_GAME);
841 setmousetarget(MT_CLIENT);
846 main.focusLeave(main);
847 main.hideNotify(main);
850 void m_toggle(float mode)
871 for(e = NULL; (e = nextent(e)) != NULL; )
873 if(e.classname != "vtbl")
879 void m_focus_item_chain(entity outermost, entity innermost)
881 if(innermost.parent != outermost)
882 m_focus_item_chain(outermost, innermost.parent);
883 innermost.parent.setFocus(innermost.parent, innermost);
886 void m_activate_window(entity wnd)
891 m_activate_window(par);
893 if(par.instanceOfModalController)
895 if(wnd.tabSelectingButton)
897 TabButton_Click(wnd.tabSelectingButton, wnd);
900 par.initializeDialog(par, wnd);
902 else if(par.instanceOfNexposee)
904 // nexposee (sorry for violating abstraction here)
905 par.selectedChild = wnd;
906 par.animationState = 1;
907 Container_setFocus(par, NULL);
909 else if(par.instanceOfContainer)
913 par.setFocus(par, wnd);
917 void m_setpointerfocus(entity wnd)
919 if(wnd.instanceOfContainer)
921 entity focus = wnd.preferredFocusedGrandChild(wnd);
924 menuMousePos = focus.origin + 0.5 * focus.size;
925 menuMousePos_x *= 1 / conwidth;
926 menuMousePos_y *= 1 / conheight;
927 if(wnd.focused) // why does this never happen?
928 m_focus_item_chain(wnd, focus);
933 void m_goto(string itemname)
939 strunzone(m_goto_buffer);
940 m_goto_buffer = strzone(itemname);
943 if(itemname == "") // this can be called by GameCommand
945 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
951 m_activate_window(main.mainNexposee);
957 for(e = NULL; (e = find(e, name, itemname)); )
958 if(e.classname != "vtbl")
963 m_activate_window(e);
964 m_setpointerfocus(e);