1 #include "cl_minigames_hud.qh"
2 #include "minigames.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 newentry, entity prev)
84 if ( !HUD_MinigameMenu_entries )
86 HUD_MinigameMenu_entries = newentry;
87 HUD_MinigameMenu_last_entry = newentry;
91 newentry.list_prev = prev;
92 newentry.list_next = prev.list_next;
94 prev.list_next.list_prev = newentry;
96 HUD_MinigameMenu_last_entry = newentry;
97 prev.list_next = newentry;
102 // minigame menu item uder the mouse
103 entity HUD_MinigameMenu_activeitem;
105 // Click the given item
106 void HUD_MinigameMenu_Click(entity menuitem)
110 WITH(entity, self, menuitem, menuitem.use());
114 // Minigame menu options: Remove the given entry
115 // Precondition: the given entry is actually in the list
116 void HUD_MinigameMenu_EraseEntry ( entity e )
118 // remove child items (if any)
121 HUD_MinigameMenu_Click(e);
125 e.list_prev.list_next = e.list_next;
127 HUD_MinigameMenu_entries = e.list_next;
130 e.list_next.list_prev = e.list_prev;
132 HUD_MinigameMenu_last_entry = e.list_prev;
134 if ( HUD_MinigameMenu_activeitem == e )
135 HUD_MinigameMenu_activeitem = world;
140 // Minigame menu options: create entry
141 entity HUD_MinigameMenu_SpawnEntry(string s, vector offset, vector fontsize, vector color,void() click)
143 entity entry = spawn();
145 entry.origin = offset;
146 entry.size = fontsize;
147 entry.colormod = color;
150 panel_pos_y += fontsize_y;
154 // Spawn a child entry of a collapsable entry
155 entity HUD_MinigameMenu_SpawnSubEntry(string s, void() click, entity parent)
157 vector item_fontsize = hud_fontsize*1.25;
158 vector item_offset = '1 0 0' * item_fontsize_x;
159 entity item = HUD_MinigameMenu_SpawnEntry(
160 s,item_offset,item_fontsize,'0.8 0.8 0.8', click );
165 // Click action for Create sub-entries
166 void HUD_MinigameMenu_ClickCreate_Entry()
168 minigame_cmd("create ",self.netname);
171 // Helper click action for collapsible entries
172 // returns true when you have to create the sub-entries
173 bool HUD_MinigameMenu_Click_ExpandCollapse()
176 if ( self.flags & 2 )
178 if ( HUD_MinigameMenu_activeitem &&
179 HUD_MinigameMenu_activeitem.owner == self )
180 HUD_MinigameMenu_activeitem = world;
182 for ( e = self.list_next; e != world && e.owner == self; e = self.list_next )
185 HUD_MinigameMenu_Click(e);
186 self.list_next = e.list_next;
189 if ( self.list_next )
190 self.list_next.list_prev = self;
192 HUD_MinigameMenu_last_entry = self;
196 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
198 if ( e.flags & 2 && e.origin_x == self.origin_x)
199 HUD_MinigameMenu_Click(e);
209 // Click action for the Create menu
210 void HUD_MinigameMenu_ClickCreate()
212 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
216 FOREACH(Minigames, true, LAMBDA(
218 curr = HUD_MinigameMenu_SpawnSubEntry(
219 it.message, HUD_MinigameMenu_ClickCreate_Entry, self );
220 curr.netname = it.netname;
221 curr.model = strzone(minigame_texture(strcat(it.netname,"/icon")));
222 HUD_MinigameMenu_InsertEntry( curr, prev );
228 // Click action for Join sub-entries
229 void HUD_MinigameMenu_ClickJoin_Entry()
231 minigame_cmd("join ",self.netname);
232 HUD_MinigameMenu_EraseEntry(self);
235 // Click action for the Join menu
236 void HUD_MinigameMenu_ClickJoin()
238 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
243 while( (e = find(e,classname,"minigame")) )
245 if ( e != active_minigame )
247 curr = HUD_MinigameMenu_SpawnSubEntry(
248 e.netname, HUD_MinigameMenu_ClickJoin_Entry, self );
249 curr.netname = e.netname;
250 curr.model = strzone(minigame_texture(strcat(e.descriptor.netname,"/icon")));
251 HUD_MinigameMenu_InsertEntry( curr, prev );
258 /*// Temporary placeholder for un-implemented Click actions
259 void HUD_MinigameMenu_ClickNoop()
261 dprint("Placeholder for ",self.message,"\n");
264 // Click action for Quit
265 void HUD_MinigameMenu_ClickQuit()
267 deactivate_minigame();
271 // Click action for Invite sub-entries
272 void HUD_MinigameMenu_ClickInvite_Entry()
274 minigame_cmd("invite #",self.netname);
277 // Click action for the Invite menu
278 void HUD_MinigameMenu_ClickInvite()
280 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
284 for(int i = 0; i < maxclients; ++i)
286 if ( player_localnum != i && playerslots[i] && entcs_GetName(i) != "" &&
287 !findfloat(world,minigame_playerslot,i+1) && playerslots[i].ping )
289 e = HUD_MinigameMenu_SpawnSubEntry(
290 strzone(entcs_GetName(i)), HUD_MinigameMenu_ClickInvite_Entry,
293 e.netname = strzone(ftos(i+1));
295 HUD_MinigameMenu_InsertEntry(e,prev);
302 void HUD_MinigameMenu_ClickCustomEntry()
304 if ( active_minigame )
305 active_minigame.minigame_event(active_minigame,"menu_click",self.netname);
308 // Adds a game-specific entry to the menu
309 void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
311 entity e = HUD_MinigameMenu_SpawnSubEntry(
312 menumessage, HUD_MinigameMenu_ClickCustomEntry, parent );
313 e.netname = event_arg;
314 HUD_MinigameMenu_InsertEntry(e, parent);
315 //dprint("CustomEntry ",ftos(num_for_edict(parent))," ",menumessage," ",event_arg,"\n");
318 // Click action for the Current Game menu
319 void HUD_MinigameMenu_ClickCurrentGame()
321 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
323 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
324 _("Quit"), HUD_MinigameMenu_ClickQuit, self ), self);
326 active_minigame.minigame_event(active_minigame,"menu_show",self);
328 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
329 _("Invite"), HUD_MinigameMenu_ClickInvite, self), self);
332 // Whether the minigame menu panel is open
333 bool HUD_MinigameMenu_IsOpened()
335 return HUD_MinigameMenu_entries != NULL;
338 // Close the minigame menu panel
339 void HUD_MinigameMenu_Close()
341 if ( HUD_MinigameMenu_IsOpened() )
344 for ( e = HUD_MinigameMenu_entries; e != world; e = p )
349 HUD_MinigameMenu_entries = world;
350 HUD_MinigameMenu_last_entry = world;
351 HUD_MinigameMenu_activeitem = world;
352 if(autocvar_hud_cursormode)
353 if ( !autocvar__hud_configure )
358 // toggle a button to manage the current game
359 void HUD_MinigameMenu_CurrentButton()
362 if ( active_minigame )
364 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
365 if ( e.classname == "hud_minigamemenu_exit" )
367 HUD_MinigameMenu_EraseEntry(e);
370 entity currb = HUD_MinigameMenu_SpawnEntry(
371 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
372 currb.classname = "hud_minigamemenu_current";
373 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
374 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
375 HUD_MinigameMenu_Click(currb);
380 for ( e = HUD_MinigameMenu_last_entry; e != world; e = p.list_prev )
383 if ( e.classname == "hud_minigamemenu_current" )
387 p = HUD_MinigameMenu_last_entry;
388 HUD_MinigameMenu_EraseEntry(e);
392 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
393 if ( e.classname == "hud_minigamemenu_exit" )
395 entity exit = HUD_MinigameMenu_SpawnEntry(
396 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
397 exit.classname = "hud_minigamemenu_exit";
398 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
402 // Open the minigame menu panel
403 void HUD_MinigameMenu_Open()
405 if ( !HUD_MinigameMenu_IsOpened() )
407 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
408 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
409 HUD_MinigameMenu_last_entry );
410 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
411 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
412 HUD_MinigameMenu_last_entry );
413 HUD_MinigameMenu_CurrentButton();
414 HUD_MinigameMenu_activeitem = world;
415 if(autocvar_hud_cursormode)
420 // Handles mouse input on to minigame menu panel
421 void HUD_MinigameMenu_MouseInput()
423 panel = HUD_PANEL(MINIGAME_MENU);
425 HUD_Panel_UpdateCvars();
429 panel_pos += '1 1 0' * panel_bg_padding;
430 panel_size -= '2 2 0' * panel_bg_padding;
435 panel_pos_y += hud_fontsize_y*2;
437 HUD_MinigameMenu_activeitem = world;
439 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
441 sz = eX*panel_size_x + eY*e.size_y;
444 if ( !HUD_MinigameMenu_activeitem && mousepos_y >= panel_pos_y && mousepos_y <= panel_pos_y + sz_y )
446 HUD_MinigameMenu_activeitem = e;
453 void HUD_MinigameMenu_DrawEntry(vector pos, string s, vector fontsize, vector color)
455 minigame_drawstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
456 fontsize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
458 // Draw a color-coded menu
459 void HUD_MinigameMenu_DrawColoredEntry(vector pos, string s, vector fontsize)
461 minigame_drawcolorcodedstring_trunc(panel_size_x-pos_x+panel_pos_x, pos, s,
462 fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
465 // Minigame menu panel UI
466 void HUD_MinigameMenu ()
468 if ( !HUD_MinigameMenu_IsOpened() )
471 HUD_Panel_UpdateCvars();
477 panel_pos += '1 1 0' * panel_bg_padding;
478 panel_size -= '2 2 0' * panel_bg_padding;
481 HUD_MinigameMenu_DrawEntry(panel_pos,_("Minigames"),hud_fontsize*2,'0.25 0.47 0.72');
482 panel_pos_y += hud_fontsize_y*2;
487 vector imgsz = '22 22 0'; // NOTE: if changed, edit where HUD_MinigameMenu_activeitem is selected
488 for ( entity e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
500 drawfill(panel_pos, eX*panel_size_x + eY*itemh, e.colormod,
501 panel_fg_alpha, DRAWFLAG_NORMAL);
507 drawpic( panel_pos+offset, e.model, imgsz, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
509 offset_y = (imgsz_y-e.size_y) / 2;
513 HUD_MinigameMenu_DrawColoredEntry(panel_pos+offset,e.message,e.size);
515 HUD_MinigameMenu_DrawEntry(panel_pos+offset,e.message,e.size,color);
517 if ( e == HUD_MinigameMenu_activeitem )
518 drawfill(panel_pos, eX*panel_size_x + eY*itemh,'1 1 1', 0.25, DRAWFLAG_ADDITIVE);
520 panel_pos_y += itemh;
524 // ====================================================================
525 // Minigame Help Panel
526 // ====================================================================
528 void HUD_MinigameHelp()
532 if(!autocvar__hud_configure)
533 help_message = active_minigame.message;
535 help_message = "Minigame message";
540 HUD_Panel_UpdateCvars();
549 pos += '1 1 0' * panel_bg_padding;
550 mySize -= '2 2 0' * panel_bg_padding;
553 minigame_drawcolorcodedstring_wrapped( mySize_x, pos, help_message,
554 hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5 );
557 // ====================================================================
558 // Minigame Panel Input
559 // ====================================================================
560 float HUD_Minigame_InputEvent(float bInputType, float nPrimary, float nSecondary)
563 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure )
568 mousepos_x = nPrimary;
569 mousepos_y = nSecondary;
570 if ( minigame_isactive() && HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
571 active_minigame.minigame_event(active_minigame,"mouse_moved",mousepos);
577 if(bInputType == 0) {
578 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
579 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
580 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
581 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
582 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
584 else if(bInputType == 1) {
585 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
586 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
587 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
588 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
589 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
594 con_keys = findkeysforcommand("toggleconsole", 0);
595 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
596 for (int i = 0; i < keys; ++i)
598 if(nPrimary == stof(argv(i)))
602 if ( minigame_isactive() && ( bInputType == 0 || bInputType == 1 ) )
605 string action = bInputType == 0 ? "pressed" : "released";
606 if ( nPrimary >= K_MOUSE1 && nPrimary <= K_MOUSE16 )
608 if ( HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
614 if ( device && active_minigame.minigame_event(
615 active_minigame,strcat(device,"_",action),nPrimary) )
618 /// TODO: bInputType == 2?
621 if ( bInputType == 0 )
623 if ( nPrimary == K_MOUSE1 && HUD_MinigameMenu_activeitem &&
624 HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
626 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
629 if ( nPrimary == K_UPARROW || nPrimary == K_KP_UPARROW )
631 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_prev )
632 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_prev;
634 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_last_entry;
637 else if ( nPrimary == K_DOWNARROW || nPrimary == K_KP_DOWNARROW )
639 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_next )
640 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_next;
642 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
645 else if ( nPrimary == K_HOME || nPrimary == K_KP_HOME )
647 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
650 else if ( nPrimary == K_END || nPrimary == K_KP_END )
652 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
655 else if ( nPrimary == K_KP_ENTER || nPrimary == K_ENTER || nPrimary == K_SPACE )
657 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
660 else if ( nPrimary == K_ESCAPE )
662 HUD_MinigameMenu_Close();
672 void HUD_Minigame_Mouse()
674 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
677 if(!autocvar_hud_cursormode)
679 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
681 mousepos_x = bound(0, mousepos_x, vid_conwidth);
682 mousepos_y = bound(0, mousepos_y, vid_conheight);
685 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
686 HUD_MinigameMenu_MouseInput();
688 vector cursorsize = '32 32 0';
689 drawpic(mousepos-'8 4 0', strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"),
690 cursorsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
693 bool HUD_Minigame_Showpanels()
695 return (HUD_MinigameMenu_IsOpened() && minigame_isactive());