| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using UnityEngine; |
| | | 3 | | using UnityEngine.Events; |
| | | 4 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 5 | | using HenryLab; |
| | | 6 | | |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// An interactable lever that snaps into an on or off position by a direct interactor |
| | | 10 | | /// </summary> |
| | | 11 | | public class XRLever : XRBaseInteractable |
| | | 12 | | { |
| | | 13 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 2.5f; |
| | | 14 | | [ExcludeFromCodeCoverage] public string Name => Str.Triggerable; |
| | | 15 | | |
| | | 16 | | [ExcludeFromCodeCoverage] |
| | | 17 | | public void Triggerring() |
| | | 18 | | { |
| | | 19 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 20 | | XRDirectInteractor interactor; |
| | | 21 | | if(!obj.TryGetComponent(out interactor)) |
| | | 22 | | { |
| | | 23 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 24 | | } |
| | | 25 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 26 | | { |
| | | 27 | | obj.AddComponent<ActionBasedController>(); |
| | | 28 | | } |
| | | 29 | | var e = new SelectEnterEventArgs() { interactorObject = interactor }; |
| | | 30 | | StartGrab(e); |
| | | 31 | | selectEntered.Invoke(e); |
| | | 32 | | OnLeverActivate?.Invoke(); |
| | | 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 | | Vector3 lookDirection = GetLookDirection(); |
| | | 50 | | handle.forward = transform.TransformDirection(lookDirection); |
| | | 51 | | OnLeverDeactivate?.Invoke(); |
| | | 52 | | var e = new SelectExitEventArgs() { interactorObject = interactor }; |
| | | 53 | | EndGrab(e); |
| | | 54 | | selectExited.Invoke(e); |
| | | 55 | | } |
| | | 56 | | [Tooltip("The object that's grabbed and manipulated")] |
| | 4 | 57 | | public Transform handle = null; |
| | | 58 | | |
| | | 59 | | [Tooltip("The initial value of the lever")] |
| | 4 | 60 | | public bool defaultValue = false; |
| | | 61 | | |
| | | 62 | | // When the lever is activated |
| | 4 | 63 | | public UnityEvent OnLeverActivate = new UnityEvent(); |
| | | 64 | | |
| | | 65 | | // When the lever is deactivated |
| | 4 | 66 | | public UnityEvent OnLeverDeactivate = new UnityEvent(); |
| | | 67 | | |
| | 8 | 68 | | public bool Value { get; private set; } = false; |
| | | 69 | | |
| | 4 | 70 | | private IXRSelectInteractor selectInteractor = null; |
| | | 71 | | |
| | | 72 | | private void Start() |
| | 2 | 73 | | { |
| | 2 | 74 | | FindSnapDirection(defaultValue); |
| | 2 | 75 | | SetValue(defaultValue); |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | protected override void OnEnable() |
| | 2 | 79 | | { |
| | 2 | 80 | | base.OnEnable(); |
| | 2 | 81 | | selectEntered.AddListener(StartGrab); |
| | 2 | 82 | | selectExited.AddListener(EndGrab); |
| | 2 | 83 | | selectExited.AddListener(ApplyValue); |
| | 2 | 84 | | } |
| | | 85 | | |
| | | 86 | | protected override void OnDisable() |
| | 2 | 87 | | { |
| | 2 | 88 | | base.OnDisable(); |
| | 2 | 89 | | selectEntered.RemoveListener(StartGrab); |
| | 2 | 90 | | selectExited.RemoveListener(EndGrab); |
| | 2 | 91 | | selectExited.RemoveListener(ApplyValue); |
| | 2 | 92 | | } |
| | | 93 | | |
| | | 94 | | private void StartGrab(SelectEnterEventArgs eventArgs) |
| | 0 | 95 | | { |
| | 0 | 96 | | selectInteractor = eventArgs.interactorObject; |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | private void EndGrab(SelectExitEventArgs eventArgs) |
| | 0 | 100 | | { |
| | 0 | 101 | | selectInteractor = null; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase) |
| | 7010 | 105 | | { |
| | 7010 | 106 | | base.ProcessInteractable(updatePhase); |
| | | 107 | | |
| | 7010 | 108 | | if (updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic) |
| | 943 | 109 | | { |
| | 943 | 110 | | if (isSelected) |
| | 0 | 111 | | { |
| | 0 | 112 | | Vector3 lookDirection = GetLookDirection(); |
| | 0 | 113 | | handle.forward = transform.TransformDirection(lookDirection); |
| | 0 | 114 | | } |
| | 943 | 115 | | } |
| | 7010 | 116 | | } |
| | | 117 | | |
| | | 118 | | private Vector3 GetLookDirection() |
| | 0 | 119 | | { |
| | 0 | 120 | | Vector3 direction = selectInteractor.transform.position - handle.position; |
| | 0 | 121 | | direction = transform.InverseTransformDirection(direction); |
| | | 122 | | |
| | 0 | 123 | | direction.x = 0; |
| | 0 | 124 | | direction.y = Mathf.Clamp(direction.y, 0, 1); |
| | | 125 | | |
| | 0 | 126 | | return direction; |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private void ApplyValue(SelectExitEventArgs eventArgs) |
| | 0 | 130 | | { |
| | 0 | 131 | | IXRSelectInteractor interactor = eventArgs.interactorObject; |
| | 0 | 132 | | bool isOn = InOnPosition(interactor.transform.position); |
| | | 133 | | |
| | 0 | 134 | | FindSnapDirection(isOn); |
| | 0 | 135 | | SetValue(isOn); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | private bool InOnPosition(Vector3 interactorPosition) |
| | 0 | 139 | | { |
| | 0 | 140 | | interactorPosition = transform.InverseTransformPoint(interactorPosition); |
| | 0 | 141 | | return (interactorPosition.z > 0); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | private void FindSnapDirection(bool isOn) |
| | 2 | 145 | | { |
| | 2 | 146 | | handle.forward = isOn ? transform.forward : -transform.forward; |
| | 2 | 147 | | } |
| | | 148 | | |
| | | 149 | | private void SetValue(bool isOn) |
| | 2 | 150 | | { |
| | 2 | 151 | | Value = isOn; |
| | | 152 | | |
| | 2 | 153 | | if (Value) |
| | 2 | 154 | | { |
| | 2 | 155 | | OnLeverActivate.Invoke(); |
| | 2 | 156 | | } |
| | | 157 | | else |
| | 0 | 158 | | { |
| | 0 | 159 | | OnLeverDeactivate.Invoke(); |
| | 0 | 160 | | } |
| | 2 | 161 | | } |
| | | 162 | | } |