1 #include "minigames.qh"
2 #include "../../client/mapvoting.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 new, entity prev)
84 if ( !HUD_MinigameMenu_entries )
86 HUD_MinigameMenu_entries = new;
87 HUD_MinigameMenu_last_entry = new;
92 new.list_next = prev.list_next;
94 prev.list_next.list_prev = new;
96 HUD_MinigameMenu_last_entry = new;
102 // minigame menu item uder the mouse
103 entity HUD_MinigameMenu_activeitem;
105 // Click the given item
106 void HUD_MinigameMenu_Click(entity menuitem)
117 // Minigame menu options: Remove the given entry
118 // Precondition: the given entry is actually in the list
119 void HUD_MinigameMenu_EraseEntry ( entity e )
121 // remove child items (if any)
124 HUD_MinigameMenu_Click(e);
128 e.list_prev.list_next = e.list_next;
130 HUD_MinigameMenu_entries = e.list_next;
133 e.list_next.list_prev = e.list_prev;
135 HUD_MinigameMenu_last_entry = e.list_prev;
137 if ( HUD_MinigameMenu_activeitem == e )
138 HUD_MinigameMenu_activeitem = world;
143 // Minigame menu options: create entry
144 entity HUD_MinigameMenu_SpawnEntry(string s, vector offset, vector fontsize, vector color,void() click)
146 entity entry = spawn();
148 entry.origin = offset;
149 entry.size = fontsize;
150 entry.colormod = color;
153 panel_pos_y += fontsize_y;
157 // Spawn a child entry of a collapsable entry
158 entity HUD_MinigameMenu_SpawnSubEntry(string s, void() click, entity parent)
160 vector item_fontsize = hud_fontsize*1.25;
161 vector item_offset = '1 0 0' * item_fontsize_x;
162 entity item = HUD_MinigameMenu_SpawnEntry(
163 s,item_offset,item_fontsize,'0.8 0.8 0.8', click );
168 // Click action for Create sub-entries
169 void HUD_MinigameMenu_ClickCreate_Entry()
171 minigame_cmd("create ",self.netname);
174 // Helper click action for collapsible entries
175 // returns true when you have to create the sub-entries
176 bool HUD_MinigameMenu_Click_ExpandCollapse()
179 if ( self.flags & 2 )
181 if ( HUD_MinigameMenu_activeitem &&
182 HUD_MinigameMenu_activeitem.owner == self )
183 HUD_MinigameMenu_activeitem = world;
185 for ( e = self.list_next; e != world && e.owner == self; e = self.list_next )
188 HUD_MinigameMenu_Click(e);
189 self.list_next = e.list_next;
192 if ( self.list_next )
193 self.list_next.list_prev = self;
195 HUD_MinigameMenu_last_entry = self;
199 for ( e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
201 if ( e.flags & 2 && e.origin_x == self.origin_x)
202 HUD_MinigameMenu_Click(e);
212 // Click action for the Create menu
213 void HUD_MinigameMenu_ClickCreate()
215 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
220 for ( e = minigame_descriptors; e != world; e = e.list_next )
222 curr = HUD_MinigameMenu_SpawnSubEntry(
223 e.message, HUD_MinigameMenu_ClickCreate_Entry, self );
224 curr.netname = e.netname;
225 curr.model = strzone(minigame_texture(strcat(e.netname,"/icon")));
226 HUD_MinigameMenu_InsertEntry( curr, prev );
232 // Click action for Join sub-entries
233 void HUD_MinigameMenu_ClickJoin_Entry()
235 minigame_cmd("join ",self.netname);
236 HUD_MinigameMenu_EraseEntry(self);
239 // Click action for the Join menu
240 void HUD_MinigameMenu_ClickJoin()
242 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
247 while( (e = find(e,classname,"minigame")) )
249 if ( e != active_minigame )
251 curr = HUD_MinigameMenu_SpawnSubEntry(
252 e.netname, HUD_MinigameMenu_ClickJoin_Entry, self );
253 curr.netname = e.netname;
254 curr.model = strzone(minigame_texture(strcat(e.descriptor.netname,"/icon")));
255 HUD_MinigameMenu_InsertEntry( curr, prev );
262 /*// Temporary placeholder for un-implemented Click actions
263 void HUD_MinigameMenu_ClickNoop()
265 dprint("Placeholder for ",self.message,"\n");
268 // Click action for Quit
269 void HUD_MinigameMenu_ClickQuit()
274 // Click action for Invite sub-entries
275 void HUD_MinigameMenu_ClickInvite_Entry()
277 minigame_cmd("invite #",self.netname);
280 // Click action for the Invite menu
281 void HUD_MinigameMenu_ClickInvite()
283 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
287 for(int i = 0; i < maxclients; ++i)
289 if ( player_localnum != i && playerslots[i] && GetPlayerName(i) != "" &&
290 !findfloat(world,minigame_playerslot,i+1) && playerslots[i].ping )
292 e = HUD_MinigameMenu_SpawnSubEntry(
293 strzone(GetPlayerName(i)), HUD_MinigameMenu_ClickInvite_Entry,
296 e.netname = strzone(ftos(i+1));
298 HUD_MinigameMenu_InsertEntry(e,prev);
305 void HUD_MinigameMenu_ClickCustomEntry()
307 if ( active_minigame )
308 active_minigame.minigame_event(active_minigame,"menu_click",self.netname);
311 // Adds a game-specific entry to the menu
312 void HUD_MinigameMenu_CustomEntry(entity parent, string menumessage, string event_arg)
314 entity e = HUD_MinigameMenu_SpawnSubEntry(
315 menumessage, HUD_MinigameMenu_ClickCustomEntry, parent );
316 e.netname = event_arg;
317 HUD_MinigameMenu_InsertEntry(e, parent);
318 //dprint("CustomEntry ",ftos(num_for_edict(parent))," ",menumessage," ",event_arg,"\n");
321 // Click action for the Current Game menu
322 void HUD_MinigameMenu_ClickCurrentGame()
324 if ( HUD_MinigameMenu_Click_ExpandCollapse() )
326 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
327 _("Quit"), HUD_MinigameMenu_ClickQuit, self ), self);
329 active_minigame.minigame_event(active_minigame,"menu_show",self);
331 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnSubEntry(
332 _("Invite"), HUD_MinigameMenu_ClickInvite, self), self);
335 // Whether the minigame menu panel is open
336 bool HUD_MinigameMenu_IsOpened()
338 return !!HUD_MinigameMenu_entries;
341 // Close the minigame menu panel
342 void HUD_MinigameMenu_Close()
344 if ( HUD_MinigameMenu_IsOpened() )
347 for ( e = HUD_MinigameMenu_entries; e != world; e = p )
352 HUD_MinigameMenu_entries = world;
353 HUD_MinigameMenu_last_entry = world;
354 HUD_MinigameMenu_activeitem = world;
355 if(autocvar_hud_cursormode)
356 if ( !autocvar__hud_configure )
361 // toggle a button to manage the current game
362 void HUD_MinigameMenu_CurrentButton()
365 if ( active_minigame )
367 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
368 if ( e.classname == "hud_minigamemenu_exit" )
370 HUD_MinigameMenu_EraseEntry(e);
373 entity currb = HUD_MinigameMenu_SpawnEntry(
374 _("Current Game"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCurrentGame );
375 currb.classname = "hud_minigamemenu_current";
376 currb.model = strzone(minigame_texture(strcat(active_minigame.descriptor.netname,"/icon")));
377 HUD_MinigameMenu_InsertEntry(currb,HUD_MinigameMenu_last_entry);
378 HUD_MinigameMenu_Click(currb);
383 for ( e = HUD_MinigameMenu_last_entry; e != world; e = p.list_prev )
386 if ( e.classname == "hud_minigamemenu_current" )
390 p = HUD_MinigameMenu_last_entry;
391 HUD_MinigameMenu_EraseEntry(e);
395 for ( e = HUD_MinigameMenu_last_entry; e != world; e = e.list_prev )
396 if ( e.classname == "hud_minigamemenu_exit" )
398 entity exit = HUD_MinigameMenu_SpawnEntry(
399 _("Exit Menu"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_Close);
400 exit.classname = "hud_minigamemenu_exit";
401 HUD_MinigameMenu_InsertEntry ( exit, HUD_MinigameMenu_last_entry );
405 // Open the minigame menu panel
406 void HUD_MinigameMenu_Open()
408 if ( !HUD_MinigameMenu_IsOpened() )
410 HUD_MinigameMenu_InsertEntry( HUD_MinigameMenu_SpawnEntry(
411 _("Create"), '0 0 0', hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickCreate),
412 HUD_MinigameMenu_last_entry );
413 HUD_MinigameMenu_InsertEntry ( HUD_MinigameMenu_SpawnEntry(
414 _("Join"),'0 0 0',hud_fontsize*1.5,'0.7 0.84 1', HUD_MinigameMenu_ClickJoin),
415 HUD_MinigameMenu_last_entry );
416 HUD_MinigameMenu_CurrentButton();
417 HUD_MinigameMenu_activeitem = world;
418 if(autocvar_hud_cursormode)
423 // Handles mouse input on to minigame menu panel
424 void HUD_MinigameMenu_MouseInput()
426 panel = HUD_PANEL(MINIGAME_MENU);
428 HUD_Panel_UpdateCvars();
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 = world;
442 for ( e = HUD_MinigameMenu_entries; e != world; 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_UpdateCvars();
480 panel_pos += '1 1 0' * panel_bg_padding;
481 panel_size -= '2 2 0' * panel_bg_padding;
484 HUD_MinigameMenu_DrawEntry(panel_pos,_("Minigames"),hud_fontsize*2,'0.25 0.47 0.72');
485 panel_pos_y += hud_fontsize_y*2;
490 vector imgsz = '22 22 0'; // NOTE: if changed, edit where HUD_MinigameMenu_activeitem is selected
491 for ( entity e = HUD_MinigameMenu_entries; e != world; e = e.list_next )
503 drawfill(panel_pos, eX*panel_size_x + eY*itemh, e.colormod,
504 panel_fg_alpha, DRAWFLAG_NORMAL);
510 drawpic( panel_pos+offset, e.model, imgsz, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
512 offset_y = (imgsz_y-e.size_y) / 2;
516 HUD_MinigameMenu_DrawColoredEntry(panel_pos+offset,e.message,e.size);
518 HUD_MinigameMenu_DrawEntry(panel_pos+offset,e.message,e.size,color);
520 if ( e == HUD_MinigameMenu_activeitem )
521 drawfill(panel_pos, eX*panel_size_x + eY*itemh,'1 1 1', 0.25, DRAWFLAG_ADDITIVE);
523 panel_pos_y += itemh;
527 // ====================================================================
528 // Minigame Help Panel
529 // ====================================================================
531 void HUD_MinigameHelp()
535 if(!autocvar__hud_configure)
536 help_message = active_minigame.message;
538 help_message = "Minigame message";
543 HUD_Panel_UpdateCvars();
552 pos += '1 1 0' * panel_bg_padding;
553 mySize -= '2 2 0' * panel_bg_padding;
556 minigame_drawcolorcodedstring_wrapped( mySize_x, pos, help_message,
557 hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5 );
560 // ====================================================================
561 // Minigame Panel Input
562 // ====================================================================
563 float HUD_Minigame_InputEvent(float bInputType, float nPrimary, float nSecondary)
566 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure )
571 mousepos_x = nPrimary;
572 mousepos_y = nSecondary;
573 if ( minigame_isactive() && HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
574 active_minigame.minigame_event(active_minigame,"mouse_moved",mousepos);
580 if(bInputType == 0) {
581 if(nPrimary == K_ALT) hudShiftState |= S_ALT;
582 if(nPrimary == K_CTRL) hudShiftState |= S_CTRL;
583 if(nPrimary == K_SHIFT) hudShiftState |= S_SHIFT;
584 if(nPrimary == K_MOUSE1) mouseClicked |= S_MOUSE1;
585 if(nPrimary == K_MOUSE2) mouseClicked |= S_MOUSE2;
587 else if(bInputType == 1) {
588 if(nPrimary == K_ALT) hudShiftState -= (hudShiftState & S_ALT);
589 if(nPrimary == K_CTRL) hudShiftState -= (hudShiftState & S_CTRL);
590 if(nPrimary == K_SHIFT) hudShiftState -= (hudShiftState & S_SHIFT);
591 if(nPrimary == K_MOUSE1) mouseClicked -= (mouseClicked & S_MOUSE1);
592 if(nPrimary == K_MOUSE2) mouseClicked -= (mouseClicked & S_MOUSE2);
597 con_keys = findkeysforcommand("toggleconsole", 0);
598 int keys = tokenize(con_keys); // findkeysforcommand returns data for this
599 for (int i = 0; i < keys; ++i)
601 if(nPrimary == stof(argv(i)))
605 if ( minigame_isactive() && ( bInputType == 0 || bInputType == 1 ) )
608 string action = bInputType == 0 ? "pressed" : "released";
609 if ( nPrimary >= K_MOUSE1 && nPrimary <= K_MOUSE16 )
611 if ( HUD_mouse_over(HUD_PANEL(MINIGAME_BOARD)) )
617 if ( device && active_minigame.minigame_event(
618 active_minigame,strcat(device,"_",action),nPrimary) )
621 /// TODO: bInputType == 2?
624 if ( bInputType == 0 )
626 if ( nPrimary == K_MOUSE1 && HUD_MinigameMenu_activeitem &&
627 HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
629 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
632 if ( nPrimary == K_UPARROW || nPrimary == K_KP_UPARROW )
634 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_prev )
635 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_prev;
637 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_last_entry;
640 else if ( nPrimary == K_DOWNARROW || nPrimary == K_KP_DOWNARROW )
642 if ( HUD_MinigameMenu_activeitem && HUD_MinigameMenu_activeitem.list_next )
643 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_activeitem.list_next;
645 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
648 else if ( nPrimary == K_HOME || nPrimary == K_KP_HOME )
650 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
653 else if ( nPrimary == K_END || nPrimary == K_KP_END )
655 HUD_MinigameMenu_activeitem = HUD_MinigameMenu_entries;
658 else if ( nPrimary == K_KP_ENTER || nPrimary == K_ENTER || nPrimary == K_SPACE )
660 HUD_MinigameMenu_Click(HUD_MinigameMenu_activeitem);
663 else if ( nPrimary == K_ESCAPE )
665 HUD_MinigameMenu_Close();
675 void HUD_Minigame_Mouse()
677 if( !HUD_MinigameMenu_IsOpened() || autocvar__hud_configure || mv_active )
680 if(!autocvar_hud_cursormode)
682 mousepos = mousepos + getmousepos() * autocvar_menu_mouse_speed;
684 mousepos_x = bound(0, mousepos_x, vid_conwidth);
685 mousepos_y = bound(0, mousepos_y, vid_conheight);
688 if ( HUD_MinigameMenu_IsOpened() && HUD_mouse_over(HUD_PANEL(MINIGAME_MENU)) )
689 HUD_MinigameMenu_MouseInput();
691 vector cursorsize = '32 32 0';
692 drawpic(mousepos-'8 4 0', strcat("gfx/menu/", autocvar_menu_skin, "/cursor.tga"),
693 cursorsize, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
696 bool HUD_Minigame_Showpanels()
698 return HUD_MinigameMenu_IsOpened() && ( autocvar__hud_configure || minigame_isactive() );