| | | 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 | | /// <summary> |
| | | 9 | | /// An interactable knob that follows the rotation of the interactor |
| | | 10 | | /// </summary> |
| | | 11 | | public class XRKnob : XRBaseInteractable, ITransformableEntity |
| | | 12 | | { |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 2.5f; |
| | | 16 | | [ExcludeFromCodeCoverage] public string Name => Str.Transformable; |
| | | 17 | | |
| | | 18 | | [ExcludeFromCodeCoverage] |
| | | 19 | | public void Triggerring() |
| | | 20 | | { |
| | | 21 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 22 | | XRDirectInteractor interactor; |
| | | 23 | | if(!obj.TryGetComponent(out interactor)) |
| | | 24 | | { |
| | | 25 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 26 | | } |
| | | 27 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 28 | | { |
| | | 29 | | obj.AddComponent<ActionBasedController>(); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | var e = new SelectEnterEventArgs() { interactorObject = interactor }; |
| | | 33 | | StartTurn(e); |
| | | 34 | | selectEntered.Invoke(e); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | [ExcludeFromCodeCoverage] |
| | | 38 | | public void Triggerred() |
| | | 39 | | { |
| | | 40 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 41 | | XRDirectInteractor interactor; |
| | | 42 | | if(!obj.TryGetComponent(out interactor)) |
| | | 43 | | { |
| | | 44 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 45 | | } |
| | | 46 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 47 | | { |
| | | 48 | | obj.AddComponent<ActionBasedController>(); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | OnValueChange.Invoke(DeltaRotation.y); |
| | | 52 | | Angle = FindRotationValue(); |
| | | 53 | | float finalRotation = ApplyRotation(Angle); |
| | | 54 | | |
| | | 55 | | SetValue(finalRotation); |
| | | 56 | | selectRotation = selectInteractor.transform.rotation; |
| | | 57 | | var e = new SelectExitEventArgs() { interactorObject = interactor }; |
| | | 58 | | EndTurn(e); |
| | | 59 | | selectExited.Invoke(e); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | [ExcludeFromCodeCoverage] public Vector3 DeltaPosition => new Vector3(0, 0, 0); |
| | | 63 | | |
| | | 64 | | [ExcludeFromCodeCoverage] public Vector3 DeltaRotation => new Vector3(0, 180, 0); |
| | | 65 | | |
| | | 66 | | [ExcludeFromCodeCoverage] public Vector3 DeltaScale => new Vector3(0, 0, 0); |
| | | 67 | | |
| | | 68 | | [Tooltip("The transform of the visual component of the knob")] |
| | 2 | 69 | | public Transform knobTransform = null; |
| | | 70 | | |
| | | 71 | | [Tooltip("The minimum range the knob can rotate")] |
| | 2 | 72 | | [Range(-180, 0)] public float minimum = -90.0f; |
| | | 73 | | |
| | | 74 | | [Tooltip("The maximum range the knob can rotate")] |
| | 2 | 75 | | [Range(0, 180)] public float maximum = 90.0f; |
| | | 76 | | |
| | | 77 | | [Tooltip("The initial value of the knob")] |
| | 2 | 78 | | [Range(0, 1)] public float defaultValue = 0.0f; |
| | | 79 | | |
| | | 80 | | [Serializable] public class ValueChangeEvent : UnityEvent<float> { } |
| | | 81 | | |
| | | 82 | | // When the knobs's value changes |
| | 2 | 83 | | public ValueChangeEvent OnValueChange = new ValueChangeEvent(); |
| | | 84 | | |
| | 8 | 85 | | public float Value { get; private set; } = 0.0f; |
| | 6 | 86 | | public float Angle { get; private set; } = 0.0f; |
| | | 87 | | |
| | 2 | 88 | | private IXRSelectInteractor selectInteractor = null; |
| | 2 | 89 | | private Quaternion selectRotation = Quaternion.identity; |
| | | 90 | | |
| | | 91 | | private void Start() |
| | 1 | 92 | | { |
| | 1 | 93 | | float defaultRotation = Mathf.Lerp(minimum, maximum, defaultValue); |
| | 1 | 94 | | ApplyRotation(defaultRotation); |
| | 1 | 95 | | SetValue(defaultRotation); |
| | 1 | 96 | | } |
| | | 97 | | |
| | | 98 | | protected override void OnEnable() |
| | 1 | 99 | | { |
| | 1 | 100 | | base.OnEnable(); |
| | 1 | 101 | | selectEntered.AddListener(StartTurn); |
| | 1 | 102 | | selectExited.AddListener(EndTurn); |
| | 1 | 103 | | } |
| | | 104 | | |
| | | 105 | | protected override void OnDisable() |
| | 1 | 106 | | { |
| | 1 | 107 | | base.OnDisable(); |
| | 1 | 108 | | selectEntered.RemoveListener(StartTurn); |
| | 1 | 109 | | selectExited.RemoveListener(EndTurn); |
| | 1 | 110 | | } |
| | | 111 | | |
| | | 112 | | private void StartTurn(SelectEnterEventArgs eventArgs) |
| | 4 | 113 | | { |
| | 4 | 114 | | selectInteractor = eventArgs.interactorObject; |
| | 4 | 115 | | selectRotation = selectInteractor.transform.rotation; |
| | 4 | 116 | | } |
| | | 117 | | |
| | | 118 | | private void EndTurn(SelectExitEventArgs eventArgs) |
| | 4 | 119 | | { |
| | 4 | 120 | | selectInteractor = null; |
| | 4 | 121 | | selectRotation = Quaternion.identity; |
| | 4 | 122 | | } |
| | | 123 | | |
| | | 124 | | public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase) |
| | 5286 | 125 | | { |
| | 5286 | 126 | | base.ProcessInteractable(updatePhase); |
| | | 127 | | |
| | 5286 | 128 | | if (updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic) |
| | 718 | 129 | | { |
| | 718 | 130 | | if (isSelected) |
| | 0 | 131 | | { |
| | 0 | 132 | | Angle = FindRotationValue(); |
| | 0 | 133 | | float finalRotation = ApplyRotation(Angle); |
| | | 134 | | |
| | 0 | 135 | | SetValue(finalRotation); |
| | 0 | 136 | | selectRotation = selectInteractor.transform.rotation; |
| | 0 | 137 | | } |
| | 718 | 138 | | } |
| | 5286 | 139 | | } |
| | | 140 | | |
| | | 141 | | private float FindRotationValue() |
| | 2 | 142 | | { |
| | 2 | 143 | | Quaternion rotationDifference = selectInteractor.transform.rotation * Quaternion.Inverse(selectRotation); |
| | 2 | 144 | | Vector3 rotatedForward = rotationDifference * knobTransform.forward; |
| | 2 | 145 | | return (Vector3.SignedAngle(knobTransform.forward, rotatedForward, transform.up)); |
| | 2 | 146 | | } |
| | | 147 | | |
| | | 148 | | private float ApplyRotation(float angle) |
| | 3 | 149 | | { |
| | 3 | 150 | | Quaternion newRotation = Quaternion.AngleAxis(angle, Vector3.up); |
| | 3 | 151 | | newRotation *= knobTransform.localRotation; |
| | | 152 | | |
| | 3 | 153 | | Vector3 eulerRotation = newRotation.eulerAngles; |
| | 3 | 154 | | eulerRotation.y = ClampAngle(eulerRotation.y); |
| | | 155 | | |
| | 3 | 156 | | knobTransform.localEulerAngles = eulerRotation; |
| | 3 | 157 | | return eulerRotation.y; |
| | 3 | 158 | | } |
| | | 159 | | |
| | | 160 | | private float ClampAngle(float angle) |
| | 3 | 161 | | { |
| | 3 | 162 | | if (angle > 180) |
| | 3 | 163 | | angle -= 360; |
| | | 164 | | |
| | 3 | 165 | | return (Mathf.Clamp(angle, minimum, maximum)); |
| | 3 | 166 | | } |
| | | 167 | | |
| | | 168 | | private void SetValue(float rotation) |
| | 3 | 169 | | { |
| | 3 | 170 | | Value = Mathf.InverseLerp(minimum, maximum, rotation); |
| | 3 | 171 | | OnValueChange.Invoke(Value); |
| | 3 | 172 | | } |
| | | 173 | | } |