| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.Events; |
| | | 5 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 6 | | using HenryLab; |
| | | 7 | | |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An interactable joystick that can move side to side, and forward and back by a direct interactor |
| | | 11 | | /// </summary> |
| | | 12 | | public class XRJoystick : XRBaseInteractable |
| | | 13 | | { |
| | | 14 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 4.5f; |
| | | 15 | | [ExcludeFromCodeCoverage] public string Name => Str.Transformable; |
| | | 16 | | |
| | | 17 | | [ExcludeFromCodeCoverage] |
| | | 18 | | public void Triggerring() |
| | | 19 | | { |
| | | 20 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 21 | | XRDirectInteractor interactor; |
| | | 22 | | if(!obj.TryGetComponent(out interactor)) |
| | | 23 | | { |
| | | 24 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 25 | | } |
| | | 26 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 27 | | { |
| | | 28 | | obj.AddComponent<ActionBasedController>(); |
| | | 29 | | } |
| | | 30 | | var e = new SelectEnterEventArgs() { interactorObject = interactor }; |
| | | 31 | | StartGrab(e); |
| | | 32 | | selectEntered.Invoke(e); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | [ExcludeFromCodeCoverage] |
| | | 36 | | public void Triggerred() |
| | | 37 | | { |
| | | 38 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 39 | | XRDirectInteractor interactor; |
| | | 40 | | if(!obj.TryGetComponent(out interactor)) |
| | | 41 | | { |
| | | 42 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 43 | | } |
| | | 44 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 45 | | { |
| | | 46 | | obj.AddComponent<ActionBasedController>(); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | OnXValueChange.Invoke(DeltaRotation.x); |
| | | 50 | | OnYValueChange.Invoke(DeltaRotation.y); |
| | | 51 | | Vector3 targetPosition = selectInteractor.transform.position; |
| | | 52 | | Vector3 newRotation = FindJoystickRotation(targetPosition); |
| | | 53 | | ApplyRotation(newRotation); |
| | | 54 | | SetValue(newRotation); |
| | | 55 | | var e = new SelectExitEventArgs() { interactorObject = interactor }; |
| | | 56 | | EndGrab(e); |
| | | 57 | | selectExited.Invoke(e); |
| | | 58 | | |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [ExcludeFromCodeCoverage] public Vector3 DeltaPosition => new Vector3(0, 0, 0); |
| | | 62 | | |
| | | 63 | | [ExcludeFromCodeCoverage] public Vector3 DeltaRotation => new Vector3(90, 30, 0); |
| | | 64 | | |
| | | 65 | | [ExcludeFromCodeCoverage] public Vector3 DeltaScale => new Vector3(0, 0, 0); |
| | | 66 | | |
| | | 67 | | public enum JoystickType |
| | | 68 | | { |
| | | 69 | | None, |
| | | 70 | | Both, |
| | | 71 | | FrontBack, |
| | | 72 | | LeftRight, |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | [Tooltip("Joystick sensitivity")] |
| | 8 | 76 | | public float rateOfChange = 0.1f; |
| | | 77 | | |
| | | 78 | | [Tooltip("Contols how the joystick behaves ")] |
| | 8 | 79 | | public JoystickType leverType = JoystickType.Both; |
| | | 80 | | |
| | | 81 | | [Tooltip("The transform of the visual component of the joystick")] |
| | 8 | 82 | | public Transform handle = null; |
| | | 83 | | |
| | | 84 | | [Serializable] public class ValueChangeEvent : UnityEvent<float> { } |
| | | 85 | | |
| | | 86 | | // When the joystick's x value changes |
| | 8 | 87 | | public ValueChangeEvent OnXValueChange = new ValueChangeEvent(); |
| | | 88 | | |
| | | 89 | | // When the joystick's y value changes |
| | 8 | 90 | | public ValueChangeEvent OnYValueChange = new ValueChangeEvent(); |
| | | 91 | | |
| | 8 | 92 | | public Vector2 Value { get; private set; } = Vector2.zero; |
| | | 93 | | |
| | | 94 | | |
| | | 95 | | |
| | 8 | 96 | | private IXRSelectInteractor selectInteractor = null; |
| | | 97 | | |
| | 8 | 98 | | private Vector3 initialPosition = Vector3.zero; |
| | | 99 | | |
| | 8 | 100 | | private readonly int minRotation = -30; |
| | 8 | 101 | | private readonly int maxRotation = 30; |
| | | 102 | | |
| | | 103 | | protected override void OnEnable() |
| | 4 | 104 | | { |
| | 4 | 105 | | base.OnEnable(); |
| | 4 | 106 | | selectEntered.AddListener(StartGrab); |
| | 4 | 107 | | selectExited.AddListener(EndGrab); |
| | 4 | 108 | | } |
| | | 109 | | |
| | | 110 | | protected override void OnDisable() |
| | 4 | 111 | | { |
| | 4 | 112 | | base.OnDisable(); |
| | 4 | 113 | | selectEntered.RemoveListener(StartGrab); |
| | 4 | 114 | | selectExited.RemoveListener(EndGrab); |
| | 4 | 115 | | } |
| | | 116 | | |
| | | 117 | | private void StartGrab(SelectEnterEventArgs eventArgs) |
| | 0 | 118 | | { |
| | 0 | 119 | | selectInteractor = eventArgs.interactorObject; |
| | 0 | 120 | | initialPosition = ConvertToLocal(selectInteractor.transform.position); |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | private void EndGrab(SelectExitEventArgs eventArgs) |
| | 0 | 124 | | { |
| | 0 | 125 | | selectInteractor = null; |
| | 0 | 126 | | ResetRotation(); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private void ResetRotation() |
| | 0 | 130 | | { |
| | 0 | 131 | | handle.localRotation = Quaternion.identity; |
| | 0 | 132 | | initialPosition = Vector3.zero; |
| | 0 | 133 | | SetValue(Vector3.zero); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase) |
| | 14020 | 137 | | { |
| | 14020 | 138 | | base.ProcessInteractable(updatePhase); |
| | | 139 | | |
| | 14020 | 140 | | if(isSelected) |
| | 0 | 141 | | { |
| | 0 | 142 | | if(updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic) |
| | 0 | 143 | | { |
| | 0 | 144 | | Vector3 targetPosition = selectInteractor.transform.position; |
| | 0 | 145 | | Vector3 newRotation = FindJoystickRotation(targetPosition); |
| | | 146 | | |
| | 0 | 147 | | ApplyRotation(newRotation); |
| | 0 | 148 | | SetValue(newRotation); |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | 14020 | 151 | | } |
| | | 152 | | |
| | | 153 | | private Vector3 FindJoystickRotation(Vector3 target) |
| | 0 | 154 | | { |
| | 0 | 155 | | Vector3 currentPosition = ConvertToLocal(target); |
| | 0 | 156 | | Vector3 positionDifference = currentPosition - initialPosition; |
| | 0 | 157 | | positionDifference.y = rateOfChange; |
| | | 158 | | |
| | 0 | 159 | | Vector3 finalRotation = Vector3.zero; |
| | | 160 | | |
| | 0 | 161 | | if(leverType == JoystickType.FrontBack || leverType == JoystickType.Both) |
| | 0 | 162 | | { |
| | 0 | 163 | | float xRotation = GetRotationDifference(positionDifference.y, positionDifference.z); |
| | 0 | 164 | | finalRotation.x = xRotation; |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | if(leverType == JoystickType.LeftRight || leverType == JoystickType.Both) |
| | 0 | 168 | | { |
| | 0 | 169 | | float zRotation = GetRotationDifference(positionDifference.y, positionDifference.x); |
| | 0 | 170 | | finalRotation.z = zRotation; |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return finalRotation; |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | private Vector3 ConvertToLocal(Vector3 target) |
| | 0 | 177 | | { |
| | 0 | 178 | | return transform.InverseTransformPoint(target); |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | private float GetRotationDifference(float yRelative, float xRelative) |
| | 0 | 182 | | { |
| | 0 | 183 | | float difference = Mathf.Atan2(xRelative, yRelative) * Mathf.Rad2Deg; |
| | 0 | 184 | | difference = Mathf.Clamp(difference, minRotation, maxRotation); |
| | 0 | 185 | | return difference; |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | private void ApplyRotation(Vector3 newRotation) |
| | 0 | 189 | | { |
| | 0 | 190 | | newRotation.z *= -1; |
| | 0 | 191 | | handle.localRotation = Quaternion.Euler(newRotation); |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | private void SetValue(Vector3 rotation) |
| | 0 | 195 | | { |
| | 0 | 196 | | Value = CalculateValue(rotation); |
| | 0 | 197 | | OnXValueChange.Invoke(Value.x); |
| | 0 | 198 | | OnYValueChange.Invoke(Value.y); |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | private Vector2 CalculateValue(Vector3 rotation) |
| | 0 | 202 | | { |
| | 0 | 203 | | Vector2 newValue = Vector2.zero; |
| | 0 | 204 | | newValue.x = Normalize(rotation.z); |
| | 0 | 205 | | newValue.y = Normalize(rotation.x); |
| | 0 | 206 | | return newValue; |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | private float Normalize(float value) |
| | 0 | 210 | | { |
| | 0 | 211 | | value = Mathf.InverseLerp(minRotation, maxRotation, value); |
| | 0 | 212 | | value = Mathf.Lerp(-1, 1, value); |
| | 0 | 213 | | return value; |
| | 0 | 214 | | } |
| | | 215 | | } |