1 #include "minigames.qh"
2 #include "../../client/mapvoting.qh"
4 // whether the mouse is over the given panel
5 bool HUD_mouse_over(entity somepanel)
7 vector pos = stov(cvar_string(strcat("hud_panel_", somepanel.panel_name, "_pos")));
8 vector sz = stov(cvar_string(strcat("hud_panel_", somepanel.panel_name, "_size")));
9 return mousepos_x >= pos_x*vid_conwidth && mousepos_x <= (pos_x+sz_x)*vid_conwidth &&
10 mousepos_y >= pos_y*vid_conheight && mousepos_y <= (pos_y+sz_y)*vid_conheight ;
13 // ====================================================================
15 // ====================================================================
17 // Draws the minigame game board
18 void HUD_MinigameBoard ()
20 entity hud_minigame = world;
22 if(!autocvar__hud_configure)
23 hud_minigame = active_minigame.descriptor;
25 hud_minigame = minigame_get_descriptor("nmm");
30 HUD_Panel_UpdateCvars();
37 hud_minigame.minigame_hud_board(pos,mySize);
40 // ====================================================================
42 // ====================================================================
43 // Draws the minigame status panel
44 void HUD_MinigameStatus ()
46 entity hud_minigame = world;
48 if(!autocvar__hud_configure)
49 hud_minigame = active_minigame.descriptor;
51 hud_minigame = minigame_get_descriptor("nmm");
56 HUD_Panel_UpdateCvars();
65 pos += '1 1 0' * panel_bg_padding;
66 mySize -= '2 2 0' * panel_bg_padding;
69 hud_minigame.minigame_hud_status(pos,mySize);
72 // ====================================================================
74 // ====================================================================
76 // Minigame menu options: list head
77 entity HUD_MinigameMenu_entries;
78 // Minigame menu options: list tail
79 entity HUD_MinigameMenu_last_entry;
81 // Minigame menu options: insert entry after the given location
82 void HUD_MinigameMenu_InsertEntry(entity new, entity prev)
84 if ( !HUD_MinigameMenu_entries )
86 HUD_MinigameMenu_entries = new;
87 HUD_MinigameMenu_last_entry = new;
92 new.list_next = prev.list_next;
94 prev.list_next.list_prev = new;
96 HUD_MinigameMenu_last_entry = new;
102 // minigame menu item uder the mouse
103 entity HUD_MinigameMenu_activeitem;
105 // Click the given item
106 void HUD_MinigameMenu_Click(entity menuitem)
117 // Minigame menu options: Remove the given entry
118 // Precondition: the given entry is actually in the list
119 void HUD_MinigameMenu_EraseEntry ( entity e )
121 // remove child items (if any)
124 HUD_MinigameMenu_Click(e);
128 e.list_prev.list_next = e.list_next;
130 HUD_MinigameMenu_entries = e.list_next;
133 e.list_next.list_prev = e.list_prev;
135 HUD_MinigameMenu_last_entry = e.list_prev;
137 if ( HUD_MinigameMenu_activeitem == e )
138 HUD_MinigameMenu_activeitem = world;
143 // Minigame menu options: create entry
144 entity HUD_MinigameMenu_SpawnEntry(string s, vector offset, vector fontsize, vector color,void() click)
146 entity entry = spawn();
148 entry.origin = offset;
149 entry.size = fontsize;
150 entry.colormod = color;
153 panel_pos_y += fontsize_y;
157 // Spawn a child entry of a collapsable entry
158 entity HUD_MinigameMenu_SpawnSubEntry(string s, void() click, entity parent)
160 vector item_fontsize = hud_fontsize*1.25;
161 vector item_offset = '1 0 0' * item_fontsize_x;
162 entity item = HUD_MinigameMenu_SpawnEntry(
163 s,item_offset,item_fontsize,'0.8 0.8 0.8', click );
168 // Click action for Create sub-entries
169 void HUD_MinigameMenu_ClickCreate_Entry()
171 minigame_cmd("create ",self.netname);
174 // Helper click action for collapsible entries
175 // returns true when you have to create the sub-entries
176 bool HUD_MinigameMenu_Click_ExpandCollapse()
179 if ( self.flags & 2 )
181 if ( HUD_MinigameMenu_activeitem &&
182 HUD_MinigameMenu_activeitem.owner == self )
183 HUD_MinigameMenu_activeitem = world;
185 for ( e = self.list_next; e != world && e.owner == self; e = self.list_next )
188 HUD_MinigameMenu_Click(e);
189 self.list_next = e.list_next;
192 if ( self.list_next )
193 self.list_next.list_prev = self;
195 HUD_MinigameMenu_last_entry = self;
199 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
201 if ( e.flags & 2 && e.origin_x == self.origin_x)
202 HUD_MinigameMenu_Click(e);
212 // Click action for the Create menu
213 void HUD_MinigameMenu_ClickCreate()
215 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
220 for ( e = minigame_descriptors; e != world; e = e.list_next )
222 curr = HUD_MinigameMenu_SpawnSubEntry(
223 e.message, HUD_MinigameMenu_ClickCreate_Entry, self );
224 curr.netname = e.netname;
225 curr.model = strzone(minigame_texture(strcat(e.netname,"/icon")));
226 HUD_MinigameMenu_InsertEntry( curr, prev );
232 // Click action for Join sub-entries
233 void HUD_MinigameMenu_ClickJoin_Entry()
235 minigame_cmd("join ",self.netname);
236 HUD_MinigameMenu_EraseEntry(self);
239 // Click action for the Join menu
240 void HUD_MinigameMenu_ClickJoin()
242 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
247 while( (e = find(e,classname,"minigame")) )
249 if ( e != active_minigame )
251 curr = HUD_MinigameMenu_SpawnSubEntry(
252 e.netname, HUD_MinigameMenu_ClickJoin_Entry, self );
253 curr.netname = e.netname;
254 curr.model = strzone(minigame_texture(strcat(e.descriptor.netname,"/icon")));
255 HUD_MinigameMenu_InsertEntry( curr, prev );
262 /*// Temporary placeholder for un-implemented Click actions
263 void HUD_MinigameMenu_ClickNoop()
265 dprint("Placeholder for ",self.message,"\n");
268 // Click action for Quit
269 void HUD_MinigameMenu_ClickQuit()
271 deactivate_minigame();
275 // Click action for Invite sub-entries
276 void HUD_MinigameMenu_ClickInvite_Entry()
278 minigame_cmd("invite #",self.netname);
281 // Click action for the Invite menu
282 void HUD_MinigameMenu_ClickInvite()
284 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
288 for(int i = 0; i < maxclients; ++i)
290 if ( player_localnum != i && playerslots[i] && GetPlayerName(i) != "" &&
291 !findfloat(world,minigame_playerslot,i+1) && playerslots[i].ping )
293 e = HUD_MinigameMenu_SpawnSubEntry(
294 strzone(GetPlayerName(i)), HUD_MinigameMenu_ClickInvite_Entry,
297 e.netname = strzone(ftos(i+1));
299 HUD_MinigameMenu_InsertEntry(e,prev);
306 void HUD_MinigameMenu_ClickCustomEntry()
308 if ( active_minigame )
309 active_minigame.minigame_event(active_minigame,"menu_click",self.netname);
312 // Adds a game-specific entry to the menu
313 void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
315 entity e = HUD_MinigameMenu_SpawnSubEntry(
316 menumessage, HUD_MinigameMenu_ClickCustomEntry, parent );
317 e.netname = event_arg;
318 HUD_MinigameMenu_InsertEntry(e, parent);
319 //dprint("CustomEntry ",ftos(num_for_edict(parent))," ",menumessage," ",event_arg,"\n");
322 // Click action for the Current Game menu
323 void HUD_MinigameMenu_ClickCurrentGame()
325 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
327 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
328 _("Quit"), HUD_MinigameMenu_ClickQuit, self ), self);
330 active_minigame.minigame_event(active_minigame,"menu_show",self);
332 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
333 _("Invite"), HUD_MinigameMenu_ClickInvite, self), self);
336 // Whether the minigame menu panel is open
337 bool HUD_MinigameMenu_IsOpened()
339 return !!HUD_MinigameMenu_entries;
342 // Close the minigame menu panel
343 void HUD_MinigameMenu_Close()
345 if ( HUD_MinigameMenu_IsOpened() )
348 for ( e = HUD_MinigameMenu_entries; e != world; e = p )
353 HUD_MinigameMenu_entries = world;
354 HUD_MinigameMenu_last_entry = world;
355 HUD_MinigameMenu_activeitem = world;
356 if(autocvar_hud_cursormode)
357 if ( !autocvar__hud_configure )
362 // toggle a button to manage the current game
363 void HUD_MinigameMenu_CurrentButton()
366 if ( active_minigame )
368 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
369 if ( e.classname == "hud_minigamemenu_exit" )
371 HUD_MinigameMenu_EraseEntry(e);
374 entity currb = HUD_MinigameMenu_SpawnEntry(
375 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
376 currb.classname = "hud_minigamemenu_current";
377 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
378 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
379 HUD_MinigameMenu_Click(currb);
384 for ( e = HUD_MinigameMenu_last_entry; e != world; e = p.list_prev )
387 if ( e.classname == "hud_minigamemenu_current" )
391 p = HUD_MinigameMenu_last_entry;
392 HUD_MinigameMenu_EraseEntry(e);
396 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
397 if ( e.classname == "hud_minigamemenu_exit" )
399 entity exit = HUD_MinigameMenu_SpawnEntry(
400 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
401 exit.classname = "hud_minigamemenu_exit";
402 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
406 // Open the minigame menu panel
407 void HUD_MinigameMenu_Open()
409 if ( !HUD_MinigameMenu_IsOpened() )
411 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
412 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
413 HUD_MinigameMenu_last_entry );
414 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
415 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
416 HUD_MinigameMenu_last_entry );
417 HUD_MinigameMenu_CurrentButton();
418 HUD_MinigameMenu_activeitem = world;
419 if(autocvar_hud_cursormode)
424 // Handles mouse input on to minigame menu panel
425 void HUD_MinigameMenu_MouseInput()
427 panel = HUD_PANEL(MINIGAME_MENU);
429 HUD_Panel_UpdateCvars();
433 panel_pos += '1 1 0' * panel_bg_padding;
434 panel_size -= '2 2 0' * panel_bg_padding;
439 panel_pos_y += hud_fontsize_y*2;
441 HUD_MinigameMenu_activeitem = world;
443 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
445 sz = eX*panel_size_x + eY*e.size_y;
448 if ( !HUD_MinigameMenu_activeitem && mousepos_y >= panel_pos_y && mousepos_y <= panel_pos_y + sz_y )
450 HUD_MinigameMenu_activeitem = e;
457 void HUD_MinigameMenu_DrawEntry(vector pos, string s, vector fontsize, vector color)
459 minigame_drawstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
460 fontsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
462 // Draw a color-coded menu
463 void HUD_MinigameMenu_DrawColoredEntry(vector pos, string s, vector fontsize)
465 minigame_drawcolorcodedstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
466 fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
469 // Minigame menu panel UI
470 void HUD_MinigameMenu ()
472 if ( !HUD_MinigameMenu_IsOpened() )
475 HUD_Panel_UpdateCvars();
481 panel_pos += '1 1 0' * panel_bg_padding;
482 panel_size -= '2 2 0' * panel_bg_padding;
485 HUD_MinigameMenu_DrawEntry(panel_pos,_("Minigames"),hud_fontsize*2,'0.25 0.47 0.72');
486 panel_pos_y += hud_fontsize_y*2;
491 vector imgsz = '22 22 0'; // NOTE: if changed, edit where HUD_MinigameMenu_activeitem is selected
492 for ( entity e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
504 drawfill(panel_pos, eX*panel_size_x + eY*itemh, e.colormod,
505 panel_fg_alpha, DRAWFLAG_NORMAL);
511 drawpic( panel_pos+offset, e.model, imgsz, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
513 offset_y = (imgsz_y-e.size_y) / 2;
517 HUD_MinigameMenu_DrawColoredEntry(panel_pos+offset,e.message,e.size);
519 HUD_MinigameMenu_DrawEntry(panel_pos+offset,e.message,e.size,color);
521 if ( e == HUD_MinigameMenu_activeitem )
522 drawfill(panel_pos, eX*panel_size_x + eY*itemh,'1 1 1', 0.25, DRAWFLAG_ADDITIVE);
524 panel_pos_y += itemh;
528 // ====================================================================
529 // Minigame Help Panel
530 // ====================================================================
532 void HUD_MinigameHelp()
536 if(!autocvar__hud_configure)
537 help_message = active_minigame.message;
539 help_message = "Minigame message";
544 HUD_Panel_UpdateCvars();
553 pos += '1 1 0' * panel_bg_padding;
554 mySize -= '2 2 0' * panel_bg_padding;
557 minigame_drawcolorcodedstring_wrapped( mySize_x, pos, help_message,
558 hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5 );
561 // ====================================================================
562 // Minigame Panel Input
563 // ====================================================================
564 float HUD_Minigame_InputEvent(float bInputType, float nPrimary, float nSecondary)
567 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure )
572 mousepos_x = nPrimary;
573 mousepos_y = nSecondary;
574 if ( minigame_isactive() && HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
575 active_minigame.minigame_event(active_minigame,"mouse_moved",mousepos);
581 if(bInputType == 0) {
582 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
583 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
584 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
585 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
586 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
588 else if(bInputType == 1) {
589 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
590 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
591 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
592 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
593 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
598 con_keys = findkeysforcommand("toggleconsole", 0);
599 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
600 for (int i = 0; i < keys; ++i)
602 if(nPrimary == stof(argv(i)))
606 if ( minigame_isactive() && ( bInputType == 0 || bInputType == 1 ) )
609 string action = bInputType == 0 ? "pressed" : "released";
610 if ( nPrimary >= K_MOUSE1 && nPrimary <= K_MOUSE16 )
612 if ( HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
618 if ( device && active_minigame.minigame_event(
619 active_minigame,strcat(device,"_",action),nPrimary) )
622 /// TODO: bInputType == 2?
625 if ( bInputType == 0 )
627 if ( nPrimary == K_MOUSE1 && HUD_MinigameMenu_activeitem &&
628 HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
630 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
633 if ( nPrimary == K_UPARROW || nPrimary == K_KP_UPARROW )
635 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_prev )
636 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_prev;
638 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_last_entry;
641 else if ( nPrimary == K_DOWNARROW || nPrimary == K_KP_DOWNARROW )
643 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_next )
644 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_next;
646 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
649 else if ( nPrimary == K_HOME || nPrimary == K_KP_HOME )
651 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
654 else if ( nPrimary == K_END || nPrimary == K_KP_END )
656 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
659 else if ( nPrimary == K_KP_ENTER || nPrimary == K_ENTER || nPrimary == K_SPACE )
661 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
664 else if ( nPrimary == K_ESCAPE )
666 HUD_MinigameMenu_Close();
676 void HUD_Minigame_Mouse()
678 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
681 if(!autocvar_hud_cursormode)
683 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
685 mousepos_x = bound(0, mousepos_x, vid_conwidth);
686 mousepos_y = bound(0, mousepos_y, vid_conheight);
689 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
690 HUD_MinigameMenu_MouseInput();
692 vector cursorsize = '32 32 0';
693 drawpic(mousepos-'8 4 0', strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"),
694 cursorsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
697 bool HUD_Minigame_Showpanels()
699 return HUD_MinigameMenu_IsOpened() && ( autocvar__hud_configure || minigame_isactive() );