< Summary

Class:DCL.Components.OnPointerEvent
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/OnPointerEvent.cs
Covered lines:35
Uncovered lines:5
Coverable lines:40
Total lines:172
Line coverage:87.5% (35 of 40)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OnPointerEvent()0%110100%
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetActionButton()0%6.65060%
Initialize(...)0%220100%
GetActionButton()0%110100%
SetHoverState(...)0%110100%
SetEventColliders(...)0%110100%
IsVisible()0%5.125083.33%
IsAtHoverDistance(...)0%110100%
ApplyChanges(...)0%220100%
OnDestroy()0%220100%
Report(...)0%2100%
GetEventType()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Interface;
 6using DCL.Models;
 7using UnityEngine;
 8using Ray = UnityEngine.Ray;
 9
 10namespace DCL.Components
 11{
 12    public enum PointerEventType
 13    {
 14        NONE,
 15        CLICK,
 16        DOWN,
 17        UP
 18    }
 19
 20    public interface IPointerEvent : IMonoBehaviour
 21    {
 22        void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit);
 23        PointerEventType GetEventType();
 24        IDCLEntity entity { get; }
 25        WebInterface.ACTION_BUTTON GetActionButton();
 26        void SetHoverState(bool state);
 27        bool IsAtHoverDistance(float distance);
 28        bool IsVisible();
 29    }
 30
 31    public class OnPointerEventHandler : IDisposable
 32    {
 33        public static bool enableInteractionHoverFeedback = true;
 34        public OnPointerEventColliders eventColliders { get; private set; }
 35
 36        private IDCLEntity entity;
 37
 38        public OnPointerEventHandler() { eventColliders = new OnPointerEventColliders(); }
 39
 40        public void SetColliders(IDCLEntity entity)
 41        {
 42            this.entity = entity;
 43            eventColliders.Initialize(entity);
 44        }
 45
 46        public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 47        {
 48            if (!enableInteractionHoverFeedback)
 49                return;
 50
 51            var hoverCanvasController = InteractionHoverCanvasController.i;
 52
 53            hoverCanvasController.enabled = showFeedback;
 54
 55            if (showFeedback)
 56            {
 57                if (hoverState)
 58                    hoverCanvasController.Setup(button, hoverText, entity);
 59
 60                hoverCanvasController.SetHoverState(hoverState);
 61            }
 62        }
 63
 64        public string GetMeshName(Collider collider)
 65        {
 66            if (collider == null || eventColliders == null)
 67                return null;
 68
 69            return eventColliders.GetMeshName(collider);
 70        }
 71
 72        public void Dispose() { eventColliders.Dispose(); }
 73    }
 74
 75    public class OnPointerEvent : UUIDComponent, IPointerEvent
 76    {
 177        public static bool enableInteractionHoverFeedback = true;
 78
 79        [System.Serializable]
 80        public new class Model : UUIDComponent.Model
 81        {
 17282            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 17283            public string hoverText = "Interact";
 17284            public float distance = 10f;
 17285            public bool showFeedback = true;
 86
 4887            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 88
 89            public WebInterface.ACTION_BUTTON GetActionButton()
 90            {
 3891                switch (button)
 92                {
 93                    case "PRIMARY":
 294                        return WebInterface.ACTION_BUTTON.PRIMARY;
 95                    case "SECONDARY":
 096                        return WebInterface.ACTION_BUTTON.SECONDARY;
 97                    case "POINTER":
 098                        return WebInterface.ACTION_BUTTON.POINTER;
 99                    default:
 36100                        return WebInterface.ACTION_BUTTON.ANY;
 101                }
 102            }
 103        }
 104
 105        public OnPointerEventHandler pointerEventHandler;
 106
 107        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 108        {
 32109            base.Initialize(scene, entity);
 110
 32111            if (model == null)
 32112                model = new OnPointerEvent.Model();
 113
 32114            pointerEventHandler = new OnPointerEventHandler();
 32115            SetEventColliders(entity);
 116
 32117            entity.OnShapeUpdated -= SetEventColliders;
 32118            entity.OnShapeUpdated += SetEventColliders;
 32119        }
 120
 38121        public WebInterface.ACTION_BUTTON GetActionButton() { return ((Model) this.model).GetActionButton(); }
 122
 123        public void SetHoverState(bool hoverState)
 124        {
 43125            Model model = (Model) this.model;
 43126            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 43127        }
 128
 184129        void SetEventColliders(IDCLEntity entity) { pointerEventHandler.SetColliders(entity); }
 130
 131        public bool IsVisible()
 132        {
 52133            if (entity == null)
 0134                return false;
 135
 52136            bool isVisible = false;
 137
 52138            if (entity.meshesInfo != null &&
 139                entity.meshesInfo.renderers != null &&
 140                entity.meshesInfo.renderers.Length > 0)
 141            {
 52142                isVisible = entity.meshesInfo.renderers[0].enabled;
 143            }
 144
 52145            return isVisible;
 146        }
 147
 148        public bool IsAtHoverDistance(float distance)
 149        {
 48150            Model model = this.model as Model;
 48151            return distance <= model.distance;
 152        }
 153
 154        public override IEnumerator ApplyChanges(BaseModel newModel)
 155        {
 48156            this.model = newModel ?? new Model();
 48157            return null;
 158        }
 159
 160        void OnDestroy()
 161        {
 32162            if (entity != null)
 32163                entity.OnShapeUpdated -= SetEventColliders;
 164
 32165            pointerEventHandler.Dispose();
 32166        }
 167
 0168        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) { }
 169
 0170        public virtual PointerEventType GetEventType() { return PointerEventType.NONE; }
 171    }
 172}