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, float algn, float force1) drawBackground =
212 sz = draw_PictureSize(img);
213 // keep aspect of image
214 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
216 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
217 // match up the height
219 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
223 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
224 // match up the width
226 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
229 ce = '0.5 0.5 0' - 0.5 * isz;
234 for(i = 0; i < strlen(s); ++i)
236 f = stof(substring(s, i, 1));
239 case 1: case 4: case 7: v_x = tl_x; break;
240 case 2: case 5: case 8: v_x = ce_x; break;
241 case 3: case 6: case 9: v_x = br_x; break;
242 default: v_x = tl_x + (br_x - tl_x) * random(); break;
246 case 7: case 8: case 9: v_y = tl_y; break;
247 case 4: case 5: case 6: v_y = ce_y; break;
248 case 1: case 2: case 3: v_y = br_y; break;
249 default: v_y = tl_y + (br_y - tl_y) * random(); break;
252 draw_Picture(v, img, isz, '1 1 1', a);
254 // force all secondary layers to use alpha 1. Prevents ugly issues
255 // with overlap. It's a flag because it cannot be used for the
257 draw_Picture(v, strcat(img, "_l", ftos(i+1)), isz, '1 1 1', 1);
259 draw_Picture(v, strcat(img, "_l", ftos(i+1)), isz, '1 1 1', a);
263 vector menuTooltipAveragedMousePos;
264 entity menuTooltipItem;
265 vector menuTooltipOrigin;
266 vector menuTooltipSize;
267 float menuTooltipAlpha;
268 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
269 float m_testmousetooltipbox(vector pos)
271 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
272 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
276 float m_testtooltipbox(vector tooltippos)
282 if(tooltippos_x + menuTooltipSize_x > 1)
284 if(tooltippos_y + menuTooltipSize_y > 1)
287 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
288 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
289 menuTooltipOrigin_z = 0;
291 menuTooltipOrigin = tooltippos;
294 float m_allocatetooltipbox(vector pos)
296 vector avoidplus, avoidminus;
299 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
300 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
303 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
304 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
309 if(m_testtooltipbox(v))
313 v_x = pos_x - menuTooltipSize_x * 0.5;
314 if(m_testtooltipbox(v))
318 v_x = pos_x - avoidminus_x;
319 if(m_testtooltipbox(v))
323 v_y = pos_y - avoidminus_y;
324 if(m_testtooltipbox(v))
328 v_x = pos_x - menuTooltipSize_x * 0.5;
329 if(m_testtooltipbox(v))
333 v_x = pos_x + avoidplus_x;
334 if(m_testtooltipbox(v))
339 entity m_findtooltipitem(entity root, vector pos)
347 while(it.instanceOfContainer)
349 while(it.instanceOfNexposee && it.focusedChild)
351 it = it.focusedChild;
352 pos = globalToBox(pos, it.Container_origin, it.Container_size);
354 if(it.instanceOfNexposee)
356 it = it.itemFromPoint(it, pos);
361 else if(it.instanceOfModalController)
362 it = it.focusedChild;
364 it = it.itemFromPoint(it, pos);
369 pos = globalToBox(pos, it.Container_origin, it.Container_size);
374 void m_tooltip(vector pos)
381 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
383 f = bound(0, frametime * 2, 1);
384 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
385 f = vlen(pos - menuTooltipAveragedMousePos);
388 it = m_findtooltipitem(main, pos);
392 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
393 if(it != menuTooltipItem)
395 switch(menuTooltipState)
400 // another item: fade out first
401 menuTooltipState = 2;
406 menuTooltipState = 1;
407 menuTooltipItem = it;
409 menuTooltipOrigin_x = -1; // unallocated
412 getWrappedLine_remaining = it.tooltip;
413 while(getWrappedLine_remaining)
415 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
417 f = draw_TextWidth(s, FALSE);
421 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
422 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
423 menuTooltipSize_z = 0;
427 // changing item while fading in: fade out first
428 menuTooltipState = 2;
431 // changing item while fading out: can't
435 else if(menuTooltipState == 2) // re-fade in?
436 menuTooltipState = 1;
439 if(!m_testmousetooltipbox(pos))
440 menuTooltipState = 2; // fade out if mouse touches it
442 switch(menuTooltipState)
445 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
446 if(menuTooltipAlpha == 1)
447 menuTooltipState = 0;
450 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
451 if(menuTooltipAlpha == 0)
453 menuTooltipState = 0;
454 menuTooltipItem = world;
461 if(menuTooltipOrigin_x < 0) // unallocated?
462 m_allocatetooltipbox(pos);
464 if(menuTooltipOrigin_x >= 0)
467 p = SKINBORDER_TOOLTIP;
469 p_y *= 1 / conheight;
470 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
471 p = menuTooltipOrigin;
472 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
473 p_y += SKINMARGIN_TOOLTIP_y / conheight;
474 getWrappedLine_remaining = menuTooltipItem.tooltip;
475 while(getWrappedLine_remaining)
477 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
478 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
490 menuMouseMode = cvar("menu_mouse_absolute");
493 UpdateConWidthHeight();
497 // TODO draw an info image about this situation
501 if(!menuNotTheFirstFrame)
503 menuNotTheFirstFrame = 1;
505 if(!cvar("menu_video_played"))
507 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
508 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
513 realFrametime = frametime = min(0.2, t - menuPrevTime);
517 t = cvar("menu_slowmo");
528 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
529 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
534 if(cvar("cl_capturevideo"))
535 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
540 gamestatus = gamestatus | GAME_ISSERVER;
541 if(clientstate() == CS_CONNECTED)
542 gamestatus = gamestatus | GAME_CONNECTED;
543 if(cvar("developer"))
544 gamestatus = gamestatus | GAME_DEVELOPER;
546 prevMenuAlpha = menuAlpha;
549 if(menuAlpha == 0 && menuLogoAlpha < 2)
551 menuLogoAlpha = menuLogoAlpha + frametime * 2;
555 menuAlpha = min(1, menuAlpha + frametime * 5);
561 menuAlpha = max(0, menuAlpha - frametime * 5);
567 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
569 if(menuLogoAlpha > 0)
571 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1), SKINALIGN_BACKGROUND, TRUE);
572 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
574 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
575 draw_drawMousePointer(menuMousePos);
580 else if(SKINALPHA_BACKGROUND_INGAME)
583 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME, SKINALIGN_BACKGROUND_INGAME, FALSE);
592 if(prevMenuAlpha > 0)
593 main.initializeDialog(main, main.firstChild);
599 draw_alpha *= menuAlpha;
604 newMouse = globalToBoxSize(getmousepos(), draw_scale);
605 if(newMouse != '0 0 0')
606 if(newMouse != menuMousePos)
608 menuMousePos = newMouse;
609 if(mouseButtonsPressed)
610 main.mouseDrag(main, menuMousePos);
612 main.mouseMove(main, menuMousePos);
620 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
621 if(dMouse != '0 0 0')
623 dMouse = globalToBoxSize(dMouse, draw_scale);
624 menuMousePos += dMouse * cvar("menu_mouse_speed");
625 menuMousePos_x = bound(0, menuMousePos_x, 1);
626 menuMousePos_y = bound(0, menuMousePos_y, 1);
627 if(mouseButtonsPressed)
628 main.mouseDrag(main, menuMousePos);
630 main.mouseMove(main, menuMousePos);
636 m_tooltip(menuMousePos);
638 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
640 draw_drawMousePointer(menuMousePos);
651 setkeydest(KEY_MENU);
652 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
657 if(mouseButtonsPressed)
658 main.mouseRelease(main, menuMousePos);
659 mouseButtonsPressed = 0;
661 main.focusEnter(main);
662 main.showNotify(main);
668 setkeydest(KEY_GAME);
669 setmousetarget(MT_CLIENT);
674 main.focusLeave(main);
675 main.hideNotify(main);
691 for(e = NULL; (e = nextent(e)) != NULL; )
698 void m_focus_item_chain(entity outermost, entity innermost)
700 if(innermost.parent != outermost)
701 m_focus_item_chain(outermost, innermost.parent);
702 innermost.parent.setFocus(innermost.parent, innermost);
705 void m_activate_window(entity wnd)
710 m_activate_window(par);
712 if(par.instanceOfModalController)
714 if(wnd.tabSelectingButton)
716 TabButton_Click(wnd.tabSelectingButton, wnd);
719 par.initializeDialog(par, wnd);
721 else if(par.instanceOfNexposee)
723 // nexposee (sorry for violating abstraction here)
724 par.selectedChild = wnd;
725 par.animationState = 1;
726 setFocusContainer(par, NULL);
728 else if(par.instanceOfContainer)
732 par.setFocus(par, wnd);
736 void m_setpointerfocus(entity wnd)
738 if(wnd.instanceOfContainer)
740 entity focus = wnd.preferredFocusedGrandChild(wnd);
743 menuMousePos = focus.origin + 0.5 * focus.size;
744 menuMousePos_x *= 1 / conwidth;
745 menuMousePos_y *= 1 / conheight;
746 if(wnd.focused) // why does this never happen?
747 m_focus_item_chain(wnd, focus);
752 void(string itemname) m_goto =
757 if(itemname == "") // this can be called by GameCommand
759 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
763 m_activate_window(main.mainNexposee);
769 e = findstring(NULL, name, itemname);
773 m_activate_window(e);
774 m_setpointerfocus(e);
780 void() m_goto_skin_selector =
784 // TODO add code to switch back to the skin selector (no idea how to do it now)
785 m_goto("skinselector");