< Summary

Class:DCL.Components.AvatarOnPointerDown
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/AvatarOnPointerDown.cs
Covered lines:11
Uncovered lines:60
Coverable lines:71
Total lines:170
Line coverage:15.4% (11 of 71)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarOnPointerDown()0%110100%
GetActionButton()0%2100%
SetHoverState(...)0%72800%
OnDisable()0%4.943040%
Awake()0%110100%
OnDestroy()0%220100%
Initialize(...)0%12300%
IsAtHoverDistance(...)0%2100%
IsVisible()0%2100%
ShouldReportPassportInputEvent(...)0%12300%
Report(...)0%56700%
GetEventType()0%2100%
ReEnableOnInfoCardClosed(...)0%2.52050%
SetColliderEnabled(...)0%2100%
SetPassportEnabled(...)0%2100%
SetOnClickReportEnabled(...)0%2100%
GetTransform()0%2100%
OnPoolRelease()0%2100%
OnPoolGet()0%2100%
ShouldReportOnClickEvent(...)0%30500%
ShouldShowHoverFeedback()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/AvatarOnPointerDown.cs

#LineLine coverage
 1using System.Linq;
 2using DCL.Controllers;
 3using DCL.Interface;
 4using UnityEngine;
 5using DCL.Helpers;
 6using DCL.Models;
 7using Ray = UnityEngine.Ray;
 8
 9namespace DCL.Components
 10{
 11    public class AvatarOnPointerDown : MonoBehaviour, IPointerInputEvent, IPoolLifecycleHandler, IAvatarOnPointerDownCol
 12    {
 13        public new Collider collider;
 14        private OnPointerEvent.Model model;
 15        private OnPointerEventHandler eventHandler;
 016        public IDCLEntity entity { get; private set; }
 17        public event System.Action OnPointerDownReport;
 18        public event System.Action OnPointerEnterReport;
 19        public event System.Action OnPointerExitReport;
 20        private bool isHovering = false;
 21
 68722        private bool passportEnabled = true;
 68723        private bool onClickReportEnabled = true;
 24        private Player avatarPlayer;
 25
 026        public WebInterface.ACTION_BUTTON GetActionButton() { return model.GetActionButton(); }
 27
 28        public void SetHoverState(bool state)
 29        {
 030            bool isHoveringDirty = state != isHovering;
 031            isHovering = state;
 032            eventHandler?.SetFeedbackState(model.showFeedback, state && passportEnabled, model.button, model.hoverText);
 033            if (!isHoveringDirty)
 034                return;
 035            if (isHovering)
 036                OnPointerEnterReport?.Invoke();
 37            else
 038                OnPointerExitReport?.Invoke();
 039        }
 40
 41        private void OnDisable()
 42        {
 68843            if (!isHovering)
 68844                return;
 045            isHovering = false;
 046            OnPointerExitReport?.Invoke();
 047        }
 48
 137249        void Awake() { CommonScriptableObjects.playerInfoCardVisibleState.OnChange += ReEnableOnInfoCardClosed; }
 50
 51        void OnDestroy()
 52        {
 68653            CommonScriptableObjects.playerInfoCardVisibleState.OnChange -= ReEnableOnInfoCardClosed;
 68654            eventHandler?.Dispose();
 68655            CollidersManager.i.RemoveEntityCollider(entity, collider);
 68656        }
 57
 58        public void Initialize(OnPointerEvent.Model model, IDCLEntity entity, Player player)
 59        {
 060            this.model = model;
 061            this.entity = entity;
 062            this.avatarPlayer = player;
 63
 064            if (eventHandler == null)
 065                eventHandler = new OnPointerEventHandler();
 66
 067            eventHandler?.SetColliders(entity);
 068            CollidersManager.i.AddOrUpdateEntityCollider(entity, collider);
 069        }
 70
 071        public bool IsAtHoverDistance(float distance) { return distance <= model.distance; }
 72
 073        public bool IsVisible() { return true; }
 74
 75        public bool ShouldReportPassportInputEvent(WebInterface.ACTION_BUTTON buttonId, HitInfo hit)
 76        {
 077            return IsAtHoverDistance(hit.distance) &&
 78                   (model.button == "ANY" || buttonId.ToString() == model.button);
 79        }
 80
 81        public void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit)
 82        {
 083            if (!enabled)
 084                return;
 85
 086            if (passportEnabled && ShouldReportPassportInputEvent(buttonId, hit))
 87            {
 088                eventHandler.SetFeedbackState(model.showFeedback, false, model.button, model.hoverText);
 089                passportEnabled = false;
 90
 091                OnPointerDownReport?.Invoke();
 92            }
 93
 094            if (onClickReportEnabled && ShouldReportOnClickEvent(buttonId, out IParcelScene playerScene))
 95            {
 096                WebInterface.ReportAvatarClick(
 97                    playerScene.sceneData.id,
 98                    avatarPlayer.id,
 99                    WorldStateUtils.ConvertUnityToScenePosition(ray.origin, playerScene),
 100                    ray.direction,
 101                    hit.distance);
 102            }
 0103        }
 104
 0105        public PointerInputEventType GetEventType() { return PointerInputEventType.DOWN; }
 106
 107        void ReEnableOnInfoCardClosed(bool newState, bool prevState)
 108        {
 2109            if (passportEnabled || newState)
 2110                return;
 111
 0112            passportEnabled = true;
 0113        }
 114        public void SetColliderEnabled(bool newEnabledState)
 115        {
 0116            collider.enabled = newEnabledState;
 0117        }
 118
 119        public void SetPassportEnabled(bool newEnabledState)
 120        {
 0121            passportEnabled = newEnabledState;
 0122            isHovering = false;
 0123        }
 124
 125        public void SetOnClickReportEnabled(bool newEnabledState)
 126        {
 0127            onClickReportEnabled = newEnabledState;
 0128        }
 129
 0130        public Transform GetTransform() { return transform; }
 131
 132        public void OnPoolRelease()
 133        {
 0134            eventHandler.Dispose();
 0135            avatarPlayer = null;
 0136        }
 137
 0138        public void OnPoolGet() { }
 139
 140        private bool ShouldReportOnClickEvent(WebInterface.ACTION_BUTTON buttonId, out IParcelScene playerScene)
 141        {
 0142            playerScene = null;
 143
 0144            if (buttonId != WebInterface.ACTION_BUTTON.POINTER)
 145            {
 0146                return false;
 147            }
 148
 0149            if (avatarPlayer == null)
 150            {
 0151                return false;
 152            }
 153
 0154            string playerSceneId = CommonScriptableObjects.sceneID.Get();
 155
 0156            if (string.IsNullOrEmpty(playerSceneId))
 157            {
 0158                return false;
 159            }
 160
 0161            playerScene = WorldStateUtils.GetCurrentScene();
 0162            return playerScene?.IsInsideSceneBoundaries(PositionUtils.UnityToWorldPosition(avatarPlayer.worldPosition)) 
 163        }
 164
 165        public bool ShouldShowHoverFeedback()
 166        {
 0167            return enabled && model.showFeedback;
 168        }
 169    }
 170}