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, float algn, float force1) drawBackground =
237 sz = draw_PictureSize(img);
238 // keep aspect of image
239 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
241 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
242 // match up the height
244 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
248 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
249 // match up the width
251 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
254 ce = '0.5 0.5 0' - 0.5 * isz;
259 for(i = 0; i < strlen(s); ++i)
261 f = stof(substring(s, i, 1));
264 case 1: case 4: case 7: v_x = tl_x; break;
265 case 2: case 5: case 8: v_x = ce_x; break;
266 case 3: case 6: case 9: v_x = br_x; break;
267 default: v_x = tl_x + (br_x - tl_x) * random(); break;
271 case 7: case 8: case 9: v_y = tl_y; break;
272 case 4: case 5: case 6: v_y = ce_y; break;
273 case 1: case 2: case 3: v_y = br_y; break;
274 default: v_y = tl_y + (br_y - tl_y) * random(); break;
277 draw_Picture(v, img, isz, '1 1 1', a);
279 // force all secondary layers to use alpha 1. Prevents ugly issues
280 // with overlap. It's a flag because it cannot be used for the
282 draw_Picture(v, strcat(img, "_l", ftos(i+1)), isz, '1 1 1', 1);
284 draw_Picture(v, strcat(img, "_l", ftos(i+1)), isz, '1 1 1', a);
288 vector menuTooltipAveragedMousePos;
289 entity menuTooltipItem;
290 vector menuTooltipOrigin;
291 vector menuTooltipSize;
292 float menuTooltipAlpha;
293 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
294 float m_testmousetooltipbox(vector pos)
296 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
297 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
301 float m_testtooltipbox(vector tooltippos)
307 if(tooltippos_x + menuTooltipSize_x > 1)
309 if(tooltippos_y + menuTooltipSize_y > 1)
312 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
313 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
314 menuTooltipOrigin_z = 0;
316 menuTooltipOrigin = tooltippos;
319 float m_allocatetooltipbox(vector pos)
321 vector avoidplus, avoidminus;
324 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
325 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
328 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
329 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
334 if(m_testtooltipbox(v))
338 v_x = pos_x - menuTooltipSize_x * 0.5;
339 if(m_testtooltipbox(v))
343 v_x = pos_x - avoidminus_x;
344 if(m_testtooltipbox(v))
348 v_y = pos_y - avoidminus_y;
349 if(m_testtooltipbox(v))
353 v_x = pos_x - menuTooltipSize_x * 0.5;
354 if(m_testtooltipbox(v))
358 v_x = pos_x + avoidplus_x;
359 if(m_testtooltipbox(v))
364 entity m_findtooltipitem(entity root, vector pos)
372 while(it.instanceOfContainer)
374 while(it.instanceOfNexposee && it.focusedChild)
376 it = it.focusedChild;
377 pos = globalToBox(pos, it.Container_origin, it.Container_size);
379 if(it.instanceOfNexposee)
381 it = it.itemFromPoint(it, pos);
386 else if(it.instanceOfModalController)
387 it = it.focusedChild;
389 it = it.itemFromPoint(it, pos);
394 pos = globalToBox(pos, it.Container_origin, it.Container_size);
399 void m_tooltip(vector pos)
406 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
408 f = bound(0, frametime * 2, 1);
409 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
410 f = vlen(pos - menuTooltipAveragedMousePos);
413 it = m_findtooltipitem(main, pos);
417 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
418 if(it != menuTooltipItem)
420 switch(menuTooltipState)
425 // another item: fade out first
426 menuTooltipState = 2;
431 menuTooltipState = 1;
432 menuTooltipItem = it;
434 menuTooltipOrigin_x = -1; // unallocated
437 getWrappedLine_remaining = it.tooltip;
438 while(getWrappedLine_remaining)
440 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
442 f = draw_TextWidth(s, FALSE);
446 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
447 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
448 menuTooltipSize_z = 0;
452 // changing item while fading in: fade out first
453 menuTooltipState = 2;
456 // changing item while fading out: can't
460 else if(menuTooltipState == 2) // re-fade in?
461 menuTooltipState = 1;
464 if(!m_testmousetooltipbox(pos))
465 menuTooltipState = 2; // fade out if mouse touches it
467 switch(menuTooltipState)
470 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
471 if(menuTooltipAlpha == 1)
472 menuTooltipState = 0;
475 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
476 if(menuTooltipAlpha == 0)
478 menuTooltipState = 0;
479 menuTooltipItem = world;
486 if(menuTooltipOrigin_x < 0) // unallocated?
487 m_allocatetooltipbox(pos);
489 if(menuTooltipOrigin_x >= 0)
492 p = SKINBORDER_TOOLTIP;
494 p_y *= 1 / conheight;
495 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
496 p = menuTooltipOrigin;
497 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
498 p_y += SKINMARGIN_TOOLTIP_y / conheight;
499 getWrappedLine_remaining = menuTooltipItem.tooltip;
500 while(getWrappedLine_remaining)
502 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
503 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
515 menuMouseMode = cvar("menu_mouse_absolute");
518 UpdateConWidthHeight();
522 // TODO draw an info image about this situation
526 if(!menuNotTheFirstFrame)
528 menuNotTheFirstFrame = 1;
530 if(!cvar("menu_video_played"))
532 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
533 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
538 realFrametime = frametime = min(0.2, t - menuPrevTime);
542 t = cvar("menu_slowmo");
553 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
554 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
559 if(cvar("cl_capturevideo"))
560 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
565 gamestatus = gamestatus | GAME_ISSERVER;
566 if(clientstate() == CS_CONNECTED)
567 gamestatus = gamestatus | GAME_CONNECTED;
568 if(cvar("developer"))
569 gamestatus = gamestatus | GAME_DEVELOPER;
571 prevMenuAlpha = menuAlpha;
574 if(menuAlpha == 0 && menuLogoAlpha < 2)
576 menuLogoAlpha = menuLogoAlpha + frametime * 2;
580 menuAlpha = min(1, menuAlpha + frametime * 5);
586 menuAlpha = max(0, menuAlpha - frametime * 5);
590 draw_reset_cropped();
592 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
594 if(menuLogoAlpha > 0)
597 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
598 draw_reset_cropped();
599 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
601 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
602 draw_drawMousePointer(menuMousePos);
607 else if(SKINALPHA_BACKGROUND_INGAME)
612 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
613 draw_reset_cropped();
617 if(menuAlpha != prevMenuAlpha)
618 cvar_set("_menu_alpha", ftos(menuAlpha));
620 draw_reset_cropped();
622 draw_reset_cropped();
626 if(prevMenuAlpha > 0)
627 main.initializeDialog(main, main.firstChild);
628 draw_reset_cropped();
633 draw_alpha *= menuAlpha;
638 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
639 if(newMouse != '0 0 0')
640 if(newMouse != menuMousePos)
642 menuMousePos = newMouse;
643 if(mouseButtonsPressed)
644 main.mouseDrag(main, menuMousePos);
646 main.mouseMove(main, menuMousePos);
654 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
655 if(dMouse != '0 0 0')
657 dMouse = globalToBoxSize(dMouse, draw_scale);
658 menuMousePos += dMouse * cvar("menu_mouse_speed");
659 menuMousePos_x = bound(0, menuMousePos_x, 1);
660 menuMousePos_y = bound(0, menuMousePos_y, 1);
661 if(mouseButtonsPressed)
662 main.mouseDrag(main, menuMousePos);
664 main.mouseMove(main, menuMousePos);
670 m_tooltip(menuMousePos);
672 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
674 draw_drawMousePointer(menuMousePos);
676 draw_reset_cropped();
685 setkeydest(KEY_MENU);
686 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
691 if(mouseButtonsPressed)
692 main.mouseRelease(main, menuMousePos);
693 mouseButtonsPressed = 0;
695 main.focusEnter(main);
696 main.showNotify(main);
702 setkeydest(KEY_GAME);
703 setmousetarget(MT_CLIENT);
708 main.focusLeave(main);
709 main.hideNotify(main);
725 for(e = NULL; (e = nextent(e)) != NULL; )
732 void m_focus_item_chain(entity outermost, entity innermost)
734 if(innermost.parent != outermost)
735 m_focus_item_chain(outermost, innermost.parent);
736 innermost.parent.setFocus(innermost.parent, innermost);
739 void m_activate_window(entity wnd)
744 m_activate_window(par);
746 if(par.instanceOfModalController)
748 if(wnd.tabSelectingButton)
750 TabButton_Click(wnd.tabSelectingButton, wnd);
753 par.initializeDialog(par, wnd);
755 else if(par.instanceOfNexposee)
757 // nexposee (sorry for violating abstraction here)
758 par.selectedChild = wnd;
759 par.animationState = 1;
760 setFocusContainer(par, NULL);
762 else if(par.instanceOfContainer)
766 par.setFocus(par, wnd);
770 void m_setpointerfocus(entity wnd)
772 if(wnd.instanceOfContainer)
774 entity focus = wnd.preferredFocusedGrandChild(wnd);
777 menuMousePos = focus.origin + 0.5 * focus.size;
778 menuMousePos_x *= 1 / conwidth;
779 menuMousePos_y *= 1 / conheight;
780 if(wnd.focused) // why does this never happen?
781 m_focus_item_chain(wnd, focus);
786 void(string itemname) m_goto =
791 if(itemname == "") // this can be called by GameCommand
793 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
797 m_activate_window(main.mainNexposee);
803 e = findstring(NULL, name, itemname);
807 m_activate_window(e);
808 m_setpointerfocus(e);
814 void() m_goto_skin_selector =
818 // TODO add code to switch back to the skin selector (no idea how to do it now)
819 m_goto("skinselector");
822 void() m_goto_video_settings =
826 // TODO add code to switch back to the skin selector (no idea how to do it now)
827 m_goto("videosettings");