< Summary

Class:DCL.Components.OnPointerEventHandler
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/OnPointerEvent.cs
Covered lines:17
Uncovered lines:2
Coverable lines:19
Total lines:172
Line coverage:89.4% (17 of 19)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OnPointerEventHandler()0%110100%
OnPointerEventHandler()0%110100%
SetColliders(...)0%110100%
SetFeedbackState(...)0%4.024088.89%
GetMeshName(...)0%3.333066.67%
Dispose()0%110100%

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    {
 133        public static bool enableInteractionHoverFeedback = true;
 3234        public OnPointerEventColliders eventColliders { get; private set; }
 35
 36        private IDCLEntity entity;
 37
 9638        public OnPointerEventHandler() { eventColliders = new OnPointerEventColliders(); }
 39
 40        public void SetColliders(IDCLEntity entity)
 41        {
 9242            this.entity = entity;
 9243            eventColliders.Initialize(entity);
 9244        }
 45
 46        public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 47        {
 4348            if (!enableInteractionHoverFeedback)
 049                return;
 50
 4351            var hoverCanvasController = InteractionHoverCanvasController.i;
 52
 4353            hoverCanvasController.enabled = showFeedback;
 54
 4355            if (showFeedback)
 56            {
 4357                if (hoverState)
 3558                    hoverCanvasController.Setup(button, hoverText, entity);
 59
 4360                hoverCanvasController.SetHoverState(hoverState);
 61            }
 4362        }
 63
 64        public string GetMeshName(Collider collider)
 65        {
 666            if (collider == null || eventColliders == null)
 067                return null;
 68
 669            return eventColliders.GetMeshName(collider);
 70        }
 71
 6472        public void Dispose() { eventColliders.Dispose(); }
 73    }
 74
 75    public class OnPointerEvent : UUIDComponent, IPointerEvent
 76    {
 77        public static bool enableInteractionHoverFeedback = true;
 78
 79        [System.Serializable]
 80        public new class Model : UUIDComponent.Model
 81        {
 82            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 83            public string hoverText = "Interact";
 84            public float distance = 10f;
 85            public bool showFeedback = true;
 86
 87            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 88
 89            public WebInterface.ACTION_BUTTON GetActionButton()
 90            {
 91                switch (button)
 92                {
 93                    case "PRIMARY":
 94                        return WebInterface.ACTION_BUTTON.PRIMARY;
 95                    case "SECONDARY":
 96                        return WebInterface.ACTION_BUTTON.SECONDARY;
 97                    case "POINTER":
 98                        return WebInterface.ACTION_BUTTON.POINTER;
 99                    default:
 100                        return WebInterface.ACTION_BUTTON.ANY;
 101                }
 102            }
 103        }
 104
 105        public OnPointerEventHandler pointerEventHandler;
 106
 107        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 108        {
 109            base.Initialize(scene, entity);
 110
 111            if (model == null)
 112                model = new OnPointerEvent.Model();
 113
 114            pointerEventHandler = new OnPointerEventHandler();
 115            SetEventColliders(entity);
 116
 117            entity.OnShapeUpdated -= SetEventColliders;
 118            entity.OnShapeUpdated += SetEventColliders;
 119        }
 120
 121        public WebInterface.ACTION_BUTTON GetActionButton() { return ((Model) this.model).GetActionButton(); }
 122
 123        public void SetHoverState(bool hoverState)
 124        {
 125            Model model = (Model) this.model;
 126            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 127        }
 128
 129        void SetEventColliders(IDCLEntity entity) { pointerEventHandler.SetColliders(entity); }
 130
 131        public bool IsVisible()
 132        {
 133            if (entity == null)
 134                return false;
 135
 136            bool isVisible = false;
 137
 138            if (entity.meshesInfo != null &&
 139                entity.meshesInfo.renderers != null &&
 140                entity.meshesInfo.renderers.Length > 0)
 141            {
 142                isVisible = entity.meshesInfo.renderers[0].enabled;
 143            }
 144
 145            return isVisible;
 146        }
 147
 148        public bool IsAtHoverDistance(float distance)
 149        {
 150            Model model = this.model as Model;
 151            return distance <= model.distance;
 152        }
 153
 154        public override IEnumerator ApplyChanges(BaseModel newModel)
 155        {
 156            this.model = newModel ?? new Model();
 157            return null;
 158        }
 159
 160        void OnDestroy()
 161        {
 162            if (entity != null)
 163                entity.OnShapeUpdated -= SetEventColliders;
 164
 165            pointerEventHandler.Dispose();
 166        }
 167
 168        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) { }
 169
 170        public virtual PointerEventType GetEventType() { return PointerEventType.NONE; }
 171    }
 172}