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.2; // 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;
75 conheight *= 800 / conwidth;
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 =
109 if(cvar_string("menu_skin") != "")
111 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
112 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
115 if(cvar_defstring("menu_skin") != "")
117 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
118 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
122 draw_currentSkin = "gfx/menu/default";
123 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
125 draw_currentSkin = strzone(draw_currentSkin);
126 while((s = fgets(fh)))
128 // these two are handled by skinlist.qc
129 if(substring(s, 0, 6) == "title ")
131 if(substring(s, 0, 7) == "author ")
133 n = tokenize_console(s);
135 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
139 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
142 n = search_getsize(glob);
143 for(i = 0; i < n; ++i)
144 precache_pic(search_getfilename(glob, i));
148 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
150 conwidth = conheight = -1;
151 UpdateConWidthHeight();
152 draw_reset_cropped();
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 void(string img, float a, string algn, float force1) drawBackground =
232 float width_is_larger;
235 vector tl, ce, br, isz;
239 sz = draw_PictureSize(img);
240 // keep aspect of image
241 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
243 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
244 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
254 for(i = 0; i < strlen(algn); ++i)
258 ce = 0.5 * (tl + br);
259 c = substring(algn, i, 1);
268 case "l": // letterbox
283 case "1": case "4": case "7": v_x = tl_x; break;
284 case "2": case "5": case "8": v_x = ce_x; break;
285 case "3": case "6": case "9": v_x = br_x; break;
286 default: v_x = tl_x + (br_x - tl_x) * random(); break;
290 case "7": case "8": case "9": v_y = tl_y; break;
291 case "4": case "5": case "6": v_y = ce_y; break;
292 case "1": case "2": case "3": v_y = br_y; break;
293 default: v_y = tl_y + (br_y - tl_y) * random(); break;
296 draw_Picture(v, img, isz, '1 1 1', a);
298 // force all secondary layers to use alpha 1. Prevents ugly issues
299 // with overlap. It's a flag because it cannot be used for the
301 draw_Picture(v, strcat(img, "_l", ftos(l+1)), isz, '1 1 1', 1);
303 draw_Picture(v, strcat(img, "_l", ftos(l+1)), isz, '1 1 1', a);
309 vector menuTooltipAveragedMousePos;
310 entity menuTooltipItem;
311 vector menuTooltipOrigin;
312 vector menuTooltipSize;
313 float menuTooltipAlpha;
314 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
315 float m_testmousetooltipbox(vector pos)
317 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
318 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
322 float m_testtooltipbox(vector tooltippos)
328 if(tooltippos_x + menuTooltipSize_x > 1)
330 if(tooltippos_y + menuTooltipSize_y > 1)
333 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
334 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
335 menuTooltipOrigin_z = 0;
337 menuTooltipOrigin = tooltippos;
340 float m_allocatetooltipbox(vector pos)
342 vector avoidplus, avoidminus;
345 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
346 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
349 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
350 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
355 if(m_testtooltipbox(v))
359 v_x = pos_x - menuTooltipSize_x * 0.5;
360 if(m_testtooltipbox(v))
364 v_x = pos_x - avoidminus_x;
365 if(m_testtooltipbox(v))
369 v_y = pos_y - avoidminus_y;
370 if(m_testtooltipbox(v))
374 v_x = pos_x - menuTooltipSize_x * 0.5;
375 if(m_testtooltipbox(v))
379 v_x = pos_x + avoidplus_x;
380 if(m_testtooltipbox(v))
385 entity m_findtooltipitem(entity root, vector pos)
393 while(it.instanceOfContainer)
395 while(it.instanceOfNexposee && it.focusedChild)
397 it = it.focusedChild;
398 pos = globalToBox(pos, it.Container_origin, it.Container_size);
400 if(it.instanceOfNexposee)
402 it = it.itemFromPoint(it, pos);
407 else if(it.instanceOfModalController)
408 it = it.focusedChild;
410 it = it.itemFromPoint(it, pos);
415 pos = globalToBox(pos, it.Container_origin, it.Container_size);
420 void m_tooltip(vector pos)
427 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
429 f = bound(0, frametime * 2, 1);
430 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
431 f = vlen(pos - menuTooltipAveragedMousePos);
434 it = m_findtooltipitem(main, pos);
438 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
439 if(it != menuTooltipItem)
441 switch(menuTooltipState)
446 // another item: fade out first
447 menuTooltipState = 2;
452 menuTooltipState = 1;
453 menuTooltipItem = it;
455 menuTooltipOrigin_x = -1; // unallocated
458 getWrappedLine_remaining = it.tooltip;
459 while(getWrappedLine_remaining)
461 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
463 f = draw_TextWidth(s, FALSE);
467 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
468 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
469 menuTooltipSize_z = 0;
473 // changing item while fading in: fade out first
474 menuTooltipState = 2;
477 // changing item while fading out: can't
481 else if(menuTooltipState == 2) // re-fade in?
482 menuTooltipState = 1;
485 if(!m_testmousetooltipbox(pos))
486 menuTooltipState = 2; // fade out if mouse touches it
488 switch(menuTooltipState)
491 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
492 if(menuTooltipAlpha == 1)
493 menuTooltipState = 0;
496 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
497 if(menuTooltipAlpha == 0)
499 menuTooltipState = 0;
500 menuTooltipItem = world;
507 if(menuTooltipOrigin_x < 0) // unallocated?
508 m_allocatetooltipbox(pos);
510 if(menuTooltipOrigin_x >= 0)
513 p = SKINBORDER_TOOLTIP;
515 p_y *= 1 / conheight;
516 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
517 p = menuTooltipOrigin;
518 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
519 p_y += SKINMARGIN_TOOLTIP_y / conheight;
520 getWrappedLine_remaining = menuTooltipItem.tooltip;
521 while(getWrappedLine_remaining)
523 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
524 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
536 menuMouseMode = cvar("menu_mouse_absolute");
539 UpdateConWidthHeight();
543 // TODO draw an info image about this situation
547 if(!menuNotTheFirstFrame)
549 menuNotTheFirstFrame = 1;
551 if(!cvar("menu_video_played"))
553 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
554 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
559 realFrametime = frametime = min(0.2, t - menuPrevTime);
563 t = cvar("menu_slowmo");
574 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
575 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
580 if(cvar("cl_capturevideo"))
581 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
586 gamestatus = gamestatus | GAME_ISSERVER;
587 if(clientstate() == CS_CONNECTED)
588 gamestatus = gamestatus | GAME_CONNECTED;
589 if(cvar("developer"))
590 gamestatus = gamestatus | GAME_DEVELOPER;
592 prevMenuAlpha = menuAlpha;
595 if(menuAlpha == 0 && menuLogoAlpha < 2)
597 menuLogoAlpha = menuLogoAlpha + frametime * 2;
601 menuAlpha = min(1, menuAlpha + frametime * 5);
607 menuAlpha = max(0, menuAlpha - frametime * 5);
611 draw_reset_cropped();
613 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
615 if(menuLogoAlpha > 0)
618 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
619 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
620 draw_reset_cropped();
621 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
623 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
624 draw_drawMousePointer(menuMousePos);
629 else if(SKINALPHA_BACKGROUND_INGAME)
634 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
635 draw_reset_cropped();
639 if(menuAlpha != prevMenuAlpha)
640 cvar_set("_menu_alpha", ftos(menuAlpha));
642 draw_reset_cropped();
644 draw_reset_cropped();
648 if(prevMenuAlpha > 0)
649 main.initializeDialog(main, main.firstChild);
650 draw_reset_cropped();
655 draw_alpha *= menuAlpha;
660 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
661 if(newMouse != '0 0 0')
662 if(newMouse != menuMousePos)
664 menuMousePos = newMouse;
665 if(mouseButtonsPressed)
666 main.mouseDrag(main, menuMousePos);
668 main.mouseMove(main, menuMousePos);
676 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
677 if(dMouse != '0 0 0')
679 dMouse = globalToBoxSize(dMouse, draw_scale);
680 menuMousePos += dMouse * cvar("menu_mouse_speed");
681 menuMousePos_x = bound(0, menuMousePos_x, 1);
682 menuMousePos_y = bound(0, menuMousePos_y, 1);
683 if(mouseButtonsPressed)
684 main.mouseDrag(main, menuMousePos);
686 main.mouseMove(main, menuMousePos);
692 m_tooltip(menuMousePos);
694 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
696 draw_drawMousePointer(menuMousePos);
698 draw_reset_cropped();
707 setkeydest(KEY_MENU);
708 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
713 if(mouseButtonsPressed)
714 main.mouseRelease(main, menuMousePos);
715 mouseButtonsPressed = 0;
717 main.focusEnter(main);
718 main.showNotify(main);
724 setkeydest(KEY_GAME);
725 setmousetarget(MT_CLIENT);
730 main.focusLeave(main);
731 main.hideNotify(main);
747 for(e = NULL; (e = nextent(e)) != NULL; )
754 void m_focus_item_chain(entity outermost, entity innermost)
756 if(innermost.parent != outermost)
757 m_focus_item_chain(outermost, innermost.parent);
758 innermost.parent.setFocus(innermost.parent, innermost);
761 void m_activate_window(entity wnd)
766 m_activate_window(par);
768 if(par.instanceOfModalController)
770 if(wnd.tabSelectingButton)
772 TabButton_Click(wnd.tabSelectingButton, wnd);
775 par.initializeDialog(par, wnd);
777 else if(par.instanceOfNexposee)
779 // nexposee (sorry for violating abstraction here)
780 par.selectedChild = wnd;
781 par.animationState = 1;
782 setFocusContainer(par, NULL);
784 else if(par.instanceOfContainer)
788 par.setFocus(par, wnd);
792 void m_setpointerfocus(entity wnd)
794 if(wnd.instanceOfContainer)
796 entity focus = wnd.preferredFocusedGrandChild(wnd);
799 menuMousePos = focus.origin + 0.5 * focus.size;
800 menuMousePos_x *= 1 / conwidth;
801 menuMousePos_y *= 1 / conheight;
802 if(wnd.focused) // why does this never happen?
803 m_focus_item_chain(wnd, focus);
808 void(string itemname) m_goto =
813 if(itemname == "") // this can be called by GameCommand
815 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
819 m_activate_window(main.mainNexposee);
825 e = findstring(NULL, name, itemname);
829 m_activate_window(e);
830 m_setpointerfocus(e);
836 void() m_goto_skin_selector =
840 // TODO add code to switch back to the skin selector (no idea how to do it now)
841 m_goto("skinselector");
844 void() m_goto_video_settings =
848 // TODO add code to switch back to the skin selector (no idea how to do it now)
849 m_goto("videosettings");