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;
33 gamestatus = gamestatus | GAME_ISSERVER;
34 if(clientstate() == CS_CONNECTED || isdemo())
35 gamestatus = gamestatus | GAME_CONNECTED;
37 gamestatus = gamestatus | GAME_DEVELOPER;
42 cvar_set("_menu_alpha", "0");
43 prvm_language = cvar_string("prvm_language");
44 if(prvm_language == "")
47 cvar_set("prvm_language", prvm_language);
48 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
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");
84 if(cvar("_menu_initialized")) // always show menu after menu_restart
88 cvar_set("_menu_initialized", "1");
91 float MENU_ASPECT = 1.25; // 1280x1024
92 float MENU_MINHEIGHT = 600;
93 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
94 void draw_reset_cropped()
96 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
98 void draw_reset_full()
100 draw_reset(realconwidth, realconheight, 0, 0);
102 void UpdateConWidthHeight()
104 conwidth_s = conwidth;
105 conheight_s = conheight;
106 realconwidth = cvar("vid_conwidth");
107 realconheight = cvar("vid_conheight");
108 if(realconwidth / realconheight > MENU_ASPECT)
111 conwidth = realconheight * MENU_ASPECT;
112 conheight = realconheight;
117 conwidth = realconwidth;
118 conheight = realconwidth / MENU_ASPECT;
120 screenconwidth = conwidth;
121 screenconheight = conheight;
122 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
124 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
125 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
127 if(conheight < MENU_MINHEIGHT)
129 conwidth *= MENU_MINHEIGHT / conheight;
130 conheight = MENU_MINHEIGHT;
134 if(conwidth_s != conwidth || conheight_s != conheight)
136 draw_reset_cropped();
137 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
142 string m_goto_buffer;
143 void m_init_delayed()
145 float fh, glob, n, i;
148 conwidth = conheight = -1;
149 UpdateConWidthHeight();
150 draw_reset_cropped();
158 if(cvar_string("menu_skin") != "")
160 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
161 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
164 if(cvar_defstring("menu_skin") != "")
166 cvar_set("menu_skin", cvar_defstring("menu_skin"));
167 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
168 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
172 draw_currentSkin = "gfx/menu/default";
173 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
177 error("cannot load any menu skin\n");
179 draw_currentSkin = strzone(draw_currentSkin);
180 while((s = fgets(fh)))
182 // these two are handled by skinlist.qc
183 if(substring(s, 0, 6) == "title ")
185 if(substring(s, 0, 7) == "author ")
187 n = tokenize_console(s);
189 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
193 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
196 n = search_getsize(glob);
197 for(i = 0; i < n; ++i)
198 precache_pic(search_getfilename(glob, i));
202 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
205 anim = spawnAnimHost();
206 main = spawnMainWindow(); main.configureMainWindow(main);
209 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
212 menuMousePos = '0.5 0.5 0';
218 m_goto(m_goto_buffer);
219 strunzone(m_goto_buffer);
220 m_goto_buffer = string_null;
224 m_display(); // delayed menu display
227 void m_keyup (float key, float ascii)
233 draw_reset_cropped();
234 main.keyUp(main, key, ascii, menuShiftState);
235 if(key >= K_MOUSE1 && key <= K_MOUSE3)
237 --mouseButtonsPressed;
238 if(!mouseButtonsPressed)
239 main.mouseRelease(main, menuMousePos);
240 if(mouseButtonsPressed < 0)
242 mouseButtonsPressed = 0;
243 dprint("Warning: released an already released button\n");
246 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
247 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
248 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
251 void m_keydown(float key, float ascii)
262 e.keyGrabbed(e, key, ascii);
266 draw_reset_cropped();
267 if(key >= K_MOUSE1 && key <= K_MOUSE3)
268 if(!mouseButtonsPressed)
269 main.mousePress(main, menuMousePos);
270 if(!main.keyDown(main, key, ascii, menuShiftState))
272 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
273 m_hide(); // disable menu on unhandled ESC
275 if(key >= K_MOUSE1 && key <= K_MOUSE3)
277 ++mouseButtonsPressed;
278 if(mouseButtonsPressed > 10)
280 mouseButtonsPressed = 10;
281 dprint("Warning: pressed an already pressed button\n");
284 if(key == K_ALT) menuShiftState |= S_ALT;
285 if(key == K_CTRL) menuShiftState |= S_CTRL;
286 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
289 float SCALEMODE_CROP = 0;
290 float SCALEMODE_LETTERBOX = 1;
291 float SCALEMODE_WIDTH = 2;
292 float SCALEMODE_HEIGHT = 3;
293 float SCALEMODE_STRETCH = 4;
294 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
296 vector sz, org, isz, isz_w, isz_h;
297 float width_is_larger;
299 sz = draw_PictureSize(img);
300 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
301 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
302 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
308 isz = (width_is_larger ? isz_h : isz_w);
310 case SCALEMODE_LETTERBOX:
311 isz = (width_is_larger ? isz_w : isz_h);
313 case SCALEMODE_WIDTH:
316 case SCALEMODE_HEIGHT:
319 case SCALEMODE_STRETCH:
324 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
325 draw_Picture(org, img, isz, '1 1 1', a);
328 void drawBackground(string img, float a, string algn, float force1)
330 if(main.mainNexposee.ModalController_state == 0)
340 scalemode = SCALEMODE_CROP;
342 for(i = 0; i < strlen(algn); ++i)
344 c = substring(algn, i, 1);
347 case "c": scalemode = SCALEMODE_CROP; goto nopic;
348 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
349 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
350 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
351 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
352 case "1": case "4": case "7": v_x = 0.0; break;
353 case "2": case "5": case "8": v_x = 0.5; break;
354 case "3": case "6": case "9": v_x = 1.0; break;
355 default: v_x = random(); break;
359 case "7": case "8": case "9": v_y = 0.0; break;
360 case "4": case "5": case "6": v_y = 0.5; break;
361 case "1": case "2": case "3": v_y = 1.0; break;
362 default: v_y = random(); break;
365 draw_Picture_Aligned(v, scalemode, img, a);
367 // force all secondary layers to use alpha 1. Prevents ugly issues
368 // with overlap. It's a flag because it cannot be used for the
370 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
372 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
379 float menu_tooltips_old;
380 vector menuTooltipAveragedMousePos;
381 entity menuTooltipItem;
382 vector menuTooltipOrigin;
383 vector menuTooltipSize;
384 float menuTooltipAlpha;
385 string menuTooltipText;
386 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
387 float m_testmousetooltipbox(vector pos)
389 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
390 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
394 float m_testtooltipbox(vector tooltippos)
400 if(tooltippos_x + menuTooltipSize_x > 1)
402 if(tooltippos_y + menuTooltipSize_y > 1)
404 menuTooltipOrigin = tooltippos;
407 float m_allocatetooltipbox(vector pos)
409 vector avoidplus, avoidminus;
412 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
413 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
416 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
417 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_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 - avoidminus_x;
432 if(m_testtooltipbox(v))
436 v_y = pos_y - avoidminus_y;
437 if(m_testtooltipbox(v))
441 v_x = pos_x - menuTooltipSize_x * 0.5;
442 if(m_testtooltipbox(v))
446 v_x = pos_x + avoidplus_x;
447 if(m_testtooltipbox(v))
452 entity m_findtooltipitem(entity root, vector pos)
460 while(it.instanceOfContainer)
462 while(it.instanceOfNexposee && it.focusedChild)
464 it = it.focusedChild;
465 pos = globalToBox(pos, it.Container_origin, it.Container_size);
467 if(it.instanceOfNexposee)
469 it = it.itemFromPoint(it, pos);
472 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
476 else if(it.instanceOfModalController)
477 it = it.focusedChild;
479 it = it.itemFromPoint(it, pos);
484 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
486 pos = globalToBox(pos, it.Container_origin, it.Container_size);
493 if (menu_tooltips == 2)
496 if (menuTooltipItem.cvarName)
498 if (getCvarsMulti(menuTooltipItem))
499 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
501 s = strcat("[", menuTooltipItem.cvarName, "]");
503 else if (menuTooltipItem.onClickCommand)
504 s = strcat("<", menuTooltipItem.onClickCommand, ">");
506 return menuTooltipItem.tooltip;
507 if (menuTooltipItem.tooltip)
508 return strcat(menuTooltipItem.tooltip, " ", s);
511 return menuTooltipItem.tooltip;
513 void m_tooltip(vector pos)
520 menu_tooltips = cvar("menu_tooltips");
523 // don't return immediately, fade out the active tooltip first
524 if (menuTooltipItem == world)
527 menu_tooltips_old = menu_tooltips;
531 f = bound(0, frametime * 2, 1);
532 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
533 f = vlen(pos - menuTooltipAveragedMousePos);
535 it = m_findtooltipitem(main, pos);
539 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
541 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
542 if(it != menuTooltipItem)
544 switch(menuTooltipState)
549 // another item: fade out first
550 menuTooltipState = 2;
555 menuTooltipState = 1;
556 menuTooltipItem = it;
558 menuTooltipOrigin_x = -1; // unallocated
561 strunzone(menuTooltipText);
562 menuTooltipText = strzone(gettooltip());
566 getWrappedLine_remaining = menuTooltipText;
567 while(getWrappedLine_remaining)
569 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
571 f = draw_TextWidth(s, FALSE, fontsize);
575 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
576 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
577 menuTooltipSize_z = 0;
581 // changing item while fading in: fade out first
582 menuTooltipState = 2;
585 // changing item while fading out: can't
589 else if(menuTooltipState == 2) // re-fade in?
590 menuTooltipState = 1;
593 if(!m_testmousetooltipbox(pos))
594 menuTooltipState = 2; // fade out if mouse touches it
596 switch(menuTooltipState)
599 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
600 if(menuTooltipAlpha == 1)
601 menuTooltipState = 0;
604 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
605 if(menuTooltipAlpha == 0)
607 menuTooltipState = 0;
608 menuTooltipItem = world;
613 if(menuTooltipItem == world)
617 strunzone(menuTooltipText);
618 menuTooltipText = string_null;
624 if(menu_tooltips != menu_tooltips_old)
626 if (menu_tooltips != 0 && menu_tooltips_old != 0)
627 menuTooltipItem = world; // reload tooltip next frame
628 menu_tooltips_old = menu_tooltips;
630 else if(menuTooltipOrigin_x < 0) // unallocated?
631 m_allocatetooltipbox(pos);
633 if(menuTooltipOrigin_x >= 0)
636 p = SKINBORDER_TOOLTIP;
638 p_y *= 1 / conheight;
639 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
640 p = menuTooltipOrigin;
641 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
642 p_y += SKINMARGIN_TOOLTIP_y / conheight;
643 getWrappedLine_remaining = menuTooltipText;
644 while(getWrappedLine_remaining)
646 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
647 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
661 execute_next_frame();
663 menuMouseMode = cvar("menu_mouse_absolute");
669 UpdateConWidthHeight();
673 // TODO draw an info image about this situation
677 if(!menuNotTheFirstFrame)
679 menuNotTheFirstFrame = 1;
681 if(!cvar("menu_video_played"))
683 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
684 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
686 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
687 localcmd("set menu_video_played 1\n");
691 realFrametime = frametime = min(0.2, t - menuPrevTime);
695 t = cvar("menu_slowmo");
706 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
707 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
712 if(cvar("cl_capturevideo"))
713 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
715 prevMenuAlpha = menuAlpha;
718 if(menuAlpha == 0 && menuLogoAlpha < 2)
720 menuLogoAlpha = menuLogoAlpha + frametime * 2;
724 menuAlpha = min(1, menuAlpha + frametime * 5);
730 menuAlpha = max(0, menuAlpha - frametime * 5);
734 draw_reset_cropped();
736 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
738 if(menuLogoAlpha > 0)
741 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
742 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
743 draw_reset_cropped();
744 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
746 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
747 draw_drawMousePointer(menuMousePos);
752 else if(SKINALPHA_BACKGROUND_INGAME)
757 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
758 draw_reset_cropped();
762 if(menuAlpha != prevMenuAlpha)
763 cvar_set("_menu_alpha", ftos(menuAlpha));
765 draw_reset_cropped();
767 draw_reset_cropped();
771 if(prevMenuAlpha > 0)
772 main.initializeDialog(main, main.firstChild);
773 draw_reset_cropped();
778 draw_alpha *= menuAlpha;
783 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
784 if(newMouse != '0 0 0')
785 if(newMouse != menuMousePos)
787 menuMousePos = newMouse;
788 if(mouseButtonsPressed)
789 main.mouseDrag(main, menuMousePos);
791 main.mouseMove(main, menuMousePos);
798 vector dMouse, minpos, maxpos;
799 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
800 if(dMouse != '0 0 0')
802 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
803 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
804 dMouse = globalToBoxSize(dMouse, draw_scale);
805 menuMousePos += dMouse * cvar("menu_mouse_speed");
806 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
807 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
808 if(mouseButtonsPressed)
809 main.mouseDrag(main, menuMousePos);
811 main.mouseMove(main, menuMousePos);
817 m_tooltip(menuMousePos);
819 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
821 draw_drawMousePointer(menuMousePos);
823 draw_reset_cropped();
832 setkeydest(KEY_MENU);
833 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
838 if(mouseButtonsPressed)
839 main.mouseRelease(main, menuMousePos);
840 mouseButtonsPressed = 0;
842 main.focusEnter(main);
843 main.showNotify(main);
849 setkeydest(KEY_GAME);
850 setmousetarget(MT_CLIENT);
855 main.focusLeave(main);
856 main.hideNotify(main);
859 void m_toggle(float mode)
880 for(e = NULL; (e = nextent(e)) != NULL; )
882 if(e.classname != "vtbl")
888 void m_focus_item_chain(entity outermost, entity innermost)
890 if(innermost.parent != outermost)
891 m_focus_item_chain(outermost, innermost.parent);
892 innermost.parent.setFocus(innermost.parent, innermost);
895 void m_activate_window(entity wnd)
900 m_activate_window(par);
902 if(par.instanceOfModalController)
904 if(wnd.tabSelectingButton)
906 TabButton_Click(wnd.tabSelectingButton, wnd);
909 par.initializeDialog(par, wnd);
911 else if(par.instanceOfNexposee)
913 // nexposee (sorry for violating abstraction here)
914 par.selectedChild = wnd;
915 par.animationState = 1;
916 Container_setFocus(par, NULL);
918 else if(par.instanceOfContainer)
922 par.setFocus(par, wnd);
926 void m_setpointerfocus(entity wnd)
928 if(wnd.instanceOfContainer)
930 entity focus = wnd.preferredFocusedGrandChild(wnd);
933 menuMousePos = focus.origin + 0.5 * focus.size;
934 menuMousePos_x *= 1 / conwidth;
935 menuMousePos_y *= 1 / conheight;
936 if(wnd.focused) // why does this never happen?
937 m_focus_item_chain(wnd, focus);
942 void m_goto(string itemname)
948 strunzone(m_goto_buffer);
949 m_goto_buffer = strzone(itemname);
952 if(itemname == "") // this can be called by GameCommand
954 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
960 m_activate_window(main.mainNexposee);
966 for(e = NULL; (e = find(e, name, itemname)); )
967 if(e.classname != "vtbl")
972 m_activate_window(e);
973 m_setpointerfocus(e);