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
58 float ddsload = cvar("r_texture_dds_load");
59 float texcomp = cvar("gl_texturecompression");
61 if(ddsload != cvar("r_texture_dds_load") || texcomp != cvar("gl_texturecompression"))
62 localcmd("\nr_restart\n");
65 float MENU_ASPECT = 1.25; // 1280x1024
66 float MENU_MINHEIGHT = 600;
67 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
68 void draw_reset_cropped()
70 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
72 void draw_reset_full()
74 draw_reset(realconwidth, realconheight, 0, 0);
76 void UpdateConWidthHeight()
78 conwidth_s = conwidth;
79 conheight_s = conheight;
80 realconwidth = cvar("vid_conwidth");
81 realconheight = cvar("vid_conheight");
82 if(realconwidth / realconheight > MENU_ASPECT)
85 conwidth = realconheight * MENU_ASPECT;
86 conheight = realconheight;
91 conwidth = realconwidth;
92 conheight = realconwidth / MENU_ASPECT;
94 screenconwidth = conwidth;
95 screenconheight = conheight;
96 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
98 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
99 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
101 if(conheight < MENU_MINHEIGHT)
103 conwidth *= MENU_MINHEIGHT / conheight;
104 conheight = MENU_MINHEIGHT;
108 if(conwidth_s != conwidth || conheight_s != conheight)
110 draw_reset_cropped();
111 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
116 void m_init_delayed()
118 float fh, glob, n, i;
121 conwidth = conheight = -1;
122 UpdateConWidthHeight();
123 draw_reset_cropped();
131 if(cvar_string("menu_skin") != "")
133 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
134 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
137 if(cvar_defstring("menu_skin") != "")
139 cvar_set("menu_skin", cvar_defstring("menu_skin"));
140 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
141 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
145 draw_currentSkin = "gfx/menu/default";
146 fh = fopen(language_filename(strcat(draw_currentSkin, "/skinvalues.txt")), FILE_READ);
150 error("cannot load any menu skin\n");
152 draw_currentSkin = strzone(draw_currentSkin);
153 while((s = fgets(fh)))
155 // these two are handled by skinlist.qc
156 if(substring(s, 0, 6) == "title ")
158 if(substring(s, 0, 7) == "author ")
160 n = tokenize_console(s);
162 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
166 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
169 n = search_getsize(glob);
170 for(i = 0; i < n; ++i)
171 precache_pic(search_getfilename(glob, i));
175 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
178 anim = spawnAnimHost();
179 main = spawnMainWindow(); main.configureMainWindow(main);
182 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
185 menuMousePos = '0.5 0.5 0';
190 m_display(); // delayed menu display
193 void m_keyup (float key, float ascii)
199 draw_reset_cropped();
200 main.keyUp(main, key, ascii, menuShiftState);
201 if(key >= K_MOUSE1 && key <= K_MOUSE3)
203 --mouseButtonsPressed;
204 if(!mouseButtonsPressed)
205 main.mouseRelease(main, menuMousePos);
206 if(mouseButtonsPressed < 0)
208 mouseButtonsPressed = 0;
209 dprint("Warning: released an already released button\n");
212 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
213 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
214 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
217 void m_keydown(float key, float ascii)
228 e.keyGrabbed(e, key, ascii);
232 draw_reset_cropped();
233 if(key >= K_MOUSE1 && key <= K_MOUSE3)
234 if(!mouseButtonsPressed)
235 main.mousePress(main, menuMousePos);
236 if(!main.keyDown(main, key, ascii, menuShiftState))
238 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
239 m_hide(); // disable menu on unhandled ESC
241 if(key >= K_MOUSE1 && key <= K_MOUSE3)
243 ++mouseButtonsPressed;
244 if(mouseButtonsPressed > 10)
246 mouseButtonsPressed = 10;
247 dprint("Warning: pressed an already pressed button\n");
250 if(key == K_ALT) menuShiftState |= S_ALT;
251 if(key == K_CTRL) menuShiftState |= S_CTRL;
252 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
255 float SCALEMODE_CROP = 0;
256 float SCALEMODE_LETTERBOX = 1;
257 float SCALEMODE_WIDTH = 2;
258 float SCALEMODE_HEIGHT = 3;
259 float SCALEMODE_STRETCH = 4;
260 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
262 vector sz, org, isz, isz_w, isz_h;
263 float width_is_larger;
265 sz = draw_PictureSize(img);
266 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
267 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
268 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
274 isz = (width_is_larger ? isz_h : isz_w);
276 case SCALEMODE_LETTERBOX:
277 isz = (width_is_larger ? isz_w : isz_h);
279 case SCALEMODE_WIDTH:
282 case SCALEMODE_HEIGHT:
285 case SCALEMODE_STRETCH:
290 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
291 draw_Picture(org, img, isz, '1 1 1', a);
294 void drawBackground(string img, float a, string algn, float force1)
296 if(main.mainNexposee.ModalController_state == 0)
306 scalemode = SCALEMODE_CROP;
308 for(i = 0; i < strlen(algn); ++i)
310 c = substring(algn, i, 1);
313 case "c": scalemode = SCALEMODE_CROP; goto nopic;
314 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
315 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
316 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
317 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
318 case "1": case "4": case "7": v_x = 0.0; break;
319 case "2": case "5": case "8": v_x = 0.5; break;
320 case "3": case "6": case "9": v_x = 1.0; break;
321 default: v_x = random(); break;
325 case "7": case "8": case "9": v_y = 0.0; break;
326 case "4": case "5": case "6": v_y = 0.5; break;
327 case "1": case "2": case "3": v_y = 1.0; break;
328 default: v_y = random(); break;
331 draw_Picture_Aligned(v, scalemode, img, a);
333 // force all secondary layers to use alpha 1. Prevents ugly issues
334 // with overlap. It's a flag because it cannot be used for the
336 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
338 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
345 float menu_tooltips_old;
346 vector menuTooltipAveragedMousePos;
347 entity menuTooltipItem;
348 vector menuTooltipOrigin;
349 vector menuTooltipSize;
350 float menuTooltipAlpha;
351 string menuTooltipText;
352 float menuTooltipState; // 0: static, 1: fading in, 2: fading out
353 float m_testmousetooltipbox(vector pos)
355 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
356 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
360 float m_testtooltipbox(vector tooltippos)
366 if(tooltippos_x + menuTooltipSize_x > 1)
368 if(tooltippos_y + menuTooltipSize_y > 1)
370 menuTooltipOrigin = tooltippos;
373 float m_allocatetooltipbox(vector pos)
375 vector avoidplus, avoidminus;
378 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth;
379 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight;
382 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x * SKINSIZE_CURSOR_x) / conwidth + menuTooltipSize_x;
383 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y * SKINSIZE_CURSOR_y) / conheight + menuTooltipSize_y;
388 if(m_testtooltipbox(v))
392 v_x = pos_x - menuTooltipSize_x * 0.5;
393 if(m_testtooltipbox(v))
397 v_x = pos_x - avoidminus_x;
398 if(m_testtooltipbox(v))
402 v_y = pos_y - avoidminus_y;
403 if(m_testtooltipbox(v))
407 v_x = pos_x - menuTooltipSize_x * 0.5;
408 if(m_testtooltipbox(v))
412 v_x = pos_x + avoidplus_x;
413 if(m_testtooltipbox(v))
418 entity m_findtooltipitem(entity root, vector pos)
426 while(it.instanceOfContainer)
428 while(it.instanceOfNexposee && it.focusedChild)
430 it = it.focusedChild;
431 pos = globalToBox(pos, it.Container_origin, it.Container_size);
433 if(it.instanceOfNexposee)
435 it = it.itemFromPoint(it, pos);
438 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
442 else if(it.instanceOfModalController)
443 it = it.focusedChild;
445 it = it.itemFromPoint(it, pos);
450 else if(menu_tooltips == 2 && (it.cvarName || it.onClickCommand))
452 pos = globalToBox(pos, it.Container_origin, it.Container_size);
459 if (menu_tooltips == 2)
462 if (menuTooltipItem.cvarName)
464 if (getCvarsMulti(menuTooltipItem))
465 s = strcat("[", menuTooltipItem.cvarName, " ", getCvarsMulti(menuTooltipItem), "]");
467 s = strcat("[", menuTooltipItem.cvarName, "]");
469 else if (menuTooltipItem.onClickCommand)
470 s = strcat("<", menuTooltipItem.onClickCommand, ">");
472 return menuTooltipItem.tooltip;
473 if (menuTooltipItem.tooltip)
474 return strcat(menuTooltipItem.tooltip, " ", s);
477 return menuTooltipItem.tooltip;
479 void m_tooltip(vector pos)
486 menu_tooltips = cvar("menu_tooltips");
489 // don't return immediately, fade out the active tooltip first
490 if (menuTooltipItem == world)
493 menu_tooltips_old = menu_tooltips;
497 f = bound(0, frametime * 2, 1);
498 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
499 f = vlen(pos - menuTooltipAveragedMousePos);
501 it = m_findtooltipitem(main, pos);
505 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
507 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
508 if(it != menuTooltipItem)
510 switch(menuTooltipState)
515 // another item: fade out first
516 menuTooltipState = 2;
521 menuTooltipState = 1;
522 menuTooltipItem = it;
524 menuTooltipOrigin_x = -1; // unallocated
527 strunzone(menuTooltipText);
528 menuTooltipText = strzone(gettooltip());
532 getWrappedLine_remaining = menuTooltipText;
533 while(getWrappedLine_remaining)
535 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
537 f = draw_TextWidth(s, FALSE, fontsize);
541 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
542 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
543 menuTooltipSize_z = 0;
547 // changing item while fading in: fade out first
548 menuTooltipState = 2;
551 // changing item while fading out: can't
555 else if(menuTooltipState == 2) // re-fade in?
556 menuTooltipState = 1;
559 if(!m_testmousetooltipbox(pos))
560 menuTooltipState = 2; // fade out if mouse touches it
562 switch(menuTooltipState)
565 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
566 if(menuTooltipAlpha == 1)
567 menuTooltipState = 0;
570 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
571 if(menuTooltipAlpha == 0)
573 menuTooltipState = 0;
574 menuTooltipItem = world;
579 if(menuTooltipItem == world)
583 strunzone(menuTooltipText);
584 menuTooltipText = string_null;
590 if(menu_tooltips != menu_tooltips_old)
592 if (menu_tooltips != 0 && menu_tooltips_old != 0)
593 menuTooltipItem = world; // reload tooltip next frame
594 menu_tooltips_old = menu_tooltips;
596 else if(menuTooltipOrigin_x < 0) // unallocated?
597 m_allocatetooltipbox(pos);
599 if(menuTooltipOrigin_x >= 0)
602 p = SKINBORDER_TOOLTIP;
604 p_y *= 1 / conheight;
605 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
606 p = menuTooltipOrigin;
607 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
608 p_y += SKINMARGIN_TOOLTIP_y / conheight;
609 getWrappedLine_remaining = menuTooltipText;
610 while(getWrappedLine_remaining)
612 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
613 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
625 execute_next_frame();
627 menuMouseMode = cvar("menu_mouse_absolute");
633 UpdateConWidthHeight();
637 // TODO draw an info image about this situation
641 if(!menuNotTheFirstFrame)
643 menuNotTheFirstFrame = 1;
645 if(!cvar("menu_video_played"))
647 localcmd("cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
648 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading FIXME
650 // ALWAYS set this cvar; if we start but menu is not active, this means we want no background music!
651 localcmd("set menu_video_played 1\n");
655 realFrametime = frametime = min(0.2, t - menuPrevTime);
659 t = cvar("menu_slowmo");
670 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
671 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
676 if(cvar("cl_capturevideo"))
677 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
681 gamestatus = gamestatus | GAME_ISSERVER;
682 if(clientstate() == CS_CONNECTED)
683 gamestatus = gamestatus | GAME_CONNECTED;
684 if(cvar("developer"))
685 gamestatus = gamestatus | GAME_DEVELOPER;
687 prevMenuAlpha = menuAlpha;
690 if(menuAlpha == 0 && menuLogoAlpha < 2)
692 menuLogoAlpha = menuLogoAlpha + frametime * 2;
696 menuAlpha = min(1, menuAlpha + frametime * 5);
702 menuAlpha = max(0, menuAlpha - frametime * 5);
706 draw_reset_cropped();
708 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
710 if(menuLogoAlpha > 0)
713 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
714 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
715 draw_reset_cropped();
716 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
718 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
719 draw_drawMousePointer(menuMousePos);
724 else if(SKINALPHA_BACKGROUND_INGAME)
729 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
730 draw_reset_cropped();
734 if(menuAlpha != prevMenuAlpha)
735 cvar_set("_menu_alpha", ftos(menuAlpha));
737 draw_reset_cropped();
739 draw_reset_cropped();
743 if(prevMenuAlpha > 0)
744 main.initializeDialog(main, main.firstChild);
745 draw_reset_cropped();
750 draw_alpha *= menuAlpha;
755 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
756 if(newMouse != '0 0 0')
757 if(newMouse != menuMousePos)
759 menuMousePos = newMouse;
760 if(mouseButtonsPressed)
761 main.mouseDrag(main, menuMousePos);
763 main.mouseMove(main, menuMousePos);
770 vector dMouse, minpos, maxpos;
771 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
772 if(dMouse != '0 0 0')
774 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
775 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
776 dMouse = globalToBoxSize(dMouse, draw_scale);
777 menuMousePos += dMouse * cvar("menu_mouse_speed");
778 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
779 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
780 if(mouseButtonsPressed)
781 main.mouseDrag(main, menuMousePos);
783 main.mouseMove(main, menuMousePos);
789 m_tooltip(menuMousePos);
791 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
793 draw_drawMousePointer(menuMousePos);
795 draw_reset_cropped();
804 setkeydest(KEY_MENU);
805 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
810 if(mouseButtonsPressed)
811 main.mouseRelease(main, menuMousePos);
812 mouseButtonsPressed = 0;
814 main.focusEnter(main);
815 main.showNotify(main);
821 setkeydest(KEY_GAME);
822 setmousetarget(MT_CLIENT);
827 main.focusLeave(main);
828 main.hideNotify(main);
831 void m_toggle(float mode)
852 for(e = NULL; (e = nextent(e)) != NULL; )
854 if(e.classname != "vtbl")
860 void m_focus_item_chain(entity outermost, entity innermost)
862 if(innermost.parent != outermost)
863 m_focus_item_chain(outermost, innermost.parent);
864 innermost.parent.setFocus(innermost.parent, innermost);
867 void m_activate_window(entity wnd)
872 m_activate_window(par);
874 if(par.instanceOfModalController)
876 if(wnd.tabSelectingButton)
878 TabButton_Click(wnd.tabSelectingButton, wnd);
881 par.initializeDialog(par, wnd);
883 else if(par.instanceOfNexposee)
885 // nexposee (sorry for violating abstraction here)
886 par.selectedChild = wnd;
887 par.animationState = 1;
888 Container_setFocus(par, NULL);
890 else if(par.instanceOfContainer)
894 par.setFocus(par, wnd);
898 void m_setpointerfocus(entity wnd)
900 if(wnd.instanceOfContainer)
902 entity focus = wnd.preferredFocusedGrandChild(wnd);
905 menuMousePos = focus.origin + 0.5 * focus.size;
906 menuMousePos_x *= 1 / conwidth;
907 menuMousePos_y *= 1 / conheight;
908 if(wnd.focused) // why does this never happen?
909 m_focus_item_chain(wnd, focus);
914 void m_goto(string itemname)
919 if(itemname == "") // this can be called by GameCommand
921 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
925 m_activate_window(main.mainNexposee);
931 for(e = NULL; (e = find(e, name, itemname)); )
932 if(e.classname != "vtbl")
937 m_activate_window(e);
938 m_setpointerfocus(e);
944 void m_goto_skin_selector()
948 // TODO add code to switch back to the skin selector (no idea how to do it now)
949 m_goto("skinselector");
952 void m_goto_language_selector()
956 // TODO add code to switch back to the language selector (no idea how to do it now)
957 m_goto("languageselector");
960 void m_goto_video_settings()
964 // TODO add code to switch back to the video settings (no idea how to do it now)
965 m_goto("videosettings");