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;
360 if(autocvar_hud_cursormode)
361 if ( !autocvar__hud_configure )
366 // toggle a button to manage the current game
367 void HUD_MinigameMenu_CurrentButton()
370 if ( active_minigame )
372 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = e.list_prev )
373 if ( e.classname == "hud_minigamemenu_exit" )
375 HUD_MinigameMenu_EraseEntry(e);
378 entity currb = HUD_MinigameMenu_SpawnEntry(
379 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
380 currb.classname = "hud_minigamemenu_current";
381 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
382 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
383 HUD_MinigameMenu_Click(currb);
388 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = p.list_prev )
391 if ( e.classname == "hud_minigamemenu_current" )
395 p = HUD_MinigameMenu_last_entry;
396 HUD_MinigameMenu_EraseEntry(e);
400 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = e.list_prev )
401 if ( e.classname == "hud_minigamemenu_exit" )
403 entity exit = HUD_MinigameMenu_SpawnEntry(
404 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
405 exit.classname = "hud_minigamemenu_exit";
406 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
410 // Open the minigame menu panel
411 void HUD_MinigameMenu_Open()
413 if ( !HUD_MinigameMenu_IsOpened() )
415 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
416 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
417 HUD_MinigameMenu_last_entry );
418 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
419 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
420 HUD_MinigameMenu_last_entry );
421 HUD_MinigameMenu_CurrentButton();
422 HUD_MinigameMenu_activeitem = NULL;
423 if(autocvar_hud_cursormode)
428 // Handles mouse input on to minigame menu panel
429 void HUD_MinigameMenu_MouseInput()
431 panel = HUD_PANEL(MINIGAMEMENU);
433 HUD_Panel_LoadCvars();
437 panel_pos += '1 1 0' * panel_bg_padding;
438 panel_size -= '2 2 0' * panel_bg_padding;
443 panel_pos_y += hud_fontsize_y*2;
445 HUD_MinigameMenu_activeitem = NULL;
447 for ( e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
449 sz = eX*panel_size_x + eY*e.size_y;
452 if ( !HUD_MinigameMenu_activeitem && mousepos_y >= panel_pos_y && mousepos_y <= panel_pos_y + sz_y )
454 HUD_MinigameMenu_activeitem = e;
461 void HUD_MinigameMenu_DrawEntry(vector pos, string s, vector fontsize, vector color)
463 minigame_drawstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
464 fontsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
466 // Draw a color-coded menu
467 void HUD_MinigameMenu_DrawColoredEntry(vector pos, string s, vector fontsize)
469 minigame_drawcolorcodedstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
470 fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
473 // Minigame menu panel UI
474 void HUD_MinigameMenu ()
476 if ( !HUD_MinigameMenu_IsOpened() )
479 HUD_Panel_LoadCvars();
486 panel_pos += '1 1 0' * panel_bg_padding;
487 panel_size -= '2 2 0' * panel_bg_padding;
490 HUD_MinigameMenu_DrawEntry(panel_pos,_("Minigames"),hud_fontsize*2,'0.25 0.47 0.72');
491 panel_pos_y += hud_fontsize_y*2;
496 vector imgsz = '22 22 0'; // NOTE: if changed, edit where HUD_MinigameMenu_activeitem is selected
497 for ( entity e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
509 drawfill(panel_pos, eX*panel_size_x + eY*itemh, e.colormod,
510 panel_fg_alpha, DRAWFLAG_NORMAL);
516 drawpic( panel_pos+offset, e.model, imgsz, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
518 offset_y = (imgsz_y-e.size_y) / 2;
522 HUD_MinigameMenu_DrawColoredEntry(panel_pos+offset,e.message,e.size);
524 HUD_MinigameMenu_DrawEntry(panel_pos+offset,e.message,e.size,color);
526 if ( e == HUD_MinigameMenu_activeitem )
527 drawfill(panel_pos, eX*panel_size_x + eY*itemh,'1 1 1', 0.25, DRAWFLAG_ADDITIVE);
529 panel_pos_y += itemh;
533 // ====================================================================
534 // Minigame Help Panel
535 // ====================================================================
537 void HUD_MinigameHelp()
541 if(!autocvar__hud_configure)
542 help_message = active_minigame.message;
544 help_message = "Minigame message";
549 HUD_Panel_LoadCvars();
558 pos += '1 1 0' * panel_bg_padding;
559 mySize -= '2 2 0' * panel_bg_padding;
562 minigame_drawcolorcodedstring_wrapped( mySize_x, pos, help_message,
563 hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5 );
566 // ====================================================================
567 // Minigame Panel Input
568 // ====================================================================
569 float HUD_Minigame_InputEvent(float bInputType, float nPrimary, float nSecondary)
572 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure )
577 mousepos_x = nPrimary;
578 mousepos_y = nSecondary;
579 if ( active_minigame && HUD_mouse_over(HUD_PANEL(MINIGAMEBOARD)) )
580 active_minigame.minigame_event(active_minigame,"mouse_moved",mousepos);
586 if(bInputType == 0) {
587 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
588 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
589 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
590 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
591 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
593 else if(bInputType == 1) {
594 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
595 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
596 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
597 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
598 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
602 string con_keys = findkeysforcommand("toggleconsole", 0);
603 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
605 for (i = 0; i < keys; ++i)
607 if(nPrimary == stof(argv(i)))
611 if ( active_minigame && ( bInputType == 0 || bInputType == 1 ) )
614 string action = bInputType == 0 ? "pressed" : "released";
615 if ( nPrimary >= K_MOUSE1 && nPrimary <= K_MOUSE16 )
617 if ( HUD_mouse_over(HUD_PANEL(MINIGAMEBOARD)) )
623 if ( device && active_minigame.minigame_event(
624 active_minigame,strcat(device,"_",action),nPrimary) )
627 /// TODO: bInputType == 2?
630 if ( bInputType == 0 )
632 if ( nPrimary == K_MOUSE1 && HUD_MinigameMenu_activeitem &&
633 HUD_mouse_over(HUD_PANEL(MINIGAMEMENU)) )
635 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
638 if ( nPrimary == K_UPARROW || nPrimary == K_KP_UPARROW )
640 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_prev )
641 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_prev;
643 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_last_entry;
646 else if ( nPrimary == K_DOWNARROW || nPrimary == K_KP_DOWNARROW )
648 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_next )
649 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_next;
651 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
654 else if ( nPrimary == K_HOME || nPrimary == K_KP_HOME )
656 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
659 else if ( nPrimary == K_END || nPrimary == K_KP_END )
661 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
664 else if ( nPrimary == K_KP_ENTER || nPrimary == K_ENTER || nPrimary == K_SPACE )
666 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
669 else if ( nPrimary == K_ESCAPE )
671 HUD_MinigameMenu_Close(NULL, NULL, NULL);
681 void HUD_Minigame_Mouse()
683 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
686 if (!autocvar_hud_cursormode)
689 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAMEMENU)) )
690 HUD_MinigameMenu_MouseInput();
692 draw_cursor_normal(mousepos, '1 1 1', panel_fg_alpha);