QuaternionControl.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine.InputSystem.Layouts;
  2. using UnityEngine.InputSystem.LowLevel;
  3. ////REVIEW: expose euler angle subcontrols?
  4. namespace UnityEngine.InputSystem.Controls
  5. {
  6. /// <summary>
  7. /// A generic input control reading quaternion (rotation) values.
  8. /// </summary>
  9. [Scripting.Preserve]
  10. public class QuaternionControl : InputControl<Quaternion>
  11. {
  12. // Accessing these components as individual controls usually doesn't make too much sense,
  13. // but having these controls allows changing the state format on the quaternion without
  14. // requiring the control to explicitly support the various different storage formats.
  15. // Also, it allows putting processors on the individual components which may be necessary
  16. // to properly convert the source data.
  17. /// <summary>
  18. /// The X component of the quaternion.
  19. /// </summary>
  20. /// <value>Control representing the X component.</value>
  21. [InputControl(displayName = "X")]
  22. public AxisControl x { get; private set; }
  23. /// <summary>
  24. /// The Y component of the quaternion.
  25. /// </summary>
  26. /// <value>Control representing the Y component.</value>
  27. [InputControl(displayName = "Y")]
  28. public AxisControl y { get; private set; }
  29. /// <summary>
  30. /// The Z component of the quaternion.
  31. /// </summary>
  32. /// <value>Control representing the Z component.</value>
  33. [InputControl(displayName = "Z")]
  34. public AxisControl z { get; private set; }
  35. /// <summary>
  36. /// The W component of the quaternion.
  37. /// </summary>
  38. /// <value>Control representing the W component.</value>
  39. [InputControl(displayName = "W")]
  40. public AxisControl w { get; private set; }
  41. /// <summary>
  42. /// Default-initialize the control.
  43. /// </summary>
  44. public QuaternionControl()
  45. {
  46. m_StateBlock.sizeInBits = sizeof(float) * 4 * 8;
  47. m_StateBlock.format = InputStateBlock.FormatQuaternion;
  48. }
  49. /// <inheritdoc/>
  50. protected override void FinishSetup()
  51. {
  52. x = GetChildControl<AxisControl>("x");
  53. y = GetChildControl<AxisControl>("y");
  54. z = GetChildControl<AxisControl>("z");
  55. w = GetChildControl<AxisControl>("w");
  56. base.FinishSetup();
  57. }
  58. /// <inheritdoc/>
  59. public override unsafe Quaternion ReadUnprocessedValueFromState(void* statePtr)
  60. {
  61. return new Quaternion(x.ReadValueFromState(statePtr), y.ReadValueFromState(statePtr), z.ReadValueFromState(statePtr),
  62. w.ReadUnprocessedValueFromState(statePtr));
  63. }
  64. /// <inheritdoc/>
  65. public override unsafe void WriteValueIntoState(Quaternion value, void* statePtr)
  66. {
  67. x.WriteValueIntoState(value.x, statePtr);
  68. y.WriteValueIntoState(value.y, statePtr);
  69. z.WriteValueIntoState(value.z, statePtr);
  70. w.WriteValueIntoState(value.w, statePtr);
  71. }
  72. }
  73. }