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");
26 check_unacceptable_compiler_bugs();
28 // list all game dirs (TEST)
35 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
38 print(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
43 float MENU_ASPECT = 1.25; // 1280x1024
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 < 600 * MENU_ASPECT)
75 conheight *= 600 * MENU_ASPECT / conwidth;
76 conwidth = 600 * MENU_ASPECT;
80 conwidth *= 600 / conheight;
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 =
100 conwidth = conheight = -1;
101 UpdateConWidthHeight();
102 draw_reset_cropped();
113 if(cvar_string("menu_skin") != "")
115 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
116 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
119 if(cvar_defstring("menu_skin") != "")
121 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
122 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
126 draw_currentSkin = "gfx/menu/default";
127 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
129 draw_currentSkin = strzone(draw_currentSkin);
130 while((s = fgets(fh)))
132 // these two are handled by skinlist.qc
133 if(substring(s, 0, 6) == "title ")
135 if(substring(s, 0, 7) == "author ")
137 n = tokenize_console(s);
139 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
143 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
146 n = search_getsize(glob);
147 for(i = 0; i < n; ++i)
148 precache_pic(search_getfilename(glob, i));
152 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
155 main = spawnMainWindow(); main.configureMainWindow(main);
158 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
161 menuMousePos = '0.5 0.5 0';
164 m_display(); // delayed menu display
167 void(float key, float ascii) m_keyup =
173 draw_reset_cropped();
174 main.keyUp(main, key, ascii, menuShiftState);
175 if(key >= K_MOUSE1 && key <= K_MOUSE3)
177 --mouseButtonsPressed;
178 if(!mouseButtonsPressed)
179 main.mouseRelease(main, menuMousePos);
180 if(mouseButtonsPressed < 0)
182 mouseButtonsPressed = 0;
183 print("Warning: released an already released button\n");
186 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
187 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
188 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
191 void(float key, float ascii) m_keydown =
202 e.keyGrabbed(e, key, ascii);
206 draw_reset_cropped();
207 if(key >= K_MOUSE1 && key <= K_MOUSE3)
208 if(!mouseButtonsPressed)
209 main.mousePress(main, menuMousePos);
210 if(!main.keyDown(main, key, ascii, menuShiftState))
212 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
213 m_hide(); // disable menu on unhandled ESC
215 if(key >= K_MOUSE1 && key <= K_MOUSE3)
217 ++mouseButtonsPressed;
218 if(mouseButtonsPressed > 10)
220 mouseButtonsPressed = 10;
221 print("Warning: pressed an already pressed button\n");
224 if(key == K_ALT) menuShiftState |= S_ALT;
225 if(key == K_CTRL) menuShiftState |= S_CTRL;
226 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
229 float SCALEMODE_CROP = 0;
230 float SCALEMODE_LETTERBOX = 1;
231 float SCALEMODE_WIDTH = 2;
232 float SCALEMODE_HEIGHT = 3;
233 float SCALEMODE_STRETCH = 4;
234 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
236 vector sz, org, isz, isz_w, isz_h;
237 float width_is_larger;
239 sz = draw_PictureSize(img);
240 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
241 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
242 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
248 isz = (width_is_larger ? isz_h : isz_w);
250 case SCALEMODE_LETTERBOX:
251 isz = (width_is_larger ? isz_w : isz_h);
253 case SCALEMODE_WIDTH:
256 case SCALEMODE_HEIGHT:
259 case SCALEMODE_STRETCH:
264 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
265 draw_Picture(org, img, isz, '1 1 1', a);
268 void(string img, float a, string algn, float force1) drawBackground =
277 scalemode = SCALEMODE_CROP;
279 for(i = 0; i < strlen(algn); ++i)
281 c = substring(algn, i, 1);
284 case "c": scalemode = SCALEMODE_CROP; goto nopic;
285 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
286 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
287 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
288 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
289 case "1": case "4": case "7": v_x = 0.0; break;
290 case "2": case "5": case "8": v_x = 0.5; break;
291 case "3": case "6": case "9": v_x = 1.0; break;
292 default: v_x = random(); break;
296 case "7": case "8": case "9": v_y = 0.0; break;
297 case "4": case "5": case "6": v_y = 0.5; break;
298 case "1": case "2": case "3": v_y = 1.0; break;
299 default: v_y = random(); break;
302 draw_Picture_Aligned(v, scalemode, img, a);
304 // force all secondary layers to use alpha 1. Prevents ugly issues
305 // with overlap. It's a flag because it cannot be used for the
307 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
309 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
315 vector menuTooltipAveragedMousePos;
316 entity menuTooltipItem;
317 vector menuTooltipOrigin;
318 vector menuTooltipSize;
319 float menuTooltipAlpha;
320 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
321 float m_testmousetooltipbox(vector pos)
323 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
324 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
328 float m_testtooltipbox(vector tooltippos)
334 if(tooltippos_x + menuTooltipSize_x > 1)
336 if(tooltippos_y + menuTooltipSize_y > 1)
339 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
340 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
341 menuTooltipOrigin_z = 0;
343 menuTooltipOrigin = tooltippos;
346 float m_allocatetooltipbox(vector pos)
348 vector avoidplus, avoidminus;
351 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
352 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
355 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
356 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
361 if(m_testtooltipbox(v))
365 v_x = pos_x - menuTooltipSize_x * 0.5;
366 if(m_testtooltipbox(v))
370 v_x = pos_x - avoidminus_x;
371 if(m_testtooltipbox(v))
375 v_y = pos_y - avoidminus_y;
376 if(m_testtooltipbox(v))
380 v_x = pos_x - menuTooltipSize_x * 0.5;
381 if(m_testtooltipbox(v))
385 v_x = pos_x + avoidplus_x;
386 if(m_testtooltipbox(v))
391 entity m_findtooltipitem(entity root, vector pos)
399 while(it.instanceOfContainer)
401 while(it.instanceOfNexposee && it.focusedChild)
403 it = it.focusedChild;
404 pos = globalToBox(pos, it.Container_origin, it.Container_size);
406 if(it.instanceOfNexposee)
408 it = it.itemFromPoint(it, pos);
413 else if(it.instanceOfModalController)
414 it = it.focusedChild;
416 it = it.itemFromPoint(it, pos);
421 pos = globalToBox(pos, it.Container_origin, it.Container_size);
426 void m_tooltip(vector pos)
433 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
435 f = bound(0, frametime * 2, 1);
436 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
437 f = vlen(pos - menuTooltipAveragedMousePos);
440 it = m_findtooltipitem(main, pos);
444 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
445 if(it != menuTooltipItem)
447 switch(menuTooltipState)
452 // another item: fade out first
453 menuTooltipState = 2;
458 menuTooltipState = 1;
459 menuTooltipItem = it;
461 menuTooltipOrigin_x = -1; // unallocated
464 getWrappedLine_remaining = it.tooltip;
465 while(getWrappedLine_remaining)
467 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
469 f = draw_TextWidth(s, FALSE);
473 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
474 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
475 menuTooltipSize_z = 0;
479 // changing item while fading in: fade out first
480 menuTooltipState = 2;
483 // changing item while fading out: can't
487 else if(menuTooltipState == 2) // re-fade in?
488 menuTooltipState = 1;
491 if(!m_testmousetooltipbox(pos))
492 menuTooltipState = 2; // fade out if mouse touches it
494 switch(menuTooltipState)
497 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
498 if(menuTooltipAlpha == 1)
499 menuTooltipState = 0;
502 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
503 if(menuTooltipAlpha == 0)
505 menuTooltipState = 0;
506 menuTooltipItem = world;
513 if(menuTooltipOrigin_x < 0) // unallocated?
514 m_allocatetooltipbox(pos);
516 if(menuTooltipOrigin_x >= 0)
519 p = SKINBORDER_TOOLTIP;
521 p_y *= 1 / conheight;
522 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
523 p = menuTooltipOrigin;
524 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
525 p_y += SKINMARGIN_TOOLTIP_y / conheight;
526 getWrappedLine_remaining = menuTooltipItem.tooltip;
527 while(getWrappedLine_remaining)
529 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
530 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
542 menuMouseMode = cvar("menu_mouse_absolute");
545 UpdateConWidthHeight();
549 // TODO draw an info image about this situation
553 if(!menuNotTheFirstFrame)
555 menuNotTheFirstFrame = 1;
557 if(!cvar("menu_video_played"))
559 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
560 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
565 realFrametime = frametime = min(0.2, t - menuPrevTime);
569 t = cvar("menu_slowmo");
580 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
581 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
586 if(cvar("cl_capturevideo"))
587 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
592 gamestatus = gamestatus | GAME_ISSERVER;
593 if(clientstate() == CS_CONNECTED)
594 gamestatus = gamestatus | GAME_CONNECTED;
595 if(cvar("developer"))
596 gamestatus = gamestatus | GAME_DEVELOPER;
598 prevMenuAlpha = menuAlpha;
601 if(menuAlpha == 0 && menuLogoAlpha < 2)
603 menuLogoAlpha = menuLogoAlpha + frametime * 2;
607 menuAlpha = min(1, menuAlpha + frametime * 5);
613 menuAlpha = max(0, menuAlpha - frametime * 5);
617 draw_reset_cropped();
619 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
621 if(menuLogoAlpha > 0)
624 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
625 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
626 draw_reset_cropped();
627 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
629 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
630 draw_drawMousePointer(menuMousePos);
635 else if(SKINALPHA_BACKGROUND_INGAME)
640 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
641 draw_reset_cropped();
645 if(menuAlpha != prevMenuAlpha)
646 cvar_set("_menu_alpha", ftos(menuAlpha));
648 draw_reset_cropped();
650 draw_reset_cropped();
654 if(prevMenuAlpha > 0)
655 main.initializeDialog(main, main.firstChild);
656 draw_reset_cropped();
661 draw_alpha *= menuAlpha;
666 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
667 if(newMouse != '0 0 0')
668 if(newMouse != menuMousePos)
670 menuMousePos = newMouse;
671 if(mouseButtonsPressed)
672 main.mouseDrag(main, menuMousePos);
674 main.mouseMove(main, menuMousePos);
681 vector dMouse, minpos, maxpos;
682 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
683 if(dMouse != '0 0 0')
685 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
686 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
687 dMouse = globalToBoxSize(dMouse, draw_scale);
688 menuMousePos += dMouse * cvar("menu_mouse_speed");
689 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
690 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
691 if(mouseButtonsPressed)
692 main.mouseDrag(main, menuMousePos);
694 main.mouseMove(main, menuMousePos);
700 m_tooltip(menuMousePos);
702 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
704 draw_drawMousePointer(menuMousePos);
706 draw_reset_cropped();
715 setkeydest(KEY_MENU);
716 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
721 if(mouseButtonsPressed)
722 main.mouseRelease(main, menuMousePos);
723 mouseButtonsPressed = 0;
725 main.focusEnter(main);
726 main.showNotify(main);
732 setkeydest(KEY_GAME);
733 setmousetarget(MT_CLIENT);
738 main.focusLeave(main);
739 main.hideNotify(main);
755 for(e = NULL; (e = nextent(e)) != NULL; )
762 void m_focus_item_chain(entity outermost, entity innermost)
764 if(innermost.parent != outermost)
765 m_focus_item_chain(outermost, innermost.parent);
766 innermost.parent.setFocus(innermost.parent, innermost);
769 void m_activate_window(entity wnd)
774 m_activate_window(par);
776 if(par.instanceOfModalController)
778 if(wnd.tabSelectingButton)
780 TabButton_Click(wnd.tabSelectingButton, wnd);
783 par.initializeDialog(par, wnd);
785 else if(par.instanceOfNexposee)
787 // nexposee (sorry for violating abstraction here)
788 par.selectedChild = wnd;
789 par.animationState = 1;
790 setFocusContainer(par, NULL);
792 else if(par.instanceOfContainer)
796 par.setFocus(par, wnd);
800 void m_setpointerfocus(entity wnd)
802 if(wnd.instanceOfContainer)
804 entity focus = wnd.preferredFocusedGrandChild(wnd);
807 menuMousePos = focus.origin + 0.5 * focus.size;
808 menuMousePos_x *= 1 / conwidth;
809 menuMousePos_y *= 1 / conheight;
810 if(wnd.focused) // why does this never happen?
811 m_focus_item_chain(wnd, focus);
816 void(string itemname) m_goto =
821 if(itemname == "") // this can be called by GameCommand
823 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
827 m_activate_window(main.mainNexposee);
833 e = findstring(NULL, name, itemname);
837 m_activate_window(e);
838 m_setpointerfocus(e);
844 void() m_goto_skin_selector =
848 // TODO add code to switch back to the skin selector (no idea how to do it now)
849 m_goto("skinselector");
852 void() m_goto_video_settings =
856 // TODO add code to switch back to the skin selector (no idea how to do it now)
857 m_goto("videosettings");