1 #include "cl_minigames_hud.qh"
3 #include <common/ent_cs.qh>
5 #include "minigames.qh"
9 #include <client/hud/hud_config.qh>
10 #include <client/mapvoting.qh>
12 // whether the mouse is over the given panel
13 bool HUD_mouse_over(entity somepanel)
15 vector pos = stov(cvar_string(strcat("hud_panel_", somepanel.panel_name, "_pos")));
16 vector sz = stov(cvar_string(strcat("hud_panel_", somepanel.panel_name, "_size")));
17 return mousepos_x >= pos_x*vid_conwidth && mousepos_x <= (pos_x+sz_x)*vid_conwidth &&
18 mousepos_y >= pos_y*vid_conheight && mousepos_y <= (pos_y+sz_y)*vid_conheight ;
21 // ====================================================================
23 // ====================================================================
25 // Draws the minigame game board
26 void HUD_MinigameBoard ()
28 entity hud_minigame = world;
30 if(!autocvar__hud_configure)
31 hud_minigame = active_minigame.descriptor;
33 hud_minigame = minigame_get_descriptor("nmm");
38 HUD_Panel_UpdateCvars();
45 hud_minigame.minigame_hud_board(pos,mySize);
48 // ====================================================================
50 // ====================================================================
51 // Draws the minigame status panel
52 void HUD_MinigameStatus ()
54 entity hud_minigame = world;
56 if(!autocvar__hud_configure)
57 hud_minigame = active_minigame.descriptor;
59 hud_minigame = minigame_get_descriptor("nmm");
64 HUD_Panel_UpdateCvars();
73 pos += '1 1 0' * panel_bg_padding;
74 mySize -= '2 2 0' * panel_bg_padding;
77 hud_minigame.minigame_hud_status(pos,mySize);
80 // ====================================================================
82 // ====================================================================
84 // Minigame menu options: list head
85 entity HUD_MinigameMenu_entries;
86 // Minigame menu options: list tail
87 entity HUD_MinigameMenu_last_entry;
89 // Minigame menu options: insert entry after the given location
90 void HUD_MinigameMenu_InsertEntry(entity newentry, entity prev)
92 if ( !HUD_MinigameMenu_entries )
94 HUD_MinigameMenu_entries = newentry;
95 HUD_MinigameMenu_last_entry = newentry;
99 newentry.list_prev = prev;
100 newentry.list_next = prev.list_next;
101 if ( prev.list_next )
102 prev.list_next.list_prev = newentry;
104 HUD_MinigameMenu_last_entry = newentry;
105 prev.list_next = newentry;
110 // minigame menu item uder the mouse
111 entity HUD_MinigameMenu_activeitem;
113 // Click the given item
114 void HUD_MinigameMenu_Click(entity menuitem)
118 menuitem.use(menuitem, NULL, NULL);
122 // Minigame menu options: Remove the given entry
123 // Precondition: the given entry is actually in the list
124 void HUD_MinigameMenu_EraseEntry ( entity e )
126 // remove child items (if any)
129 HUD_MinigameMenu_Click(e);
133 e.list_prev.list_next = e.list_next;
135 HUD_MinigameMenu_entries = e.list_next;
138 e.list_next.list_prev = e.list_prev;
140 HUD_MinigameMenu_last_entry = e.list_prev;
142 if ( HUD_MinigameMenu_activeitem == e )
143 HUD_MinigameMenu_activeitem = world;
148 // Minigame menu options: create entry
149 entity HUD_MinigameMenu_SpawnEntry(string s, vector offset, vector fontsize, vector color,void(entity, entity, entity) click)
151 entity entry = spawn();
153 entry.origin = offset;
154 entry.size = fontsize;
155 entry.colormod = color;
158 panel_pos_y += fontsize_y;
162 // Spawn a child entry of a collapsable entry
163 entity HUD_MinigameMenu_SpawnSubEntry(string s, void(entity, entity, entity) click, entity parent)
165 vector item_fontsize = hud_fontsize*1.25;
166 vector item_offset = '1 0 0' * item_fontsize_x;
167 entity item = HUD_MinigameMenu_SpawnEntry(
168 s,item_offset,item_fontsize,'0.8 0.8 0.8', click );
173 // Click action for Create sub-entries
174 void HUD_MinigameMenu_ClickCreate_Entry(entity this, entity actor, entity trigger)
176 minigame_cmd("create ", this.netname);
179 // Helper click action for collapsible entries
180 // returns true when you have to create the sub-entries
181 bool HUD_MinigameMenu_Click_ExpandCollapse(entity this)
184 if ( this.flags & 2 )
186 if ( HUD_MinigameMenu_activeitem &&
187 HUD_MinigameMenu_activeitem.owner == this )
188 HUD_MinigameMenu_activeitem = world;
190 for ( e = this.list_next; e != world && e.owner == this; e = this.list_next )
193 HUD_MinigameMenu_Click(e);
194 this.list_next = e.list_next;
197 if ( this.list_next )
198 this.list_next.list_prev = this;
200 HUD_MinigameMenu_last_entry = this;
204 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
206 if ( e.flags & 2 && e.origin_x == this.origin_x)
207 HUD_MinigameMenu_Click(e);
217 // Click action for the Create menu
218 void HUD_MinigameMenu_ClickCreate(entity this, entity actor, entity trigger)
220 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
224 FOREACH(Minigames, true, {
225 curr = HUD_MinigameMenu_SpawnSubEntry(it.message, HUD_MinigameMenu_ClickCreate_Entry, this);
226 curr.netname = it.netname;
227 curr.model = strzone(minigame_texture(strcat(it.netname,"/icon")));
228 HUD_MinigameMenu_InsertEntry( curr, prev );
234 // Click action for Join sub-entries
235 void HUD_MinigameMenu_ClickJoin_Entry(entity this, entity actor, entity trigger)
237 minigame_cmd("join ",this.netname);
238 HUD_MinigameMenu_EraseEntry(this);
241 // Click action for the Join menu
242 void HUD_MinigameMenu_ClickJoin(entity this, entity actor, entity trigger)
244 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
249 while( (e = find(e,classname,"minigame")) )
251 if ( e != active_minigame )
253 curr = HUD_MinigameMenu_SpawnSubEntry(
254 e.netname, HUD_MinigameMenu_ClickJoin_Entry, this );
255 curr.netname = e.netname;
256 curr.model = strzone(minigame_texture(strcat(e.descriptor.netname,"/icon")));
257 HUD_MinigameMenu_InsertEntry( curr, prev );
264 /*// Temporary placeholder for un-implemented Click actions
265 void HUD_MinigameMenu_ClickNoop()
267 dprint("Placeholder for ",self.message,"\n");
270 // Click action for Quit
271 void HUD_MinigameMenu_ClickQuit(entity this, entity actor, entity trigger)
273 deactivate_minigame();
277 // Click action for Invite sub-entries
278 void HUD_MinigameMenu_ClickInvite_Entry(entity this, entity actor, entity trigger)
280 minigame_cmd("invite #",this.netname);
283 // Click action for the Invite menu
284 void HUD_MinigameMenu_ClickInvite(entity this, entity actor, entity trigger)
286 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
290 for(int i = 0; i < maxclients; ++i)
292 if ( player_localnum != i && playerslots[i] && entcs_GetName(i) != "" &&
293 !findfloat(world,minigame_playerslot,i+1) && playerslots[i].ping )
295 e = HUD_MinigameMenu_SpawnSubEntry(
296 strzone(entcs_GetName(i)), HUD_MinigameMenu_ClickInvite_Entry,
299 e.netname = strzone(ftos(i+1));
301 HUD_MinigameMenu_InsertEntry(e,prev);
308 void HUD_MinigameMenu_ClickCustomEntry(entity this, entity actor, entity trigger)
310 if ( active_minigame )
311 active_minigame.minigame_event(active_minigame,"menu_click",this.netname);
314 // Adds a game-specific entry to the menu
315 void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
317 entity e = HUD_MinigameMenu_SpawnSubEntry(
318 menumessage, HUD_MinigameMenu_ClickCustomEntry, parent );
319 e.netname = event_arg;
320 HUD_MinigameMenu_InsertEntry(e, parent);
321 //dprint("CustomEntry ",ftos(num_for_edict(parent))," ",menumessage," ",event_arg,"\n");
324 // Click action for the Current Game menu
325 void HUD_MinigameMenu_ClickCurrentGame(entity this, entity actor, entity trigger)
327 if ( HUD_MinigameMenu_Click_ExpandCollapse(this) )
329 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
330 _("Quit"), HUD_MinigameMenu_ClickQuit, this ), this);
332 active_minigame.minigame_event(active_minigame,"menu_show",this);
334 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
335 _("Invite"), HUD_MinigameMenu_ClickInvite, this), this);
338 // Whether the minigame menu panel is open
339 bool HUD_MinigameMenu_IsOpened()
341 return HUD_MinigameMenu_entries != NULL;
344 // Close the minigame menu panel
345 void HUD_MinigameMenu_Close(entity this, entity actor, entity trigger)
347 if ( HUD_MinigameMenu_IsOpened() )
350 for ( e = HUD_MinigameMenu_entries; e != world; e = p )
355 HUD_MinigameMenu_entries = world;
356 HUD_MinigameMenu_last_entry = world;
357 HUD_MinigameMenu_activeitem = world;
358 if(autocvar_hud_cursormode)
359 if ( !autocvar__hud_configure )
364 // toggle a button to manage the current game
365 void HUD_MinigameMenu_CurrentButton()
368 if ( active_minigame )
370 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
371 if ( e.classname == "hud_minigamemenu_exit" )
373 HUD_MinigameMenu_EraseEntry(e);
376 entity currb = HUD_MinigameMenu_SpawnEntry(
377 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
378 currb.classname = "hud_minigamemenu_current";
379 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
380 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
381 HUD_MinigameMenu_Click(currb);
386 for ( e = HUD_MinigameMenu_last_entry; e != world; e = p.list_prev )
389 if ( e.classname == "hud_minigamemenu_current" )
393 p = HUD_MinigameMenu_last_entry;
394 HUD_MinigameMenu_EraseEntry(e);
398 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
399 if ( e.classname == "hud_minigamemenu_exit" )
401 entity exit = HUD_MinigameMenu_SpawnEntry(
402 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
403 exit.classname = "hud_minigamemenu_exit";
404 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
408 // Open the minigame menu panel
409 void HUD_MinigameMenu_Open()
411 if ( !HUD_MinigameMenu_IsOpened() )
413 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
414 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
415 HUD_MinigameMenu_last_entry );
416 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
417 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
418 HUD_MinigameMenu_last_entry );
419 HUD_MinigameMenu_CurrentButton();
420 HUD_MinigameMenu_activeitem = world;
421 if(autocvar_hud_cursormode)
426 // Handles mouse input on to minigame menu panel
427 void HUD_MinigameMenu_MouseInput()
429 panel = HUD_PANEL(MINIGAME_MENU);
431 HUD_Panel_UpdateCvars();
435 panel_pos += '1 1 0' * panel_bg_padding;
436 panel_size -= '2 2 0' * panel_bg_padding;
441 panel_pos_y += hud_fontsize_y*2;
443 HUD_MinigameMenu_activeitem = world;
445 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
447 sz = eX*panel_size_x + eY*e.size_y;
450 if ( !HUD_MinigameMenu_activeitem && mousepos_y >= panel_pos_y && mousepos_y <= panel_pos_y + sz_y )
452 HUD_MinigameMenu_activeitem = e;
459 void HUD_MinigameMenu_DrawEntry(vector pos, string s, vector fontsize, vector color)
461 minigame_drawstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
462 fontsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
464 // Draw a color-coded menu
465 void HUD_MinigameMenu_DrawColoredEntry(vector pos, string s, vector fontsize)
467 minigame_drawcolorcodedstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
468 fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
471 // Minigame menu panel UI
472 void HUD_MinigameMenu ()
474 if ( !HUD_MinigameMenu_IsOpened() )
477 HUD_Panel_UpdateCvars();
483 panel_pos += '1 1 0' * panel_bg_padding;
484 panel_size -= '2 2 0' * panel_bg_padding;
487 HUD_MinigameMenu_DrawEntry(panel_pos,_("Minigames"),hud_fontsize*2,'0.25 0.47 0.72');
488 panel_pos_y += hud_fontsize_y*2;
493 vector imgsz = '22 22 0'; // NOTE: if changed, edit where HUD_MinigameMenu_activeitem is selected
494 for ( entity e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
506 drawfill(panel_pos, eX*panel_size_x + eY*itemh, e.colormod,
507 panel_fg_alpha, DRAWFLAG_NORMAL);
513 drawpic( panel_pos+offset, e.model, imgsz, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
515 offset_y = (imgsz_y-e.size_y) / 2;
519 HUD_MinigameMenu_DrawColoredEntry(panel_pos+offset,e.message,e.size);
521 HUD_MinigameMenu_DrawEntry(panel_pos+offset,e.message,e.size,color);
523 if ( e == HUD_MinigameMenu_activeitem )
524 drawfill(panel_pos, eX*panel_size_x + eY*itemh,'1 1 1', 0.25, DRAWFLAG_ADDITIVE);
526 panel_pos_y += itemh;
530 // ====================================================================
531 // Minigame Help Panel
532 // ====================================================================
534 void HUD_MinigameHelp()
538 if(!autocvar__hud_configure)
539 help_message = active_minigame.message;
541 help_message = "Minigame message";
546 HUD_Panel_UpdateCvars();
555 pos += '1 1 0' * panel_bg_padding;
556 mySize -= '2 2 0' * panel_bg_padding;
559 minigame_drawcolorcodedstring_wrapped( mySize_x, pos, help_message,
560 hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5 );
563 // ====================================================================
564 // Minigame Panel Input
565 // ====================================================================
566 float HUD_Minigame_InputEvent(float bInputType, float nPrimary, float nSecondary)
569 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure )
574 mousepos_x = nPrimary;
575 mousepos_y = nSecondary;
576 if ( minigame_isactive() && HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
577 active_minigame.minigame_event(active_minigame,"mouse_moved",mousepos);
583 if(bInputType == 0) {
584 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
585 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
586 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
587 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
588 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
590 else if(bInputType == 1) {
591 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
592 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
593 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
594 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
595 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
600 con_keys = findkeysforcommand("toggleconsole", 0);
601 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
602 for (int i = 0; i < keys; ++i)
604 if(nPrimary == stof(argv(i)))
608 if ( minigame_isactive() && ( bInputType == 0 || bInputType == 1 ) )
611 string action = bInputType == 0 ? "pressed" : "released";
612 if ( nPrimary >= K_MOUSE1 && nPrimary <= K_MOUSE16 )
614 if ( HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
620 if ( device && active_minigame.minigame_event(
621 active_minigame,strcat(device,"_",action),nPrimary) )
624 /// TODO: bInputType == 2?
627 if ( bInputType == 0 )
629 if ( nPrimary == K_MOUSE1 && HUD_MinigameMenu_activeitem &&
630 HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
632 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
635 if ( nPrimary == K_UPARROW || nPrimary == K_KP_UPARROW )
637 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_prev )
638 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_prev;
640 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_last_entry;
643 else if ( nPrimary == K_DOWNARROW || nPrimary == K_KP_DOWNARROW )
645 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_next )
646 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_next;
648 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
651 else if ( nPrimary == K_HOME || nPrimary == K_KP_HOME )
653 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
656 else if ( nPrimary == K_END || nPrimary == K_KP_END )
658 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
661 else if ( nPrimary == K_KP_ENTER || nPrimary == K_ENTER || nPrimary == K_SPACE )
663 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
666 else if ( nPrimary == K_ESCAPE )
668 HUD_MinigameMenu_Close(NULL, NULL, NULL);
678 void HUD_Minigame_Mouse()
680 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
683 if(!autocvar_hud_cursormode)
685 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
687 mousepos_x = bound(0, mousepos_x, vid_conwidth);
688 mousepos_y = bound(0, mousepos_y, vid_conheight);
691 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
692 HUD_MinigameMenu_MouseInput();
694 draw_cursor_normal(mousepos, '1 1 1', panel_fg_alpha);
697 bool HUD_Minigame_Showpanels()
699 return (HUD_MinigameMenu_IsOpened() && minigame_isactive());