No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector3Composite.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.ComponentModel;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.Utilities;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. using UnityEngine.InputSystem.Editor;
  8. using UnityEngine.UIElements;
  9. #endif
  10. namespace UnityEngine.InputSystem.Composites
  11. {
  12. /// <summary>
  13. /// A 3D vector formed from six floating-point inputs.
  14. /// </summary>
  15. /// <remarks>
  16. /// Depending on the setting of <see cref="mode"/>, the vector is either in the [-1..1]
  17. /// range on each axis (normalized or not depending on <see cref="mode"/>) or is in the
  18. /// full value range of the input controls.
  19. ///
  20. /// <example>
  21. /// <code>
  22. /// action.AddCompositeBinding("3DVector")
  23. /// .With("Forward", "&lt;Keyboard&gt;/w")
  24. /// .With("Backward", "&lt;Keyboard&gt;/s")
  25. /// .With("Left", "&lt;Keyboard&gt;/a")
  26. /// .With("Right", "&lt;Keyboard&gt;/d")
  27. /// .With("Up", "&lt;Keyboard&gt;/q")
  28. /// .With("Down", "&lt;Keyboard&gt;/e");
  29. /// </code>
  30. /// </example>
  31. /// </remarks>
  32. /// <seealso cref="Vector2Composite"/>
  33. [DisplayStringFormat("{up}+{down}/{left}+{right}/{forward}+{backward}")]
  34. [DisplayName("Up/Down/Left/Right/Forward/Backward Composite")]
  35. public class Vector3Composite : InputBindingComposite<Vector3>
  36. {
  37. /// <summary>
  38. /// Binding for the button that represents the up (that is, <c>(0,1,0)</c>) direction of the vector.
  39. /// </summary>
  40. /// <remarks>
  41. /// This property is automatically assigned by the input system.
  42. /// </remarks>
  43. // ReSharper disable once MemberCanBePrivate.Global
  44. // ReSharper disable once FieldCanBeMadeReadOnly.Global
  45. [InputControl(layout = "Axis")] public int up;
  46. /// <summary>
  47. /// Binding for the button that represents the down (that is, <c>(0,-1,0)</c>) direction of the vector.
  48. /// </summary>
  49. /// <remarks>
  50. /// This property is automatically assigned by the input system.
  51. /// </remarks>
  52. // ReSharper disable once MemberCanBePrivate.Global
  53. // ReSharper disable once FieldCanBeMadeReadOnly.Global
  54. [InputControl(layout = "Axis")] public int down;
  55. /// <summary>
  56. /// Binding for the button that represents the left (that is, <c>(-1,0,0)</c>) direction of the vector.
  57. /// </summary>
  58. /// <remarks>
  59. /// This property is automatically assigned by the input system.
  60. /// </remarks>
  61. // ReSharper disable once MemberCanBePrivate.Global
  62. // ReSharper disable once FieldCanBeMadeReadOnly.Global
  63. [InputControl(layout = "Axis")] public int left;
  64. /// <summary>
  65. /// Binding for the button that represents the right (that is, <c>(1,0,0)</c>) direction of the vector.
  66. /// </summary>
  67. /// <remarks>
  68. /// This property is automatically assigned by the input system.
  69. /// </remarks>
  70. // ReSharper disable once MemberCanBePrivate.Global
  71. // ReSharper disable once FieldCanBeMadeReadOnly.Global
  72. [InputControl(layout = "Axis")] public int right;
  73. /// <summary>
  74. /// Binding for the button that represents the right (that is, <c>(0,0,1)</c>) direction of the vector.
  75. /// </summary>
  76. /// <remarks>
  77. /// This property is automatically assigned by the input system.
  78. /// </remarks>
  79. // ReSharper disable once MemberCanBePrivate.Global
  80. // ReSharper disable once FieldCanBeMadeReadOnly.Global
  81. [InputControl(layout = "Axis")] public int forward;
  82. /// <summary>
  83. /// Binding for the button that represents the right (that is, <c>(0,0,-1)</c>) direction of the vector.
  84. /// </summary>
  85. /// <remarks>
  86. /// This property is automatically assigned by the input system.
  87. /// </remarks>
  88. // ReSharper disable once MemberCanBePrivate.Global
  89. // ReSharper disable once FieldCanBeMadeReadOnly.Global
  90. [InputControl(layout = "Axis")] public int backward;
  91. /// <summary>
  92. /// How to synthesize a <c>Vector3</c> from the values read from <see cref="up"/>, <see cref="down"/>,
  93. /// <see cref="left"/>, <see cref="right"/>, <see cref="forward"/>, and <see cref="backward"/>.
  94. /// </summary>
  95. /// <value>Determines how X, Y, and Z of the resulting <c>Vector3</c> are formed from input values.</value>
  96. public Mode mode = Mode.Analog;
  97. /// <inheritdoc/>
  98. public override Vector3 ReadValue(ref InputBindingCompositeContext context)
  99. {
  100. if (mode == Mode.Analog)
  101. {
  102. var upValue = context.ReadValue<float>(up);
  103. var downValue = context.ReadValue<float>(down);
  104. var leftValue = context.ReadValue<float>(left);
  105. var rightValue = context.ReadValue<float>(right);
  106. var forwardValue = context.ReadValue<float>(forward);
  107. var backwardValue = context.ReadValue<float>(backward);
  108. return new Vector3(rightValue - leftValue, upValue - downValue, forwardValue - backwardValue);
  109. }
  110. else
  111. {
  112. var upValue = context.ReadValueAsButton(up) ? 1f : 0f;
  113. var downValue = context.ReadValueAsButton(down) ? -1f : 0f;
  114. var leftValue = context.ReadValueAsButton(left) ? -1f : 0f;
  115. var rightValue = context.ReadValueAsButton(right) ? 1f : 0f;
  116. var forwardValue = context.ReadValueAsButton(forward) ? 1f : 0f;
  117. var backwardValue = context.ReadValueAsButton(backward) ? -1f : 0f;
  118. var vector = new Vector3(leftValue + rightValue, upValue + downValue, forwardValue + backwardValue);
  119. if (mode == Mode.DigitalNormalized)
  120. vector = vector.normalized;
  121. return vector;
  122. }
  123. }
  124. /// <inheritdoc />
  125. public override float EvaluateMagnitude(ref InputBindingCompositeContext context)
  126. {
  127. var value = ReadValue(ref context);
  128. return value.magnitude;
  129. }
  130. /// <summary>
  131. /// Determines how a <c>Vector3</c> is synthesized from part controls.
  132. /// </summary>
  133. public enum Mode
  134. {
  135. /// <summary>
  136. /// Part controls are treated as analog meaning that the floating-point values read from controls
  137. /// will come through as is (minus the fact that the down and left direction values are negated).
  138. /// </summary>
  139. Analog,
  140. /// <summary>
  141. /// Part controls are treated as buttons (on/off) and the resulting vector is normalized. This means
  142. /// that if, for example, both left and up are pressed, instead of returning a vector (-1,1,0), a vector
  143. /// of roughly (-0.7,0.7,0) (that is, corresponding to <c>new Vector3(-1,1,0).normalized</c>) is returned instead.
  144. /// </summary>
  145. DigitalNormalized,
  146. /// <summary>
  147. /// Part controls are treated as buttons (on/off) and the resulting vector is not normalized. This means
  148. /// that if both left and up are pressed, for example, the resulting vector is (-1,1,0) and has a length
  149. /// greater than 1.
  150. /// </summary>
  151. Digital,
  152. }
  153. }
  154. #if UNITY_EDITOR
  155. internal class Vector3CompositeEditor : InputParameterEditor<Vector3Composite>
  156. {
  157. private GUIContent m_ModeLabel = new GUIContent("Mode",
  158. "How to synthesize a Vector3 from the inputs. Digital "
  159. + "treats part bindings as buttons (on/off) whereas Analog preserves "
  160. + "floating-point magnitudes as read from controls.");
  161. public override void OnGUI()
  162. {
  163. target.mode = (Vector3Composite.Mode)EditorGUILayout.EnumPopup(m_ModeLabel, target.mode);
  164. }
  165. #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  166. public override void OnDrawVisualElements(VisualElement root, Action onChangedCallback)
  167. {
  168. var modeField = new EnumField(m_ModeLabel.text, target.mode)
  169. {
  170. tooltip = m_ModeLabel.tooltip
  171. };
  172. modeField.RegisterValueChangedCallback(evt =>
  173. {
  174. target.mode = (Vector3Composite.Mode)evt.newValue;
  175. onChangedCallback();
  176. });
  177. root.Add(modeField);
  178. }
  179. #endif
  180. }
  181. #endif
  182. }