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"));
33 cvar_set("_menu_prvm_language", prvm_language);
35 check_unacceptable_compiler_bugs();
38 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK()));
41 // list all game dirs (TEST)
48 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
51 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
55 // needs to be done so early because of the constants they create
59 float ddsload = cvar("r_texture_dds_load");
60 float texcomp = cvar("gl_texturecompression");
62 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
63 localcmd("\nr_restart\n");
67 float MENU_ASPECT = 1.25; // 1280x1024
68 float MENU_MINHEIGHT = 600;
69 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
70 void draw_reset_cropped()
72 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
74 void draw_reset_full()
76 draw_reset(realconwidth, realconheight, 0, 0);
78 void UpdateConWidthHeight()
80 conwidth_s = conwidth;
81 conheight_s = conheight;
82 realconwidth = cvar("vid_conwidth");
83 realconheight = cvar("vid_conheight");
84 if(realconwidth / realconheight > MENU_ASPECT)
87 conwidth = realconheight * MENU_ASPECT;
88 conheight = realconheight;
93 conwidth = realconwidth;
94 conheight = realconwidth / MENU_ASPECT;
96 screenconwidth = conwidth;
97 screenconheight = conheight;
98 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
100 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
101 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
103 if(conheight < MENU_MINHEIGHT)
105 conwidth *= MENU_MINHEIGHT / conheight;
106 conheight = MENU_MINHEIGHT;
110 if(conwidth_s != conwidth || conheight_s != conheight)
112 draw_reset_cropped();
113 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
118 void m_init_delayed()
120 float fh, glob, n, i;
123 conwidth = conheight = -1;
124 UpdateConWidthHeight();
125 draw_reset_cropped();
133 if(cvar_string("menu_skin") != "")
135 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
136 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
139 if(cvar_defstring("menu_skin") != "")
141 cvar_set("menu_skin", cvar_defstring("menu_skin"));
142 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
143 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
147 draw_currentSkin = "gfx/menu/default";
148 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
152 error("cannot load any menu skin\n");
154 draw_currentSkin = strzone(draw_currentSkin);
155 while((s = fgets(fh)))
157 // these two are handled by skinlist.qc
158 if(substring(s, 0, 6) == "title ")
160 if(substring(s, 0, 7) == "author ")
162 n = tokenize_console(s);
164 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
168 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
171 n = search_getsize(glob);
172 for(i = 0; i < n; ++i)
173 precache_pic(search_getfilename(glob, i));
177 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
180 anim = spawnAnimHost();
181 main = spawnMainWindow(); main.configureMainWindow(main);
184 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
187 menuMousePos = '0.5 0.5 0';
192 m_display(); // delayed menu display
195 void m_keyup (float key, float ascii)
201 draw_reset_cropped();
202 main.keyUp(main, key, ascii, menuShiftState);
203 if(key >= K_MOUSE1 && key <= K_MOUSE3)
205 --mouseButtonsPressed;
206 if(!mouseButtonsPressed)
207 main.mouseRelease(main, menuMousePos);
208 if(mouseButtonsPressed < 0)
210 mouseButtonsPressed = 0;
211 dprint("Warning: released an already released button\n");
214 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
215 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
216 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
219 void m_keydown(float key, float ascii)
230 e.keyGrabbed(e, key, ascii);
234 draw_reset_cropped();
235 if(key >= K_MOUSE1 && key <= K_MOUSE3)
236 if(!mouseButtonsPressed)
237 main.mousePress(main, menuMousePos);
238 if(!main.keyDown(main, key, ascii, menuShiftState))
240 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
241 m_hide(); // disable menu on unhandled ESC
243 if(key >= K_MOUSE1 && key <= K_MOUSE3)
245 ++mouseButtonsPressed;
246 if(mouseButtonsPressed > 10)
248 mouseButtonsPressed = 10;
249 dprint("Warning: pressed an already pressed button\n");
252 if(key == K_ALT) menuShiftState |= S_ALT;
253 if(key == K_CTRL) menuShiftState |= S_CTRL;
254 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
257 float SCALEMODE_CROP = 0;
258 float SCALEMODE_LETTERBOX = 1;
259 float SCALEMODE_WIDTH = 2;
260 float SCALEMODE_HEIGHT = 3;
261 float SCALEMODE_STRETCH = 4;
262 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
264 vector sz, org, isz, isz_w, isz_h;
265 float width_is_larger;
267 sz = draw_PictureSize(img);
268 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
269 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
270 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
276 isz = (width_is_larger ? isz_h : isz_w);
278 case SCALEMODE_LETTERBOX:
279 isz = (width_is_larger ? isz_w : isz_h);
281 case SCALEMODE_WIDTH:
284 case SCALEMODE_HEIGHT:
287 case SCALEMODE_STRETCH:
292 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
293 draw_Picture(org, img, isz, '1 1 1', a);
296 void drawBackground(string img, float a, string algn, float force1)
298 if(main.mainNexposee.ModalController_state == 0)
308 scalemode = SCALEMODE_CROP;
310 for(i = 0; i < strlen(algn); ++i)
312 c = substring(algn, i, 1);
315 case "c": scalemode = SCALEMODE_CROP; goto nopic;
316 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
317 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
318 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
319 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
320 case "1": case "4": case "7": v_x = 0.0; break;
321 case "2": case "5": case "8": v_x = 0.5; break;
322 case "3": case "6": case "9": v_x = 1.0; break;
323 default: v_x = random(); break;
327 case "7": case "8": case "9": v_y = 0.0; break;
328 case "4": case "5": case "6": v_y = 0.5; break;
329 case "1": case "2": case "3": v_y = 1.0; break;
330 default: v_y = random(); break;
333 draw_Picture_Aligned(v, scalemode, img, a);
335 // force all secondary layers to use alpha 1. Prevents ugly issues
336 // with overlap. It's a flag because it cannot be used for the
338 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
340 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
347 float menu_tooltips_old;
348 vector menuTooltipAveragedMousePos;
349 entity menuTooltipItem;
350 vector menuTooltipOrigin;
351 vector menuTooltipSize;
352 float menuTooltipAlpha;
353 string menuTooltipText;
354 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
355 float m_testmousetooltipbox(vector pos)
357 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
358 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
362 float m_testtooltipbox(vector tooltippos)
368 if(tooltippos_x + menuTooltipSize_x > 1)
370 if(tooltippos_y + menuTooltipSize_y > 1)
372 menuTooltipOrigin = tooltippos;
375 float m_allocatetooltipbox(vector pos)
377 vector avoidplus, avoidminus;
380 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
381 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
384 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
385 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
390 if(m_testtooltipbox(v))
394 v_x = pos_x - menuTooltipSize_x * 0.5;
395 if(m_testtooltipbox(v))
399 v_x = pos_x - avoidminus_x;
400 if(m_testtooltipbox(v))
404 v_y = pos_y - avoidminus_y;
405 if(m_testtooltipbox(v))
409 v_x = pos_x - menuTooltipSize_x * 0.5;
410 if(m_testtooltipbox(v))
414 v_x = pos_x + avoidplus_x;
415 if(m_testtooltipbox(v))
420 entity m_findtooltipitem(entity root, vector pos)
428 while(it.instanceOfContainer)
430 while(it.instanceOfNexposee && it.focusedChild)
432 it = it.focusedChild;
433 pos = globalToBox(pos, it.Container_origin, it.Container_size);
435 if(it.instanceOfNexposee)
437 it = it.itemFromPoint(it, pos);
440 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
444 else if(it.instanceOfModalController)
445 it = it.focusedChild;
447 it = it.itemFromPoint(it, pos);
452 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
454 pos = globalToBox(pos, it.Container_origin, it.Container_size);
461 if (menu_tooltips == 2)
464 if (menuTooltipItem.cvarName)
466 if (getCvarsMulti(menuTooltipItem))
467 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
469 s = strcat("[", menuTooltipItem.cvarName, "]");
471 else if (menuTooltipItem.onClickCommand)
472 s = strcat("<", menuTooltipItem.onClickCommand, ">");
474 return menuTooltipItem.tooltip;
475 if (menuTooltipItem.tooltip)
476 return strcat(menuTooltipItem.tooltip, " ", s);
479 return menuTooltipItem.tooltip;
481 void m_tooltip(vector pos)
488 menu_tooltips = cvar("menu_tooltips");
491 // don't return immediately, fade out the active tooltip first
492 if (menuTooltipItem == world)
495 menu_tooltips_old = menu_tooltips;
499 f = bound(0, frametime * 2, 1);
500 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
501 f = vlen(pos - menuTooltipAveragedMousePos);
503 it = m_findtooltipitem(main, pos);
507 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
509 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
510 if(it != menuTooltipItem)
512 switch(menuTooltipState)
517 // another item: fade out first
518 menuTooltipState = 2;
523 menuTooltipState = 1;
524 menuTooltipItem = it;
526 menuTooltipOrigin_x = -1; // unallocated
529 strunzone(menuTooltipText);
530 menuTooltipText = strzone(gettooltip());
534 getWrappedLine_remaining = menuTooltipText;
535 while(getWrappedLine_remaining)
537 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
539 f = draw_TextWidth(s, FALSE, fontsize);
543 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
544 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
545 menuTooltipSize_z = 0;
549 // changing item while fading in: fade out first
550 menuTooltipState = 2;
553 // changing item while fading out: can't
557 else if(menuTooltipState == 2) // re-fade in?
558 menuTooltipState = 1;
561 if(!m_testmousetooltipbox(pos))
562 menuTooltipState = 2; // fade out if mouse touches it
564 switch(menuTooltipState)
567 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
568 if(menuTooltipAlpha == 1)
569 menuTooltipState = 0;
572 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
573 if(menuTooltipAlpha == 0)
575 menuTooltipState = 0;
576 menuTooltipItem = world;
581 if(menuTooltipItem == world)
585 strunzone(menuTooltipText);
586 menuTooltipText = string_null;
592 if(menu_tooltips != menu_tooltips_old)
594 if (menu_tooltips != 0 && menu_tooltips_old != 0)
595 menuTooltipItem = world; // reload tooltip next frame
596 menu_tooltips_old = menu_tooltips;
598 else if(menuTooltipOrigin_x < 0) // unallocated?
599 m_allocatetooltipbox(pos);
601 if(menuTooltipOrigin_x >= 0)
604 p = SKINBORDER_TOOLTIP;
606 p_y *= 1 / conheight;
607 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
608 p = menuTooltipOrigin;
609 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
610 p_y += SKINMARGIN_TOOLTIP_y / conheight;
611 getWrappedLine_remaining = menuTooltipText;
612 while(getWrappedLine_remaining)
614 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
615 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
627 execute_next_frame();
629 menuMouseMode = cvar("menu_mouse_absolute");
635 UpdateConWidthHeight();
639 // TODO draw an info image about this situation
643 if(!menuNotTheFirstFrame)
645 menuNotTheFirstFrame = 1;
647 if(!cvar("menu_video_played"))
649 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
650 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
652 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
653 localcmd("set menu_video_played 1\n");
657 realFrametime = frametime = min(0.2, t - menuPrevTime);
661 t = cvar("menu_slowmo");
672 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
673 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
678 if(cvar("cl_capturevideo"))
679 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
683 gamestatus = gamestatus | GAME_ISSERVER;
684 if(clientstate() == CS_CONNECTED)
685 gamestatus = gamestatus | GAME_CONNECTED;
686 if(cvar("developer"))
687 gamestatus = gamestatus | GAME_DEVELOPER;
689 prevMenuAlpha = menuAlpha;
692 if(menuAlpha == 0 && menuLogoAlpha < 2)
694 menuLogoAlpha = menuLogoAlpha + frametime * 2;
698 menuAlpha = min(1, menuAlpha + frametime * 5);
704 menuAlpha = max(0, menuAlpha - frametime * 5);
708 draw_reset_cropped();
710 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
712 if(menuLogoAlpha > 0)
715 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
716 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
717 draw_reset_cropped();
718 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
720 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
721 draw_drawMousePointer(menuMousePos);
726 else if(SKINALPHA_BACKGROUND_INGAME)
731 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
732 draw_reset_cropped();
736 if(menuAlpha != prevMenuAlpha)
737 cvar_set("_menu_alpha", ftos(menuAlpha));
739 draw_reset_cropped();
741 draw_reset_cropped();
745 if(prevMenuAlpha > 0)
746 main.initializeDialog(main, main.firstChild);
747 draw_reset_cropped();
752 draw_alpha *= menuAlpha;
757 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
758 if(newMouse != '0 0 0')
759 if(newMouse != menuMousePos)
761 menuMousePos = newMouse;
762 if(mouseButtonsPressed)
763 main.mouseDrag(main, menuMousePos);
765 main.mouseMove(main, menuMousePos);
772 vector dMouse, minpos, maxpos;
773 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
774 if(dMouse != '0 0 0')
776 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
777 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
778 dMouse = globalToBoxSize(dMouse, draw_scale);
779 menuMousePos += dMouse * cvar("menu_mouse_speed");
780 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
781 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
782 if(mouseButtonsPressed)
783 main.mouseDrag(main, menuMousePos);
785 main.mouseMove(main, menuMousePos);
791 m_tooltip(menuMousePos);
793 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
795 draw_drawMousePointer(menuMousePos);
797 draw_reset_cropped();
806 setkeydest(KEY_MENU);
807 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
812 if(mouseButtonsPressed)
813 main.mouseRelease(main, menuMousePos);
814 mouseButtonsPressed = 0;
816 main.focusEnter(main);
817 main.showNotify(main);
823 setkeydest(KEY_GAME);
824 setmousetarget(MT_CLIENT);
829 main.focusLeave(main);
830 main.hideNotify(main);
833 void m_toggle(float mode)
854 for(e = NULL; (e = nextent(e)) != NULL; )
856 if(e.classname != "vtbl")
862 void m_focus_item_chain(entity outermost, entity innermost)
864 if(innermost.parent != outermost)
865 m_focus_item_chain(outermost, innermost.parent);
866 innermost.parent.setFocus(innermost.parent, innermost);
869 void m_activate_window(entity wnd)
874 m_activate_window(par);
876 if(par.instanceOfModalController)
878 if(wnd.tabSelectingButton)
880 TabButton_Click(wnd.tabSelectingButton, wnd);
883 par.initializeDialog(par, wnd);
885 else if(par.instanceOfNexposee)
887 // nexposee (sorry for violating abstraction here)
888 par.selectedChild = wnd;
889 par.animationState = 1;
890 Container_setFocus(par, NULL);
892 else if(par.instanceOfContainer)
896 par.setFocus(par, wnd);
900 void m_setpointerfocus(entity wnd)
902 if(wnd.instanceOfContainer)
904 entity focus = wnd.preferredFocusedGrandChild(wnd);
907 menuMousePos = focus.origin + 0.5 * focus.size;
908 menuMousePos_x *= 1 / conwidth;
909 menuMousePos_y *= 1 / conheight;
910 if(wnd.focused) // why does this never happen?
911 m_focus_item_chain(wnd, focus);
916 void m_goto(string itemname)
921 if(itemname == "") // this can be called by GameCommand
923 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
927 m_activate_window(main.mainNexposee);
933 for(e = NULL; (e = find(e, name, itemname)); )
934 if(e.classname != "vtbl")
939 m_activate_window(e);
940 m_setpointerfocus(e);
946 void m_goto_skin_selector()
950 // TODO add code to switch back to the skin selector (no idea how to do it now)
951 m_goto("skinselector");
954 void m_goto_language_selector()
958 // TODO add code to switch back to the language selector (no idea how to do it now)
959 m_goto("languageselector");
962 void m_goto_video_settings()
966 // TODO add code to switch back to the video settings (no idea how to do it now)
967 m_goto("videosettings");