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();
28 print("^4MQC Build information: ", WATERMARK(), "\n");
31 // list all game dirs (TEST)
38 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
41 print(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
46 float MENU_ASPECT = 1.25; // 1280x1024
47 float MENU_MINHEIGHT = 600;
48 float conwidth_s, conheight_s, realconwidth, realconheight, screenconwidth, screenconheight;
49 void draw_reset_cropped()
51 draw_reset(screenconwidth, screenconheight, 0.5 * (realconwidth - screenconwidth), 0.5 * (realconheight - screenconheight));
53 void draw_reset_full()
55 draw_reset(realconwidth, realconheight, 0, 0);
57 void UpdateConWidthHeight()
59 conwidth_s = conwidth;
60 conheight_s = conheight;
61 realconwidth = cvar("vid_conwidth");
62 realconheight = cvar("vid_conheight");
63 if(realconwidth / realconheight > MENU_ASPECT)
66 conwidth = realconheight * MENU_ASPECT;
67 conheight = realconheight;
72 conwidth = realconwidth;
73 conheight = realconwidth / MENU_ASPECT;
75 screenconwidth = conwidth;
76 screenconheight = conheight;
77 if(conwidth < MENU_MINHEIGHT * MENU_ASPECT)
79 conheight *= MENU_MINHEIGHT * MENU_ASPECT / conwidth;
80 conwidth = MENU_MINHEIGHT * MENU_ASPECT;
82 if(conheight < MENU_MINHEIGHT)
84 conwidth *= MENU_MINHEIGHT / conheight;
85 conheight = MENU_MINHEIGHT;
89 if(conwidth_s != conwidth || conheight_s != conheight)
92 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
97 void() m_init_delayed =
102 conwidth = conheight = -1;
103 UpdateConWidthHeight();
104 draw_reset_cropped();
115 if(cvar_string("menu_skin") != "")
117 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
118 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
121 if(cvar_defstring("menu_skin") != "")
123 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
124 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
128 draw_currentSkin = "gfx/menu/default";
129 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
131 draw_currentSkin = strzone(draw_currentSkin);
132 while((s = fgets(fh)))
134 // these two are handled by skinlist.qc
135 if(substring(s, 0, 6) == "title ")
137 if(substring(s, 0, 7) == "author ")
139 n = tokenize_console(s);
141 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
145 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
148 n = search_getsize(glob);
149 for(i = 0; i < n; ++i)
150 precache_pic(search_getfilename(glob, i));
154 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
157 anim = spawnAnimHost();
158 main = spawnMainWindow(); main.configureMainWindow(main);
161 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
164 menuMousePos = '0.5 0.5 0';
167 m_display(); // delayed menu display
170 void(float key, float ascii) m_keyup =
176 draw_reset_cropped();
177 main.keyUp(main, key, ascii, menuShiftState);
178 if(key >= K_MOUSE1 && key <= K_MOUSE3)
180 --mouseButtonsPressed;
181 if(!mouseButtonsPressed)
182 main.mouseRelease(main, menuMousePos);
183 if(mouseButtonsPressed < 0)
185 mouseButtonsPressed = 0;
186 print("Warning: released an already released button\n");
189 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
190 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
191 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
194 void(float key, float ascii) m_keydown =
205 e.keyGrabbed(e, key, ascii);
209 draw_reset_cropped();
210 if(key >= K_MOUSE1 && key <= K_MOUSE3)
211 if(!mouseButtonsPressed)
212 main.mousePress(main, menuMousePos);
213 if(!main.keyDown(main, key, ascii, menuShiftState))
215 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
216 m_hide(); // disable menu on unhandled ESC
218 if(key >= K_MOUSE1 && key <= K_MOUSE3)
220 ++mouseButtonsPressed;
221 if(mouseButtonsPressed > 10)
223 mouseButtonsPressed = 10;
224 print("Warning: pressed an already pressed button\n");
227 if(key == K_ALT) menuShiftState |= S_ALT;
228 if(key == K_CTRL) menuShiftState |= S_CTRL;
229 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
232 float SCALEMODE_CROP = 0;
233 float SCALEMODE_LETTERBOX = 1;
234 float SCALEMODE_WIDTH = 2;
235 float SCALEMODE_HEIGHT = 3;
236 float SCALEMODE_STRETCH = 4;
237 void draw_Picture_Aligned(vector algn, float scalemode, string img, float a)
239 vector sz, org, isz, isz_w, isz_h;
240 float width_is_larger;
242 sz = draw_PictureSize(img);
243 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
244 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
245 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
251 isz = (width_is_larger ? isz_h : isz_w);
253 case SCALEMODE_LETTERBOX:
254 isz = (width_is_larger ? isz_w : isz_h);
256 case SCALEMODE_WIDTH:
259 case SCALEMODE_HEIGHT:
262 case SCALEMODE_STRETCH:
267 org = eX * (algn_x * (1 - isz_x)) + eY * (algn_y * (1 - isz_y));
268 draw_Picture(org, img, isz, '1 1 1', a);
271 void(string img, float a, string algn, float force1) drawBackground =
273 if(main.mainNexposee.ModalController_state == 0)
283 scalemode = SCALEMODE_CROP;
285 for(i = 0; i < strlen(algn); ++i)
287 c = substring(algn, i, 1);
290 case "c": scalemode = SCALEMODE_CROP; goto nopic;
291 case "l": scalemode = SCALEMODE_LETTERBOX; goto nopic;
292 case "h": scalemode = SCALEMODE_HEIGHT; goto nopic;
293 case "w": scalemode = SCALEMODE_WIDTH; goto nopic;
294 case "s": scalemode = SCALEMODE_STRETCH; goto nopic;
295 case "1": case "4": case "7": v_x = 0.0; break;
296 case "2": case "5": case "8": v_x = 0.5; break;
297 case "3": case "6": case "9": v_x = 1.0; break;
298 default: v_x = random(); break;
302 case "7": case "8": case "9": v_y = 0.0; break;
303 case "4": case "5": case "6": v_y = 0.5; break;
304 case "1": case "2": case "3": v_y = 1.0; break;
305 default: v_y = random(); break;
308 draw_Picture_Aligned(v, scalemode, img, a);
310 // force all secondary layers to use alpha 1. Prevents ugly issues
311 // with overlap. It's a flag because it cannot be used for the
313 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), 1);
315 draw_Picture_Aligned(v, scalemode, strcat(img, "_l", ftos(l+1)), a);
321 vector menuTooltipAveragedMousePos;
322 entity menuTooltipItem;
323 vector menuTooltipOrigin;
324 vector menuTooltipSize;
325 float menuTooltipAlpha;
326 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
327 float m_testmousetooltipbox(vector pos)
329 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
330 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
334 float m_testtooltipbox(vector tooltippos)
340 if(tooltippos_x + menuTooltipSize_x > 1)
342 if(tooltippos_y + menuTooltipSize_y > 1)
345 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
346 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
347 menuTooltipOrigin_z = 0;
349 menuTooltipOrigin = tooltippos;
352 float m_allocatetooltipbox(vector pos)
354 vector avoidplus, avoidminus;
357 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
358 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
361 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
362 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
367 if(m_testtooltipbox(v))
371 v_x = pos_x - menuTooltipSize_x * 0.5;
372 if(m_testtooltipbox(v))
376 v_x = pos_x - avoidminus_x;
377 if(m_testtooltipbox(v))
381 v_y = pos_y - avoidminus_y;
382 if(m_testtooltipbox(v))
386 v_x = pos_x - menuTooltipSize_x * 0.5;
387 if(m_testtooltipbox(v))
391 v_x = pos_x + avoidplus_x;
392 if(m_testtooltipbox(v))
397 entity m_findtooltipitem(entity root, vector pos)
405 while(it.instanceOfContainer)
407 while(it.instanceOfNexposee && it.focusedChild)
409 it = it.focusedChild;
410 pos = globalToBox(pos, it.Container_origin, it.Container_size);
412 if(it.instanceOfNexposee)
414 it = it.itemFromPoint(it, pos);
419 else if(it.instanceOfModalController)
420 it = it.focusedChild;
422 it = it.itemFromPoint(it, pos);
427 pos = globalToBox(pos, it.Container_origin, it.Container_size);
432 void m_tooltip(vector pos)
439 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
441 f = bound(0, frametime * 2, 1);
442 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
443 f = vlen(pos - menuTooltipAveragedMousePos);
446 it = m_findtooltipitem(main, pos);
450 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
451 if(it != menuTooltipItem)
453 switch(menuTooltipState)
458 // another item: fade out first
459 menuTooltipState = 2;
464 menuTooltipState = 1;
465 menuTooltipItem = it;
467 menuTooltipOrigin_x = -1; // unallocated
470 getWrappedLine_remaining = it.tooltip;
471 while(getWrappedLine_remaining)
473 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
475 f = draw_TextWidth(s, FALSE, fontsize);
479 menuTooltipSize_x = w + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
480 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
481 menuTooltipSize_z = 0;
485 // changing item while fading in: fade out first
486 menuTooltipState = 2;
489 // changing item while fading out: can't
493 else if(menuTooltipState == 2) // re-fade in?
494 menuTooltipState = 1;
497 if(!m_testmousetooltipbox(pos))
498 menuTooltipState = 2; // fade out if mouse touches it
500 switch(menuTooltipState)
503 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
504 if(menuTooltipAlpha == 1)
505 menuTooltipState = 0;
508 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
509 if(menuTooltipAlpha == 0)
511 menuTooltipState = 0;
512 menuTooltipItem = world;
519 if(menuTooltipOrigin_x < 0) // unallocated?
520 m_allocatetooltipbox(pos);
522 if(menuTooltipOrigin_x >= 0)
525 p = SKINBORDER_TOOLTIP;
527 p_y *= 1 / conheight;
528 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
529 p = menuTooltipOrigin;
530 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
531 p_y += SKINMARGIN_TOOLTIP_y / conheight;
532 getWrappedLine_remaining = menuTooltipItem.tooltip;
533 while(getWrappedLine_remaining)
535 s = getWrappedLine(SKINWIDTH_TOOLTIP, fontsize, draw_TextWidth_WithoutColors);
536 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
548 menuMouseMode = cvar("menu_mouse_absolute");
554 UpdateConWidthHeight();
558 // TODO draw an info image about this situation
562 if(!menuNotTheFirstFrame)
564 menuNotTheFirstFrame = 1;
566 if(!cvar("menu_video_played"))
568 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/default/welcome.ogg\n");
569 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
574 realFrametime = frametime = min(0.2, t - menuPrevTime);
578 t = cvar("menu_slowmo");
589 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
590 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
595 if(cvar("cl_capturevideo"))
596 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
600 gamestatus = gamestatus | GAME_ISSERVER;
601 if(clientstate() == CS_CONNECTED)
602 gamestatus = gamestatus | GAME_CONNECTED;
603 if(cvar("developer"))
604 gamestatus = gamestatus | GAME_DEVELOPER;
606 prevMenuAlpha = menuAlpha;
609 if(menuAlpha == 0 && menuLogoAlpha < 2)
611 menuLogoAlpha = menuLogoAlpha + frametime * 2;
615 menuAlpha = min(1, menuAlpha + frametime * 5);
621 menuAlpha = max(0, menuAlpha - frametime * 5);
625 draw_reset_cropped();
627 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
629 if(menuLogoAlpha > 0)
632 draw_Fill('0 0 0', '1 1 0', SKINCOLOR_BACKGROUND, 1);
633 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
634 draw_reset_cropped();
635 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
637 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
638 draw_drawMousePointer(menuMousePos);
643 else if(SKINALPHA_BACKGROUND_INGAME)
648 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
649 draw_reset_cropped();
653 if(menuAlpha != prevMenuAlpha)
654 cvar_set("_menu_alpha", ftos(menuAlpha));
656 draw_reset_cropped();
658 draw_reset_cropped();
662 if(prevMenuAlpha > 0)
663 main.initializeDialog(main, main.firstChild);
664 draw_reset_cropped();
669 draw_alpha *= menuAlpha;
674 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
675 if(newMouse != '0 0 0')
676 if(newMouse != menuMousePos)
678 menuMousePos = newMouse;
679 if(mouseButtonsPressed)
680 main.mouseDrag(main, menuMousePos);
682 main.mouseMove(main, menuMousePos);
689 vector dMouse, minpos, maxpos;
690 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
691 if(dMouse != '0 0 0')
693 minpos = globalToBox('0 0 0', draw_shift, draw_scale);
694 maxpos = globalToBox(eX * (realconwidth - 1) + eY * (realconheight - 1), draw_shift, draw_scale);
695 dMouse = globalToBoxSize(dMouse, draw_scale);
696 menuMousePos += dMouse * cvar("menu_mouse_speed");
697 menuMousePos_x = bound(minpos_x, menuMousePos_x, maxpos_x);
698 menuMousePos_y = bound(minpos_y, menuMousePos_y, maxpos_y);
699 if(mouseButtonsPressed)
700 main.mouseDrag(main, menuMousePos);
702 main.mouseMove(main, menuMousePos);
708 m_tooltip(menuMousePos);
710 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
712 draw_drawMousePointer(menuMousePos);
714 draw_reset_cropped();
723 setkeydest(KEY_MENU);
724 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
729 if(mouseButtonsPressed)
730 main.mouseRelease(main, menuMousePos);
731 mouseButtonsPressed = 0;
733 main.focusEnter(main);
734 main.showNotify(main);
740 setkeydest(KEY_GAME);
741 setmousetarget(MT_CLIENT);
746 main.focusLeave(main);
747 main.hideNotify(main);
763 for(e = NULL; (e = nextent(e)) != NULL; )
765 if(e.classname != "vtbl")
771 void m_focus_item_chain(entity outermost, entity innermost)
773 if(innermost.parent != outermost)
774 m_focus_item_chain(outermost, innermost.parent);
775 innermost.parent.setFocus(innermost.parent, innermost);
778 void m_activate_window(entity wnd)
783 m_activate_window(par);
785 if(par.instanceOfModalController)
787 if(wnd.tabSelectingButton)
789 TabButton_Click(wnd.tabSelectingButton, wnd);
792 par.initializeDialog(par, wnd);
794 else if(par.instanceOfNexposee)
796 // nexposee (sorry for violating abstraction here)
797 par.selectedChild = wnd;
798 par.animationState = 1;
799 Container_setFocus(par, NULL);
801 else if(par.instanceOfContainer)
805 par.setFocus(par, wnd);
809 void m_setpointerfocus(entity wnd)
811 if(wnd.instanceOfContainer)
813 entity focus = wnd.preferredFocusedGrandChild(wnd);
816 menuMousePos = focus.origin + 0.5 * focus.size;
817 menuMousePos_x *= 1 / conwidth;
818 menuMousePos_y *= 1 / conheight;
819 if(wnd.focused) // why does this never happen?
820 m_focus_item_chain(wnd, focus);
825 void(string itemname) m_goto =
830 if(itemname == "") // this can be called by GameCommand
832 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
836 m_activate_window(main.mainNexposee);
842 e = findstring(NULL, name, itemname);
846 m_activate_window(e);
847 m_setpointerfocus(e);
853 void() m_goto_skin_selector =
857 // TODO add code to switch back to the skin selector (no idea how to do it now)
858 m_goto("skinselector");
861 void() m_goto_video_settings =
865 // TODO add code to switch back to the skin selector (no idea how to do it now)
866 m_goto("videosettings");