JinxRyu/Ryujinx/Ui/Input/KeyboardKeyAssigner.cs
Jose Padilla ad7d22777f
Controller Input handling refactoring (#1751)
* This should fix issue #1374 in Linux

Changes:
- Bind buttons by detecting the transition from down to up.
- Bind axis by detecting movement from value higher than 50% to a value lower than 50%.

Caveats:
- I have tested only with DS3 in Linux (Fedora 32).
- ZL and ZR detection works by accident. This code doesn't take negative axis into account.
  The reason it works is because axis are managed in absolute value. So when pressing ZL/ZR
  axis value goes from -1 to 1 (or 1 to 0 and back to 1) and this hits the axis detector.
- Likely I have broken all the other controllers xD (testing needed).

* Assign keyboardPressed

* Make a more robust detection of pressed buttons when using a controller

* Add interface to bind buttons from Joystick and Keyboard

* Fix style issues after code review by @AcK77  (Thanks!)

* Move new classes to Ryujinx.Ui.Input namespace

* Use explicit types instead of var

* Update Ryujinx/Ui/Input/JoystickButtonAssigner.cs

Co-authored-by: Mary <thog@protonmail.com>

* Update Ryujinx/Ui/Input/JoystickButtonAssigner.cs

Co-authored-by: Mary <thog@protonmail.com>

* Update Ryujinx/Ui/Input/JoystickButtonAssigner.cs

Co-authored-by: Mary <thog@protonmail.com>

* Update Ryujinx/Ui/Input/JoystickButtonAssigner.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Add a new empty line before

* Up

Co-authored-by: Jose Padilla <jose@prensalink.com>
Co-authored-by: Mary <thog@protonmail.com>
Co-authored-by: Ac_K <Acoustik666@gmail.com>
2021-02-21 00:22:55 +01:00

51 lines
No EOL
1.2 KiB
C#

using OpenTK.Input;
using System;
using Key = Ryujinx.Configuration.Hid.Key;
namespace Ryujinx.Ui.Input
{
class KeyboardKeyAssigner : ButtonAssigner
{
private int _index;
private KeyboardState _keyboardState;
public KeyboardKeyAssigner(int index)
{
_index = index;
}
public void Init() { }
public void ReadInput()
{
_keyboardState = KeyboardController.GetKeyboardState(_index);
}
public bool HasAnyButtonPressed()
{
return _keyboardState.IsAnyKeyDown;
}
public bool ShouldCancel()
{
return Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Escape);
}
public string GetPressedButton()
{
string keyPressed = "";
foreach (Key key in Enum.GetValues(typeof(Key)))
{
if (_keyboardState.IsKeyDown((OpenTK.Input.Key)key))
{
keyPressed = key.ToString();
break;
}
}
return !ShouldCancel() ? keyPressed : "";
}
}
}