Skip to content

Commit

Permalink
Changed: Custom Input Handling for Controller to Handle Bubble Up/Down
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Aug 3, 2022
1 parent 61c6817 commit d629147
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
6 changes: 3 additions & 3 deletions source/Reloaded.Mod.Launcher/Pages/BasePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ private void OnLoaded(object sender, RoutedEventArgs e)
{
_owner = Window.GetWindow(this);
_owner!.KeyDown += TrySwitchPage;
ControllerSupport.OnProcessCustomInputs += ProcessCustomInputs;
ControllerSupport.SubscribeCustomInputs(ProcessCustomInputs);
}

~BasePage() => Dispose();

public void Dispose()
{
_owner!.KeyDown -= TrySwitchPage;
ControllerSupport.OnProcessCustomInputs -= ProcessCustomInputs;
ControllerSupport.UnsubscribeCustomInputs(ProcessCustomInputs);
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ private void Application_Click(object sender, RoutedEventArgs e)
ViewModel.SwitchToApplication(tuple);
}

private void ProcessCustomInputs(in ControllerState state)
private void ProcessCustomInputs(in ControllerState state, ref bool handled)
{
if (!Window.GetWindow(this)!.IsActive)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AppSummaryPage()
InitializeComponent();
ViewModel = Lib.IoC.Get<ConfigureModsViewModel>();

ControllerSupport.OnProcessCustomInputs += OnProcessCustomInputs;
ControllerSupport.SubscribeCustomInputs(OnProcessCustomInputs);
_manipulator = new DictionaryResourceManipulator(this.Contents.Resources);
_modsViewSource = _manipulator.Get<CollectionViewSource>("FilteredMods");
_modsViewSource.Filter += ModsViewSourceOnFilter;
Expand All @@ -27,7 +27,7 @@ public AppSummaryPage()

public void Dispose()
{
ControllerSupport.OnProcessCustomInputs -= OnProcessCustomInputs;
ControllerSupport.UnsubscribeCustomInputs(OnProcessCustomInputs);
ViewModel?.Dispose();
GC.SuppressFinalize(this);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ private static void ProcessKeyboardItemShift(object sender, KeyEventArgs e)

#region Controller Controls

private void OnProcessCustomInputs(in ControllerState state)
private void OnProcessCustomInputs(in ControllerState state, ref bool handled)
{
if (!WpfUtilities.TryGetFocusedElementAndWindow(out var window, out var focused))
return;
Expand Down
30 changes: 28 additions & 2 deletions source/Reloaded.Mod.Launcher/Utility/ControllerSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static class ControllerSupport

public static Navigator Navigator { get; private set; } = null!;

public static event ProcessCustomInputsDelegate OnProcessCustomInputs = (in ControllerState state) => { };
private static List<ProcessCustomInputsRoutedDelegate> _processCustomInputsPreview = new();
private static List<ProcessCustomInputsRoutedDelegate> _processCustomInputs = new();

public static void Init()
{
Expand All @@ -33,6 +34,12 @@ public static void Init()
});
}

// Subscriptions for events handling custom preview.
public static void SubscribePreviewCustomInputs(ProcessCustomInputsRoutedDelegate processEvents) => _processCustomInputsPreview.Add(processEvents);
public static void UnsubscribePreviewCustomInputs(ProcessCustomInputsRoutedDelegate processEvents) => _processCustomInputsPreview.Remove(processEvents);
public static void SubscribeCustomInputs(ProcessCustomInputsRoutedDelegate processEvents) => _processCustomInputs.Add(processEvents);
public static void UnsubscribeCustomInputs(ProcessCustomInputsRoutedDelegate processEvents) => _processCustomInputs.Remove(processEvents);

/// <summary>
/// Tries to get the page scroll direction based on input.
/// </summary>
Expand Down Expand Up @@ -78,7 +85,22 @@ internal static bool TryGetListScrollDirection(ControllerState state, out int di
private static void ProcessCustomInputs(in ControllerState state)
{
ProcessCustomControls(state);
OnProcessCustomInputs(state);

// Send out preview event.
bool isHandled = false;
foreach (var previewEvent in _processCustomInputsPreview)
{
previewEvent(state, ref isHandled);
if (isHandled)
return;
}

foreach (var bubbleUpEvent in _processCustomInputs)
{
bubbleUpEvent(state, ref isHandled);
if (isHandled)
return;
}
}

private static void ProcessCustomControls(in ControllerState state)
Expand Down Expand Up @@ -136,4 +158,8 @@ private static void HandleNumeric(ControllerState state, NumericUpDown nud)
if (state.IsButtonPressed(Decrement) || (state.IsButtonHeld(Modifier) && state.IsButtonPressed(Up | Left)))
DecrementValue();
}

/// <param name="state">The current state of the controller.</param>
/// <param name="handled">Whether the event has been handled. If handled state is signaled, no further events will be called.</param>
public delegate void ProcessCustomInputsRoutedDelegate(in ControllerState state, ref bool handled);
}

0 comments on commit d629147

Please sign in to comment.