暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Vector2Control.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine.InputSystem.Layouts;
  2. using UnityEngine.InputSystem.LowLevel;
  3. using UnityEngine.InputSystem.Utilities;
  4. namespace UnityEngine.InputSystem.Controls
  5. {
  6. /// <summary>
  7. /// A floating-point 2D vector control composed of two <see cref="AxisControl"/>s.
  8. /// </summary>
  9. /// <remarks>
  10. /// An example is <see cref="Pointer.position"/>.
  11. ///
  12. /// <example>
  13. /// <code>
  14. /// Debug.Log(string.Format("Mouse position x={0} y={1}",
  15. /// Mouse.current.position.x.ReadValue(),
  16. /// Mouse.current.position.y.ReadValue()));
  17. /// </code>
  18. /// </example>
  19. ///
  20. /// Normalization is not implied. The X and Y coordinates can be in any range or units.
  21. /// </remarks>
  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; 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; 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. switch (m_OptimizedControlDataType)
  54. {
  55. case InputStateBlock.kFormatVector2:
  56. return *(Vector2*)((byte*)statePtr + (int)m_StateBlock.byteOffset);
  57. default:
  58. return new Vector2(
  59. x.ReadUnprocessedValueFromStateWithCaching(statePtr),
  60. y.ReadUnprocessedValueFromStateWithCaching(statePtr));
  61. }
  62. }
  63. /// <inheritdoc />
  64. public override unsafe void WriteValueIntoState(Vector2 value, void* statePtr)
  65. {
  66. switch (m_OptimizedControlDataType)
  67. {
  68. case InputStateBlock.kFormatVector2:
  69. *(Vector2*)((byte*)statePtr + (int)m_StateBlock.byteOffset) = value;
  70. break;
  71. default:
  72. x.WriteValueIntoState(value.x, statePtr);
  73. y.WriteValueIntoState(value.y, statePtr);
  74. break;
  75. }
  76. }
  77. /// <inheritdoc />
  78. public override unsafe float EvaluateMagnitude(void* statePtr)
  79. {
  80. ////REVIEW: this can go beyond 1; that okay?
  81. return ReadValueFromStateWithCaching(statePtr).magnitude;
  82. }
  83. protected override FourCC CalculateOptimizedControlDataType()
  84. {
  85. if (
  86. m_StateBlock.sizeInBits == sizeof(float) * 2 * 8 &&
  87. m_StateBlock.bitOffset == 0 &&
  88. x.optimizedControlDataType == InputStateBlock.FormatFloat &&
  89. y.optimizedControlDataType == InputStateBlock.FormatFloat &&
  90. y.m_StateBlock.byteOffset == x.m_StateBlock.byteOffset + 4
  91. )
  92. return InputStateBlock.FormatVector2;
  93. return InputStateBlock.FormatInvalid;
  94. }
  95. }
  96. }