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();
116 main = spawnMainWindow(); main.configureMainWindow(main);
119 main.resizeNotify(main, '0 0 0', eX * conwidth + eY * conheight, '0 0 0', eX * conwidth + eY * conheight);
122 menuMousePos = '0.5 0.5 0';
125 m_display(); // delayed menu display
128 void(float key, float ascii) m_keyup =
135 main.keyUp(main, key, ascii, menuShiftState);
136 if(key >= K_MOUSE1 && key <= K_MOUSE3)
138 --mouseButtonsPressed;
139 if(!mouseButtonsPressed)
140 main.mouseRelease(main, menuMousePos);
141 if(mouseButtonsPressed < 0)
143 mouseButtonsPressed = 0;
144 print("Warning: released an already released button\n");
147 if(key == K_ALT) menuShiftState -= (menuShiftState & S_ALT);
148 if(key == K_CTRL) menuShiftState -= (menuShiftState & S_CTRL);
149 if(key == K_SHIFT) menuShiftState -= (menuShiftState & S_SHIFT);
152 void(float key, float ascii) m_keydown =
163 e.keyGrabbed(e, key, ascii);
168 if(key >= K_MOUSE1 && key <= K_MOUSE3)
169 if(!mouseButtonsPressed)
170 main.mousePress(main, menuMousePos);
171 if(!main.keyDown(main, key, ascii, menuShiftState))
173 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED)) // don't back out to console only
174 m_hide(); // disable menu on unhandled ESC
176 if(key >= K_MOUSE1 && key <= K_MOUSE3)
178 ++mouseButtonsPressed;
179 if(mouseButtonsPressed > 10)
181 mouseButtonsPressed = 10;
182 print("Warning: pressed an already pressed button\n");
185 if(key == K_ALT) menuShiftState |= S_ALT;
186 if(key == K_CTRL) menuShiftState |= S_CTRL;
187 if(key == K_SHIFT) menuShiftState |= S_SHIFT;
190 void(string img, float a) drawBackground =
194 sz = draw_PictureSize(img);
195 // keep aspect of image
196 if(sz_x * draw_scale_y >= sz_y * draw_scale_x)
198 // that is, sz_x/sz_y >= draw_scale_x/draw_scale_y
199 // match up the height
201 isz_x = isz_y * (sz_x / sz_y) * (draw_scale_y / draw_scale_x);
205 // that is, sz_x/sz_y <= draw_scale_x/draw_scale_y
206 // match up the width
208 isz_y = isz_x * (sz_y / sz_x) * (draw_scale_x / draw_scale_y);
210 draw_Picture('0.5 0.5 0' - 0.5 * isz, img, isz, '1 1 1', a);
214 vector menuTooltipAveragedMousePos;
215 entity menuTooltipItem;
216 vector menuTooltipOrigin;
217 vector menuTooltipSize;
218 float menuTooltipAlpha;
219 float menuTooltipState; // 0: no tooltip, 1: fading in, 2: displaying, 3: fading out
220 float m_testmousetooltipbox(vector pos)
222 if(pos_x >= menuTooltipOrigin_x && pos_x < menuTooltipOrigin_x + menuTooltipSize_x)
223 if(pos_y >= menuTooltipOrigin_y && pos_y < menuTooltipOrigin_y + menuTooltipSize_y)
227 float m_testtooltipbox(vector tooltippos)
233 if(tooltippos_x + menuTooltipSize_x > 1)
235 if(tooltippos_y + menuTooltipSize_y > 1)
238 menuTooltipOrigin_x = rint(tooltippos_x * cvar("vid_width")) / cvar("vid_width");
239 menuTooltipOrigin_y = rint(tooltippos_y * cvar("vid_height")) / cvar("vid_height");
240 menuTooltipOrigin_z = 0;
242 menuTooltipOrigin = tooltippos;
245 float m_allocatetooltipbox(vector pos)
247 vector avoidplus, avoidminus;
250 avoidplus_x = (SKINAVOID_TOOLTIP_x + SKINSIZE_CURSOR_x - SKINOFFSET_CURSOR_x) / conwidth;
251 avoidplus_y = (SKINAVOID_TOOLTIP_y + SKINSIZE_CURSOR_y - SKINOFFSET_CURSOR_y) / conheight;
254 avoidminus_x = (SKINAVOID_TOOLTIP_x + SKINOFFSET_CURSOR_x) / conwidth + menuTooltipSize_x;
255 avoidminus_y = (SKINAVOID_TOOLTIP_y + SKINOFFSET_CURSOR_y) / conheight + menuTooltipSize_y;
260 if(m_testtooltipbox(v))
264 v_x = pos_x - menuTooltipSize_x * 0.5;
265 if(m_testtooltipbox(v))
269 v_x = pos_x - avoidminus_x;
270 if(m_testtooltipbox(v))
274 v_y = pos_y - avoidminus_y;
275 if(m_testtooltipbox(v))
279 v_x = pos_x - menuTooltipSize_x * 0.5;
280 if(m_testtooltipbox(v))
284 v_x = pos_x + avoidplus_x;
285 if(m_testtooltipbox(v))
290 entity m_findtooltipitem(entity root, vector pos)
298 while(it.instanceOfContainer)
300 if(it.instanceOfNexposee)
304 it = it.focusedChild;
305 pos = globalToBox(pos, it.Container_origin, it.Container_size);
306 continue; // don't show dialog tooltips when the dialog is open
310 it = it.itemFromPoint(it, pos);
316 else if(it.instanceOfModalController)
317 it = it.focusedChild;
319 it = it.itemFromPoint(it, pos);
324 pos = globalToBox(pos, it.Container_origin, it.Container_size);
329 void m_tooltip(vector pos)
336 fontsize = '1 0 0' * (SKINFONTSIZE_TOOLTIP / conwidth) + '0 1 0' * (SKINFONTSIZE_TOOLTIP / conheight);
338 f = bound(0, frametime * 2, 1);
339 menuTooltipAveragedMousePos = menuTooltipAveragedMousePos * (1 - f) + pos * f;
340 f = vlen(pos - menuTooltipAveragedMousePos);
343 it = m_findtooltipitem(main, pos);
347 // float menuTooltipState; // 0: static, 1: fading in, 2: fading out
348 if(it != menuTooltipItem)
350 switch(menuTooltipState)
355 // another item: fade out first
356 menuTooltipState = 2;
361 menuTooltipState = 1;
362 menuTooltipItem = it;
364 menuTooltipOrigin_x = -1; // unallocated
367 getWrappedLine_remaining = it.tooltip;
368 while(getWrappedLine_remaining)
370 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
372 f = draw_TextWidth(s, FALSE);
376 menuTooltipSize_x = w * fontsize_x + 2 * (SKINMARGIN_TOOLTIP_x / conwidth);
377 menuTooltipSize_y = i * fontsize_y + 2 * (SKINMARGIN_TOOLTIP_y / conheight);
378 menuTooltipSize_z = 0;
382 // changing item while fading in: fade out first
383 menuTooltipState = 2;
386 // changing item while fading out: can't
390 else if(menuTooltipState == 2) // re-fade in?
391 menuTooltipState = 1;
394 if(!m_testmousetooltipbox(pos))
395 menuTooltipState = 2; // fade out if mouse touches it
397 switch(menuTooltipState)
400 menuTooltipAlpha = bound(0, menuTooltipAlpha + 5 * frametime, 1);
401 if(menuTooltipAlpha == 1)
402 menuTooltipState = 0;
405 menuTooltipAlpha = bound(0, menuTooltipAlpha - 2 * frametime, 1);
406 if(menuTooltipAlpha == 0)
408 menuTooltipState = 0;
409 menuTooltipItem = world;
416 if(menuTooltipOrigin_x < 0) // unallocated?
417 m_allocatetooltipbox(pos);
419 if(menuTooltipOrigin_x >= 0)
422 p = SKINBORDER_TOOLTIP;
424 p_y *= 1 / conheight;
425 draw_BorderPicture(menuTooltipOrigin, SKINGFX_TOOLTIP, menuTooltipSize, '1 1 1', menuTooltipAlpha, p);
426 p = menuTooltipOrigin;
427 p_x += SKINMARGIN_TOOLTIP_x / conwidth;
428 p_y += SKINMARGIN_TOOLTIP_y / conheight;
429 getWrappedLine_remaining = menuTooltipItem.tooltip;
430 while(getWrappedLine_remaining)
432 s = getWrappedLine(SKINWIDTH_TOOLTIP / fontsize_x, draw_TextWidth_WithoutColors);
433 draw_Text(p, s, fontsize, '1 1 1', SKINALPHA_TOOLTIP * menuTooltipAlpha, FALSE);
445 menuMouseMode = cvar("menu_mouse_absolute");
448 UpdateConWidthHeight();
452 // TODO draw an info image about this situation
456 if(!menuNotTheFirstFrame)
458 menuNotTheFirstFrame = 1;
460 if(!cvar("menu_video_played"))
462 localcmd("set menu_video_played 1; cd loop $menu_cdtrack; play sound/announcer/male/welcome.ogg\n");
463 menuLogoAlpha = -0.8; // no idea why, but when I start this at zero, it jumps instead of fading
468 realFrametime = frametime = min(0.2, t - menuPrevTime);
472 t = cvar("menu_slowmo");
483 if(getmousetarget() == (menuMouseMode ? MT_CLIENT : MT_MENU) && (getkeydest() == KEY_MENU || getkeydest() == KEY_MENU_GRABBED))
484 setkeydest(keyGrabber ? KEY_MENU_GRABBED : KEY_MENU);
489 if(cvar("cl_capturevideo"))
490 frametime = t / cvar("cl_capturevideo_fps"); // make capturevideo work smoothly
495 gamestatus = gamestatus | GAME_ISSERVER;
496 if(clientstate() == CS_CONNECTED)
497 gamestatus = gamestatus | GAME_CONNECTED;
498 if(cvar("developer"))
499 gamestatus = gamestatus | GAME_DEVELOPER;
501 prevMenuAlpha = menuAlpha;
504 if(menuAlpha == 0 && menuLogoAlpha < 2)
506 menuLogoAlpha = menuLogoAlpha + frametime * 2;
510 menuAlpha = min(1, menuAlpha + frametime * 5);
516 menuAlpha = max(0, menuAlpha - frametime * 5);
522 if(!(gamestatus & (GAME_CONNECTED | GAME_ISSERVER)))
524 if(menuLogoAlpha > 0)
526 drawBackground(SKINGFX_BACKGROUND, bound(0, menuLogoAlpha, 1));
527 if(menuAlpha <= 0 && SKINALPHA_CURSOR_INTRO > 0)
529 draw_alpha = SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1);
530 draw_drawMousePointer(menuMousePos);
535 else if(SKINALPHA_BACKGROUND_INGAME)
538 drawBackground(SKINGFX_BACKGROUND_INGAME, menuAlpha * SKINALPHA_BACKGROUND_INGAME);
547 if(prevMenuAlpha > 0)
548 main.initializeDialog(main, main.firstChild);
554 draw_alpha *= menuAlpha;
559 newMouse = globalToBoxSize(getmousepos(), draw_scale);
560 if(newMouse != '0 0 0')
561 if(newMouse != menuMousePos)
563 menuMousePos = newMouse;
564 if(mouseButtonsPressed)
565 main.mouseDrag(main, menuMousePos);
567 main.mouseMove(main, menuMousePos);
575 dMouse = getmousepos() * (frametime / realFrametime); // for capturevideo
576 if(dMouse != '0 0 0')
578 dMouse = globalToBoxSize(dMouse, draw_scale);
579 menuMousePos += dMouse * cvar("menu_mouse_speed");
580 menuMousePos_x = bound(0, menuMousePos_x, 1);
581 menuMousePos_y = bound(0, menuMousePos_y, 1);
582 if(mouseButtonsPressed)
583 main.mouseDrag(main, menuMousePos);
585 main.mouseMove(main, menuMousePos);
591 m_tooltip(menuMousePos);
593 draw_alpha = max(draw_alpha, SKINALPHA_CURSOR_INTRO * bound(0, menuLogoAlpha, 1));
595 draw_drawMousePointer(menuMousePos);
606 setkeydest(KEY_MENU);
607 setmousetarget((menuMouseMode ? MT_CLIENT : MT_MENU));
612 if(mouseButtonsPressed)
613 main.mouseRelease(main, menuMousePos);
614 mouseButtonsPressed = 0;
616 main.focusEnter(main);
617 main.showNotify(main);
623 setkeydest(KEY_GAME);
624 setmousetarget(MT_CLIENT);
629 main.focusLeave(main);
630 main.hideNotify(main);
646 for(e = NULL; (e = nextent(e)) != NULL; )
653 void m_focus_item_chain(entity outermost, entity innermost)
655 if(innermost.parent != outermost)
656 m_focus_item_chain(outermost, innermost.parent);
657 innermost.parent.setFocus(innermost.parent, innermost);
660 void m_activate_window(entity wnd)
665 m_activate_window(par);
667 if(par.instanceOfModalController)
669 if(wnd.tabSelectingButton)
671 TabButton_Click(wnd.tabSelectingButton, wnd);
674 par.initializeDialog(par, wnd);
676 else if(par.instanceOfNexposee)
678 // nexposee (sorry for violating abstraction here)
679 par.selectedChild = wnd;
680 par.animationState = 1;
681 setFocusContainer(par, NULL);
683 else if(par.instanceOfContainer)
687 par.setFocus(par, wnd);
691 void m_setpointerfocus(entity wnd)
693 if(wnd.instanceOfContainer)
695 entity focus = wnd.preferredFocusedGrandChild(wnd);
698 menuMousePos = focus.origin + 0.5 * focus.size;
699 menuMousePos_x *= 1 / conwidth;
700 menuMousePos_y *= 1 / conheight;
701 if(wnd.focused) // why does this never happen?
702 m_focus_item_chain(wnd, focus);
707 void(string itemname) m_goto =
712 if(itemname == "") // this can be called by GameCommand
714 if(gamestatus & (GAME_ISSERVER | GAME_CONNECTED))
718 m_activate_window(main.mainNexposee);
724 e = findstring(NULL, name, itemname);
728 m_activate_window(e);
729 m_setpointerfocus(e);
735 void() m_goto_skin_selector =
739 // TODO add code to switch back to the skin selector (no idea how to do it now)
740 m_goto("skinselector");