Skip to content

Commit

Permalink
fix: MenuItemType.Input
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed Jun 26, 2024
1 parent b51e69b commit 24b7d62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Extract version and create tag
id: extract_version
run: |
version="1.0.3"
version="1.0.4"
echo "Version found: $version"
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
Expand Down
28 changes: 17 additions & 11 deletions src/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ public class Menu
private static readonly SayEvent OnSayTeam = new("say_team", OnSayEvent);
public static event EventHandler<MenuEvent>? OnDrawMenu;

private static void OnSayEvent(CCSPlayerController? controller, string message)
private static HookResult OnSayEvent(CCSPlayerController? controller, string message)
{
if (controller == null || !controller.IsValid || !Menus.TryGetValue(controller, out var value))
return;
return HookResult.Continue;

var menu = value.Peek();

if (!menu.AcceptInput)
return;
return HookResult.Continue;

var selectedItem = menu.Items[menu.Option];
selectedItem.DataString = message;
menu.AcceptInput = false;

menu.Callback?.Invoke(MenuButtons.Input, menu, selectedItem);
return HookResult.Handled;
}

protected static void RaiseDrawMenu(CCSPlayerController controller, MenuBase menu, MenuItem? selectedItem)
Expand Down Expand Up @@ -127,14 +128,17 @@ private static void TimerRepeat()

case MenuButtons.Back:
if (menu.AcceptInput)
{
menu.AcceptInput = false;
break;
}

if (menus.Count > 1)
{
menu.Callback?.Invoke(buttons, menu, null);
menus.Pop();
}

continue;

case MenuButtons.Exit:
Expand All @@ -156,7 +160,9 @@ private static void TimerRepeat()
public static void DrawMenu(CCSPlayerController controller, MenuBase menu, MenuItem? selectedItem)
{
var html = "";
var menus = Menus[controller];

if (!Menus.TryGetValue(controller, out var menus))
return;

if (menus.Count > 1)
{
Expand Down Expand Up @@ -196,7 +202,7 @@ public static void DrawMenu(CCSPlayerController controller, MenuBase menu, MenuI
break;

case MenuItemType.Input:
html += FormatInput(menu, menuItem);
html += FormatInput(menu, menuItem, selectedItem!);
break;

case MenuItemType.Bool:
Expand Down Expand Up @@ -297,19 +303,19 @@ private static string FormatSlider(MenuBase menu, MenuItem menuItem)
return html;
}

private static string FormatInput(MenuBase menu, MenuItem menuItem)
private static string FormatInput(MenuBase menu, MenuItem menuItem, MenuItem selectedItem)
{
var html = "";

if (menu.AcceptInput)
if (menu.AcceptInput && menuItem == selectedItem)
html += menu.Selector[(int)MenuCursor.Left].ToString();

if (menuItem.DataString.Length == 0)
html += menu.Input.ToString();
html += menu.Input.Value;
else
html += menuItem.DataString;
html += $"{menu.Input.Prefix}{menuItem.DataString}{menu.Input.Suffix}";

if (menu.AcceptInput)
if (menu.AcceptInput && menuItem == selectedItem)
html += menu.Selector[(int)MenuCursor.Right].ToString();

return html;
Expand Down
6 changes: 3 additions & 3 deletions src/SayEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace Menu;

public class SayEvent
{
public SayEvent(string sayCommand, Action<CCSPlayerController, string> callback)
public SayEvent(string sayCommand, Func<CCSPlayerController, string, HookResult> callback)
{
var wrappedHandler = new Action<int, IntPtr>((i, ptr) =>
var wrappedHandler = new Func<int, IntPtr, HookResult>((i, ptr) =>
{
var caller = (i != -1) ? new CCSPlayerController(NativeAPI.GetEntityFromIndex(i + 1)) : null;
callback.Invoke(caller!, NativeAPI.CommandGetArgString(ptr).Trim('"'));
return callback.Invoke(caller!, NativeAPI.CommandGetArgString(ptr).Trim('"'));
});

var functionReference = FunctionReference.Create(wrappedHandler);
Expand Down

0 comments on commit 24b7d62

Please sign in to comment.