Skip to content

Commit d957b2d

Browse files
committed
Proof of concept implementation improvements
- Added NavigateBack support - Only accept input when it is presented - Updated visual logic for number keys menu with new navigation
1 parent 465b715 commit d957b2d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

tests/ProofOfConcepts/NumberKeysMenuAPI.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ void writeLine(string line = "", bool background = false)
116116
}
117117

118118
bool showExitButton = focusedMenu.Parents.Where(x => !x.PlayerCanClose).Any() == false;
119-
bool showBackButton = showExitButton
120-
? itemsStart == 0 && focusedMenu.PlayerCanClose && focusedMenu.Parent is not null
121-
: itemsStart == 0 && focusedMenu.PlayerCanClose;
119+
bool showBackButton = focusedMenu switch
120+
{
121+
{ CurrentPage: not 0 } => false,
122+
{ NavigateBack: not null } => true,
123+
_ => showExitButton switch
124+
{
125+
true => focusedMenu.PlayerCanClose && focusedMenu.Parent is not null,
126+
false => focusedMenu.PlayerCanClose,
127+
},
128+
};
122129
bool showPrevButton = itemsStart > 0;
123130
bool showNextButton = itemsStart + itemsInPage < focusedMenu.Items.Count;
124131
bool showNavigation = showBackButton || showPrevButton || showNextButton;
@@ -169,11 +176,20 @@ void ISampleMenu.HandleKey(CCSPlayerController player, ConsoleKeyInfo key)
169176
var itemsStart = focusedMenu.CurrentPage * ItemsPerPage;
170177
var itemsInPage = Math.Min(focusedMenu.Items.Count, itemsStart + ItemsPerPage) - itemsStart;
171178

172-
bool showBackButton = itemsStart == 0 && focusedMenu.PlayerCanClose;
179+
bool showExitButton = focusedMenu.Parents.Where(x => !x.PlayerCanClose).Any() == false;
180+
bool showBackButton = focusedMenu switch
181+
{
182+
{ CurrentPage: not 0 } => false,
183+
{ NavigateBack: not null } => true,
184+
_ => showExitButton switch
185+
{
186+
true => focusedMenu.PlayerCanClose && focusedMenu.Parent is not null,
187+
false => focusedMenu.PlayerCanClose,
188+
},
189+
};
173190
bool showPrevButton = itemsStart > 0;
174191
bool showNextButton = itemsStart + itemsInPage < focusedMenu.Items.Count;
175192
bool showNavigation = showBackButton || showPrevButton || showNextButton;
176-
bool showExitButton = focusedMenu.Parents.Where(x => !x.PlayerCanClose).Any() == false;
177193

178194
switch (key.Key)
179195
{
@@ -186,7 +202,12 @@ void ISampleMenu.HandleKey(CCSPlayerController player, ConsoleKeyInfo key)
186202
if (showPrevButton)
187203
focusedMenu.CurrentPage--;
188204
else if (showBackButton)
189-
focusedMenu.Close();
205+
{
206+
if (focusedMenu.NavigateBack is not null)
207+
focusedMenu.NavigateBack(focusedMenu);
208+
else
209+
focusedMenu.Close();
210+
}
190211
break;
191212
case ConsoleKey.D9:
192213
if (showNextButton)
@@ -229,7 +250,7 @@ public void SortPriorities()
229250
}
230251
}
231252

232-
internal class NumberKeysMenu : IMenu, IMenuPriorityExtension
253+
internal class NumberKeysMenu : IMenu, IMenuPriorityExtension, INavigateBackMenuExtension
233254
{
234255
public required NumbersPlayerMenuState MenuState { get; init; }
235256
public required List<NumberKeysMenu> Parents { get; init; }
@@ -284,6 +305,9 @@ public double Priority
284305
}
285306
}
286307
public bool IsFocused => MenuState.FocusStack.Count > 0 && MenuState.FocusStack[0] == this;
308+
309+
// INavigateBackMenuExtension
310+
public Action<IMenu>? NavigateBack { get; set; }
287311
}
288312

289313
internal class NumbersMenuItem : IMenuItem, IMenuItemSubtitleExtension

tests/ProofOfConcepts/WASDMenuAPI.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ void ISampleMenu.HandleKey(CCSPlayerController player, ConsoleKeyInfo key)
145145
focusedMenu.Items[focusedMenu.CurrentPosition].RaiseSelected();
146146
break;
147147
case ConsoleKey.A:
148-
focusedMenu.Close();
148+
if (focusedMenu.NavigateBack is not null)
149+
focusedMenu.NavigateBack(focusedMenu);
150+
else if (focusedMenu.PlayerCanClose)
151+
focusedMenu.Close();
149152
break;
150153
case ConsoleKey.W:
151154
focusedMenu.CurrentPosition = Math.Max(focusedMenu.CurrentPosition - 1, 0);
@@ -190,7 +193,7 @@ public void SortPriorities()
190193
}
191194
}
192195

193-
internal class WASDMenu : IMenu, IMenuPriorityExtension
196+
internal class WASDMenu : IMenu, IMenuPriorityExtension, INavigateBackMenuExtension
194197
{
195198
public required WASDPlayerMenuState MenuState { get; init; }
196199
public required List<WASDMenu> Parents { get; init; }
@@ -246,6 +249,9 @@ public double Priority
246249
}
247250
}
248251
public bool IsFocused => MenuState.FocusStack.Count > 0 && MenuState.FocusStack[0] == this;
252+
253+
// INavigateBackMenuExtension
254+
public Action<IMenu>? NavigateBack { get; set; }
249255
}
250256

251257
internal class WASDMenuItem : IMenuItem

0 commit comments

Comments
 (0)