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;
24 check_unacceptable_compiler_bugs();
26 // list all game dirs (TEST)
33 s = getgamedirinfo(i, GETGAMEDIRINFO_NAME);
36 print(s, ": ", getgamedirinfo(i, GETGAMEDIRINFO_DESCRIPTION));
41 void UpdateConWidthHeight()
43 float conwidth_s, conheight_s;
44 conwidth_s = conwidth;
45 conheight_s = conheight;
46 conwidth = cvar("vid_conwidth");
47 conheight = cvar("vid_conheight");
50 conheight *= 800 / conwidth;
55 conwidth *= 600 / conheight;
60 if(conwidth_s != conwidth || conheight_s != conheight)
63 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
68 void() m_init_delayed =
84 if(cvar_string("menu_skin") != "")
86 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
87 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
90 if(cvar_defstring("menu_skin") != "")
92 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
93 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
97 draw_currentSkin = "gfx/menu/default";
98 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
100 draw_currentSkin = strzone(draw_currentSkin);
101 while((s = fgets(fh)))
103 // these two are handled by skinlist.qc
104 if(substring(s, 0, 6) == "title ")
106 if(substring(s, 0, 7) == "author ")
108 n = tokenize_console(s);
110 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
114 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
117 n = search_getsize(glob);
118 for(i = 0; i < n; ++i)
119 precache_pic(search_getfilename(glob, i));
123 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
125 conwidth = conheight = -1;
127 UpdateConWidthHeight();
130 main = spawnMainWindow(); main.configureMainWindow(main);
133 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
136 menuMousePos = '0.5 0.5 0';
139 m_display(); // delayed menu display
142 void(float key, float ascii) m_keyup =
149 main.keyUp(main, key, ascii, menuShiftState);
150 if(key >= K_MOUSE1 && key <= K_MOUSE3)
152 --mouseButtonsPressed;
153 if(!mouseButtonsPressed)
154 main.mouseRelease(main, menuMousePos);
155 if(mouseButtonsPressed < 0)
157 mouseButtonsPressed = 0;
158 print("Warning: released an already released button\n");
161 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
162 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
163 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
166 void(float key, float ascii) m_keydown =
177 e.keyGrabbed(e, key, ascii);
182 if(key >= K_MOUSE1 && key <= K_MOUSE3)
183 if(!mouseButtonsPressed)
184 main.mousePress(main, menuMousePos);
185 if(!main.keyDown(main, key, ascii, menuShiftState))
187 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
188 m_hide(); // disable menu on unhandled ESC
190 if(key >= K_MOUSE1 && key <= K_MOUSE3)
192 ++mouseButtonsPressed;
193 if(mouseButtonsPressed > 10)
195 mouseButtonsPressed = 10;
196 print("Warning: pressed an already pressed button\n");
199 if(key == K_ALT) menuShiftState |= S_ALT;
200 if(key == K_CTRL) menuShiftState |= S_CTRL;
201 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
204 void(string img, float a) drawBackground =
208 sz = draw_PictureSize(img);
209 // keep aspect of image
210 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
212 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
213 // match up the height
215 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
219 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
220 // match up the width
222 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
224 draw_Picture('0.5 0.5 0' - 0.5 * isz, img, isz, '1 1 1', a);
228 vector menuTooltipAveragedMousePos;
229 entity menuTooltipItem;
230 vector menuTooltipOrigin;
231 vector menuTooltipSize;
232 float menuTooltipAlpha;
233 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
234 float m_testmousetooltipbox(vector pos)
236 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
237 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
241 float m_testtooltipbox(vector tooltippos)
247 if(tooltippos_x + menuTooltipSize_x > 1)
249 if(tooltippos_y + menuTooltipSize_y > 1)
252 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
253 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
254 menuTooltipOrigin_z = 0;
256 menuTooltipOrigin = tooltippos;
259 float m_allocatetooltipbox(vector pos)
261 vector avoidplus, avoidminus;
264 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
265 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
268 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
269 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
274 if(m_testtooltipbox(v))
278 v_x = pos_x - menuTooltipSize_x * 0.5;
279 if(m_testtooltipbox(v))
283 v_x = pos_x - avoidminus_x;
284 if(m_testtooltipbox(v))
288 v_y = pos_y - avoidminus_y;
289 if(m_testtooltipbox(v))
293 v_x = pos_x - menuTooltipSize_x * 0.5;
294 if(m_testtooltipbox(v))
298 v_x = pos_x + avoidplus_x;
299 if(m_testtooltipbox(v))
304 entity m_findtooltipitem(entity root, vector pos)
312 while(it.instanceOfContainer)
314 while(it.instanceOfNexposee && it.focusedChild)
316 it = it.focusedChild;
317 pos = globalToBox(pos, it.Container_origin, it.Container_size);
319 if(it.instanceOfNexposee)
321 it = it.itemFromPoint(it, pos);
326 else if(it.instanceOfModalController)
327 it = it.focusedChild;
329 it = it.itemFromPoint(it, pos);
334 pos = globalToBox(pos, it.Container_origin, it.Container_size);
339 void m_tooltip(vector pos)
346 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
348 f = bound(0, frametime * 2, 1);
349 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
350 f = vlen(pos - menuTooltipAveragedMousePos);
353 it = m_findtooltipitem(main, pos);
357 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
358 if(it != menuTooltipItem)
360 switch(menuTooltipState)
365 // another item: fade out first
366 menuTooltipState = 2;
371 menuTooltipState = 1;
372 menuTooltipItem = it;
374 menuTooltipOrigin_x = -1; // unallocated
377 getWrappedLine_remaining = it.tooltip;
378 while(getWrappedLine_remaining)
380 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
382 f = draw_TextWidth(s, FALSE);
386 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
387 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
388 menuTooltipSize_z = 0;
392 // changing item while fading in: fade out first
393 menuTooltipState = 2;
396 // changing item while fading out: can't
400 else if(menuTooltipState == 2) // re-fade in?
401 menuTooltipState = 1;
404 if(!m_testmousetooltipbox(pos))
405 menuTooltipState = 2; // fade out if mouse touches it
407 switch(menuTooltipState)
410 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
411 if(menuTooltipAlpha == 1)
412 menuTooltipState = 0;
415 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
416 if(menuTooltipAlpha == 0)
418 menuTooltipState = 0;
419 menuTooltipItem = world;
426 if(menuTooltipOrigin_x < 0) // unallocated?
427 m_allocatetooltipbox(pos);
429 if(menuTooltipOrigin_x >= 0)
432 p = SKINBORDER_TOOLTIP;
434 p_y *= 1 / conheight;
435 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
436 p = menuTooltipOrigin;
437 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
438 p_y += SKINMARGIN_TOOLTIP_y / conheight;
439 getWrappedLine_remaining = menuTooltipItem.tooltip;
440 while(getWrappedLine_remaining)
442 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
443 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
455 menuMouseMode = cvar("menu_mouse_absolute");
458 UpdateConWidthHeight();
462 // TODO draw an info image about this situation
466 if(!menuNotTheFirstFrame)
468 menuNotTheFirstFrame = 1;
470 if(!cvar("menu_video_played"))
472 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
473 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
478 realFrametime = frametime = min(0.2, t - menuPrevTime);
482 t = cvar("menu_slowmo");
493 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
494 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
499 if(cvar("cl_capturevideo"))
500 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
505 gamestatus = gamestatus | GAME_ISSERVER;
506 if(clientstate() == CS_CONNECTED)
507 gamestatus = gamestatus | GAME_CONNECTED;
508 if(cvar("developer"))
509 gamestatus = gamestatus | GAME_DEVELOPER;
511 prevMenuAlpha = menuAlpha;
514 if(menuAlpha == 0 && menuLogoAlpha < 2)
516 menuLogoAlpha = menuLogoAlpha + frametime * 2;
520 menuAlpha = min(1, menuAlpha + frametime * 5);
526 menuAlpha = max(0, menuAlpha - frametime * 5);
532 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
534 if(menuLogoAlpha > 0)
536 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1));
537 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
539 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
540 draw_drawMousePointer(menuMousePos);
545 else if(SKINALPHA_BACKGROUND_INGAME)
548 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME);
557 if(prevMenuAlpha > 0)
558 main.initializeDialog(main, main.firstChild);
564 draw_alpha *= menuAlpha;
569 newMouse = globalToBoxSize(getmousepos(), draw_scale);
570 if(newMouse != '0 0 0')
571 if(newMouse != menuMousePos)
573 menuMousePos = newMouse;
574 if(mouseButtonsPressed)
575 main.mouseDrag(main, menuMousePos);
577 main.mouseMove(main, menuMousePos);
585 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
586 if(dMouse != '0 0 0')
588 dMouse = globalToBoxSize(dMouse, draw_scale);
589 menuMousePos += dMouse * cvar("menu_mouse_speed");
590 menuMousePos_x = bound(0, menuMousePos_x, 1);
591 menuMousePos_y = bound(0, menuMousePos_y, 1);
592 if(mouseButtonsPressed)
593 main.mouseDrag(main, menuMousePos);
595 main.mouseMove(main, menuMousePos);
601 m_tooltip(menuMousePos);
603 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
605 draw_drawMousePointer(menuMousePos);
616 setkeydest(KEY_MENU);
617 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
622 if(mouseButtonsPressed)
623 main.mouseRelease(main, menuMousePos);
624 mouseButtonsPressed = 0;
626 main.focusEnter(main);
627 main.showNotify(main);
633 setkeydest(KEY_GAME);
634 setmousetarget(MT_CLIENT);
639 main.focusLeave(main);
640 main.hideNotify(main);
656 for(e = NULL; (e = nextent(e)) != NULL; )
663 void m_focus_item_chain(entity outermost, entity innermost)
665 if(innermost.parent != outermost)
666 m_focus_item_chain(outermost, innermost.parent);
667 innermost.parent.setFocus(innermost.parent, innermost);
670 void m_activate_window(entity wnd)
675 m_activate_window(par);
677 if(par.instanceOfModalController)
679 if(wnd.tabSelectingButton)
681 TabButton_Click(wnd.tabSelectingButton, wnd);
684 par.initializeDialog(par, wnd);
686 else if(par.instanceOfNexposee)
688 // nexposee (sorry for violating abstraction here)
689 par.selectedChild = wnd;
690 par.animationState = 1;
691 setFocusContainer(par, NULL);
693 else if(par.instanceOfContainer)
697 par.setFocus(par, wnd);
701 void m_setpointerfocus(entity wnd)
703 if(wnd.instanceOfContainer)
705 entity focus = wnd.preferredFocusedGrandChild(wnd);
708 menuMousePos = focus.origin + 0.5 * focus.size;
709 menuMousePos_x *= 1 / conwidth;
710 menuMousePos_y *= 1 / conheight;
711 if(wnd.focused) // why does this never happen?
712 m_focus_item_chain(wnd, focus);
717 void(string itemname) m_goto =
722 if(itemname == "") // this can be called by GameCommand
724 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
728 m_activate_window(main.mainNexposee);
734 e = findstring(NULL, name, itemname);
738 m_activate_window(e);
739 m_setpointerfocus(e);
745 void() m_goto_skin_selector =
749 // TODO add code to switch back to the skin selector (no idea how to do it now)
750 m_goto("skinselector");