MotionDevice.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Ryujinx.Common.Configuration.Hid;
  2. using Ryujinx.Configuration;
  3. using System;
  4. using System.Numerics;
  5. namespace Ryujinx.Motion
  6. {
  7. public class MotionDevice
  8. {
  9. public Vector3 Gyroscope { get; private set; }
  10. public Vector3 Accelerometer { get; private set; }
  11. public Vector3 Rotation { get; private set; }
  12. public float[] Orientation { get; private set; }
  13. private Client _motionSource;
  14. public MotionDevice(Client motionSource)
  15. {
  16. _motionSource = motionSource;
  17. }
  18. public void RegisterController(PlayerIndex player)
  19. {
  20. InputConfig config = ConfigurationState.Instance.Hid.InputConfig.Value.Find(x => x.PlayerIndex == player);
  21. if (config != null && config.EnableMotion)
  22. {
  23. string host = config.DsuServerHost;
  24. int port = config.DsuServerPort;
  25. _motionSource.RegisterClient((int)player, host, port);
  26. _motionSource.RequestData((int)player, config.Slot);
  27. if (config.ControllerType == ControllerType.JoyconPair && !config.MirrorInput)
  28. {
  29. _motionSource.RequestData((int)player, config.AltSlot);
  30. }
  31. }
  32. }
  33. public void Poll(InputConfig config, int slot)
  34. {
  35. Orientation = new float[9];
  36. if (!config.EnableMotion || !_motionSource.TryGetData((int)config.PlayerIndex, slot, out MotionInput input))
  37. {
  38. Accelerometer = new Vector3();
  39. Gyroscope = new Vector3();
  40. return;
  41. }
  42. Gyroscope = Truncate(input.Gyroscrope * 0.0027f, 3);
  43. Accelerometer = Truncate(input.Accelerometer, 3);
  44. Rotation = Truncate(input.Rotation * 0.0027f, 3);
  45. Matrix4x4 orientation = input.GetOrientation();
  46. Orientation[0] = Math.Clamp(orientation.M11, -1f, 1f);
  47. Orientation[1] = Math.Clamp(orientation.M12, -1f, 1f);
  48. Orientation[2] = Math.Clamp(orientation.M13, -1f, 1f);
  49. Orientation[3] = Math.Clamp(orientation.M21, -1f, 1f);
  50. Orientation[4] = Math.Clamp(orientation.M22, -1f, 1f);
  51. Orientation[5] = Math.Clamp(orientation.M23, -1f, 1f);
  52. Orientation[6] = Math.Clamp(orientation.M31, -1f, 1f);
  53. Orientation[7] = Math.Clamp(orientation.M32, -1f, 1f);
  54. Orientation[8] = Math.Clamp(orientation.M33, -1f, 1f);
  55. }
  56. private static Vector3 Truncate(Vector3 value, int decimals)
  57. {
  58. float power = MathF.Pow(10, decimals);
  59. value.X = float.IsNegative(value.X) ? MathF.Ceiling(value.X * power) / power : MathF.Floor(value.X * power) / power;
  60. value.Y = float.IsNegative(value.Y) ? MathF.Ceiling(value.Y * power) / power : MathF.Floor(value.Y * power) / power;
  61. value.Z = float.IsNegative(value.Z) ? MathF.Ceiling(value.Z * power) / power : MathF.Floor(value.Z * power) / power;
  62. return value;
  63. }
  64. }
  65. }