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)
35 hud_minigame = active_minigame.descriptor;
38 hud_minigame = minigame_get_descriptor("nmm");
43 HUD_Panel_LoadCvars();
50 hud_minigame.minigame_hud_board(pos,mySize);
53 // ====================================================================
55 // ====================================================================
56 // Draws the minigame status panel
57 void HUD_MinigameStatus ()
59 entity hud_minigame = NULL;
61 if(!autocvar__hud_configure)
65 hud_minigame = active_minigame.descriptor;
68 hud_minigame = minigame_get_descriptor("nmm");
73 HUD_Panel_LoadCvars();
82 pos += '1 1 0' * panel_bg_padding;
83 mySize -= '2 2 0' * panel_bg_padding;
87 hud_minigame.minigame_hud_status(pos,mySize);
90 // ====================================================================
92 // ====================================================================
94 // Minigame menu options: list head
95 entity HUD_MinigameMenu_entries;
96 // Minigame menu options: list tail
97 entity HUD_MinigameMenu_last_entry;
99 // Minigame menu options: insert entry after the given location
100 void HUD_MinigameMenu_InsertEntry(entity newentry, entity prev)
102 if ( !HUD_MinigameMenu_entries )
104 HUD_MinigameMenu_entries = newentry;
105 HUD_MinigameMenu_last_entry = newentry;
109 newentry.list_prev = prev;
110 newentry.list_next = prev.list_next;
111 if ( prev.list_next )
112 prev.list_next.list_prev = newentry;
114 HUD_MinigameMenu_last_entry = newentry;
115 prev.list_next = newentry;
120 // minigame menu item uder the mouse
121 entity HUD_MinigameMenu_activeitem;
123 // Click the given item
124 void HUD_MinigameMenu_Click(entity menuitem)
128 menuitem.use(menuitem, NULL, NULL);
132 // Minigame menu options: Remove the given entry
133 // Precondition: the given entry is actually in the list
134 void HUD_MinigameMenu_EraseEntry ( entity e )
136 // remove child items (if any)
139 HUD_MinigameMenu_Click(e);
143 e.list_prev.list_next = e.list_next;
145 HUD_MinigameMenu_entries = e.list_next;
148 e.list_next.list_prev = e.list_prev;
150 HUD_MinigameMenu_last_entry = e.list_prev;
152 if ( HUD_MinigameMenu_activeitem == e )
153 HUD_MinigameMenu_activeitem = NULL;
158 // Minigame menu options: create entry
159 entity HUD_MinigameMenu_SpawnEntry(string s, vector offset, vector fontsize, vector color,void(entity, entity, entity) click)
161 entity entry = spawn();
163 entry.origin = offset;
164 entry.size = fontsize;
165 entry.colormod = color;
168 panel_pos_y += fontsize_y;
172 // Spawn a child entry of a collapsable entry
173 entity HUD_MinigameMenu_SpawnSubEntry(string s, void(entity, entity, entity) click, entity parent)
175 vector item_fontsize = hud_fontsize*1.25;
176 vector item_offset = '1 0 0' * item_fontsize_x;
177 entity item = HUD_MinigameMenu_SpawnEntry(
178 s,item_offset,item_fontsize,'0.8 0.8 0.8', click );
183 // Click action for Create sub-entries
184 void HUD_MinigameMenu_ClickCreate_Entry(entity this, entity actor, entity trigger)
186 minigame_cmd("create ", this.netname);
189 // Helper click action for collapsible entries
190 // returns true when you have to create the sub-entries
191 bool HUD_MinigameMenu_Click_ExpandCollapse(entity this)
194 if ( this.flags & 2 )
196 if ( HUD_MinigameMenu_activeitem &&
197 HUD_MinigameMenu_activeitem.owner == this )
198 HUD_MinigameMenu_activeitem = NULL;
200 for ( e = this.list_next; e != NULL && e.owner == this; e = this.list_next )
203 HUD_MinigameMenu_Click(e);
204 this.list_next = e.list_next;
207 if ( this.list_next )
208 this.list_next.list_prev = this;
210 HUD_MinigameMenu_last_entry = this;
214 for ( e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
216 if ( e.flags & 2 && e.origin_x == this.origin_x)
217 HUD_MinigameMenu_Click(e);
227 // Click action for the Create menu
228 void HUD_MinigameMenu_ClickCreate(entity this, entity actor, entity trigger)
230 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
234 FOREACH(Minigames, true, {
235 curr = HUD_MinigameMenu_SpawnSubEntry(it.message, HUD_MinigameMenu_ClickCreate_Entry, this);
236 curr.netname = it.netname;
237 curr.model = strzone(minigame_texture(strcat(it.netname,"/icon")));
238 HUD_MinigameMenu_InsertEntry( curr, prev );
244 // Click action for Join sub-entries
245 void HUD_MinigameMenu_ClickJoin_Entry(entity this, entity actor, entity trigger)
247 minigame_cmd("join ",this.netname);
248 HUD_MinigameMenu_EraseEntry(this);
251 // Click action for the Join menu
252 void HUD_MinigameMenu_ClickJoin(entity this, entity actor, entity trigger)
254 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
259 while( (e = find(e,classname,"minigame")) )
261 if ( e != active_minigame )
263 curr = HUD_MinigameMenu_SpawnSubEntry(
264 e.netname, HUD_MinigameMenu_ClickJoin_Entry, this );
265 curr.netname = e.netname;
266 curr.model = strzone(minigame_texture(strcat(e.descriptor.netname,"/icon")));
267 HUD_MinigameMenu_InsertEntry( curr, prev );
274 /*// Temporary placeholder for un-implemented Click actions
275 void HUD_MinigameMenu_ClickNoop()
277 dprint("Placeholder for ",this.message,"\n");
280 // Click action for Quit
281 void HUD_MinigameMenu_ClickQuit(entity this, entity actor, entity trigger)
283 deactivate_minigame();
287 // Click action for Invite sub-entries
288 void HUD_MinigameMenu_ClickInvite_Entry(entity this, entity actor, entity trigger)
290 minigame_cmd("invite #",this.netname);
293 // Click action for the Invite menu
294 void HUD_MinigameMenu_ClickInvite(entity this, entity actor, entity trigger)
296 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
300 for(int i = 0; i < maxclients; ++i)
302 if ( player_localnum != i && playerslots[i] && entcs_GetName(i) != "" &&
303 !findfloat(NULL,minigame_playerslot,i+1) && playerslots[i].ping )
305 e = HUD_MinigameMenu_SpawnSubEntry(
306 strzone(entcs_GetName(i)), HUD_MinigameMenu_ClickInvite_Entry,
309 e.netname = strzone(ftos(i+1));
311 HUD_MinigameMenu_InsertEntry(e,prev);
318 void HUD_MinigameMenu_ClickCustomEntry(entity this, entity actor, entity trigger)
320 if ( active_minigame )
321 active_minigame.minigame_event(active_minigame,"menu_click",this.netname);
324 // Adds a game-specific entry to the menu
325 void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
327 entity e = HUD_MinigameMenu_SpawnSubEntry(
328 menumessage, HUD_MinigameMenu_ClickCustomEntry, parent );
329 e.netname = event_arg;
330 HUD_MinigameMenu_InsertEntry(e, parent);
331 //dprint("CustomEntry ",ftos(num_for_edict(parent))," ",menumessage," ",event_arg,"\n");
334 // Click action for the Current Game menu
335 void HUD_MinigameMenu_ClickCurrentGame(entity this, entity actor, entity trigger)
337 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
339 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
340 _("Quit"), HUD_MinigameMenu_ClickQuit, this ), this);
342 active_minigame.minigame_event(active_minigame,"menu_show",this);
344 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
345 _("Invite"), HUD_MinigameMenu_ClickInvite, this), this);
348 // Whether the minigame menu panel is open
349 bool HUD_MinigameMenu_IsOpened()
351 return HUD_MinigameMenu_entries != NULL;
354 // Close the minigame menu panel
355 void HUD_MinigameMenu_Close(entity this, entity actor, entity trigger)
357 if ( HUD_MinigameMenu_IsOpened() )
360 for ( e = HUD_MinigameMenu_entries; e != NULL; e = p )
365 HUD_MinigameMenu_entries = NULL;
366 HUD_MinigameMenu_last_entry = NULL;
367 HUD_MinigameMenu_activeitem = NULL;
371 // toggle a button to manage the current game
372 void HUD_MinigameMenu_CurrentButton()
375 if ( active_minigame )
377 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = e.list_prev )
378 if ( e.classname == "hud_minigamemenu_exit" )
380 HUD_MinigameMenu_EraseEntry(e);
383 entity currb = HUD_MinigameMenu_SpawnEntry(
384 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
385 currb.classname = "hud_minigamemenu_current";
386 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
387 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
388 HUD_MinigameMenu_Click(currb);
393 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = p.list_prev )
396 if ( e.classname == "hud_minigamemenu_current" )
400 p = HUD_MinigameMenu_last_entry;
401 HUD_MinigameMenu_EraseEntry(e);
405 for ( e = HUD_MinigameMenu_last_entry; e != NULL; e = e.list_prev )
406 if ( e.classname == "hud_minigamemenu_exit" )
408 entity exit = HUD_MinigameMenu_SpawnEntry(
409 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
410 exit.classname = "hud_minigamemenu_exit";
411 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
415 // Open the minigame menu panel
416 void HUD_MinigameMenu_Open()
418 if ( !HUD_MinigameMenu_IsOpened() )
420 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
421 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
422 HUD_MinigameMenu_last_entry );
423 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
424 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
425 HUD_MinigameMenu_last_entry );
426 HUD_MinigameMenu_CurrentButton();
427 HUD_MinigameMenu_activeitem = NULL;
431 // Handles mouse input on to minigame menu panel
432 void HUD_MinigameMenu_MouseInput()
434 panel = HUD_PANEL(MINIGAMEMENU);
436 HUD_Panel_LoadCvars();
440 panel_pos += '1 1 0' * panel_bg_padding;
441 panel_size -= '2 2 0' * panel_bg_padding;
446 panel_pos_y += hud_fontsize_y*2;
448 HUD_MinigameMenu_activeitem = NULL;
450 for ( e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
452 sz = eX*panel_size_x + eY*e.size_y;
455 if ( !HUD_MinigameMenu_activeitem && mousepos_y >= panel_pos_y && mousepos_y <= panel_pos_y + sz_y )
457 HUD_MinigameMenu_activeitem = e;
464 void HUD_MinigameMenu_DrawEntry(vector pos, string s, vector fontsize, vector color)
466 minigame_drawstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
467 fontsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
469 // Draw a color-coded menu
470 void HUD_MinigameMenu_DrawColoredEntry(vector pos, string s, vector fontsize)
472 minigame_drawcolorcodedstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
473 fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
476 // Minigame menu panel UI
477 void HUD_MinigameMenu ()
479 if ( !HUD_MinigameMenu_IsOpened() )
482 HUD_Panel_LoadCvars();
489 panel_pos += '1 1 0' * panel_bg_padding;
490 panel_size -= '2 2 0' * panel_bg_padding;
493 HUD_MinigameMenu_DrawEntry(panel_pos,_("Minigames"),hud_fontsize*2,'0.25 0.47 0.72');
494 panel_pos_y += hud_fontsize_y*2;
499 vector imgsz = '22 22 0'; // NOTE: if changed, edit where HUD_MinigameMenu_activeitem is selected
500 for ( entity e = HUD_MinigameMenu_entries; e != NULL; e = e.list_next )
512 drawfill(panel_pos, eX*panel_size_x + eY*itemh, e.colormod,
513 panel_fg_alpha, DRAWFLAG_NORMAL);
519 drawpic( panel_pos+offset, e.model, imgsz, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
521 offset_y = (imgsz_y-e.size_y) / 2;
525 HUD_MinigameMenu_DrawColoredEntry(panel_pos+offset,e.message,e.size);
527 HUD_MinigameMenu_DrawEntry(panel_pos+offset,e.message,e.size,color);
529 if ( e == HUD_MinigameMenu_activeitem )
530 drawfill(panel_pos, eX*panel_size_x + eY*itemh,'1 1 1', 0.25, DRAWFLAG_ADDITIVE);
532 panel_pos_y += itemh;
536 // ====================================================================
537 // Minigame Help Panel
538 // ====================================================================
540 void HUD_MinigameHelp()
544 if(!autocvar__hud_configure)
546 if (!active_minigame)
548 help_message = active_minigame.message;
551 help_message = "Minigame message";
556 HUD_Panel_LoadCvars();
565 pos += '1 1 0' * panel_bg_padding;
566 mySize -= '2 2 0' * panel_bg_padding;
569 minigame_drawcolorcodedstring_wrapped( mySize_x, pos, help_message,
570 hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5 );
573 // ====================================================================
574 // Minigame Panel Input
575 // ====================================================================
576 float HUD_Minigame_InputEvent(float bInputType, float nPrimary, float nSecondary)
579 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure )
584 mousepos_x = nPrimary;
585 mousepos_y = nSecondary;
586 if ( active_minigame && HUD_mouse_over(HUD_PANEL(MINIGAMEBOARD)) )
587 active_minigame.minigame_event(active_minigame,"mouse_moved",mousepos);
593 if(bInputType == 0) {
594 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
595 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
596 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
597 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
598 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
600 else if(bInputType == 1) {
601 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
602 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
603 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
604 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
605 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
609 string con_keys = findkeysforcommand("toggleconsole", 0);
610 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
612 for (i = 0; i < keys; ++i)
614 if(nPrimary == stof(argv(i)))
618 if ( active_minigame && ( bInputType == 0 || bInputType == 1 ) )
621 string action = bInputType == 0 ? "pressed" : "released";
622 if ( nPrimary >= K_MOUSE1 && nPrimary <= K_MOUSE16 )
624 if ( HUD_mouse_over(HUD_PANEL(MINIGAMEBOARD)) )
630 if ( device && active_minigame.minigame_event(
631 active_minigame,strcat(device,"_",action),nPrimary) )
634 /// TODO: bInputType == 2?
637 if ( bInputType == 0 )
639 if ( nPrimary == K_MOUSE1 && HUD_MinigameMenu_activeitem &&
640 HUD_mouse_over(HUD_PANEL(MINIGAMEMENU)) )
642 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
645 if ( nPrimary == K_UPARROW || nPrimary == K_KP_UPARROW )
647 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_prev )
648 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_prev;
650 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_last_entry;
653 else if ( nPrimary == K_DOWNARROW || nPrimary == K_KP_DOWNARROW )
655 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_next )
656 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_next;
658 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
661 else if ( nPrimary == K_HOME || nPrimary == K_KP_HOME )
663 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
666 else if ( nPrimary == K_END || nPrimary == K_KP_END )
668 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
671 else if ( nPrimary == K_KP_ENTER || nPrimary == K_ENTER || nPrimary == K_SPACE )
673 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
676 else if ( nPrimary == K_ESCAPE )
678 HUD_MinigameMenu_Close(NULL, NULL, NULL);
688 void HUD_Minigame_Mouse()
690 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
693 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAMEMENU)) )
694 HUD_MinigameMenu_MouseInput();