1 #include "cl_minigames_hud.qh"
3 #include <client/autocvars.qh>
4 #include <common/ent_cs.qh>
6 #include "minigames.qh"
10 #include <client/hud/hud_config.qh>
11 #include <client/mapvoting.qh>
13 // whether the mouse is over the given panel
14 bool HUD_mouse_over(entity somepanel)
16 vector pos = stov(cvar_string(strcat("hud_panel_", somepanel.panel_name, "_pos")));
17 vector sz = stov(cvar_string(strcat("hud_panel_", somepanel.panel_name, "_size")));
18 return mousepos_x >= pos_x*vid_conwidth && mousepos_x <= (pos_x+sz_x)*vid_conwidth &&
19 mousepos_y >= pos_y*vid_conheight && mousepos_y <= (pos_y+sz_y)*vid_conheight ;
22 // ====================================================================
24 // ====================================================================
26 // Draws the minigame game board
27 void HUD_MinigameBoard ()
29 entity hud_minigame = NULL;
31 if(!autocvar__hud_configure)
32 hud_minigame = active_minigame.descriptor;
34 hud_minigame = minigame_get_descriptor("nmm");
39 HUD_Panel_LoadCvars();
46 hud_minigame.minigame_hud_board(pos,mySize);
49 // ====================================================================
51 // ====================================================================
52 // Draws the minigame status panel
53 void HUD_MinigameStatus ()
55 entity hud_minigame = NULL;
57 if(!autocvar__hud_configure)
58 hud_minigame = active_minigame.descriptor;
60 hud_minigame = minigame_get_descriptor("nmm");
65 HUD_Panel_LoadCvars();
74 pos += '1 1 0' * panel_bg_padding;
75 mySize -= '2 2 0' * panel_bg_padding;
79 hud_minigame.minigame_hud_status(pos,mySize);
82 // ====================================================================
84 // ====================================================================
86 // Minigame menu options: list head
87 entity HUD_MinigameMenu_entries;
88 // Minigame menu options: list tail
89 entity HUD_MinigameMenu_last_entry;
91 // Minigame menu options: insert entry after the given location
92 void HUD_MinigameMenu_InsertEntry(entity newentry, entity prev)
94 if ( !HUD_MinigameMenu_entries )
96 HUD_MinigameMenu_entries = newentry;
97 HUD_MinigameMenu_last_entry = newentry;
101 newentry.list_prev = prev;
102 newentry.list_next = prev.list_next;
103 if ( prev.list_next )
104 prev.list_next.list_prev = newentry;
106 HUD_MinigameMenu_last_entry = newentry;
107 prev.list_next = newentry;
112 // minigame menu item uder the mouse
113 entity HUD_MinigameMenu_activeitem;
115 // Click the given item
116 void HUD_MinigameMenu_Click(entity menuitem)
120 menuitem.use(menuitem, NULL, NULL);
124 // Minigame menu options: Remove the given entry
125 // Precondition: the given entry is actually in the list
126 void HUD_MinigameMenu_EraseEntry ( entity e )
128 // remove child items (if any)
131 HUD_MinigameMenu_Click(e);
135 e.list_prev.list_next = e.list_next;
137 HUD_MinigameMenu_entries = e.list_next;
140 e.list_next.list_prev = e.list_prev;
142 HUD_MinigameMenu_last_entry = e.list_prev;
144 if ( HUD_MinigameMenu_activeitem == e )
145 HUD_MinigameMenu_activeitem = NULL;
150 // Minigame menu options: create entry
151 entity HUD_MinigameMenu_SpawnEntry(string s, vector offset, vector fontsize, vector color,void(entity, entity, entity) click)
153 entity entry = spawn();
155 entry.origin = offset;
156 entry.size = fontsize;
157 entry.colormod = color;
160 panel_pos_y += fontsize_y;
164 // Spawn a child entry of a collapsable entry
165 entity HUD_MinigameMenu_SpawnSubEntry(string s, void(entity, entity, entity) click, entity parent)
167 vector item_fontsize = hud_fontsize*1.25;
168 vector item_offset = '1 0 0' * item_fontsize_x;
169 entity item = HUD_MinigameMenu_SpawnEntry(
170 s,item_offset,item_fontsize,'0.8 0.8 0.8', click );
175 // Click action for Create sub-entries
176 void HUD_MinigameMenu_ClickCreate_Entry(entity this, entity actor, entity trigger)
178 minigame_cmd("create ", this.netname);
181 // Helper click action for collapsible entries
182 // returns true when you have to create the sub-entries
183 bool HUD_MinigameMenu_Click_ExpandCollapse(entity this)
186 if ( this.flags & 2 )
188 if ( HUD_MinigameMenu_activeitem &&
189 HUD_MinigameMenu_activeitem.owner == this )
190 HUD_MinigameMenu_activeitem = NULL;
192 for ( e = this.list_next; e != NULL && e.owner == this; e = this.list_next )
195 HUD_MinigameMenu_Click(e);
196 this.list_next = e.list_next;
199 if ( this.list_next )
200 this.list_next.list_prev = this;
202 HUD_MinigameMenu_last_entry = this;
206 for ( e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
208 if ( e.flags & 2 && e.origin_x == this.origin_x)
209 HUD_MinigameMenu_Click(e);
219 // Click action for the Create menu
220 void HUD_MinigameMenu_ClickCreate(entity this, entity actor, entity trigger)
222 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
226 FOREACH(Minigames, true, {
227 curr = HUD_MinigameMenu_SpawnSubEntry(it.message, HUD_MinigameMenu_ClickCreate_Entry, this);
228 curr.netname = it.netname;
229 curr.model = strzone(minigame_texture(strcat(it.netname,"/icon")));
230 HUD_MinigameMenu_InsertEntry( curr, prev );
236 // Click action for Join sub-entries
237 void HUD_MinigameMenu_ClickJoin_Entry(entity this, entity actor, entity trigger)
239 minigame_cmd("join ",this.netname);
240 HUD_MinigameMenu_EraseEntry(this);
243 // Click action for the Join menu
244 void HUD_MinigameMenu_ClickJoin(entity this, entity actor, entity trigger)
246 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
251 while( (e = find(e,classname,"minigame")) )
253 if ( e != active_minigame )
255 curr = HUD_MinigameMenu_SpawnSubEntry(
256 e.netname, HUD_MinigameMenu_ClickJoin_Entry, this );
257 curr.netname = e.netname;
258 curr.model = strzone(minigame_texture(strcat(e.descriptor.netname,"/icon")));
259 HUD_MinigameMenu_InsertEntry( curr, prev );
266 /*// Temporary placeholder for un-implemented Click actions
267 void HUD_MinigameMenu_ClickNoop()
269 dprint("Placeholder for ",this.message,"\n");
272 // Click action for Quit
273 void HUD_MinigameMenu_ClickQuit(entity this, entity actor, entity trigger)
275 deactivate_minigame();
279 // Click action for Invite sub-entries
280 void HUD_MinigameMenu_ClickInvite_Entry(entity this, entity actor, entity trigger)
282 minigame_cmd("invite #",this.netname);
285 // Click action for the Invite menu
286 void HUD_MinigameMenu_ClickInvite(entity this, entity actor, entity trigger)
288 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
292 for(int i = 0; i < maxclients; ++i)
294 if ( player_localnum != i && playerslots[i] && entcs_GetName(i) != "" &&
295 !findfloat(NULL,minigame_playerslot,i+1) && playerslots[i].ping )
297 e = HUD_MinigameMenu_SpawnSubEntry(
298 strzone(entcs_GetName(i)), HUD_MinigameMenu_ClickInvite_Entry,
301 e.netname = strzone(ftos(i+1));
303 HUD_MinigameMenu_InsertEntry(e,prev);
310 void HUD_MinigameMenu_ClickCustomEntry(entity this, entity actor, entity trigger)
312 if ( active_minigame )
313 active_minigame.minigame_event(active_minigame,"menu_click",this.netname);
316 // Adds a game-specific entry to the menu
317 void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
319 entity e = HUD_MinigameMenu_SpawnSubEntry(
320 menumessage, HUD_MinigameMenu_ClickCustomEntry, parent );
321 e.netname = event_arg;
322 HUD_MinigameMenu_InsertEntry(e, parent);
323 //dprint("CustomEntry ",ftos(num_for_edict(parent))," ",menumessage," ",event_arg,"\n");
326 // Click action for the Current Game menu
327 void HUD_MinigameMenu_ClickCurrentGame(entity this, entity actor, entity trigger)
329 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
331 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
332 _("Quit"), HUD_MinigameMenu_ClickQuit, this ), this);
334 active_minigame.minigame_event(active_minigame,"menu_show",this);
336 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
337 _("Invite"), HUD_MinigameMenu_ClickInvite, this), this);
340 // Whether the minigame menu panel is open
341 bool HUD_MinigameMenu_IsOpened()
343 return HUD_MinigameMenu_entries != NULL;
346 // Close the minigame menu panel
347 void HUD_MinigameMenu_Close(entity this, entity actor, entity trigger)
349 if ( HUD_MinigameMenu_IsOpened() )
352 for ( e = HUD_MinigameMenu_entries; e != NULL; e = p )
357 HUD_MinigameMenu_entries = NULL;
358 HUD_MinigameMenu_last_entry = NULL;
359 HUD_MinigameMenu_activeitem = NULL;
363 // toggle a button to manage the current game
364 void HUD_MinigameMenu_CurrentButton()
367 if ( active_minigame )
369 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = e.list_prev )
370 if ( e.classname == "hud_minigamemenu_exit" )
372 HUD_MinigameMenu_EraseEntry(e);
375 entity currb = HUD_MinigameMenu_SpawnEntry(
376 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
377 currb.classname = "hud_minigamemenu_current";
378 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
379 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
380 HUD_MinigameMenu_Click(currb);
385 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = p.list_prev )
388 if ( e.classname == "hud_minigamemenu_current" )
392 p = HUD_MinigameMenu_last_entry;
393 HUD_MinigameMenu_EraseEntry(e);
397 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = e.list_prev )
398 if ( e.classname == "hud_minigamemenu_exit" )
400 entity exit = HUD_MinigameMenu_SpawnEntry(
401 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
402 exit.classname = "hud_minigamemenu_exit";
403 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
407 // Open the minigame menu panel
408 void HUD_MinigameMenu_Open()
410 if ( !HUD_MinigameMenu_IsOpened() )
412 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
413 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
414 HUD_MinigameMenu_last_entry );
415 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
416 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
417 HUD_MinigameMenu_last_entry );
418 HUD_MinigameMenu_CurrentButton();
419 HUD_MinigameMenu_activeitem = NULL;
423 // Handles mouse input on to minigame menu panel
424 void HUD_MinigameMenu_MouseInput()
426 panel = HUD_PANEL(MINIGAMEMENU);
428 HUD_Panel_LoadCvars();
432 panel_pos += '1 1 0' * panel_bg_padding;
433 panel_size -= '2 2 0' * panel_bg_padding;
438 panel_pos_y += hud_fontsize_y*2;
440 HUD_MinigameMenu_activeitem = NULL;
442 for ( e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
444 sz = eX*panel_size_x + eY*e.size_y;
447 if ( !HUD_MinigameMenu_activeitem && mousepos_y >= panel_pos_y && mousepos_y <= panel_pos_y + sz_y )
449 HUD_MinigameMenu_activeitem = e;
456 void HUD_MinigameMenu_DrawEntry(vector pos, string s, vector fontsize, vector color)
458 minigame_drawstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
459 fontsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
461 // Draw a color-coded menu
462 void HUD_MinigameMenu_DrawColoredEntry(vector pos, string s, vector fontsize)
464 minigame_drawcolorcodedstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
465 fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
468 // Minigame menu panel UI
469 void HUD_MinigameMenu ()
471 if ( !HUD_MinigameMenu_IsOpened() )
474 HUD_Panel_LoadCvars();
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 != NULL; 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_LoadCvars();
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 ( active_minigame && HUD_mouse_over(HUD_PANEL(MINIGAMEBOARD)) )
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);
597 string con_keys = findkeysforcommand("toggleconsole", 0);
598 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
600 for (i = 0; i < keys; ++i)
602 if(nPrimary == stof(argv(i)))
606 if ( active_minigame && ( 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(MINIGAMEBOARD)) )
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(MINIGAMEMENU)) )
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(NULL, NULL, NULL);
676 void HUD_Minigame_Mouse()
678 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
681 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAMEMENU)) )
682 HUD_MinigameMenu_MouseInput();