Skip to content

Commit

Permalink
added Open method to each menu type (#385)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Wilson <[email protected]>
Co-authored-by: Roflmuffin <[email protected]>
  • Loading branch information
3 people authored Apr 1, 2024
1 parent ab211a4 commit 211516c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
23 changes: 17 additions & 6 deletions managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,31 @@ public abstract class BaseMenu : IMenu
public List<ChatMenuOption> MenuOptions { get; } = new();
public PostSelectAction PostSelectAction { get; set; } = PostSelectAction.Reset;
public bool ExitButton { get; set; } = true;

protected BaseMenu(string title)
{
Title = title;
}

public virtual ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect, bool disabled = false)

public virtual ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect,
bool disabled = false)
{
var option = new ChatMenuOption(display, disabled, onSelect);
MenuOptions.Add(option);
return option;
}

public abstract void Open(CCSPlayerController player);

public void OpenToAll()
{
foreach (var player in Utilities.GetPlayers())
{
Open(player);
}
}
}

// This must be called ChatMenuOption to maintain backwards compatibility with the old API
public class ChatMenuOption
{
Expand Down Expand Up @@ -114,7 +125,7 @@ public void OnKeyPress(CCSPlayerController player, int key)
if (menuItemIndex >= 0 && menuItemIndex < Menu.MenuOptions.Count)
{
var menuOption = Menu.MenuOptions[menuItemIndex];

if (!menuOption.Disabled)
{
menuOption.OnSelect(Player, menuOption);
Expand Down Expand Up @@ -142,7 +153,7 @@ public virtual void Reset()
Page = 0;
PrevPageOffsets.Clear();
}

public virtual void Close()
{
MenuManager.CloseActiveMenu(Player);
Expand Down
14 changes: 14 additions & 0 deletions managed/CounterStrikeSharp.API/Modules/Menu/CenterHtmlMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@ namespace CounterStrikeSharp.API.Modules.Menu;

public class CenterHtmlMenu : BaseMenu
{
private readonly BasePlugin? _plugin;

public CenterHtmlMenu(string title, BasePlugin plugin) : base(ModifyTitle(title))
{
_plugin = plugin;
}

public CenterHtmlMenu(string title) : base(ModifyTitle(title))
{
}

public override void Open(CCSPlayerController player)
{
if (_plugin == null) return;

MenuManager.OpenCenterHtmlMenu(_plugin, player, this);
}

public override ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect,
bool disabled = false)
Expand Down
5 changes: 5 additions & 0 deletions managed/CounterStrikeSharp.API/Modules/Menu/ChatMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public ChatMenu(string title) : base(title)
{
ExitButton = false;
}

public override void Open(CCSPlayerController player)
{
MenuManager.OpenChatMenu(player, this);
}
}

public class ChatMenuInstance : BaseMenuInstance
Expand Down
5 changes: 5 additions & 0 deletions managed/CounterStrikeSharp.API/Modules/Menu/ConsoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class ConsoleMenu : BaseMenu
public ConsoleMenu(string title) : base(title)
{
}

public override void Open(CCSPlayerController player)
{
MenuManager.OpenConsoleMenu(player, this);
}
}

public class ConsoleMenuInstance : BaseMenuInstance
Expand Down
12 changes: 7 additions & 5 deletions managed/CounterStrikeSharp.API/Modules/Menu/IMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ namespace CounterStrikeSharp.API.Modules.Menu;

public interface IMenu
{
public string Title { get; set; }
public List<ChatMenuOption> MenuOptions { get; }
public PostSelectAction PostSelectAction
string Title { get; set; }
List<ChatMenuOption> MenuOptions { get; }
PostSelectAction PostSelectAction
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public bool ExitButton { get; set; }
bool ExitButton { get; set; }

public ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect, bool disabled = false);
ChatMenuOption AddMenuOption(string display, Action<CCSPlayerController, ChatMenuOption> onSelect, bool disabled = false);
void Open(CCSPlayerController player);
void OpenToAll();
}

public interface IMenuInstance
Expand Down
6 changes: 3 additions & 3 deletions managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ public static void CloseActiveMenu(CCSPlayerController player)

ActiveMenus.Remove(player.Handle);
}

public static void OpenChatMenu(CCSPlayerController player, ChatMenu menu)
{
CloseActiveMenu(player);

ActiveMenus[player.Handle] = new ChatMenuInstance(player, menu);
ActiveMenus[player.Handle].Display();
}

public static void OpenCenterHtmlMenu(BasePlugin plugin, CCSPlayerController player, CenterHtmlMenu menu)
{
CloseActiveMenu(player);

ActiveMenus[player.Handle] = new CenterHtmlMenuInstance(plugin, player, menu);
ActiveMenus[player.Handle].Display();
}

public static void OpenConsoleMenu(CCSPlayerController player, ConsoleMenu menu)
{
CloseActiveMenu(player);
Expand Down

0 comments on commit 211516c

Please sign in to comment.