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();
27 void UpdateConWidthHeight()
29 float conwidth_s, conheight_s;
30 conwidth_s = conwidth;
31 conheight_s = conheight;
32 conwidth = cvar("vid_conwidth");
33 conheight = cvar("vid_conheight");
36 conheight *= 800 / conwidth;
41 conwidth *= 600 / conheight;
46 if(conwidth_s != conwidth || conheight_s != conheight)
49 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
54 void() m_init_delayed =
70 if(cvar_string("menu_skin") != "")
72 draw_currentSkin = strcat("gfx/menu/", cvar_string("menu_skin"));
73 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
76 if(cvar_defstring("menu_skin") != "")
78 draw_currentSkin = strcat("gfx/menu/", cvar_defstring("menu_skin"));
79 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
83 draw_currentSkin = "gfx/menu/default";
84 fh = fopen(strcat(draw_currentSkin, "/skinvalues.txt"), FILE_READ);
86 draw_currentSkin = strzone(draw_currentSkin);
87 while((s = fgets(fh)))
89 // these two are handled by skinlist.qc
90 if(substring(s, 0, 6) == "title ")
92 if(substring(s, 0, 7) == "author ")
94 n = tokenize_console(s);
96 Skin_ApplySetting(argv(0), substring(s, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
100 glob = search_begin(strcat(draw_currentSkin, "/*.tga"), TRUE, TRUE);
103 n = search_getsize(glob);
104 for(i = 0; i < n; ++i)
105 precache_pic(search_getfilename(glob, i));
109 draw_setMousePointer(SKINGFX_CURSOR, SKINSIZE_CURSOR, SKINOFFSET_CURSOR);
111 conwidth = conheight = -1;
113 UpdateConWidthHeight();
114 main = spawnMainWindow(); main.configureMainWindow(main);
115 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
118 menuMousePos = '0.5 0.5 0';
121 m_display(); // delayed menu display
124 void(float key, float ascii) m_keyup =
131 main.keyUp(main, key, ascii, menuShiftState);
132 if(key >= K_MOUSE1 && key <= K_MOUSE3)
134 --mouseButtonsPressed;
135 if(!mouseButtonsPressed)
136 main.mouseRelease(main, menuMousePos);
137 if(mouseButtonsPressed < 0)
139 mouseButtonsPressed = 0;
140 print("Warning: released an already released button\n");
143 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
144 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
145 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
148 void(float key, float ascii) m_keydown =
159 e.keyGrabbed(e, key, ascii);
164 if(key >= K_MOUSE1 && key <= K_MOUSE3)
165 if(!mouseButtonsPressed)
166 main.mousePress(main, menuMousePos);
167 if(!main.keyDown(main, key, ascii, menuShiftState))
169 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
170 m_hide(); // disable menu on unhandled ESC
172 if(key >= K_MOUSE1 && key <= K_MOUSE3)
174 ++mouseButtonsPressed;
175 if(mouseButtonsPressed > 10)
177 mouseButtonsPressed = 10;
178 print("Warning: pressed an already pressed button\n");
181 if(key == K_ALT) menuShiftState |= S_ALT;
182 if(key == K_CTRL) menuShiftState |= S_CTRL;
183 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
186 void(string img, float a) drawBackground =
190 sz = draw_PictureSize(img);
191 // keep aspect of image
192 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
194 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
195 // match up the height
197 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
201 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
202 // match up the width
204 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
206 draw_Picture('0.5 0.5 0' - 0.5 * isz, img, isz, '1 1 1', a);
210 vector menuTooltipAveragedMousePos;
211 entity menuTooltipItem;
212 vector menuTooltipOrigin;
213 vector menuTooltipSize;
214 float menuTooltipAlpha;
215 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
216 float m_testmousetooltipbox(vector pos)
218 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
219 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
223 float m_testtooltipbox(vector tooltippos)
229 if(tooltippos_x + menuTooltipSize_x > 1)
231 if(tooltippos_y + menuTooltipSize_y > 1)
233 menuTooltipOrigin = tooltippos;
236 float m_allocatetooltipbox(vector pos)
238 vector avoidplus, avoidminus;
241 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
242 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
245 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
246 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
251 if(m_testtooltipbox(v))
255 v_x = pos_x - menuTooltipSize_x * 0.5;
256 if(m_testtooltipbox(v))
260 v_x = pos_x - avoidminus_x;
261 if(m_testtooltipbox(v))
265 v_y = pos_y - avoidminus_y;
266 if(m_testtooltipbox(v))
270 v_x = pos_x - menuTooltipSize_x * 0.5;
271 if(m_testtooltipbox(v))
275 v_x = pos_x + avoidplus_x;
276 if(m_testtooltipbox(v))
281 entity m_findtooltipitem(entity root, vector pos)
289 while(it.instanceOfContainer)
291 if(it.instanceOfNexposee)
295 it = it.focusedChild;
299 it = it.itemFromPoint(it, pos);
305 else if(it.instanceOfModalController)
306 it = it.focusedChild;
308 it = it.itemFromPoint(it, pos);
311 pos = globalToBox(pos, it.Container_origin, it.Container_size);
316 void m_tooltip(vector pos)
323 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
325 f = bound(0, frametime * 2, 1);
326 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
327 f = vlen(pos - menuTooltipAveragedMousePos);
330 it = m_findtooltipitem(main, pos);
334 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
335 if(it != menuTooltipItem)
337 switch(menuTooltipState)
342 // another item: fade out first
343 menuTooltipState = 2;
348 menuTooltipState = 1;
349 menuTooltipItem = it;
351 menuTooltipOrigin_x = -1; // unallocated
354 getWrappedLine_remaining = it.tooltip;
355 while(getWrappedLine_remaining)
357 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
359 f = draw_TextWidth(s, FALSE);
363 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
364 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
365 menuTooltipSize_z = 0;
369 // changing item while fading in: fade out first
370 menuTooltipState = 2;
373 // changing item while fading out: can't
377 else if(menuTooltipState == 2) // re-fade in?
378 menuTooltipState = 1;
381 if(!m_testmousetooltipbox(pos))
382 menuTooltipState = 2; // fade out if mouse touches it
384 switch(menuTooltipState)
387 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
388 if(menuTooltipAlpha == 1)
389 menuTooltipState = 0;
392 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
393 if(menuTooltipAlpha == 0)
395 menuTooltipState = 0;
396 menuTooltipItem = world;
403 if(menuTooltipOrigin_x < 0) // unallocated?
404 m_allocatetooltipbox(pos);
406 if(menuTooltipOrigin_x >= 0)
409 p = SKINBORDER_TOOLTIP;
411 p_y *= 1 / conheight;
412 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
413 p = menuTooltipOrigin;
414 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
415 p_y += SKINMARGIN_TOOLTIP_y / conheight;
416 getWrappedLine_remaining = menuTooltipItem.tooltip;
417 while(getWrappedLine_remaining)
419 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
420 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
432 menuMouseMode = cvar("menu_mouse_absolute");
435 UpdateConWidthHeight();
439 // TODO draw an info image about this situation
443 if(!menuNotTheFirstFrame)
445 menuNotTheFirstFrame = 1;
447 if(!cvar("menu_video_played"))
449 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
450 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
455 realFrametime = frametime = min(0.2, t - menuPrevTime);
459 t = cvar("menu_slowmo");
470 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
471 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
476 if(cvar("cl_capturevideo"))
477 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
482 gamestatus = gamestatus | GAME_ISSERVER;
483 if(clientstate() == CS_CONNECTED)
484 gamestatus = gamestatus | GAME_CONNECTED;
485 if(cvar("developer"))
486 gamestatus = gamestatus | GAME_DEVELOPER;
488 prevMenuAlpha = menuAlpha;
491 if(menuAlpha == 0 && menuLogoAlpha < 2)
493 menuLogoAlpha = menuLogoAlpha + frametime * 2;
497 menuAlpha = min(1, menuAlpha + frametime * 5);
503 menuAlpha = max(0, menuAlpha - frametime * 5);
509 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
511 if(menuLogoAlpha > 0)
513 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1));
514 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
516 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
517 draw_drawMousePointer(menuMousePos);
522 else if(SKINALPHA_BACKGROUND_INGAME)
525 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME);
534 if(prevMenuAlpha > 0)
535 main.initializeDialog(main, main.firstChild);
541 draw_alpha *= menuAlpha;
546 newMouse = globalToBoxSize(getmousepos(), draw_scale);
547 if(newMouse != '0 0 0')
548 if(newMouse != menuMousePos)
550 menuMousePos = newMouse;
551 if(mouseButtonsPressed)
552 main.mouseDrag(main, menuMousePos);
554 main.mouseMove(main, menuMousePos);
562 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
563 if(dMouse != '0 0 0')
565 dMouse = globalToBoxSize(dMouse, draw_scale);
566 menuMousePos += dMouse * cvar("menu_mouse_speed");
567 menuMousePos_x = bound(0, menuMousePos_x, 1);
568 menuMousePos_y = bound(0, menuMousePos_y, 1);
569 if(mouseButtonsPressed)
570 main.mouseDrag(main, menuMousePos);
572 main.mouseMove(main, menuMousePos);
578 m_tooltip(menuMousePos);
580 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
582 draw_drawMousePointer(menuMousePos);
593 setkeydest(KEY_MENU);
594 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
599 if(mouseButtonsPressed)
600 main.mouseRelease(main, menuMousePos);
601 mouseButtonsPressed = 0;
603 main.focusEnter(main);
604 main.showNotify(main);
610 setkeydest(KEY_GAME);
611 setmousetarget(MT_CLIENT);
616 main.focusLeave(main);
617 main.hideNotify(main);
633 for(e = NULL; (e = nextent(e)) != NULL; )
640 void m_focus_item_chain(entity outermost, entity innermost)
642 if(innermost.parent != outermost)
643 m_focus_item_chain(outermost, innermost.parent);
644 innermost.parent.setFocus(innermost.parent, innermost);
647 void m_activate_window(entity wnd)
652 m_activate_window(par);
654 if(par.instanceOfModalController)
656 if(wnd.tabSelectingButton)
658 TabButton_Click(wnd.tabSelectingButton, wnd);
661 par.initializeDialog(par, wnd);
663 else if(par.instanceOfNexposee)
665 // nexposee (sorry for violating abstraction here)
666 par.selectedChild = wnd;
667 par.animationState = 1;
668 setFocusContainer(par, NULL);
670 else if(par.instanceOfContainer)
674 par.setFocus(par, wnd);
678 void m_setpointerfocus(entity wnd)
680 if(wnd.instanceOfContainer)
682 entity focus = wnd.preferredFocusedGrandChild(wnd);
685 menuMousePos = focus.origin + 0.5 * focus.size;
686 menuMousePos_x *= 1 / conwidth;
687 menuMousePos_y *= 1 / conheight;
688 if(wnd.focused) // why does this never happen?
689 m_focus_item_chain(wnd, focus);
694 void(string itemname) m_goto =
699 if(itemname == "") // this can be called by GameCommand
701 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
705 m_activate_window(main.mainNexposee);
711 e = findstring(NULL, name, itemname);
715 m_activate_window(e);
716 m_setpointerfocus(e);
722 void() m_goto_skin_selector =
726 // TODO add code to switch back to the skin selector (no idea how to do it now)
727 m_goto("skinselector");