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);
38 prvm_language = strzone(prvm_language);
39 cvar_set("_menu_prvm_language", prvm_language);
41 check_unacceptable_compiler_bugs();
44 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK()));
47 // list all game dirs (TEST)
54 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
57 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
61 // needs to be done so early because of the constants they create
65 float ddsload = cvar("r_texture_dds_load");
66 float texcomp = cvar("gl_texturecompression");
68 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
69 localcmd("\nr_restart\n");
73 float MENU_ASPECT = 1.25; // 1280x1024
74 float MENU_MINHEIGHT = 600;
75 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
76 void draw_reset_cropped()
78 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
80 void draw_reset_full()
82 draw_reset(realconwidth, realconheight, 0, 0);
84 void UpdateConWidthHeight()
86 conwidth_s = conwidth;
87 conheight_s = conheight;
88 realconwidth = cvar("vid_conwidth");
89 realconheight = cvar("vid_conheight");
90 if(realconwidth / realconheight > MENU_ASPECT)
93 conwidth = realconheight * MENU_ASPECT;
94 conheight = realconheight;
99 conwidth = realconwidth;
100 conheight = realconwidth / MENU_ASPECT;
102 screenconwidth = conwidth;
103 screenconheight = conheight;
104 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
106 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
107 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
109 if(conheight < MENU_MINHEIGHT)
111 conwidth *= MENU_MINHEIGHT / conheight;
112 conheight = MENU_MINHEIGHT;
116 if(conwidth_s != conwidth || conheight_s != conheight)
118 draw_reset_cropped();
119 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
124 void m_init_delayed()
126 float fh, glob, n, i;
129 conwidth = conheight = -1;
130 UpdateConWidthHeight();
131 draw_reset_cropped();
139 if(cvar_string("menu_skin") != "")
141 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
142 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
145 if(cvar_defstring("menu_skin") != "")
147 cvar_set("menu_skin", cvar_defstring("menu_skin"));
148 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
149 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
153 draw_currentSkin = "gfx/menu/default";
154 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
158 error("cannot load any menu skin\n");
160 draw_currentSkin = strzone(draw_currentSkin);
161 while((s = fgets(fh)))
163 // these two are handled by skinlist.qc
164 if(substring(s, 0, 6) == "title ")
166 if(substring(s, 0, 7) == "author ")
168 n = tokenize_console(s);
170 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
174 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
177 n = search_getsize(glob);
178 for(i = 0; i < n; ++i)
179 precache_pic(search_getfilename(glob, i));
183 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
186 anim = spawnAnimHost();
187 main = spawnMainWindow(); main.configureMainWindow(main);
190 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
193 menuMousePos = '0.5 0.5 0';
198 m_display(); // delayed menu display
201 void m_keyup (float key, float ascii)
207 draw_reset_cropped();
208 main.keyUp(main, key, ascii, menuShiftState);
209 if(key >= K_MOUSE1 && key <= K_MOUSE3)
211 --mouseButtonsPressed;
212 if(!mouseButtonsPressed)
213 main.mouseRelease(main, menuMousePos);
214 if(mouseButtonsPressed < 0)
216 mouseButtonsPressed = 0;
217 dprint("Warning: released an already released button\n");
220 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
221 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
222 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
225 void m_keydown(float key, float ascii)
236 e.keyGrabbed(e, key, ascii);
240 draw_reset_cropped();
241 if(key >= K_MOUSE1 && key <= K_MOUSE3)
242 if(!mouseButtonsPressed)
243 main.mousePress(main, menuMousePos);
244 if(!main.keyDown(main, key, ascii, menuShiftState))
246 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
247 m_hide(); // disable menu on unhandled ESC
249 if(key >= K_MOUSE1 && key <= K_MOUSE3)
251 ++mouseButtonsPressed;
252 if(mouseButtonsPressed > 10)
254 mouseButtonsPressed = 10;
255 dprint("Warning: pressed an already pressed button\n");
258 if(key == K_ALT) menuShiftState |= S_ALT;
259 if(key == K_CTRL) menuShiftState |= S_CTRL;
260 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
263 float SCALEMODE_CROP = 0;
264 float SCALEMODE_LETTERBOX = 1;
265 float SCALEMODE_WIDTH = 2;
266 float SCALEMODE_HEIGHT = 3;
267 float SCALEMODE_STRETCH = 4;
268 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
270 vector sz, org, isz, isz_w, isz_h;
271 float width_is_larger;
273 sz = draw_PictureSize(img);
274 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
275 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
276 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
282 isz = (width_is_larger ? isz_h : isz_w);
284 case SCALEMODE_LETTERBOX:
285 isz = (width_is_larger ? isz_w : isz_h);
287 case SCALEMODE_WIDTH:
290 case SCALEMODE_HEIGHT:
293 case SCALEMODE_STRETCH:
298 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
299 draw_Picture(org, img, isz, '1 1 1', a);
302 void drawBackground(string img, float a, string algn, float force1)
304 if(main.mainNexposee.ModalController_state == 0)
314 scalemode = SCALEMODE_CROP;
316 for(i = 0; i < strlen(algn); ++i)
318 c = substring(algn, i, 1);
321 case "c": scalemode = SCALEMODE_CROP; goto nopic;
322 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
323 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
324 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
325 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
326 case "1": case "4": case "7": v_x = 0.0; break;
327 case "2": case "5": case "8": v_x = 0.5; break;
328 case "3": case "6": case "9": v_x = 1.0; break;
329 default: v_x = random(); break;
333 case "7": case "8": case "9": v_y = 0.0; break;
334 case "4": case "5": case "6": v_y = 0.5; break;
335 case "1": case "2": case "3": v_y = 1.0; break;
336 default: v_y = random(); break;
339 draw_Picture_Aligned(v, scalemode, img, a);
341 // force all secondary layers to use alpha 1. Prevents ugly issues
342 // with overlap. It's a flag because it cannot be used for the
344 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
346 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
353 float menu_tooltips_old;
354 vector menuTooltipAveragedMousePos;
355 entity menuTooltipItem;
356 vector menuTooltipOrigin;
357 vector menuTooltipSize;
358 float menuTooltipAlpha;
359 string menuTooltipText;
360 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
361 float m_testmousetooltipbox(vector pos)
363 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
364 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
368 float m_testtooltipbox(vector tooltippos)
374 if(tooltippos_x + menuTooltipSize_x > 1)
376 if(tooltippos_y + menuTooltipSize_y > 1)
378 menuTooltipOrigin = tooltippos;
381 float m_allocatetooltipbox(vector pos)
383 vector avoidplus, avoidminus;
386 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
387 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
390 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
391 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
396 if(m_testtooltipbox(v))
400 v_x = pos_x - menuTooltipSize_x * 0.5;
401 if(m_testtooltipbox(v))
405 v_x = pos_x - avoidminus_x;
406 if(m_testtooltipbox(v))
410 v_y = pos_y - avoidminus_y;
411 if(m_testtooltipbox(v))
415 v_x = pos_x - menuTooltipSize_x * 0.5;
416 if(m_testtooltipbox(v))
420 v_x = pos_x + avoidplus_x;
421 if(m_testtooltipbox(v))
426 entity m_findtooltipitem(entity root, vector pos)
434 while(it.instanceOfContainer)
436 while(it.instanceOfNexposee && it.focusedChild)
438 it = it.focusedChild;
439 pos = globalToBox(pos, it.Container_origin, it.Container_size);
441 if(it.instanceOfNexposee)
443 it = it.itemFromPoint(it, pos);
446 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
450 else if(it.instanceOfModalController)
451 it = it.focusedChild;
453 it = it.itemFromPoint(it, pos);
458 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
460 pos = globalToBox(pos, it.Container_origin, it.Container_size);
467 if (menu_tooltips == 2)
470 if (menuTooltipItem.cvarName)
472 if (getCvarsMulti(menuTooltipItem))
473 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
475 s = strcat("[", menuTooltipItem.cvarName, "]");
477 else if (menuTooltipItem.onClickCommand)
478 s = strcat("<", menuTooltipItem.onClickCommand, ">");
480 return menuTooltipItem.tooltip;
481 if (menuTooltipItem.tooltip)
482 return strcat(menuTooltipItem.tooltip, " ", s);
485 return menuTooltipItem.tooltip;
487 void m_tooltip(vector pos)
494 menu_tooltips = cvar("menu_tooltips");
497 // don't return immediately, fade out the active tooltip first
498 if (menuTooltipItem == world)
501 menu_tooltips_old = menu_tooltips;
505 f = bound(0, frametime * 2, 1);
506 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
507 f = vlen(pos - menuTooltipAveragedMousePos);
509 it = m_findtooltipitem(main, pos);
513 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
515 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
516 if(it != menuTooltipItem)
518 switch(menuTooltipState)
523 // another item: fade out first
524 menuTooltipState = 2;
529 menuTooltipState = 1;
530 menuTooltipItem = it;
532 menuTooltipOrigin_x = -1; // unallocated
535 strunzone(menuTooltipText);
536 menuTooltipText = strzone(gettooltip());
540 getWrappedLine_remaining = menuTooltipText;
541 while(getWrappedLine_remaining)
543 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
545 f = draw_TextWidth(s, FALSE, fontsize);
549 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
550 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
551 menuTooltipSize_z = 0;
555 // changing item while fading in: fade out first
556 menuTooltipState = 2;
559 // changing item while fading out: can't
563 else if(menuTooltipState == 2) // re-fade in?
564 menuTooltipState = 1;
567 if(!m_testmousetooltipbox(pos))
568 menuTooltipState = 2; // fade out if mouse touches it
570 switch(menuTooltipState)
573 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
574 if(menuTooltipAlpha == 1)
575 menuTooltipState = 0;
578 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
579 if(menuTooltipAlpha == 0)
581 menuTooltipState = 0;
582 menuTooltipItem = world;
587 if(menuTooltipItem == world)
591 strunzone(menuTooltipText);
592 menuTooltipText = string_null;
598 if(menu_tooltips != menu_tooltips_old)
600 if (menu_tooltips != 0 && menu_tooltips_old != 0)
601 menuTooltipItem = world; // reload tooltip next frame
602 menu_tooltips_old = menu_tooltips;
604 else if(menuTooltipOrigin_x < 0) // unallocated?
605 m_allocatetooltipbox(pos);
607 if(menuTooltipOrigin_x >= 0)
610 p = SKINBORDER_TOOLTIP;
612 p_y *= 1 / conheight;
613 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
614 p = menuTooltipOrigin;
615 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
616 p_y += SKINMARGIN_TOOLTIP_y / conheight;
617 getWrappedLine_remaining = menuTooltipText;
618 while(getWrappedLine_remaining)
620 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
621 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
633 execute_next_frame();
635 menuMouseMode = cvar("menu_mouse_absolute");
641 UpdateConWidthHeight();
645 // TODO draw an info image about this situation
649 if(!menuNotTheFirstFrame)
651 menuNotTheFirstFrame = 1;
653 if(!cvar("menu_video_played"))
655 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
656 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
658 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
659 localcmd("set menu_video_played 1\n");
663 realFrametime = frametime = min(0.2, t - menuPrevTime);
667 t = cvar("menu_slowmo");
678 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
679 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
684 if(cvar("cl_capturevideo"))
685 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
689 gamestatus = gamestatus | GAME_ISSERVER;
690 if(clientstate() == CS_CONNECTED)
691 gamestatus = gamestatus | GAME_CONNECTED;
692 if(cvar("developer"))
693 gamestatus = gamestatus | GAME_DEVELOPER;
695 prevMenuAlpha = menuAlpha;
698 if(menuAlpha == 0 && menuLogoAlpha < 2)
700 menuLogoAlpha = menuLogoAlpha + frametime * 2;
704 menuAlpha = min(1, menuAlpha + frametime * 5);
710 menuAlpha = max(0, menuAlpha - frametime * 5);
714 draw_reset_cropped();
716 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
718 if(menuLogoAlpha > 0)
721 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
722 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
723 draw_reset_cropped();
724 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
726 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
727 draw_drawMousePointer(menuMousePos);
732 else if(SKINALPHA_BACKGROUND_INGAME)
737 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
738 draw_reset_cropped();
742 if(menuAlpha != prevMenuAlpha)
743 cvar_set("_menu_alpha", ftos(menuAlpha));
745 draw_reset_cropped();
747 draw_reset_cropped();
751 if(prevMenuAlpha > 0)
752 main.initializeDialog(main, main.firstChild);
753 draw_reset_cropped();
758 draw_alpha *= menuAlpha;
763 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
764 if(newMouse != '0 0 0')
765 if(newMouse != menuMousePos)
767 menuMousePos = newMouse;
768 if(mouseButtonsPressed)
769 main.mouseDrag(main, menuMousePos);
771 main.mouseMove(main, menuMousePos);
778 vector dMouse, minpos, maxpos;
779 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
780 if(dMouse != '0 0 0')
782 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
783 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
784 dMouse = globalToBoxSize(dMouse, draw_scale);
785 menuMousePos += dMouse * cvar("menu_mouse_speed");
786 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
787 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
788 if(mouseButtonsPressed)
789 main.mouseDrag(main, menuMousePos);
791 main.mouseMove(main, menuMousePos);
797 m_tooltip(menuMousePos);
799 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
801 draw_drawMousePointer(menuMousePos);
803 draw_reset_cropped();
812 setkeydest(KEY_MENU);
813 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
818 if(mouseButtonsPressed)
819 main.mouseRelease(main, menuMousePos);
820 mouseButtonsPressed = 0;
822 main.focusEnter(main);
823 main.showNotify(main);
829 setkeydest(KEY_GAME);
830 setmousetarget(MT_CLIENT);
835 main.focusLeave(main);
836 main.hideNotify(main);
839 void m_toggle(float mode)
860 for(e = NULL; (e = nextent(e)) != NULL; )
862 if(e.classname != "vtbl")
868 void m_focus_item_chain(entity outermost, entity innermost)
870 if(innermost.parent != outermost)
871 m_focus_item_chain(outermost, innermost.parent);
872 innermost.parent.setFocus(innermost.parent, innermost);
875 void m_activate_window(entity wnd)
880 m_activate_window(par);
882 if(par.instanceOfModalController)
884 if(wnd.tabSelectingButton)
886 TabButton_Click(wnd.tabSelectingButton, wnd);
889 par.initializeDialog(par, wnd);
891 else if(par.instanceOfNexposee)
893 // nexposee (sorry for violating abstraction here)
894 par.selectedChild = wnd;
895 par.animationState = 1;
896 Container_setFocus(par, NULL);
898 else if(par.instanceOfContainer)
902 par.setFocus(par, wnd);
906 void m_setpointerfocus(entity wnd)
908 if(wnd.instanceOfContainer)
910 entity focus = wnd.preferredFocusedGrandChild(wnd);
913 menuMousePos = focus.origin + 0.5 * focus.size;
914 menuMousePos_x *= 1 / conwidth;
915 menuMousePos_y *= 1 / conheight;
916 if(wnd.focused) // why does this never happen?
917 m_focus_item_chain(wnd, focus);
922 void m_goto(string itemname)
927 if(itemname == "") // this can be called by GameCommand
929 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
933 m_activate_window(main.mainNexposee);
939 for(e = NULL; (e = find(e, name, itemname)); )
940 if(e.classname != "vtbl")
945 m_activate_window(e);
946 m_setpointerfocus(e);
952 void m_goto_skin_selector()
956 // TODO add code to switch back to the skin selector (no idea how to do it now)
957 m_goto("skinselector");
960 void m_goto_language_selector()
964 // TODO add code to switch back to the language selector (no idea how to do it now)
965 m_goto("languageselector");
968 void m_goto_video_settings()
972 // TODO add code to switch back to the video settings (no idea how to do it now)
973 m_goto("videosettings");