< 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:183
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%5.035088.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 PointerInputEventType
 13    {
 14        NONE,
 15        CLICK,
 16        DOWN,
 17        UP
 18    }
 19
 20    public interface IPointerEvent : IMonoBehaviour
 21    {
 22        IDCLEntity entity { get; }
 23        void SetHoverState(bool state);
 24        bool IsAtHoverDistance(float distance);
 25        bool IsVisible();
 26    }
 27
 28    public interface IPointerInputEvent : IPointerEvent
 29    {
 30        void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit);
 31        PointerInputEventType GetEventType();
 32        WebInterface.ACTION_BUTTON GetActionButton();
 33        bool ShouldShowHoverFeedback();
 34    }
 35
 36    public class OnPointerEventHandler : IDisposable
 37    {
 138        public static bool enableInteractionHoverFeedback = true;
 3439        public OnPointerEventColliders eventColliders { get; private set; }
 40
 41        private IDCLEntity entity;
 42
 10243        public OnPointerEventHandler() { eventColliders = new OnPointerEventColliders(); }
 44
 45        public void SetColliders(IDCLEntity entity)
 46        {
 7247            this.entity = entity;
 7248            eventColliders.Initialize(entity);
 7249        }
 50
 51        public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 52        {
 29453            var hoverCanvasController = InteractionHoverCanvasController.i;
 54
 29455            if (!enableInteractionHoverFeedback || hoverCanvasController == null)
 056                return;
 57
 29458            hoverCanvasController.enabled = showFeedback;
 59
 29460            if (showFeedback)
 61            {
 29462                if (hoverState)
 27863                    hoverCanvasController.Setup(button, hoverText, entity);
 64
 29465                hoverCanvasController.SetHoverState(hoverState);
 66            }
 29467        }
 68
 69        public string GetMeshName(Collider collider)
 70        {
 671            if (collider == null || eventColliders == null)
 072                return null;
 73
 674            return eventColliders.GetMeshName(collider);
 75        }
 76
 6877        public void Dispose() { eventColliders.Dispose(); }
 78    }
 79
 80    public class OnPointerEvent : UUIDComponent, IPointerInputEvent
 81    {
 82        public static bool enableInteractionHoverFeedback = true;
 83
 84        [System.Serializable]
 85        public new class Model : UUIDComponent.Model
 86        {
 87            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 88            public string hoverText = "Interact";
 89            public float distance = 10f;
 90            public bool showFeedback = true;
 91
 92            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 93
 94            public WebInterface.ACTION_BUTTON GetActionButton()
 95            {
 96                switch (button)
 97                {
 98                    case "PRIMARY":
 99                        return WebInterface.ACTION_BUTTON.PRIMARY;
 100                    case "SECONDARY":
 101                        return WebInterface.ACTION_BUTTON.SECONDARY;
 102                    case "POINTER":
 103                        return WebInterface.ACTION_BUTTON.POINTER;
 104                    default:
 105                        return WebInterface.ACTION_BUTTON.ANY;
 106                }
 107            }
 108        }
 109
 110        public OnPointerEventHandler pointerEventHandler;
 111
 112        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 113        {
 114            base.Initialize(scene, entity);
 115
 116            if (model == null)
 117                model = new OnPointerEvent.Model();
 118
 119            pointerEventHandler = new OnPointerEventHandler();
 120            SetEventColliders(entity);
 121
 122            entity.OnShapeUpdated -= SetEventColliders;
 123            entity.OnShapeUpdated += SetEventColliders;
 124        }
 125
 126        public WebInterface.ACTION_BUTTON GetActionButton() { return ((Model) this.model).GetActionButton(); }
 127
 128        public void SetHoverState(bool hoverState)
 129        {
 130            Model model = (Model) this.model;
 131            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 132        }
 133
 134        void SetEventColliders(IDCLEntity entity) { pointerEventHandler.SetColliders(entity); }
 135
 136        public bool IsVisible()
 137        {
 138            if (entity == null)
 139                return false;
 140
 141            bool isVisible = false;
 142
 143            if (entity.meshesInfo != null &&
 144                entity.meshesInfo.renderers != null &&
 145                entity.meshesInfo.renderers.Length > 0)
 146            {
 147                isVisible = entity.meshesInfo.renderers[0].enabled;
 148            }
 149
 150            return isVisible;
 151        }
 152
 153        public bool IsAtHoverDistance(float distance)
 154        {
 155            Model model = this.model as Model;
 156            return distance <= model.distance;
 157        }
 158
 159        public override IEnumerator ApplyChanges(BaseModel newModel)
 160        {
 161            this.model = newModel ?? new Model();
 162            return null;
 163        }
 164
 165        public bool ShouldShowHoverFeedback()
 166        {
 167            Model model = this.model as Model;
 168            return model.showFeedback;
 169        }
 170
 171        void OnDestroy()
 172        {
 173            if (entity != null)
 174                entity.OnShapeUpdated -= SetEventColliders;
 175
 176            pointerEventHandler.Dispose();
 177        }
 178
 179        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) { }
 180
 181        public virtual PointerInputEventType GetEventType() { return PointerInputEventType.NONE; }
 182    }
 183}