Move most logic into new StickVisualizer class and add keyboard support.

This commit is contained in:
MutantAura
2024-05-31 01:42:33 +01:00
committed by Evan Husted
parent cfa5ad0757
commit 16ca8e5005
6 changed files with 355 additions and 67 deletions

View File

@@ -1,12 +1,40 @@
using Avalonia.Svg.Skia;
using CommunityToolkit.Mvvm.ComponentModel;
using Ryujinx.Ava.UI.Models.Input;
using Ryujinx.Input;
using System.Threading;
using System.Threading.Tasks;
namespace Ryujinx.Ava.UI.ViewModels.Input
{
public partial class KeyboardInputViewModel : BaseModel
{
[ObservableProperty] private KeyboardInputConfig _config;
private (float, float) _leftBuffer = (0, 0);
private (float, float) _rightBuffer = (0, 0);
private StickVisualizer _stickVisualizer;
public StickVisualizer StickVisualizer
{
get => _stickVisualizer;
set
{
_stickVisualizer = value;
OnPropertyChanged();
}
}
private KeyboardInputConfig _config;
public KeyboardInputConfig Config
{
get => _config;
set
{
_config = value;
StickVisualizer.UpdateConfig(_config);
OnPropertyChanged();
}
}
private bool _isLeft;
public bool IsLeft
@@ -43,7 +71,65 @@ namespace Ryujinx.Ava.UI.ViewModels.Input
ParentModel = model;
model.NotifyChangesEvent += OnParentModelChanged;
OnParentModelChanged();
_stickVisualizer = new();
Config = config;
StickVisualizer.PollToken = StickVisualizer.PollTokenSource.Token;
Task.Run(() => PollKeyboard(StickVisualizer.PollToken));
}
private async Task PollKeyboard(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
if (ParentModel.IsKeyboard)
{
IKeyboard keyboard = (IKeyboard)ParentModel.AvaloniaKeyboardDriver.GetGamepad("0");
var snap = keyboard.GetKeyboardStateSnapshot();
if (snap.IsPressed((Key)Config.LeftStickRight))
{
_leftBuffer.Item1 += 1;
}
if (snap.IsPressed((Key)Config.LeftStickLeft))
{
_leftBuffer.Item1 -= 1;
}
if (snap.IsPressed((Key)Config.LeftStickUp))
{
_leftBuffer.Item2 += 1;
}
if (snap.IsPressed((Key)Config.LeftStickDown))
{
_leftBuffer.Item2 -= 1;
}
if (snap.IsPressed((Key)Config.RightStickRight))
{
_rightBuffer.Item1 += 1;
}
if (snap.IsPressed((Key)Config.RightStickLeft))
{
_rightBuffer.Item1 -= 1;
}
if (snap.IsPressed((Key)Config.RightStickUp))
{
_rightBuffer.Item2 += 1;
}
if (snap.IsPressed((Key)Config.RightStickDown))
{
_rightBuffer.Item2 -= 1;
}
StickVisualizer.UiStickLeft = _leftBuffer;
StickVisualizer.UiStickRight = _rightBuffer;
}
await Task.Delay(StickVisualizer.DrawStickPollRate, token);
}
StickVisualizer.PollTokenSource.Dispose();
}
public void OnParentModelChanged()