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;
23 cvar_set("_menu_alpha", "0");
25 check_unacceptable_compiler_bugs();
27 // list all game dirs (TEST)
34 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
37 print(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
42 float MENU_ASPECT = 1.25; // 1280x1024
43 float MENU_MINHEIGHT = 600;
44 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
45 void draw_reset_cropped()
47 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
49 void draw_reset_full()
51 draw_reset(realconwidth, realconheight, 0, 0);
53 void UpdateConWidthHeight()
55 conwidth_s = conwidth;
56 conheight_s = conheight;
57 realconwidth = cvar("vid_conwidth");
58 realconheight = cvar("vid_conheight");
59 if(realconwidth / realconheight > MENU_ASPECT)
62 conwidth = realconheight * MENU_ASPECT;
63 conheight = realconheight;
68 conwidth = realconwidth;
69 conheight = realconwidth / MENU_ASPECT;
71 screenconwidth = conwidth;
72 screenconheight = conheight;
73 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
75 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
76 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
78 if(conheight < MENU_MINHEIGHT)
80 conwidth *= MENU_MINHEIGHT / conheight;
81 conheight = MENU_MINHEIGHT;
85 if(conwidth_s != conwidth || conheight_s != conheight)
88 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
93 void() m_init_delayed =
98 conwidth = conheight = -1;
99 UpdateConWidthHeight();
100 draw_reset_cropped();
111 if(cvar_string("menu_skin") != "")
113 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
114 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
117 if(cvar_defstring("menu_skin") != "")
119 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
120 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
124 draw_currentSkin = "gfx/menu/default";
125 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
127 draw_currentSkin = strzone(draw_currentSkin);
128 while((s = fgets(fh)))
130 // these two are handled by skinlist.qc
131 if(substring(s, 0, 6) == "title ")
133 if(substring(s, 0, 7) == "author ")
135 n = tokenize_console(s);
137 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
141 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
144 n = search_getsize(glob);
145 for(i = 0; i < n; ++i)
146 precache_pic(search_getfilename(glob, i));
150 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
153 anim = spawnAnimHost();
154 main = spawnMainWindow(); main.configureMainWindow(main);
157 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
160 menuMousePos = '0.5 0.5 0';
163 m_display(); // delayed menu display
166 void(float key, float ascii) m_keyup =
172 draw_reset_cropped();
173 main.keyUp(main, key, ascii, menuShiftState);
174 if(key >= K_MOUSE1 && key <= K_MOUSE3)
176 --mouseButtonsPressed;
177 if(!mouseButtonsPressed)
178 main.mouseRelease(main, menuMousePos);
179 if(mouseButtonsPressed < 0)
181 mouseButtonsPressed = 0;
182 print("Warning: released an already released button\n");
185 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
186 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
187 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
190 void(float key, float ascii) m_keydown =
201 e.keyGrabbed(e, key, ascii);
205 draw_reset_cropped();
206 if(key >= K_MOUSE1 && key <= K_MOUSE3)
207 if(!mouseButtonsPressed)
208 main.mousePress(main, menuMousePos);
209 if(!main.keyDown(main, key, ascii, menuShiftState))
211 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
212 m_hide(); // disable menu on unhandled ESC
214 if(key >= K_MOUSE1 && key <= K_MOUSE3)
216 ++mouseButtonsPressed;
217 if(mouseButtonsPressed > 10)
219 mouseButtonsPressed = 10;
220 print("Warning: pressed an already pressed button\n");
223 if(key == K_ALT) menuShiftState |= S_ALT;
224 if(key == K_CTRL) menuShiftState |= S_CTRL;
225 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
228 float SCALEMODE_CROP = 0;
229 float SCALEMODE_LETTERBOX = 1;
230 float SCALEMODE_WIDTH = 2;
231 float SCALEMODE_HEIGHT = 3;
232 float SCALEMODE_STRETCH = 4;
233 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
235 vector sz, org, isz, isz_w, isz_h;
236 float width_is_larger;
238 sz = draw_PictureSize(img);
239 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
240 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
241 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
247 isz = (width_is_larger ? isz_h : isz_w);
249 case SCALEMODE_LETTERBOX:
250 isz = (width_is_larger ? isz_w : isz_h);
252 case SCALEMODE_WIDTH:
255 case SCALEMODE_HEIGHT:
258 case SCALEMODE_STRETCH:
263 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
264 draw_Picture(org, img, isz, '1 1 1', a);
267 void(string img, float a, string algn, float force1) drawBackground =
276 scalemode = SCALEMODE_CROP;
278 for(i = 0; i < strlen(algn); ++i)
280 c = substring(algn, i, 1);
283 case "c": scalemode = SCALEMODE_CROP; goto nopic;
284 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
285 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
286 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
287 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
288 case "1": case "4": case "7": v_x = 0.0; break;
289 case "2": case "5": case "8": v_x = 0.5; break;
290 case "3": case "6": case "9": v_x = 1.0; break;
291 default: v_x = random(); break;
295 case "7": case "8": case "9": v_y = 0.0; break;
296 case "4": case "5": case "6": v_y = 0.5; break;
297 case "1": case "2": case "3": v_y = 1.0; break;
298 default: v_y = random(); break;
301 draw_Picture_Aligned(v, scalemode, img, a);
303 // force all secondary layers to use alpha 1. Prevents ugly issues
304 // with overlap. It's a flag because it cannot be used for the
306 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
308 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
314 vector menuTooltipAveragedMousePos;
315 entity menuTooltipItem;
316 vector menuTooltipOrigin;
317 vector menuTooltipSize;
318 float menuTooltipAlpha;
319 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
320 float m_testmousetooltipbox(vector pos)
322 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
323 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
327 float m_testtooltipbox(vector tooltippos)
333 if(tooltippos_x + menuTooltipSize_x > 1)
335 if(tooltippos_y + menuTooltipSize_y > 1)
338 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
339 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
340 menuTooltipOrigin_z = 0;
342 menuTooltipOrigin = tooltippos;
345 float m_allocatetooltipbox(vector pos)
347 vector avoidplus, avoidminus;
350 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
351 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
354 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
355 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
360 if(m_testtooltipbox(v))
364 v_x = pos_x - menuTooltipSize_x * 0.5;
365 if(m_testtooltipbox(v))
369 v_x = pos_x - avoidminus_x;
370 if(m_testtooltipbox(v))
374 v_y = pos_y - avoidminus_y;
375 if(m_testtooltipbox(v))
379 v_x = pos_x - menuTooltipSize_x * 0.5;
380 if(m_testtooltipbox(v))
384 v_x = pos_x + avoidplus_x;
385 if(m_testtooltipbox(v))
390 entity m_findtooltipitem(entity root, vector pos)
398 while(it.instanceOfContainer)
400 while(it.instanceOfNexposee && it.focusedChild)
402 it = it.focusedChild;
403 pos = globalToBox(pos, it.Container_origin, it.Container_size);
405 if(it.instanceOfNexposee)
407 it = it.itemFromPoint(it, pos);
412 else if(it.instanceOfModalController)
413 it = it.focusedChild;
415 it = it.itemFromPoint(it, pos);
420 pos = globalToBox(pos, it.Container_origin, it.Container_size);
425 void m_tooltip(vector pos)
432 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
434 f = bound(0, frametime * 2, 1);
435 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
436 f = vlen(pos - menuTooltipAveragedMousePos);
439 it = m_findtooltipitem(main, pos);
443 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
444 if(it != menuTooltipItem)
446 switch(menuTooltipState)
451 // another item: fade out first
452 menuTooltipState = 2;
457 menuTooltipState = 1;
458 menuTooltipItem = it;
460 menuTooltipOrigin_x = -1; // unallocated
463 getWrappedLine_remaining = it.tooltip;
464 while(getWrappedLine_remaining)
466 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
468 f = draw_TextWidth(s, FALSE, fontsize);
472 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
473 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
474 menuTooltipSize_z = 0;
478 // changing item while fading in: fade out first
479 menuTooltipState = 2;
482 // changing item while fading out: can't
486 else if(menuTooltipState == 2) // re-fade in?
487 menuTooltipState = 1;
490 if(!m_testmousetooltipbox(pos))
491 menuTooltipState = 2; // fade out if mouse touches it
493 switch(menuTooltipState)
496 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
497 if(menuTooltipAlpha == 1)
498 menuTooltipState = 0;
501 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
502 if(menuTooltipAlpha == 0)
504 menuTooltipState = 0;
505 menuTooltipItem = world;
512 if(menuTooltipOrigin_x < 0) // unallocated?
513 m_allocatetooltipbox(pos);
515 if(menuTooltipOrigin_x >= 0)
518 p = SKINBORDER_TOOLTIP;
520 p_y *= 1 / conheight;
521 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
522 p = menuTooltipOrigin;
523 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
524 p_y += SKINMARGIN_TOOLTIP_y / conheight;
525 getWrappedLine_remaining = menuTooltipItem.tooltip;
526 while(getWrappedLine_remaining)
528 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
529 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
541 menuMouseMode = cvar("menu_mouse_absolute");
547 UpdateConWidthHeight();
551 // TODO draw an info image about this situation
555 if(!menuNotTheFirstFrame)
557 menuNotTheFirstFrame = 1;
559 if(!cvar("menu_video_played"))
561 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
562 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
567 realFrametime = frametime = min(0.2, t - menuPrevTime);
571 t = cvar("menu_slowmo");
582 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
583 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
588 if(cvar("cl_capturevideo"))
589 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
593 gamestatus = gamestatus | GAME_ISSERVER;
594 if(clientstate() == CS_CONNECTED)
595 gamestatus = gamestatus | GAME_CONNECTED;
596 if(cvar("developer"))
597 gamestatus = gamestatus | GAME_DEVELOPER;
599 prevMenuAlpha = menuAlpha;
602 if(menuAlpha == 0 && menuLogoAlpha < 2)
604 menuLogoAlpha = menuLogoAlpha + frametime * 2;
608 menuAlpha = min(1, menuAlpha + frametime * 5);
614 menuAlpha = max(0, menuAlpha - frametime * 5);
618 draw_reset_cropped();
620 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
622 if(menuLogoAlpha > 0)
625 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
626 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
627 draw_reset_cropped();
628 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
630 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
631 draw_drawMousePointer(menuMousePos);
636 else if(SKINALPHA_BACKGROUND_INGAME)
641 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
642 draw_reset_cropped();
646 if(menuAlpha != prevMenuAlpha)
647 cvar_set("_menu_alpha", ftos(menuAlpha));
649 draw_reset_cropped();
651 draw_reset_cropped();
655 if(prevMenuAlpha > 0)
656 main.initializeDialog(main, main.firstChild);
657 draw_reset_cropped();
662 draw_alpha *= menuAlpha;
667 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
668 if(newMouse != '0 0 0')
669 if(newMouse != menuMousePos)
671 menuMousePos = newMouse;
672 if(mouseButtonsPressed)
673 main.mouseDrag(main, menuMousePos);
675 main.mouseMove(main, menuMousePos);
682 vector dMouse, minpos, maxpos;
683 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
684 if(dMouse != '0 0 0')
686 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
687 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
688 dMouse = globalToBoxSize(dMouse, draw_scale);
689 menuMousePos += dMouse * cvar("menu_mouse_speed");
690 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
691 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
692 if(mouseButtonsPressed)
693 main.mouseDrag(main, menuMousePos);
695 main.mouseMove(main, menuMousePos);
701 m_tooltip(menuMousePos);
703 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
705 draw_drawMousePointer(menuMousePos);
707 draw_reset_cropped();
716 setkeydest(KEY_MENU);
717 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
722 if(mouseButtonsPressed)
723 main.mouseRelease(main, menuMousePos);
724 mouseButtonsPressed = 0;
726 main.focusEnter(main);
727 main.showNotify(main);
733 setkeydest(KEY_GAME);
734 setmousetarget(MT_CLIENT);
739 main.focusLeave(main);
740 main.hideNotify(main);
756 for(e = NULL; (e = nextent(e)) != NULL; )
763 void m_focus_item_chain(entity outermost, entity innermost)
765 if(innermost.parent != outermost)
766 m_focus_item_chain(outermost, innermost.parent);
767 innermost.parent.setFocus(innermost.parent, innermost);
770 void m_activate_window(entity wnd)
775 m_activate_window(par);
777 if(par.instanceOfModalController)
779 if(wnd.tabSelectingButton)
781 TabButton_Click(wnd.tabSelectingButton, wnd);
784 par.initializeDialog(par, wnd);
786 else if(par.instanceOfNexposee)
788 // nexposee (sorry for violating abstraction here)
789 par.selectedChild = wnd;
790 par.animationState = 1;
791 setFocusContainer(par, NULL);
793 else if(par.instanceOfContainer)
797 par.setFocus(par, wnd);
801 void m_setpointerfocus(entity wnd)
803 if(wnd.instanceOfContainer)
805 entity focus = wnd.preferredFocusedGrandChild(wnd);
808 menuMousePos = focus.origin + 0.5 * focus.size;
809 menuMousePos_x *= 1 / conwidth;
810 menuMousePos_y *= 1 / conheight;
811 if(wnd.focused) // why does this never happen?
812 m_focus_item_chain(wnd, focus);
817 void(string itemname) m_goto =
822 if(itemname == "") // this can be called by GameCommand
824 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
828 m_activate_window(main.mainNexposee);
834 e = findstring(NULL, name, itemname);
838 m_activate_window(e);
839 m_setpointerfocus(e);
845 void() m_goto_skin_selector =
849 // TODO add code to switch back to the skin selector (no idea how to do it now)
850 m_goto("skinselector");
853 void() m_goto_video_settings =
857 // TODO add code to switch back to the skin selector (no idea how to do it now)
858 m_goto("videosettings");