A game about forced loneliness, made by TACStudios
1using UnityEngine.InputSystem.Layouts;
2using UnityEngine.InputSystem.LowLevel;
3using UnityEngine.InputSystem.Utilities;
4
5namespace UnityEngine.InputSystem.Controls
6{
7 /// <summary>
8 /// A floating-point 2D vector control composed of two <see cref="AxisControl"/>s.
9 /// </summary>
10 /// <remarks>
11 /// An example is <see cref="Pointer.position"/>.
12 ///
13 /// <example>
14 /// <code>
15 /// Debug.Log(string.Format("Mouse position x={0} y={1}",
16 /// Mouse.current.position.x.ReadValue(),
17 /// Mouse.current.position.y.ReadValue()));
18 /// </code>
19 /// </example>
20 ///
21 /// Normalization is not implied. The X and Y coordinates can be in any range or units.
22 /// </remarks>
23 public class Vector2Control : InputControl<Vector2>
24 {
25 /// <summary>
26 /// Horizontal position of the control.
27 /// </summary>
28 /// <value>Control representing horizontal motion input.</value>
29 [InputControl(offset = 0, displayName = "X")]
30 public AxisControl x { get; set; }
31
32 /// <summary>
33 /// Vertical position of the control.
34 /// </summary>
35 /// <value>Control representing vertical motion input.</value>
36 [InputControl(offset = 4, displayName = "Y")]
37 public AxisControl y { get; set; }
38
39 /// <summary>
40 /// Default-initialize the control.
41 /// </summary>
42 public Vector2Control()
43 {
44 m_StateBlock.format = InputStateBlock.FormatVector2;
45 }
46
47 /// <inheritdoc />
48 protected override void FinishSetup()
49 {
50 x = GetChildControl<AxisControl>("x");
51 y = GetChildControl<AxisControl>("y");
52
53 base.FinishSetup();
54 }
55
56 /// <inheritdoc />
57 public override unsafe Vector2 ReadUnprocessedValueFromState(void* statePtr)
58 {
59 switch (m_OptimizedControlDataType)
60 {
61 case InputStateBlock.kFormatVector2:
62 return *(Vector2*)((byte*)statePtr + (int)m_StateBlock.byteOffset);
63 default:
64 return new Vector2(
65 x.ReadUnprocessedValueFromStateWithCaching(statePtr),
66 y.ReadUnprocessedValueFromStateWithCaching(statePtr));
67 }
68 }
69
70 /// <inheritdoc />
71 public override unsafe void WriteValueIntoState(Vector2 value, void* statePtr)
72 {
73 switch (m_OptimizedControlDataType)
74 {
75 case InputStateBlock.kFormatVector2:
76 *(Vector2*)((byte*)statePtr + (int)m_StateBlock.byteOffset) = value;
77 break;
78 default:
79 x.WriteValueIntoState(value.x, statePtr);
80 y.WriteValueIntoState(value.y, statePtr);
81 break;
82 }
83 }
84
85 /// <inheritdoc />
86 public override unsafe float EvaluateMagnitude(void* statePtr)
87 {
88 ////REVIEW: this can go beyond 1; that okay?
89 return ReadValueFromStateWithCaching(statePtr).magnitude;
90 }
91
92 protected override FourCC CalculateOptimizedControlDataType()
93 {
94 if (
95 m_StateBlock.sizeInBits == sizeof(float) * 2 * 8 &&
96 m_StateBlock.bitOffset == 0 &&
97 x.optimizedControlDataType == InputStateBlock.FormatFloat &&
98 y.optimizedControlDataType == InputStateBlock.FormatFloat &&
99 y.m_StateBlock.byteOffset == x.m_StateBlock.byteOffset + 4
100 )
101 return InputStateBlock.FormatVector2;
102
103 return InputStateBlock.FormatInvalid;
104 }
105 }
106}