< Summary

Class:XRLever
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Basics/Assets/_Course Library/Scripts/Core/XRLever.cs
Covered lines:65
Uncovered lines:4
Coverable lines:69
Total lines:162
Line coverage:94.2% (65 of 69)
Covered branches:0
Total branches:0
Covered methods:14
Total methods:14
Method coverage:100% (14 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%000100%
EndGrab(...)0%000100%
ProcessInteractable(...)0%00063.64%
GetLookDirection()0%000100%
ApplyValue(...)0%000100%
InOnPosition(...)0%000100%
FindSnapDirection(...)0%000100%
SetValue(...)0%000100%

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, ITriggerableEntity
 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")]
 257    public Transform handle = null;
 58
 59    [Tooltip("The initial value of the lever")]
 260    public bool defaultValue = false;
 61
 62    // When the lever is activated
 263    public UnityEvent OnLeverActivate = new UnityEvent();
 64
 65    // When the lever is deactivated
 266    public UnityEvent OnLeverDeactivate = new UnityEvent();
 67
 668    public bool Value { get; private set; } = false;
 69
 270    private IXRSelectInteractor selectInteractor = null;
 71
 72    private void Start()
 173    {
 174        FindSnapDirection(defaultValue);
 175        SetValue(defaultValue);
 176    }
 77
 78    protected override void OnEnable()
 179    {
 180        base.OnEnable();
 181        selectEntered.AddListener(StartGrab);
 182        selectExited.AddListener(EndGrab);
 183        selectExited.AddListener(ApplyValue);
 184    }
 85
 86    protected override void OnDisable()
 187    {
 188        base.OnDisable();
 189        selectEntered.RemoveListener(StartGrab);
 190        selectExited.RemoveListener(EndGrab);
 191        selectExited.RemoveListener(ApplyValue);
 192    }
 93
 94    private void StartGrab(SelectEnterEventArgs eventArgs)
 295    {
 296        selectInteractor = eventArgs.interactorObject;
 297    }
 98
 99    private void EndGrab(SelectExitEventArgs eventArgs)
 2100    {
 2101        selectInteractor = null;
 2102    }
 103
 104    public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
 5286105    {
 5286106        base.ProcessInteractable(updatePhase);
 107
 5286108        if (updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic)
 718109        {
 718110            if (isSelected)
 0111            {
 0112                Vector3 lookDirection = GetLookDirection();
 0113                handle.forward = transform.TransformDirection(lookDirection);
 0114            }
 718115        }
 5286116    }
 117
 118    private Vector3 GetLookDirection()
 1119    {
 1120        Vector3 direction = selectInteractor.transform.position - handle.position;
 1121        direction = transform.InverseTransformDirection(direction);
 122
 1123        direction.x = 0;
 1124        direction.y = Mathf.Clamp(direction.y, 0, 1);
 125
 1126        return direction;
 1127    }
 128
 129    private void ApplyValue(SelectExitEventArgs eventArgs)
 1130    {
 1131        IXRSelectInteractor interactor = eventArgs.interactorObject;
 1132        bool isOn = InOnPosition(interactor.transform.position);
 133
 1134        FindSnapDirection(isOn);
 1135        SetValue(isOn);
 1136    }
 137
 138    private bool InOnPosition(Vector3 interactorPosition)
 1139    {
 1140        interactorPosition = transform.InverseTransformPoint(interactorPosition);
 1141        return (interactorPosition.z > 0);
 1142    }
 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)
 1154        {
 1155            OnLeverActivate.Invoke();
 1156        }
 157        else
 1158        {
 1159            OnLeverDeactivate.Invoke();
 1160        }
 2161    }
 162}