Vector2Control.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnityEngine.InputSystem.Layouts;
  2. using UnityEngine.InputSystem.LowLevel;
  3. namespace UnityEngine.InputSystem.Controls
  4. {
  5. /// <summary>
  6. /// A floating-point 2D vector control composed of two <see cref="AxisControl"/>s.
  7. /// </summary>
  8. /// <remarks>
  9. /// An example is <see cref="Pointer.position"/>.
  10. ///
  11. /// <example>
  12. /// <code>
  13. /// Debug.Log(string.Format("Mouse position x={0} y={1}",
  14. /// Mouse.current.position.x.ReadValue(),
  15. /// Mouse.current.position.y.ReadValue()));
  16. /// </code>
  17. ///
  18. /// Normalization is not implied. The X and Y coordinates can be in any range or units.
  19. /// </example>
  20. /// </remarks>
  21. [Scripting.Preserve]
  22. public class Vector2Control : InputControl<Vector2>
  23. {
  24. /// <summary>
  25. /// Horizontal position of the control.
  26. /// </summary>
  27. /// <value>Control representing horizontal motion input.</value>
  28. [InputControl(offset = 0, displayName = "X")]
  29. public AxisControl x { get; private set; }
  30. /// <summary>
  31. /// Vertical position of the control.
  32. /// </summary>
  33. /// <value>Control representing vertical motion input.</value>
  34. [InputControl(offset = 4, displayName = "Y")]
  35. public AxisControl y { get; private set; }
  36. /// <summary>
  37. /// Default-initialize the control.
  38. /// </summary>
  39. public Vector2Control()
  40. {
  41. m_StateBlock.format = InputStateBlock.FormatVector2;
  42. }
  43. /// <inheritdoc />
  44. protected override void FinishSetup()
  45. {
  46. x = GetChildControl<AxisControl>("x");
  47. y = GetChildControl<AxisControl>("y");
  48. base.FinishSetup();
  49. }
  50. /// <inheritdoc />
  51. public override unsafe Vector2 ReadUnprocessedValueFromState(void* statePtr)
  52. {
  53. return new Vector2(
  54. x.ReadUnprocessedValueFromState(statePtr),
  55. y.ReadUnprocessedValueFromState(statePtr));
  56. }
  57. /// <inheritdoc />
  58. public override unsafe void WriteValueIntoState(Vector2 value, void* statePtr)
  59. {
  60. x.WriteValueIntoState(value.x, statePtr);
  61. y.WriteValueIntoState(value.y, statePtr);
  62. }
  63. /// <inheritdoc />
  64. public override unsafe float EvaluateMagnitude(void* statePtr)
  65. {
  66. ////REVIEW: this can go beyond 1; that okay?
  67. return ReadValueFromState(statePtr).magnitude;
  68. }
  69. }
  70. }