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"));
34 check_unacceptable_compiler_bugs();
37 print(sprintf(_("^4MQC Build information: ^1%s\n"), WATERMARK()));
40 // list all game dirs (TEST)
47 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
50 dprint(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
54 // needs to be done so early because of the constants they create
61 float MENU_ASPECT = 1.25; // 1280x1024
62 float MENU_MINHEIGHT = 600;
63 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
64 void draw_reset_cropped()
66 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
68 void draw_reset_full()
70 draw_reset(realconwidth, realconheight, 0, 0);
72 void UpdateConWidthHeight()
74 conwidth_s = conwidth;
75 conheight_s = conheight;
76 realconwidth = cvar("vid_conwidth");
77 realconheight = cvar("vid_conheight");
78 if(realconwidth / realconheight > MENU_ASPECT)
81 conwidth = realconheight * MENU_ASPECT;
82 conheight = realconheight;
87 conwidth = realconwidth;
88 conheight = realconwidth / MENU_ASPECT;
90 screenconwidth = conwidth;
91 screenconheight = conheight;
92 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
94 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
95 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
97 if(conheight < MENU_MINHEIGHT)
99 conwidth *= MENU_MINHEIGHT / conheight;
100 conheight = MENU_MINHEIGHT;
104 if(conwidth_s != conwidth || conheight_s != conheight)
106 draw_reset_cropped();
107 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
112 void m_init_delayed()
114 float fh, glob, n, i;
117 conwidth = conheight = -1;
118 UpdateConWidthHeight();
119 draw_reset_cropped();
127 if(cvar_string("menu_skin") != "")
129 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
130 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
133 if(cvar_defstring("menu_skin") != "")
135 cvar_set("menu_skin", cvar_defstring("menu_skin"));
136 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
137 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
141 draw_currentSkin = "gfx/menu/default";
142 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
146 error("cannot load any menu skin\n");
148 draw_currentSkin = strzone(draw_currentSkin);
149 while((s = fgets(fh)))
151 // these two are handled by skinlist.qc
152 if(substring(s, 0, 6) == "title ")
154 if(substring(s, 0, 7) == "author ")
156 n = tokenize_console(s);
158 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
162 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
165 n = search_getsize(glob);
166 for(i = 0; i < n; ++i)
167 precache_pic(search_getfilename(glob, i));
171 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
174 anim = spawnAnimHost();
175 main = spawnMainWindow(); main.configureMainWindow(main);
178 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
181 menuMousePos = '0.5 0.5 0';
186 m_display(); // delayed menu display
189 void m_keyup (float key, float ascii)
195 draw_reset_cropped();
196 main.keyUp(main, key, ascii, menuShiftState);
197 if(key >= K_MOUSE1 && key <= K_MOUSE3)
199 --mouseButtonsPressed;
200 if(!mouseButtonsPressed)
201 main.mouseRelease(main, menuMousePos);
202 if(mouseButtonsPressed < 0)
204 mouseButtonsPressed = 0;
205 dprint("Warning: released an already released button\n");
208 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
209 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
210 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
213 void m_keydown(float key, float ascii)
224 e.keyGrabbed(e, key, ascii);
228 draw_reset_cropped();
229 if(key >= K_MOUSE1 && key <= K_MOUSE3)
230 if(!mouseButtonsPressed)
231 main.mousePress(main, menuMousePos);
232 if(!main.keyDown(main, key, ascii, menuShiftState))
234 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
235 m_hide(); // disable menu on unhandled ESC
237 if(key >= K_MOUSE1 && key <= K_MOUSE3)
239 ++mouseButtonsPressed;
240 if(mouseButtonsPressed > 10)
242 mouseButtonsPressed = 10;
243 dprint("Warning: pressed an already pressed button\n");
246 if(key == K_ALT) menuShiftState |= S_ALT;
247 if(key == K_CTRL) menuShiftState |= S_CTRL;
248 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
251 float SCALEMODE_CROP = 0;
252 float SCALEMODE_LETTERBOX = 1;
253 float SCALEMODE_WIDTH = 2;
254 float SCALEMODE_HEIGHT = 3;
255 float SCALEMODE_STRETCH = 4;
256 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
258 vector sz, org, isz, isz_w, isz_h;
259 float width_is_larger;
261 sz = draw_PictureSize(img);
262 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
263 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
264 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
270 isz = (width_is_larger ? isz_h : isz_w);
272 case SCALEMODE_LETTERBOX:
273 isz = (width_is_larger ? isz_w : isz_h);
275 case SCALEMODE_WIDTH:
278 case SCALEMODE_HEIGHT:
281 case SCALEMODE_STRETCH:
286 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
287 draw_Picture(org, img, isz, '1 1 1', a);
290 void drawBackground(string img, float a, string algn, float force1)
292 if(main.mainNexposee.ModalController_state == 0)
302 scalemode = SCALEMODE_CROP;
304 for(i = 0; i < strlen(algn); ++i)
306 c = substring(algn, i, 1);
309 case "c": scalemode = SCALEMODE_CROP; goto nopic;
310 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
311 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
312 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
313 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
314 case "1": case "4": case "7": v_x = 0.0; break;
315 case "2": case "5": case "8": v_x = 0.5; break;
316 case "3": case "6": case "9": v_x = 1.0; break;
317 default: v_x = random(); break;
321 case "7": case "8": case "9": v_y = 0.0; break;
322 case "4": case "5": case "6": v_y = 0.5; break;
323 case "1": case "2": case "3": v_y = 1.0; break;
324 default: v_y = random(); break;
327 draw_Picture_Aligned(v, scalemode, img, a);
329 // force all secondary layers to use alpha 1. Prevents ugly issues
330 // with overlap. It's a flag because it cannot be used for the
332 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
334 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
341 float menu_tooltips_old;
342 vector menuTooltipAveragedMousePos;
343 entity menuTooltipItem;
344 vector menuTooltipOrigin;
345 vector menuTooltipSize;
346 float menuTooltipAlpha;
347 string menuTooltipText;
348 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
349 float m_testmousetooltipbox(vector pos)
351 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
352 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
356 float m_testtooltipbox(vector tooltippos)
362 if(tooltippos_x + menuTooltipSize_x > 1)
364 if(tooltippos_y + menuTooltipSize_y > 1)
366 menuTooltipOrigin = tooltippos;
369 float m_allocatetooltipbox(vector pos)
371 vector avoidplus, avoidminus;
374 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
375 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
378 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
379 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
384 if(m_testtooltipbox(v))
388 v_x = pos_x - menuTooltipSize_x * 0.5;
389 if(m_testtooltipbox(v))
393 v_x = pos_x - avoidminus_x;
394 if(m_testtooltipbox(v))
398 v_y = pos_y - avoidminus_y;
399 if(m_testtooltipbox(v))
403 v_x = pos_x - menuTooltipSize_x * 0.5;
404 if(m_testtooltipbox(v))
408 v_x = pos_x + avoidplus_x;
409 if(m_testtooltipbox(v))
414 entity m_findtooltipitem(entity root, vector pos)
422 while(it.instanceOfContainer)
424 while(it.instanceOfNexposee && it.focusedChild)
426 it = it.focusedChild;
427 pos = globalToBox(pos, it.Container_origin, it.Container_size);
429 if(it.instanceOfNexposee)
431 it = it.itemFromPoint(it, pos);
434 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
438 else if(it.instanceOfModalController)
439 it = it.focusedChild;
441 it = it.itemFromPoint(it, pos);
446 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
448 pos = globalToBox(pos, it.Container_origin, it.Container_size);
455 if (menu_tooltips == 2)
458 if (menuTooltipItem.cvarName)
460 if (getCvarsMulti(menuTooltipItem))
461 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
463 s = strcat("[", menuTooltipItem.cvarName, "]");
465 else if (menuTooltipItem.onClickCommand)
466 s = strcat("<", menuTooltipItem.onClickCommand, ">");
468 return menuTooltipItem.tooltip;
469 if (menuTooltipItem.tooltip)
470 return strcat(menuTooltipItem.tooltip, " ", s);
473 return menuTooltipItem.tooltip;
475 void m_tooltip(vector pos)
482 menu_tooltips = cvar("menu_tooltips");
485 // don't return immediately, fade out the active tooltip first
486 if (menuTooltipItem == world)
489 menu_tooltips_old = menu_tooltips;
493 f = bound(0, frametime * 2, 1);
494 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
495 f = vlen(pos - menuTooltipAveragedMousePos);
497 it = m_findtooltipitem(main, pos);
501 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
503 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
504 if(it != menuTooltipItem)
506 switch(menuTooltipState)
511 // another item: fade out first
512 menuTooltipState = 2;
517 menuTooltipState = 1;
518 menuTooltipItem = it;
520 menuTooltipOrigin_x = -1; // unallocated
523 strunzone(menuTooltipText);
524 menuTooltipText = strzone(gettooltip());
528 getWrappedLine_remaining = menuTooltipText;
529 while(getWrappedLine_remaining)
531 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
533 f = draw_TextWidth(s, FALSE, fontsize);
537 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
538 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
539 menuTooltipSize_z = 0;
543 // changing item while fading in: fade out first
544 menuTooltipState = 2;
547 // changing item while fading out: can't
551 else if(menuTooltipState == 2) // re-fade in?
552 menuTooltipState = 1;
555 if(!m_testmousetooltipbox(pos))
556 menuTooltipState = 2; // fade out if mouse touches it
558 switch(menuTooltipState)
561 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
562 if(menuTooltipAlpha == 1)
563 menuTooltipState = 0;
566 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
567 if(menuTooltipAlpha == 0)
569 menuTooltipState = 0;
570 menuTooltipItem = world;
575 if(menuTooltipItem == world)
579 strunzone(menuTooltipText);
580 menuTooltipText = string_null;
586 if(menu_tooltips != menu_tooltips_old)
588 if (menu_tooltips != 0 && menu_tooltips_old != 0)
589 menuTooltipItem = world; // reload tooltip next frame
590 menu_tooltips_old = menu_tooltips;
592 else if(menuTooltipOrigin_x < 0) // unallocated?
593 m_allocatetooltipbox(pos);
595 if(menuTooltipOrigin_x >= 0)
598 p = SKINBORDER_TOOLTIP;
600 p_y *= 1 / conheight;
601 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
602 p = menuTooltipOrigin;
603 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
604 p_y += SKINMARGIN_TOOLTIP_y / conheight;
605 getWrappedLine_remaining = menuTooltipText;
606 while(getWrappedLine_remaining)
608 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
609 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
621 execute_next_frame();
623 menuMouseMode = cvar("menu_mouse_absolute");
629 UpdateConWidthHeight();
633 // TODO draw an info image about this situation
637 if(!menuNotTheFirstFrame)
639 menuNotTheFirstFrame = 1;
641 if(!cvar("menu_video_played"))
643 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
644 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
646 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
647 localcmd("set menu_video_played 1\n");
651 realFrametime = frametime = min(0.2, t - menuPrevTime);
655 t = cvar("menu_slowmo");
666 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
667 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
672 if(cvar("cl_capturevideo"))
673 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
677 gamestatus = gamestatus | GAME_ISSERVER;
678 if(clientstate() == CS_CONNECTED)
679 gamestatus = gamestatus | GAME_CONNECTED;
680 if(cvar("developer"))
681 gamestatus = gamestatus | GAME_DEVELOPER;
683 prevMenuAlpha = menuAlpha;
686 if(menuAlpha == 0 && menuLogoAlpha < 2)
688 menuLogoAlpha = menuLogoAlpha + frametime * 2;
692 menuAlpha = min(1, menuAlpha + frametime * 5);
698 menuAlpha = max(0, menuAlpha - frametime * 5);
702 draw_reset_cropped();
704 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
706 if(menuLogoAlpha > 0)
709 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
710 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
711 draw_reset_cropped();
712 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
714 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
715 draw_drawMousePointer(menuMousePos);
720 else if(SKINALPHA_BACKGROUND_INGAME)
725 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
726 draw_reset_cropped();
730 if(menuAlpha != prevMenuAlpha)
731 cvar_set("_menu_alpha", ftos(menuAlpha));
733 draw_reset_cropped();
735 draw_reset_cropped();
739 if(prevMenuAlpha > 0)
740 main.initializeDialog(main, main.firstChild);
741 draw_reset_cropped();
746 draw_alpha *= menuAlpha;
751 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
752 if(newMouse != '0 0 0')
753 if(newMouse != menuMousePos)
755 menuMousePos = newMouse;
756 if(mouseButtonsPressed)
757 main.mouseDrag(main, menuMousePos);
759 main.mouseMove(main, menuMousePos);
766 vector dMouse, minpos, maxpos;
767 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
768 if(dMouse != '0 0 0')
770 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
771 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
772 dMouse = globalToBoxSize(dMouse, draw_scale);
773 menuMousePos += dMouse * cvar("menu_mouse_speed");
774 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
775 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
776 if(mouseButtonsPressed)
777 main.mouseDrag(main, menuMousePos);
779 main.mouseMove(main, menuMousePos);
785 m_tooltip(menuMousePos);
787 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
789 draw_drawMousePointer(menuMousePos);
791 draw_reset_cropped();
800 setkeydest(KEY_MENU);
801 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
806 if(mouseButtonsPressed)
807 main.mouseRelease(main, menuMousePos);
808 mouseButtonsPressed = 0;
810 main.focusEnter(main);
811 main.showNotify(main);
817 setkeydest(KEY_GAME);
818 setmousetarget(MT_CLIENT);
823 main.focusLeave(main);
824 main.hideNotify(main);
827 void m_toggle(float mode)
848 for(e = NULL; (e = nextent(e)) != NULL; )
850 if(e.classname != "vtbl")
856 void m_focus_item_chain(entity outermost, entity innermost)
858 if(innermost.parent != outermost)
859 m_focus_item_chain(outermost, innermost.parent);
860 innermost.parent.setFocus(innermost.parent, innermost);
863 void m_activate_window(entity wnd)
868 m_activate_window(par);
870 if(par.instanceOfModalController)
872 if(wnd.tabSelectingButton)
874 TabButton_Click(wnd.tabSelectingButton, wnd);
877 par.initializeDialog(par, wnd);
879 else if(par.instanceOfNexposee)
881 // nexposee (sorry for violating abstraction here)
882 par.selectedChild = wnd;
883 par.animationState = 1;
884 Container_setFocus(par, NULL);
886 else if(par.instanceOfContainer)
890 par.setFocus(par, wnd);
894 void m_setpointerfocus(entity wnd)
896 if(wnd.instanceOfContainer)
898 entity focus = wnd.preferredFocusedGrandChild(wnd);
901 menuMousePos = focus.origin + 0.5 * focus.size;
902 menuMousePos_x *= 1 / conwidth;
903 menuMousePos_y *= 1 / conheight;
904 if(wnd.focused) // why does this never happen?
905 m_focus_item_chain(wnd, focus);
910 void m_goto(string itemname)
915 if(itemname == "") // this can be called by GameCommand
917 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
921 m_activate_window(main.mainNexposee);
927 for(e = NULL; (e = find(e, name, itemname)); )
928 if(e.classname != "vtbl")
933 m_activate_window(e);
934 m_setpointerfocus(e);
940 void m_goto_skin_selector()
944 // TODO add code to switch back to the skin selector (no idea how to do it now)
945 m_goto("skinselector");
948 void m_goto_language_selector()
952 // TODO add code to switch back to the language selector (no idea how to do it now)
953 m_goto("languageselector");
956 void m_goto_video_settings()
960 // TODO add code to switch back to the video settings (no idea how to do it now)
961 m_goto("videosettings");