| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using UnityEngine; |
| | | 3 | | using UnityEngine.Events; |
| | | 4 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 5 | | using HenryLab; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// An interactable that can be pressed by a direct interactor |
| | | 9 | | /// </summary> |
| | | 10 | | public class XRButton : XRBaseInteractable |
| | | 11 | | { |
| | | 12 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 2.5f; |
| | | 13 | | [ExcludeFromCodeCoverage] public string Name => Str.Triggerable; |
| | | 14 | | |
| | | 15 | | [ExcludeFromCodeCoverage] |
| | | 16 | | public void Triggerring() |
| | | 17 | | { |
| | | 18 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 19 | | XRDirectInteractor interactor; |
| | | 20 | | if(!obj.TryGetComponent(out interactor)) |
| | | 21 | | { |
| | | 22 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 23 | | } |
| | | 24 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 25 | | { |
| | | 26 | | obj.AddComponent<ActionBasedController>(); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | buttonTransform = transform; |
| | | 30 | | StartPress(new HoverEnterEventArgs() { interactorObject = interactor }); |
| | | 31 | | float height = FindButtonHeight(); |
| | | 32 | | ApplyHeight(height); |
| | | 33 | | OnPress?.Invoke(); |
| | | 34 | | |
| | | 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 | | EndPress(new HoverExitEventArgs() { interactorObject = interactor }); |
| | | 52 | | OnRelease?.Invoke(); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | [Tooltip("The transform of the visual component of the button")] |
| | 16 | 56 | | public Transform buttonTransform = null; |
| | | 57 | | |
| | | 58 | | [Tooltip("The distance the button can be pressed")] |
| | 16 | 59 | | public float pressDistance = 0.1f; |
| | | 60 | | |
| | | 61 | | // When the button is pressed |
| | 16 | 62 | | public UnityEvent OnPress = new UnityEvent(); |
| | | 63 | | |
| | | 64 | | // When the button is released |
| | 16 | 65 | | public UnityEvent OnRelease = new UnityEvent(); |
| | | 66 | | |
| | 16 | 67 | | private float yMin = 0.0f; |
| | 16 | 68 | | private float yMax = 0.0f; |
| | | 69 | | |
| | 16 | 70 | | private IXRHoverInteractor hoverInteractor = null; |
| | | 71 | | |
| | 16 | 72 | | private float hoverHeight = 0.0f; |
| | 16 | 73 | | private float startHeight = 0.0f; |
| | 16 | 74 | | private bool previousPress = false; |
| | | 75 | | |
| | | 76 | | |
| | | 77 | | |
| | | 78 | | protected override void OnEnable() |
| | 8 | 79 | | { |
| | 8 | 80 | | base.OnEnable(); |
| | 8 | 81 | | hoverEntered.AddListener(StartPress); |
| | 8 | 82 | | hoverExited.AddListener(EndPress); |
| | 8 | 83 | | } |
| | | 84 | | |
| | | 85 | | protected override void OnDisable() |
| | 8 | 86 | | { |
| | 8 | 87 | | base.OnDisable(); |
| | 8 | 88 | | hoverEntered.RemoveListener(StartPress); |
| | 8 | 89 | | hoverExited.RemoveListener(EndPress); |
| | 8 | 90 | | } |
| | | 91 | | |
| | | 92 | | private void StartPress(HoverEnterEventArgs eventArgs) |
| | 0 | 93 | | { |
| | 0 | 94 | | hoverInteractor = eventArgs.interactorObject; |
| | 0 | 95 | | hoverHeight = GetLocalYPosition(hoverInteractor.transform.position); |
| | 0 | 96 | | startHeight = buttonTransform.localPosition.y; |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | private void EndPress(HoverExitEventArgs eventArgs) |
| | 0 | 100 | | { |
| | 0 | 101 | | hoverInteractor = null; |
| | 0 | 102 | | hoverHeight = 0.0f; |
| | 0 | 103 | | startHeight = 0.0f; |
| | 0 | 104 | | ApplyHeight(yMax); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | private void Start() |
| | 8 | 108 | | { |
| | 8 | 109 | | SetMinMax(); |
| | 8 | 110 | | } |
| | | 111 | | |
| | | 112 | | private void SetMinMax() |
| | 8 | 113 | | { |
| | 8 | 114 | | yMin = buttonTransform.localPosition.y - pressDistance; |
| | 8 | 115 | | yMax = buttonTransform.localPosition.y; |
| | 8 | 116 | | } |
| | | 117 | | |
| | | 118 | | public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase) |
| | 28040 | 119 | | { |
| | 28040 | 120 | | if(updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic) |
| | 3772 | 121 | | { |
| | 3772 | 122 | | if(isHovered) |
| | 0 | 123 | | { |
| | 0 | 124 | | float height = FindButtonHeight(); |
| | 0 | 125 | | ApplyHeight(height); |
| | 0 | 126 | | } |
| | 3772 | 127 | | } |
| | 28040 | 128 | | } |
| | | 129 | | |
| | | 130 | | private float FindButtonHeight() |
| | 0 | 131 | | { |
| | 0 | 132 | | float newHoverHeight = GetLocalYPosition(hoverInteractor.transform.position); |
| | 0 | 133 | | float hoverDifference = hoverHeight - newHoverHeight; |
| | 0 | 134 | | return startHeight - hoverDifference; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | private float GetLocalYPosition(Vector3 position) |
| | 0 | 138 | | { |
| | 0 | 139 | | Vector3 localPosition = transform.InverseTransformPoint(position); |
| | 0 | 140 | | return localPosition.y; |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | private void ApplyHeight(float position) |
| | 0 | 144 | | { |
| | 0 | 145 | | SetButtonPosition(position); |
| | 0 | 146 | | CheckPress(); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private void SetButtonPosition(float position) |
| | 0 | 150 | | { |
| | 0 | 151 | | Vector3 newPosition = buttonTransform.localPosition; |
| | 0 | 152 | | newPosition.y = Mathf.Clamp(position, yMin, yMax); |
| | 0 | 153 | | buttonTransform.localPosition = newPosition; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | private void CheckPress() |
| | 0 | 157 | | { |
| | 0 | 158 | | bool inPosition = InPosition(); |
| | | 159 | | |
| | 0 | 160 | | if(inPosition != previousPress) |
| | 0 | 161 | | { |
| | 0 | 162 | | previousPress = inPosition; |
| | | 163 | | |
| | 0 | 164 | | if(inPosition) |
| | 0 | 165 | | { |
| | 0 | 166 | | OnPress.Invoke(); |
| | 0 | 167 | | } |
| | | 168 | | else |
| | 0 | 169 | | { |
| | 0 | 170 | | OnRelease.Invoke(); |
| | 0 | 171 | | } |
| | 0 | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | private bool InPosition() |
| | 0 | 176 | | { |
| | 0 | 177 | | float threshold = yMin + (pressDistance * 0.5f); |
| | 0 | 178 | | return buttonTransform.localPosition.y < threshold; |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | public override bool IsSelectableBy(IXRSelectInteractor interactor) |
| | 0 | 182 | | { |
| | 0 | 183 | | return false; |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | |
| | | 187 | | } |