ryujinx/Ryujinx/Ui/NpadController.cs

144 lines
5.6 KiB
C#
Raw Normal View History

using OpenTK;
using OpenTK.Input;
using Ryujinx.Common.Configuration.Hid;
using Ryujinx.HLE.Input;
using System;
using InnerNpadController = Ryujinx.Common.Configuration.Hid.NpadController;
Add features to GUI (#757) * controller image changes depending on the selected controller type the new controller image assets are temporary until i get new ones * Game list scans subdirs for games * Key file existence check * Only shows Program NCAs in Application list * Change shown GUI columns without restarting * Sort by column if you click on the column header Columns are sorted as text so there are inaccuracies on some columns * Fix sort on Time Played, Last Played and File Size columns * Add ability to designate favourite games #1 TODO: - Make fav games persistent - Fix invisible check marks due to theme * Add ability to designate favourite games #2 Also removed default theme * Added a Windows specific build condition and a Linux bug fix * bugfix * Load metadata from JSONs * Temp bug fix for MacOS * lil clean up * requested changes * Misc fixes * edited schema and config * Show the TitleID of games on the title bar * gui column config option have names * Async loading of game list * bugfix and cleanup * thog's requested changes * requested changes and cleanup still need to fix the gtk seizure * Fix issue where an ExeFS as a NSP didn't show up in the application list * Minor fixes * catch glib unhandled exceptions * Make sure to do UI manipulation in the main thread * Print path of invalid files * Ac_k's requested changes * Return of the dark theme * move AboutInfo struct to another file * sort usings * changes - gdkchan's requested changes that have been marked resolved - made some structs internal as they aren't used outside of the GUI - renamed Ryujinx.UI to Ryujinx.Ui to fit naming convention and folder structure - fixed bug where controller type dropdown box is stretched
2019-11-29 05:32:51 +01:00
namespace Ryujinx.Ui.Input
{
public class NpadController
{
private InnerNpadController _inner;
// NOTE: This should be initialized AFTER GTK for compat reasons with OpenTK SDL2 backend and GTK on Linux.
// BODY: Usage of Joystick.GetState must be defer to after GTK full initialization. Otherwise, GTK will segfault because SDL2 was already init *sighs*
public NpadController(InnerNpadController inner)
{
_inner = inner;
}
private bool IsEnabled()
{
return _inner.Enabled && Joystick.GetState(_inner.Index).IsConnected;
}
public ControllerButtons GetButtons()
{
if (!IsEnabled())
{
return 0;
}
JoystickState joystickState = Joystick.GetState(_inner.Index);
ControllerButtons buttons = 0;
if (IsActivated(joystickState, _inner.LeftJoycon.DPadUp)) buttons |= ControllerButtons.DpadUp;
if (IsActivated(joystickState, _inner.LeftJoycon.DPadDown)) buttons |= ControllerButtons.DpadDown;
if (IsActivated(joystickState, _inner.LeftJoycon.DPadLeft)) buttons |= ControllerButtons.DpadLeft;
if (IsActivated(joystickState, _inner.LeftJoycon.DPadRight)) buttons |= ControllerButtons.DPadRight;
if (IsActivated(joystickState, _inner.LeftJoycon.StickButton)) buttons |= ControllerButtons.StickLeft;
if (IsActivated(joystickState, _inner.LeftJoycon.ButtonMinus)) buttons |= ControllerButtons.Minus;
if (IsActivated(joystickState, _inner.LeftJoycon.ButtonL)) buttons |= ControllerButtons.L;
if (IsActivated(joystickState, _inner.LeftJoycon.ButtonZl)) buttons |= ControllerButtons.Zl;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonA)) buttons |= ControllerButtons.A;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonB)) buttons |= ControllerButtons.B;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonX)) buttons |= ControllerButtons.X;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonY)) buttons |= ControllerButtons.Y;
if (IsActivated(joystickState, _inner.RightJoycon.StickButton)) buttons |= ControllerButtons.StickRight;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonPlus)) buttons |= ControllerButtons.Plus;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonR)) buttons |= ControllerButtons.R;
if (IsActivated(joystickState, _inner.RightJoycon.ButtonZr)) buttons |= ControllerButtons.Zr;
return buttons;
}
private bool IsActivated(JoystickState joystickState,ControllerInputId controllerInputId)
{
if (controllerInputId <= ControllerInputId.Button20)
{
return joystickState.IsButtonDown((int)controllerInputId);
}
else if (controllerInputId <= ControllerInputId.Axis5)
{
int axis = controllerInputId - ControllerInputId.Axis0;
return joystickState.GetAxis(axis) > _inner.TriggerThreshold;
}
else if (controllerInputId <= ControllerInputId.Hat2Right)
{
int hat = (controllerInputId - ControllerInputId.Hat0Up) / 4;
int baseHatId = (int)ControllerInputId.Hat0Up + (hat * 4);
JoystickHatState hatState = joystickState.GetHat((JoystickHat)hat);
if (hatState.IsUp && ((int)controllerInputId % baseHatId == 0)) return true;
if (hatState.IsDown && ((int)controllerInputId % baseHatId == 1)) return true;
if (hatState.IsLeft && ((int)controllerInputId % baseHatId == 2)) return true;
if (hatState.IsRight && ((int)controllerInputId % baseHatId == 3)) return true;
}
return false;
}
public (short, short) GetLeftStick()
{
if (!IsEnabled())
{
return (0, 0);
}
return GetStick(_inner.LeftJoycon.Stick);
}
public (short, short) GetRightStick()
{
if (!IsEnabled())
{
return (0, 0);
}
return GetStick(_inner.RightJoycon.Stick);
}
private (short, short) GetStick(ControllerInputId stickInputId)
{
if (stickInputId < ControllerInputId.Axis0 || stickInputId > ControllerInputId.Axis5)
{
return (0, 0);
}
JoystickState jsState = Joystick.GetState(_inner.Index);
int xAxis = stickInputId - ControllerInputId.Axis0;
float xValue = jsState.GetAxis(xAxis);
float yValue = 0 - jsState.GetAxis(xAxis + 1); // Invert Y-axis
return ApplyDeadzone(new Vector2(xValue, yValue));
}
private (short, short) ApplyDeadzone(Vector2 axis)
{
return (ClampAxis(MathF.Abs(axis.X) > _inner.Deadzone ? axis.X : 0f),
ClampAxis(MathF.Abs(axis.Y) > _inner.Deadzone ? axis.Y : 0f));
}
private static short ClampAxis(float value)
{
if (value <= -short.MaxValue)
{
return -short.MaxValue;
}
else
{
return (short)(value * short.MaxValue);
}
}
}
}