Skip to content

Commit 267e33f

Browse files
authored
Merge pull request #84 from ELDment/beta-1
2 parents fa08da0 + 0d7d3a9 commit 267e33f

19 files changed

+792
-799
lines changed

managed/src/Entrypoint.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ internal class Entrypoint
99
{
1010
[UnmanagedCallersOnly]
1111
[SecurityCritical]
12-
public unsafe static void Start(IntPtr nativeTable, int nativeTableSize, IntPtr basePath)
13-
{
14-
try {
12+
public unsafe static void Start( IntPtr nativeTable, int nativeTableSize, IntPtr basePath )
13+
{
14+
try
15+
{
1516
Bootstrap.Start(nativeTable, nativeTableSize, Marshal.PtrToStringUTF8(basePath)!);
16-
} catch (Exception e) {
17+
}
18+
catch (Exception e)
19+
{
1720
AnsiConsole.WriteException(e);
1821
}
1922
}
20-
21-
}
23+
}

managed/src/SwiftlyS2.Core/Modules/Menus/Menu.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,21 @@ public void UseSlideOption( IPlayer player, bool isRight )
390390
Rerender(player, false);
391391
}
392392

393+
[Obsolete("Use GetCurrentOption instead")]
393394
public bool IsOptionSlider( IPlayer player )
394395
{
395-
var option = Options[SelectedIndex[player]];
396-
return option is SliderMenuButton || option is ChoiceMenuOption;
396+
return Options[SelectedIndex[player]] is SliderMenuButton || Options[SelectedIndex[player]] is ChoiceMenuOption;
397397
}
398398

399+
[Obsolete("Use GetCurrentOption instead")]
399400
public bool IsCurrentOptionSelectable( IPlayer player )
400401
{
401-
var option = Options[SelectedIndex[player]];
402-
return IsOptionSelectable(option);
402+
return IsOptionSelectable(Options[SelectedIndex[player]]);
403+
}
404+
405+
public IOption? GetCurrentOption( IPlayer player )
406+
{
407+
return !SelectedIndex.TryGetValue(player, out var index) || index < 0 || index >= Options.Count ? null : Options[index];
403408
}
404409

405410
public bool IsOptionSelectable( IOption option )

