Skip to content

Commit 5974469

Browse files
committed
fix: allowed to create menu types separately.
1 parent ba544c1 commit 5974469

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

TestPlugin/TestPlugin.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CounterStrikeSharp.API;
1+
using System.Drawing;
2+
using CounterStrikeSharp.API;
23
using CounterStrikeSharp.API.Core;
34
using CounterStrikeSharp.API.Modules.Commands;
45
using CS2ScreenMenuAPI;
@@ -64,6 +65,7 @@ public class TestMenu : BasePlugin
6465
public override void Load(bool hotReload)
6566
{
6667
AddCommand("css_test", "test menu", Command_Test);
68+
6769
}
6870
public void Command_NoPlayerArg()
6971
{
@@ -114,9 +116,30 @@ public void Command_Test(CCSPlayerController? player, CommandInfo info)
114116
{
115117
CreateVoteMenu(p, mainMenu);
116118
});
119+
mainMenu.AddItem("Scrollable Menu", (p, o) =>
120+
{
121+
CreateScrollableMenu(player, mainMenu);
122+
});
117123

118124
mainMenu.Display();
119125
}
126+
private Menu CreateScrollableMenu(CCSPlayerController player, Menu prevMenu)
127+
{
128+
Menu scrollableMenu = new Menu(this)
129+
{
130+
Title = "Scrollable Menu",
131+
IsSubMenu = true,
132+
PrevMenu = prevMenu
133+
};
134+
scrollableMenu.SetMenuType(MenuType.Scrollable);
135+
136+
for (int i = 1; i < 5; i++)
137+
{
138+
scrollableMenu.AddItem(i.ToString(), (p, o) => { });
139+
}
140+
scrollableMenu.Display(player);
141+
return scrollableMenu;
142+
}
120143
private Menu CreateVoteMenu(CCSPlayerController player, Menu prevMenu)
121144
{
122145
Menu voteMenu = new Menu(player, this)

TestPlugin/TestPlugin.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
<ItemGroup>
1010
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.314" />
11-
<PackageReference Include="CS2ScreenMenuAPI" Version="3.0.8" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<Reference Include="CS2ScreenMenuAPI">
15+
<HintPath>../../CS2ScreenMenuAPI</HintPath>
16+
</Reference>
1217
</ItemGroup>
1318

1419
</Project>

src/Internal/Menu.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Menu : IMenu, IDisposable
3333
public int ItemsPerPage { get; } = 6;
3434
public PostSelect PostSelect = PostSelect.Nothing;
3535
public MenuType MenuType = MenuType.KeyPress;
36+
private bool _menuTypeExplicitlySet = false;
3637
public bool HasExitButon { get; set; }
3738
public bool ShowPageCount { get; set; }
3839
public bool ShowControlsInfo { get; set; }
@@ -43,6 +44,7 @@ public class Menu : IMenu, IDisposable
4344
internal string ScrollUpKey = "W";
4445
internal string ScrollDownKey = "S";
4546
internal string SelectKey = "E";
47+
internal string ExitKey = "Tab";
4648

4749
public Menu(CCSPlayerController player, BasePlugin plugin)
4850
{
@@ -622,6 +624,7 @@ private void OnTick()
622624
if (ButtonMapping.TryGetValue(ScrollUpKey, out PlayerButtons scrollUpButton) && (button & scrollUpButton) == 0 && (g_OldButtons & scrollUpButton) != 0) { ScrollUp(); }
623625
else if (ButtonMapping.TryGetValue(ScrollDownKey, out PlayerButtons scrollDownButton) && (button & scrollDownButton) == 0 && (g_OldButtons & scrollDownButton) != 0) { ScrollDown(); }
624626
else if (ButtonMapping.TryGetValue(SelectKey, out PlayerButtons selectButton) && (button & selectButton) == 0 && (g_OldButtons & selectButton) != 0) { SelectCurrentOption(); }
627+
else if (ButtonMapping.TryGetValue(ExitKey, out PlayerButtons exitButton) && (button & exitButton) == 0 && (g_OldButtons & exitButton) != 0) { Close(_player); }
625628
g_OldButtons = button;
626629
}
627630
}
@@ -632,25 +635,41 @@ private void OnCheckTransmit(CCheckTransmitInfoList infoList)
632635
}
633636
private void ConfigureSettings()
634637
{
635-
MenuType = _config.Settings.MenuType switch
638+
if (!_menuTypeExplicitlySet)
636639
{
637-
"Scrollable" => MenuType.Scrollable,
638-
"Both" => MenuType.Both,
639-
_ => MenuType.KeyPress,
640-
};
640+
MenuType = _config.Settings.MenuType switch
641+
{
642+
"Scrollable" => MenuType.Scrollable,
643+
"Both" => MenuType.Both,
644+
_ => MenuType.KeyPress,
645+
};
646+
}
641647
HasExitButon = _config.Settings.HasExitOption;
642648
ShowResolutionOption = _config.Settings.ShowResolutionOption;
643649
ShowPageCount = _config.Settings.ShowPageCount;
644650
ShowDisabledOptionNum = _config.Settings.ShowDisabledOptionNum;
645651
ShowControlsInfo = _config.Settings.ShowControlsInfo;
652+
ConfigureControlsForMenuType();
653+
}
654+
655+
private void ConfigureControlsForMenuType()
656+
{
646657
if (MenuType != MenuType.KeyPress)
647658
{
648659
ScrollUpKey = _config.Controls.ScrollUp;
649660
ScrollDownKey = _config.Controls.ScrollDown;
650661
SelectKey = _config.Controls.Select;
662+
ExitKey = _config.Controls.Exit;
651663
}
652664
}
653665

666+
public void SetMenuType(MenuType menuType)
667+
{
668+
MenuType = menuType;
669+
_menuTypeExplicitlySet = true;
670+
ConfigureControlsForMenuType();
671+
}
672+
654673
internal int GetEnabledOptionsCountOnCurrentPage()
655674
{
656675
int startIndex = CurrentPage * ItemsPerPage;

src/interfaces/IMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface IMenu
1212
void Refresh();
1313
void Display();
1414
void Display(CCSPlayerController player);
15+
void SetMenuType(MenuType menuType);
1516
}
1617
public enum PostSelect
1718
{

0 commit comments

Comments
 (0)