< Summary

Class:XRLever
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Basics/Assets/_Course Library/Scripts/Core/XRLever.cs
Covered lines:39
Uncovered lines:30
Coverable lines:69
Total lines:162
Line coverage:56.5% (39 of 69)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:14
Method coverage:64.2% (9 of 14)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
XRLever()0%000100%
Start()0%000100%
OnEnable()0%000100%
OnDisable()0%000100%
StartGrab(...)0%0000%
EndGrab(...)0%0000%
ProcessInteractable(...)0%00063.64%
GetLookDirection()0%0000%
ApplyValue(...)0%0000%
InOnPosition(...)0%0000%
FindSnapDirection(...)0%000100%
SetValue(...)0%00070%

File(s)

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

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using UnityEngine;
 3using UnityEngine.Events;
 4using UnityEngine.XR.Interaction.Toolkit;
 5using HenryLab;
 6
 7
 8/// <summary>
 9/// An interactable lever that snaps into an on or off position by a direct interactor
 10/// </summary>
 11public 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")]
 457    public Transform handle = null;
 58
 59    [Tooltip("The initial value of the lever")]
 460    public bool defaultValue = false;
 61
 62    // When the lever is activated
 463    public UnityEvent OnLeverActivate = new UnityEvent();
 64
 65    // When the lever is deactivated
 466    public UnityEvent OnLeverDeactivate = new UnityEvent();
 67
 868    public bool Value { get; private set; } = false;
 69
 470    private IXRSelectInteractor selectInteractor = null;
 71
 72    private void Start()
 273    {
 274        FindSnapDirection(defaultValue);
 275        SetValue(defaultValue);
 276    }
 77
 78    protected override void OnEnable()
 279    {
 280        base.OnEnable();
 281        selectEntered.AddListener(StartGrab);
 282        selectExited.AddListener(EndGrab);
 283        selectExited.AddListener(ApplyValue);
 284    }
 85
 86    protected override void OnDisable()
 287    {
 288        base.OnDisable();
 289        selectEntered.RemoveListener(StartGrab);
 290        selectExited.RemoveListener(EndGrab);
 291        selectExited.RemoveListener(ApplyValue);
 292    }
 93
 94    private void StartGrab(SelectEnterEventArgs eventArgs)
 095    {
 096        selectInteractor = eventArgs.interactorObject;
 097    }
 98
 99    private void EndGrab(SelectExitEventArgs eventArgs)
 0100    {
 0101        selectInteractor = null;
 0102    }
 103
 104    public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
 7010105    {
 7010106        base.ProcessInteractable(updatePhase);
 107
 7010108        if (updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic)
 943109        {
 943110            if (isSelected)
 0111            {
 0112                Vector3 lookDirection = GetLookDirection();
 0113                handle.forward = transform.TransformDirection(lookDirection);
 0114            }
 943115        }
 7010116    }
 117
 118    private Vector3 GetLookDirection()
 0119    {
 0120        Vector3 direction = selectInteractor.transform.position - handle.position;
 0121        direction = transform.InverseTransformDirection(direction);
 122
 0123        direction.x = 0;
 0124        direction.y = Mathf.Clamp(direction.y, 0, 1);
 125
 0126        return direction;
 0127    }
 128
 129    private void ApplyValue(SelectExitEventArgs eventArgs)
 0130    {
 0131        IXRSelectInteractor interactor = eventArgs.interactorObject;
 0132        bool isOn = InOnPosition(interactor.transform.position);
 133
 0134        FindSnapDirection(isOn);
 0135        SetValue(isOn);
 0136    }
 137
 138    private bool InOnPosition(Vector3 interactorPosition)
 0139    {
 0140        interactorPosition = transform.InverseTransformPoint(interactorPosition);
 0141        return (interactorPosition.z > 0);
 0142    }
 143
 144    private void FindSnapDirection(bool isOn)
 2145    {
 2146        handle.forward = isOn ? transform.forward : -transform.forward;
 2147    }
 148
 149    private void SetValue(bool isOn)
 2150    {
 2151        Value = isOn;
 152
 2153        if (Value)
 2154        {
 2155            OnLeverActivate.Invoke();
 2156        }
 157        else
 0158        {
 0159            OnLeverDeactivate.Invoke();
 0160        }
 2161    }
 162}