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;
241 sz = draw_PictureSize(img);
242 // keep aspect of image
243 width_is_larger = (sz_x * draw_scale_y >= sz_y * draw_scale_x);
245 isz_w = '1 0 0' + '0 1 0' * ((sz_y / sz_x) * (draw_scale_x / draw_scale_y));
246 isz_h = '0 1 0' + '1 0 0' * ((sz_x / sz_y) * (draw_scale_y / draw_scale_x));
256 for(i = 0; i < strlen(algn); ++i)
260 ce = 0.5 * (tl + br);
261 c = substring(algn, i, 1);
270 case "l": // letterbox
285 case "1": case "4": case "7": v_x = tl_x; break;
286 case "2": case "5": case "8": v_x = ce_x; break;
287 case "3": case "6": case "9": v_x = br_x; break;
288 default: v_x = tl_x + (br_x - tl_x) * random(); break;
292 case "7": case "8": case "9": v_y = tl_y; break;
293 case "4": case "5": case "6": v_y = ce_y; break;
294 case "1": case "2": case "3": v_y = br_y; break;
295 default: v_y = tl_y + (br_y - tl_y) * random(); break;
298 draw_Picture(v, img, isz, '1 1 1', a);
300 // force all secondary layers to use alpha 1. Prevents ugly issues
301 // with overlap. It's a flag because it cannot be used for the
303 draw_Picture(v, strcat(img, "_l", ftos(l+1)), isz, '1 1 1', 1);
305 draw_Picture(v, strcat(img, "_l", ftos(l+1)), isz, '1 1 1', a);
311 vector menuTooltipAveragedMousePos;
312 entity menuTooltipItem;
313 vector menuTooltipOrigin;
314 vector menuTooltipSize;
315 float menuTooltipAlpha;
316 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
317 float m_testmousetooltipbox(vector pos)
319 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
320 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
324 float m_testtooltipbox(vector tooltippos)
330 if(tooltippos_x + menuTooltipSize_x > 1)
332 if(tooltippos_y + menuTooltipSize_y > 1)
335 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
336 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
337 menuTooltipOrigin_z = 0;
339 menuTooltipOrigin = tooltippos;
342 float m_allocatetooltipbox(vector pos)
344 vector avoidplus, avoidminus;
347 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
348 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
351 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
352 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
357 if(m_testtooltipbox(v))
361 v_x = pos_x - menuTooltipSize_x * 0.5;
362 if(m_testtooltipbox(v))
366 v_x = pos_x - avoidminus_x;
367 if(m_testtooltipbox(v))
371 v_y = pos_y - avoidminus_y;
372 if(m_testtooltipbox(v))
376 v_x = pos_x - menuTooltipSize_x * 0.5;
377 if(m_testtooltipbox(v))
381 v_x = pos_x + avoidplus_x;
382 if(m_testtooltipbox(v))
387 entity m_findtooltipitem(entity root, vector pos)
395 while(it.instanceOfContainer)
397 while(it.instanceOfNexposee && it.focusedChild)
399 it = it.focusedChild;
400 pos = globalToBox(pos, it.Container_origin, it.Container_size);
402 if(it.instanceOfNexposee)
404 it = it.itemFromPoint(it, pos);
409 else if(it.instanceOfModalController)
410 it = it.focusedChild;
412 it = it.itemFromPoint(it, pos);
417 pos = globalToBox(pos, it.Container_origin, it.Container_size);
422 void m_tooltip(vector pos)
429 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
431 f = bound(0, frametime * 2, 1);
432 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
433 f = vlen(pos - menuTooltipAveragedMousePos);
436 it = m_findtooltipitem(main, pos);
440 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
441 if(it != menuTooltipItem)
443 switch(menuTooltipState)
448 // another item: fade out first
449 menuTooltipState = 2;
454 menuTooltipState = 1;
455 menuTooltipItem = it;
457 menuTooltipOrigin_x = -1; // unallocated
460 getWrappedLine_remaining = it.tooltip;
461 while(getWrappedLine_remaining)
463 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
465 f = draw_TextWidth(s, FALSE);
469 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
470 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
471 menuTooltipSize_z = 0;
475 // changing item while fading in: fade out first
476 menuTooltipState = 2;
479 // changing item while fading out: can't
483 else if(menuTooltipState == 2) // re-fade in?
484 menuTooltipState = 1;
487 if(!m_testmousetooltipbox(pos))
488 menuTooltipState = 2; // fade out if mouse touches it
490 switch(menuTooltipState)
493 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
494 if(menuTooltipAlpha == 1)
495 menuTooltipState = 0;
498 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
499 if(menuTooltipAlpha == 0)
501 menuTooltipState = 0;
502 menuTooltipItem = world;
509 if(menuTooltipOrigin_x < 0) // unallocated?
510 m_allocatetooltipbox(pos);
512 if(menuTooltipOrigin_x >= 0)
515 p = SKINBORDER_TOOLTIP;
517 p_y *= 1 / conheight;
518 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
519 p = menuTooltipOrigin;
520 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
521 p_y += SKINMARGIN_TOOLTIP_y / conheight;
522 getWrappedLine_remaining = menuTooltipItem.tooltip;
523 while(getWrappedLine_remaining)
525 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
526 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
538 menuMouseMode = cvar("menu_mouse_absolute");
541 UpdateConWidthHeight();
545 // TODO draw an info image about this situation
549 if(!menuNotTheFirstFrame)
551 menuNotTheFirstFrame = 1;
553 if(!cvar("menu_video_played"))
555 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
556 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
561 realFrametime = frametime = min(0.2, t - menuPrevTime);
565 t = cvar("menu_slowmo");
576 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
577 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
582 if(cvar("cl_capturevideo"))
583 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
588 gamestatus = gamestatus | GAME_ISSERVER;
589 if(clientstate() == CS_CONNECTED)
590 gamestatus = gamestatus | GAME_CONNECTED;
591 if(cvar("developer"))
592 gamestatus = gamestatus | GAME_DEVELOPER;
594 prevMenuAlpha = menuAlpha;
597 if(menuAlpha == 0 && menuLogoAlpha < 2)
599 menuLogoAlpha = menuLogoAlpha + frametime * 2;
603 menuAlpha = min(1, menuAlpha + frametime * 5);
609 menuAlpha = max(0, menuAlpha - frametime * 5);
613 draw_reset_cropped();
615 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
617 if(menuLogoAlpha > 0)
620 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
621 draw_reset_cropped();
622 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
624 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
625 draw_drawMousePointer(menuMousePos);
630 else if(SKINALPHA_BACKGROUND_INGAME)
635 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
636 draw_reset_cropped();
640 if(menuAlpha != prevMenuAlpha)
641 cvar_set("_menu_alpha", ftos(menuAlpha));
643 draw_reset_cropped();
645 draw_reset_cropped();
649 if(prevMenuAlpha > 0)
650 main.initializeDialog(main, main.firstChild);
651 draw_reset_cropped();
656 draw_alpha *= menuAlpha;
661 newMouse = globalToBox(getmousepos(), draw_shift, draw_scale);
662 if(newMouse != '0 0 0')
663 if(newMouse != menuMousePos)
665 menuMousePos = newMouse;
666 if(mouseButtonsPressed)
667 main.mouseDrag(main, menuMousePos);
669 main.mouseMove(main, menuMousePos);
677 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
678 if(dMouse != '0 0 0')
680 dMouse = globalToBoxSize(dMouse, draw_scale);
681 menuMousePos += dMouse * cvar("menu_mouse_speed");
682 menuMousePos_x = bound(0, menuMousePos_x, 1);
683 menuMousePos_y = bound(0, menuMousePos_y, 1);
684 if(mouseButtonsPressed)
685 main.mouseDrag(main, menuMousePos);
687 main.mouseMove(main, menuMousePos);
693 m_tooltip(menuMousePos);
695 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
697 draw_drawMousePointer(menuMousePos);
699 draw_reset_cropped();
708 setkeydest(KEY_MENU);
709 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
714 if(mouseButtonsPressed)
715 main.mouseRelease(main, menuMousePos);
716 mouseButtonsPressed = 0;
718 main.focusEnter(main);
719 main.showNotify(main);
725 setkeydest(KEY_GAME);
726 setmousetarget(MT_CLIENT);
731 main.focusLeave(main);
732 main.hideNotify(main);
748 for(e = NULL; (e = nextent(e)) != NULL; )
755 void m_focus_item_chain(entity outermost, entity innermost)
757 if(innermost.parent != outermost)
758 m_focus_item_chain(outermost, innermost.parent);
759 innermost.parent.setFocus(innermost.parent, innermost);
762 void m_activate_window(entity wnd)
767 m_activate_window(par);
769 if(par.instanceOfModalController)
771 if(wnd.tabSelectingButton)
773 TabButton_Click(wnd.tabSelectingButton, wnd);
776 par.initializeDialog(par, wnd);
778 else if(par.instanceOfNexposee)
780 // nexposee (sorry for violating abstraction here)
781 par.selectedChild = wnd;
782 par.animationState = 1;
783 setFocusContainer(par, NULL);
785 else if(par.instanceOfContainer)
789 par.setFocus(par, wnd);
793 void m_setpointerfocus(entity wnd)
795 if(wnd.instanceOfContainer)
797 entity focus = wnd.preferredFocusedGrandChild(wnd);
800 menuMousePos = focus.origin + 0.5 * focus.size;
801 menuMousePos_x *= 1 / conwidth;
802 menuMousePos_y *= 1 / conheight;
803 if(wnd.focused) // why does this never happen?
804 m_focus_item_chain(wnd, focus);
809 void(string itemname) m_goto =
814 if(itemname == "") // this can be called by GameCommand
816 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
820 m_activate_window(main.mainNexposee);
826 e = findstring(NULL, name, itemname);
830 m_activate_window(e);
831 m_setpointerfocus(e);
837 void() m_goto_skin_selector =
841 // TODO add code to switch back to the skin selector (no idea how to do it now)
842 m_goto("skinselector");
845 void() m_goto_video_settings =
849 // TODO add code to switch back to the skin selector (no idea how to do it now)
850 m_goto("videosettings");