2023-01-06 23:35:21 +00:00
using Avalonia.Controls ;
2023-03-03 02:00:19 +00:00
using Avalonia.Controls.ApplicationLifetimes ;
2023-01-06 23:35:21 +00:00
using Avalonia.Controls.Primitives ;
using Avalonia.Input ;
using Avalonia.Interactivity ;
using Ryujinx.Ava.Input ;
using Ryujinx.Ava.UI.Helpers ;
2023-03-03 02:00:19 +00:00
using Ryujinx.Ava.UI.ViewModels ;
using Ryujinx.Ava.UI.Windows ;
2023-01-06 23:35:21 +00:00
using Ryujinx.Input ;
using Ryujinx.Input.Assigner ;
2023-03-03 02:00:19 +00:00
using System.Collections.Generic ;
2023-01-06 23:35:21 +00:00
namespace Ryujinx.Ava.UI.Views.Settings
{
public partial class SettingsHotkeysView : UserControl
{
2023-03-03 02:00:19 +00:00
public SettingsViewModel ViewModel ;
private HotkeyButtonKeyAssigner _currentAssigner ;
private readonly MainWindow _mainWindow ;
private readonly IGamepadDriver _avaloniaKeyboardDriver ;
2023-01-06 23:35:21 +00:00
public SettingsHotkeysView ( )
{
InitializeComponent ( ) ;
2023-03-03 02:00:19 +00:00
_mainWindow = ( MainWindow ) ( ( IClassicDesktopStyleApplicationLifetime ) Avalonia . Application . Current . ApplicationLifetime ) . MainWindow ;
_avaloniaKeyboardDriver = new AvaloniaKeyboardDriver ( this ) ;
2023-01-06 23:35:21 +00:00
}
private void MouseClick ( object sender , PointerPressedEventArgs e )
{
bool shouldUnbind = e . GetCurrentPoint ( this ) . Properties . IsMiddleButtonPressed ;
_currentAssigner ? . Cancel ( shouldUnbind ) ;
PointerPressed - = MouseClick ;
}
2023-03-03 02:00:19 +00:00
2023-01-06 23:35:21 +00:00
private void Button_Checked ( object sender , RoutedEventArgs e )
{
if ( sender is ToggleButton button )
{
if ( _currentAssigner ! = null & & button = = _currentAssigner . ToggledButton )
{
return ;
}
if ( _currentAssigner = = null & & button . IsChecked ! = null & & ( bool ) button . IsChecked )
{
2023-03-03 02:00:19 +00:00
_currentAssigner = new HotkeyButtonKeyAssigner ( button , ViewModel . UpdateHotkey ) ;
2023-01-06 23:35:21 +00:00
FocusManager . Instance ? . Focus ( this , NavigationMethod . Pointer ) ;
PointerPressed + = MouseClick ;
2023-03-03 02:00:19 +00:00
_currentAssigner . GetInputAndAssign ( CreateButtonAssigner ( ) ) ;
2023-01-06 23:35:21 +00:00
}
else
{
if ( _currentAssigner ! = null )
{
ToggleButton oldButton = _currentAssigner . ToggledButton ;
_currentAssigner . Cancel ( ) ;
_currentAssigner = null ;
button . IsChecked = false ;
}
}
}
}
2023-03-03 02:00:19 +00:00
private IButtonAssigner CreateButtonAssigner ( )
{
List < IButtonAssigner > assigners = new List < IButtonAssigner > ( ) ;
IGamepadDriver keyboardDriver ;
IGamepadDriver gamepadDriver = _mainWindow . InputManager . GamepadDriver ;
if ( _mainWindow . InputManager . KeyboardDriver is AvaloniaKeyboardDriver )
{
// NOTE: To get input in this window, we need to bind a custom keyboard driver instead of using the InputManager one as the main window isn't focused...
keyboardDriver = _avaloniaKeyboardDriver ;
}
else
{
keyboardDriver = _mainWindow . InputManager . KeyboardDriver ;
}
foreach ( string id in keyboardDriver . GamepadsIds )
{
assigners . Add ( new KeyboardKeyAssigner ( ( IKeyboard ) keyboardDriver . GetGamepad ( id ) , allowModifiers : true ) ) ;
}
foreach ( string id in gamepadDriver . GamepadsIds )
{
assigners . Add ( new GamepadButtonAssigner ( gamepadDriver . GetGamepad ( id ) , 0.2f , forStick : false ) ) ;
}
return new MultiButtonAssigner ( assigners ) ;
}
2023-01-06 23:35:21 +00:00
private void Button_Unchecked ( object sender , RoutedEventArgs e )
{
_currentAssigner ? . Cancel ( ) ;
_currentAssigner = null ;
}
public void Dispose ( )
{
_currentAssigner ? . Cancel ( ) ;
_currentAssigner = null ;
}
}
}