< Summary

Class:XRButton
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Basics/Assets/_Course Library/Scripts/Core/XRButton.cs
Covered lines:33
Uncovered lines:54
Coverable lines:87
Total lines:187
Line coverage:37.9% (33 of 87)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:15
Method coverage:40% (6 of 15)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
XRButton()0%000100%
OnEnable()0%000100%
OnDisable()0%000100%
StartPress(...)0%0000%
EndPress(...)0%0000%
Start()0%000100%
SetMinMax()0%000100%
ProcessInteractable(...)0%00060%
FindButtonHeight()0%0000%
GetLocalYPosition(...)0%0000%
ApplyHeight(...)0%0000%
SetButtonPosition(...)0%0000%
CheckPress()0%0000%
InPosition()0%0000%
IsSelectableBy(...)0%0000%

File(s)

E:/Unity/Unity Project/VR-Basics/Assets/_Course Library/Scripts/Core/XRButton.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using UnityEngine;
 3using UnityEngine.Events;
 4using UnityEngine.XR.Interaction.Toolkit;
 5using HenryLab;
 6
 7/// <summary>
 8/// An interactable that can be pressed by a direct interactor
 9/// </summary>
 10public 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")]
 1656    public Transform buttonTransform = null;
 57
 58    [Tooltip("The distance the button can be pressed")]
 1659    public float pressDistance = 0.1f;
 60
 61    // When the button is pressed
 1662    public UnityEvent OnPress = new UnityEvent();
 63
 64    // When the button is released
 1665    public UnityEvent OnRelease = new UnityEvent();
 66
 1667    private float yMin = 0.0f;
 1668    private float yMax = 0.0f;
 69
 1670    private IXRHoverInteractor hoverInteractor = null;
 71
 1672    private float hoverHeight = 0.0f;
 1673    private float startHeight = 0.0f;
 1674    private bool previousPress = false;
 75
 76
 77
 78    protected override void OnEnable()
 879    {
 880        base.OnEnable();
 881        hoverEntered.AddListener(StartPress);
 882        hoverExited.AddListener(EndPress);
 883    }
 84
 85    protected override void OnDisable()
 886    {
 887        base.OnDisable();
 888        hoverEntered.RemoveListener(StartPress);
 889        hoverExited.RemoveListener(EndPress);
 890    }
 91
 92    private void StartPress(HoverEnterEventArgs eventArgs)
 093    {
 094        hoverInteractor = eventArgs.interactorObject;
 095        hoverHeight = GetLocalYPosition(hoverInteractor.transform.position);
 096        startHeight = buttonTransform.localPosition.y;
 097    }
 98
 99    private void EndPress(HoverExitEventArgs eventArgs)
 0100    {
 0101        hoverInteractor = null;
 0102        hoverHeight = 0.0f;
 0103        startHeight = 0.0f;
 0104        ApplyHeight(yMax);
 0105    }
 106
 107    private void Start()
 8108    {
 8109        SetMinMax();
 8110    }
 111
 112    private void SetMinMax()
 8113    {
 8114        yMin = buttonTransform.localPosition.y - pressDistance;
 8115        yMax = buttonTransform.localPosition.y;
 8116    }
 117
 118    public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
 28040119    {
 28040120        if(updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic)
 3772121        {
 3772122            if(isHovered)
 0123            {
 0124                float height = FindButtonHeight();
 0125                ApplyHeight(height);
 0126            }
 3772127        }
 28040128    }
 129
 130    private float FindButtonHeight()
 0131    {
 0132        float newHoverHeight = GetLocalYPosition(hoverInteractor.transform.position);
 0133        float hoverDifference = hoverHeight - newHoverHeight;
 0134        return startHeight - hoverDifference;
 0135    }
 136
 137    private float GetLocalYPosition(Vector3 position)
 0138    {
 0139        Vector3 localPosition = transform.InverseTransformPoint(position);
 0140        return localPosition.y;
 0141    }
 142
 143    private void ApplyHeight(float position)
 0144    {
 0145        SetButtonPosition(position);
 0146        CheckPress();
 0147    }
 148
 149    private void SetButtonPosition(float position)
 0150    {
 0151        Vector3 newPosition = buttonTransform.localPosition;
 0152        newPosition.y = Mathf.Clamp(position, yMin, yMax);
 0153        buttonTransform.localPosition = newPosition;
 0154    }
 155
 156    private void CheckPress()
 0157    {
 0158        bool inPosition = InPosition();
 159
 0160        if(inPosition != previousPress)
 0161        {
 0162            previousPress = inPosition;
 163
 0164            if(inPosition)
 0165            {
 0166                OnPress.Invoke();
 0167            }
 168            else
 0169            {
 0170                OnRelease.Invoke();
 0171            }
 0172        }
 0173    }
 174
 175    private bool InPosition()
 0176    {
 0177        float threshold = yMin + (pressDistance * 0.5f);
 0178        return buttonTransform.localPosition.y < threshold;
 0179    }
 180
 181    public override bool IsSelectableBy(IXRSelectInteractor interactor)
 0182    {
 0183        return false;
 0184    }
 185
 186
 187}