Ryujinx/Ryujinx.Ava/UI/Views/Settings/SettingsHotkeysView.axaml.cs

81 lines
2.5 KiB
C#
Raw Normal View History

2022-12-25 15:37:08 +00:00
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Ryujinx.Ava.Input;
using Ryujinx.Ava.UI.Helpers;
using Ryujinx.Input;
using Ryujinx.Input.Assigner;
2023-01-03 23:04:12 +00:00
namespace Ryujinx.Ava.UI.Views.Settings
2022-12-25 15:37:08 +00:00
{
2023-01-03 23:04:12 +00:00
public partial class SettingsHotkeysView : UserControl
2022-12-25 15:37:08 +00:00
{
2023-01-03 23:04:12 +00:00
private ButtonKeyAssigner _currentAssigner;
private IGamepadDriver AvaloniaKeyboardDriver;
public SettingsHotkeysView()
{
InitializeComponent();
AvaloniaKeyboardDriver = new AvaloniaKeyboardDriver(this);
}
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
private void MouseClick(object sender, PointerPressedEventArgs e)
{
bool shouldUnbind = e.GetCurrentPoint(this).Properties.IsMiddleButtonPressed;
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
_currentAssigner?.Cancel(shouldUnbind);
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
PointerPressed -= MouseClick;
}
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
private void Button_Checked(object sender, RoutedEventArgs e)
2022-12-25 15:37:08 +00:00
{
2023-01-03 23:04:12 +00:00
if (sender is ToggleButton button)
2022-12-25 15:37:08 +00:00
{
2023-01-03 23:04:12 +00:00
if (_currentAssigner != null && button == _currentAssigner.ToggledButton)
{
return;
}
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
if (_currentAssigner == null && button.IsChecked != null && (bool)button.IsChecked)
{
_currentAssigner = new ButtonKeyAssigner(button);
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
FocusManager.Instance?.Focus(this, NavigationMethod.Pointer);
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
PointerPressed += MouseClick;
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
var keyboard = (IKeyboard)AvaloniaKeyboardDriver.GetGamepad(AvaloniaKeyboardDriver.GamepadsIds[0]);
IButtonAssigner assigner = new KeyboardKeyAssigner(keyboard);
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
_currentAssigner.GetInputAndAssign(assigner);
}
else
2022-12-25 15:37:08 +00:00
{
2023-01-03 23:04:12 +00:00
if (_currentAssigner != null)
{
ToggleButton oldButton = _currentAssigner.ToggledButton;
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
_currentAssigner.Cancel();
_currentAssigner = null;
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
button.IsChecked = false;
}
2022-12-25 15:37:08 +00:00
}
}
}
2023-01-03 23:04:12 +00:00
private void Button_Unchecked(object sender, RoutedEventArgs e)
{
_currentAssigner?.Cancel();
_currentAssigner = null;
}
2022-12-25 15:37:08 +00:00
2023-01-03 23:04:12 +00:00
public void Dispose()
{
_currentAssigner?.Cancel();
_currentAssigner = null;
}
2022-12-25 15:37:08 +00:00
}
}