managed/src/SwiftlyS2.Core/Modules/Menus/MenuManager.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.Globalization;
3+
using SwiftlyS2.Core.Menu.Options;
34
using SwiftlyS2.Core.Natives;
45
using SwiftlyS2.Shared;
56
using SwiftlyS2.Shared.Events;
@@ -151,10 +152,17 @@ void KeyStateChange( IOnClientKeyStateChangedEvent @event )
151152
}
152153
else if (@event.Key == useKey)
153154
{
154-
if (menu.IsOptionSlider(player)) menu.UseSlideOption(player, true);
155-
else menu.UseSelection(player);
155+
var option = menu.GetCurrentOption(player);
156+
if (option is SliderMenuButton || option is ChoiceMenuOption)
157+
{
158+
menu.UseSlideOption(player, true);
159+
}
160+
else
161+
{
162+
menu.UseSelection(player);
163+
}
156164

157-
if (menu.HasSound)
165+
if (menu.HasSound && (option?.HasSound() ?? false))
158166
{
159167
_useSound.Recipients.AddRecipient(@event.PlayerId);
160168
_useSound.Emit();
@@ -198,10 +206,17 @@ void KeyStateChange( IOnClientKeyStateChangedEvent @event )
198206
}
199207
else if (@event.Key == KeyKind.D)
200208
{
201-
if (menu.IsOptionSlider(player)) menu.UseSlideOption(player, true);
202-
else menu.UseSelection(player);
209+
var option = menu.GetCurrentOption(player);
210+
if (option is SliderMenuButton || option is ChoiceMenuOption)
211+
{
212+
menu.UseSlideOption(player, true);
213+
}
214+
else
215+
{
216+
menu.UseSelection(player);
217+
}
203218

204-
if (menu.HasSound)
219+
if (menu.HasSound && (option?.HasSound() ?? false))
205220
{
206221
_useSound.Recipients.AddRecipient(@event.PlayerId);
207222
_useSound.Emit();

managed/src/SwiftlyS2.Core/Modules/Menus/Options/AsyncButtonMenuOption.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,29 @@ internal class AsyncButtonMenuOption : IOption
2424

2525
private string? _loadingText;
2626

27-
public AsyncButtonMenuOption(string text, Func<IPlayer, Task>? onClickAsync = null, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null)
27+
public AsyncButtonMenuOption( string text, Func<IPlayer, Task>? onClickAsync = null, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null )
2828
{
2929
Text = text;
3030
OnClickAsync = onClickAsync;
3131
Size = size;
3232
OverflowStyle = overflowStyle;
3333
}
3434

35-
public AsyncButtonMenuOption(string text, Func<IPlayer, IOption, Task>? onClickAsync, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null)
35+
public AsyncButtonMenuOption( string text, Func<IPlayer, IOption, Task>? onClickAsync, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null )
3636
{
3737
Text = text;
3838
OnClickAsyncWithOption = onClickAsync;
3939
Size = size;
4040
OverflowStyle = overflowStyle;
4141
}
4242

43-
public bool ShouldShow(IPlayer player)
44-
{
45-
return VisibilityCheck?.Invoke(player) ?? true;
46-
}
43+
public bool ShouldShow( IPlayer player ) => VisibilityCheck?.Invoke(player) ?? true;
4744

48-
public bool CanInteract(IPlayer player)
49-
{
50-
return !IsLoading && (EnabledCheck?.Invoke(player) ?? true);
51-
}
45+
public bool CanInteract( IPlayer player ) => !IsLoading && (EnabledCheck?.Invoke(player) ?? true);
46+
47+
public bool HasSound() => true;
5248

53-
public string GetDisplayText(IPlayer player, bool updateHorizontalStyle = false)
49+
public string GetDisplayText( IPlayer player, bool updateHorizontalStyle = false )
5450
{
5551
var sizeClass = MenuSizeHelper.GetSizeClass(Size);
5652

@@ -73,7 +69,7 @@ public IMenuTextSize GetTextSize()
7369
return Size;
7470
}
7571

76-
public async Task ExecuteAsync(IPlayer player, string? loadingText = null)
72+
public async Task ExecuteAsync( IPlayer player, string? loadingText = null )
7773
{
7874
if (OnClickAsync == null && OnClickAsyncWithOption == null) return;
7975

@@ -92,7 +88,7 @@ public async Task ExecuteAsync(IPlayer player, string? loadingText = null)
9288
}
9389
}
9490

95-
public void SetLoadingText(string? text)
91+
public void SetLoadingText( string? text )
9692
{
9793
_loadingText = text;
9894
}

managed/src/SwiftlyS2.Core/Modules/Menus/Options/ButtonMenuOption.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,29 @@ internal class ButtonMenuOption : IOption
2121
public bool Visible => true;
2222
public bool Enabled => true;
2323

24-
public ButtonMenuOption(string text, Action<IPlayer>? onClick = null, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null)
24+
public ButtonMenuOption( string text, Action<IPlayer>? onClick = null, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null )
2525
{
2626
Text = text;
2727
OnClick = onClick;
2828
Size = size;
2929
OverflowStyle = overflowStyle;
3030
}
3131

32-
public ButtonMenuOption(string text, Action<IPlayer, IOption>? onClick, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null)
32+
public ButtonMenuOption( string text, Action<IPlayer, IOption>? onClick, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null )
3333
{
3434
Text = text;
3535
OnClickWithOption = onClick;
3636
Size = size;
3737
OverflowStyle = overflowStyle;
3838
}
3939

40-
public bool ShouldShow(IPlayer player)
41-
{
42-
return VisibilityCheck?.Invoke(player) ?? true;
43-
}
40+
public bool ShouldShow( IPlayer player ) => VisibilityCheck?.Invoke(player) ?? true;
4441

45-
public bool CanInteract(IPlayer player)
46-
{
47-
return EnabledCheck?.Invoke(player) ?? true;
48-
}
42+
public bool CanInteract( IPlayer player ) => EnabledCheck?.Invoke(player) ?? true;
43+
44+
public bool HasSound() => true;
4945

50-
public string GetDisplayText(IPlayer player, bool updateHorizontalStyle = false)
46+
public string GetDisplayText( IPlayer player, bool updateHorizontalStyle = false )
5147
{
5248
var sizeClass = MenuSizeHelper.GetSizeClass(Size);
5349

managed/src/SwiftlyS2.Core/Modules/Menus/Options/ChoiceMenuButton.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class ChoiceMenuOption : IOption
2323

2424
public string SelectedChoice => Choices.Count > 0 ? Choices[SelectedIndex] : "";
2525

26-
public ChoiceMenuOption(string text, IEnumerable<string> choices, string? defaultChoice = null, Action<IPlayer, string>? onChange = null, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null)
26+
public ChoiceMenuOption( string text, IEnumerable<string> choices, string? defaultChoice = null, Action<IPlayer, string>? onChange = null, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null )
2727
{
2828
Text = text;
2929
Choices = [.. choices];
@@ -40,7 +40,7 @@ public ChoiceMenuOption(string text, IEnumerable<string> choices, string? defaul
4040
OverflowStyle = overflowStyle;
4141
}
4242

43-
public ChoiceMenuOption(string text, IEnumerable<string> choices, string? defaultChoice, Action<IPlayer, IOption, string>? onChange, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null)
43+
public ChoiceMenuOption( string text, IEnumerable<string> choices, string? defaultChoice, Action<IPlayer, IOption, string>? onChange, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null )
4444
{
4545
Text = text;
4646
Choices = [.. choices];
@@ -57,17 +57,13 @@ public ChoiceMenuOption(string text, IEnumerable<string> choices, string? defaul
5757
OverflowStyle = overflowStyle;
5858
}
5959

60-
public bool ShouldShow(IPlayer player)
61-
{
62-
return VisibilityCheck?.Invoke(player) ?? true;
63-
}
60+
public bool ShouldShow( IPlayer player ) => VisibilityCheck?.Invoke(player) ?? true;
6461

65-
public bool CanInteract(IPlayer player)
66-
{
67-
return EnabledCheck?.Invoke(player) ?? true;
68-
}
62+
public bool CanInteract( IPlayer player ) => EnabledCheck?.Invoke(player) ?? true;
63+
64+
public bool HasSound() => true;
6965

70-
public string GetDisplayText(IPlayer player, bool updateHorizontalStyle = false)
66+
public string GetDisplayText( IPlayer player, bool updateHorizontalStyle = false )
7167
{
7268
var sizeClass = MenuSizeHelper.GetSizeClass(Size);
7369

@@ -86,16 +82,22 @@ public IMenuTextSize GetTextSize()
8682
return Size;
8783
}
8884

89-
public void Next(IPlayer player)
85+
public void Next( IPlayer player )
9086
{
91-
if (!CanInteract(player) || Choices.Count == 0) return;
87+
if (!CanInteract(player) || Choices.Count == 0)
88+
{
89+
return;
90+
}
9291
SelectedIndex = (SelectedIndex + 1) % Choices.Count;
9392
OnChange?.Invoke(player, SelectedChoice);
9493
OnChangeWithOption?.Invoke(player, this, SelectedChoice);
9594
}
96-
public void Previous(IPlayer player)
95+
public void Previous( IPlayer player )
9796
{
98-
if (!CanInteract(player) || Choices.Count == 0) return;
97+
if (!CanInteract(player) || Choices.Count == 0)
98+
{
99+
return;
100+
}
99101
SelectedIndex = (SelectedIndex - 1 + Choices.Count) % Choices.Count;
100102
OnChange?.Invoke(player, SelectedChoice);
101103
OnChangeWithOption?.Invoke(player, this, SelectedChoice);

managed/src/SwiftlyS2.Core/Modules/Menus/Options/DynamicMenuOption.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,37 @@ internal class DynamicMenuOption : IOption
2121
public IMenu? Menu { get; set; }
2222
public MenuHorizontalStyle? OverflowStyle { get; init; }
2323

24-
public string Text
25-
{
24+
public string Text {
2625
get => _cachedText;
2726
set => _cachedText = value;
2827
}
2928

3029
public bool Visible => true;
3130
public bool Enabled => true;
3231

33-
public DynamicMenuOption(Func<IPlayer, string> textProvider, TimeSpan updateInterval, Action<IPlayer>? onClick = null, IMenuTextSize size = IMenuTextSize.Medium)
32+
public DynamicMenuOption( Func<IPlayer, string> textProvider, TimeSpan updateInterval, Action<IPlayer>? onClick = null, IMenuTextSize size = IMenuTextSize.Medium )
3433
{
3534
_textProvider = textProvider;
3635
_updateInterval = updateInterval;
3736
_onClick = onClick;
3837
_size = size;
3938
}
4039

41-
public DynamicMenuOption(Func<string> textProvider, TimeSpan updateInterval, Action<IPlayer>? onClick = null, IMenuTextSize size = IMenuTextSize.Medium)
40+
public DynamicMenuOption( Func<string> textProvider, TimeSpan updateInterval, Action<IPlayer>? onClick = null, IMenuTextSize size = IMenuTextSize.Medium )
4241
{
4342
_textProvider = _ => textProvider();
4443
_updateInterval = updateInterval;
4544
_onClick = onClick;
4645
_size = size;
4746
}
4847

49-
public bool ShouldShow(IPlayer player)
50-
{
51-
return _visibilityCheck?.Invoke(player) ?? true;
52-
}
48+
public bool ShouldShow( IPlayer player ) => _visibilityCheck?.Invoke(player) ?? true;
5349

54-
public bool CanInteract(IPlayer player)
55-
{
56-
return _onClick != null && (_enabledCheck?.Invoke(player) ?? true);
57-
}
50+
public bool CanInteract( IPlayer player ) => _onClick != null && (_enabledCheck?.Invoke(player) ?? true);
51+
52+
public bool HasSound() => false;
5853

59-
public string GetDisplayText(IPlayer player, bool updateHorizontalStyle = false)
54+
public string GetDisplayText( IPlayer player, bool updateHorizontalStyle = false )
6055
{
6156
var sizeClass = MenuSizeHelper.GetSizeClass(_size);
6257

@@ -86,7 +81,7 @@ public IMenuTextSize GetTextSize()
8681
return _size;
8782
}
8883

89-
public void Click(IPlayer player)
84+
public void Click( IPlayer player )
9085
{
9186
if (CanInteract(player))
9287
{
@@ -99,26 +94,26 @@ public void Click(IPlayer player)
9994
}
10095
}
10196

102-
public DynamicMenuOption WithVisibilityCheck(Func<IPlayer, bool> check)
97+
public DynamicMenuOption WithVisibilityCheck( Func<IPlayer, bool> check )
10398
{
10499
_visibilityCheck = check;
105100
return this;
106101
}
107102

108-
public DynamicMenuOption WithEnabledCheck(Func<IPlayer, bool> check)
103+
public DynamicMenuOption WithEnabledCheck( Func<IPlayer, bool> check )
109104
{
110105
_enabledCheck = check;
111106
return this;
112107
}
113108

114-
public DynamicMenuOption WithValidation(Func<IPlayer, bool> check, Action<IPlayer>? onFailed = null)
109+
public DynamicMenuOption WithValidation( Func<IPlayer, bool> check, Action<IPlayer>? onFailed = null )
115110
{
116111
_validationCheck = check;
117112
_onValidationFailed = onFailed;
118113
return this;
119114
}
120115

121-
public DynamicMenuOption WithCloseOnSelect(bool close = true)
116+
public DynamicMenuOption WithCloseOnSelect( bool close = true )
122117
{
123118
_closeOnSelect = close;
124119
return this;

managed/src/SwiftlyS2.Core/Modules/Menus/Options/ProgressBarMenuButton.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SwiftlyS2.Core.Menu.Options;
66

7-
internal class ProgressBarMenuOption(string text, Func<float> progressProvider, int barWidth = 20, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null) : IOption
7+
internal class ProgressBarMenuOption( string text, Func<float> progressProvider, int barWidth = 20, IMenuTextSize size = IMenuTextSize.Medium, MenuHorizontalStyle? overflowStyle = null ) : IOption
88
{
99
public string Text { get; set; } = text;
1010
public Func<float> ProgressProvider { get; set; } = progressProvider;
@@ -19,10 +19,11 @@ internal class ProgressBarMenuOption(string text, Func<float> progressProvider,
1919
public bool Visible => true;
2020
public bool Enabled => false;
2121

22-
public bool ShouldShow(IPlayer player) => true;
23-
public bool CanInteract(IPlayer player) => false;
22+
public bool ShouldShow( IPlayer player ) => true;
23+
public bool CanInteract( IPlayer player ) => false;
24+
public bool HasSound() => false;
2425

25-
public string GetDisplayText(IPlayer player, bool updateHorizontalStyle = false)
26+
public string GetDisplayText( IPlayer player, bool updateHorizontalStyle = false )
2627
{
2728
var sizeClass = MenuSizeHelper.GetSizeClass(Size);
2829

managed/src/SwiftlyS2.Core/Modules/Menus/Options/SeparatorMenuButton.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ public SeparatorMenuOption()
1717
Text = "─────────────────────";
1818
}
1919

20-
public bool ShouldShow(IPlayer player) => true;
21-
public bool CanInteract(IPlayer player) => false;
20+
public bool ShouldShow( IPlayer player ) => true;
21+
public bool CanInteract( IPlayer player ) => false;
22+
public bool HasSound() => false;
2223

23-
public string GetDisplayText(IPlayer player, bool updateHorizontalStyle = false)
24+
public string GetDisplayText( IPlayer player, bool updateHorizontalStyle = false )
2425
{
2526
return $"<font color='{Menu!.RenderColor.ToHex(true)}'>{Text}</font>";
2627
}

0 commit comments

Comments
 (0)