< Summary

Class:DCL.Components.OnPointerEvent
Assembly:DCL.Components.Events
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/UUIDEventComponentsPlugin/UUIDComponent/OnPointerEvent.cs
Covered lines:42
Uncovered lines:5
Coverable lines:47
Total lines:197
Line coverage:89.3% (42 of 47)
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%5.024060%
Initialize(...)0%220100%
GetActionButton()0%110100%
SetHoverState(...)0%110100%
SetEventColliders(...)0%110100%
IsVisible()0%5.125083.33%
IsAtHoverDistance(...)0%110100%
ApplyChanges(...)0%220100%
ShouldShowHoverFeedback()0%110100%
OnDestroy()0%220100%
Report(...)0%2100%
GetEventType()0%2100%
UpdateOutOfBoundariesState(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/UUIDEventComponentsPlugin/UUIDComponent/OnPointerEvent.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Interface;
 6using DCL.Models;
 7using DCLPlugins.UUIDEventComponentsPlugin.UUIDComponent.Interfaces;
 8using UnityEngine;
 9using Ray = UnityEngine.Ray;
 10
 11namespace DCL.Components
 12{
 13    public class OnPointerEventHandler : IDisposable
 14    {
 15        public static bool enableInteractionHoverFeedback = true;
 16        public OnPointerEventColliders eventColliders { get; private set; }
 17
 18        private IDCLEntity entity;
 19
 20        public OnPointerEventHandler()
 21        {
 22            eventColliders = new OnPointerEventColliders();
 23        }
 24
 25        public void SetColliders(IDCLEntity entity)
 26        {
 27            this.entity = entity;
 28            eventColliders.Initialize(entity);
 29        }
 30
 31        public void UpdateCollidersEnabledBasedOnRenderers(IDCLEntity entity)
 32        {
 33            this.entity = entity;
 34            eventColliders.UpdateCollidersEnabledBasedOnRenderers(entity);
 35        }
 36
 37        public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 38        {
 39            if (!enableInteractionHoverFeedback)
 40                return;
 41
 42            var cursorData = DataStore.i.Get<DataStore_Cursor>();
 43            cursorData.hoverFeedbackEnabled.Set(showFeedback);
 44
 45            if (showFeedback)
 46            {
 47                if (hoverState)
 48                {
 49                    cursorData.hoverFeedbackButton.Set(button);
 50                    cursorData.hoverFeedbackText.Set(hoverText);
 51                }
 52
 53                cursorData.hoverFeedbackHoverState.Set(hoverState);
 54            }
 55        }
 56
 57        public string GetMeshName(Collider collider)
 58        {
 59            if (collider == null || eventColliders == null)
 60                return null;
 61
 62            return eventColliders.GetMeshName(collider);
 63        }
 64
 65        public void Dispose()
 66        {
 67            eventColliders.Dispose();
 68        }
 69    }
 70
 71    public class OnPointerEvent : UUIDComponent, IPointerInputEvent, IOutOfSceneBoundariesHandler
 72    {
 173        public static bool enableInteractionHoverFeedback = true;
 74
 75        [System.Serializable]
 76        public new class Model : UUIDComponent.Model
 77        {
 21778            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 21779            public string hoverText = "Interact";
 21780            public float distance = 10f;
 21781            public bool showFeedback = true;
 82
 83            public override BaseModel GetDataFromJSON(string json)
 84            {
 5485                return Utils.SafeFromJson<Model>(json);
 86            }
 87
 88            public WebInterface.ACTION_BUTTON GetActionButton()
 89            {
 1621490                switch (button)
 91                {
 92                    case "PRIMARY":
 193                        return WebInterface.ACTION_BUTTON.PRIMARY;
 94                    case "SECONDARY":
 095                        return WebInterface.ACTION_BUTTON.SECONDARY;
 96                    case "POINTER":
 097                        return WebInterface.ACTION_BUTTON.POINTER;
 98                    default:
 1621399                        return WebInterface.ACTION_BUTTON.ANY;
 100                }
 101            }
 102        }
 103
 104        public OnPointerEventHandler pointerEventHandler;
 105
 106        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 107        {
 38108            base.Initialize(scene, entity);
 109
 38110            if (model == null)
 38111                model = new OnPointerEvent.Model();
 112
 38113            pointerEventHandler = new OnPointerEventHandler();
 38114            SetEventColliders(entity);
 115
 38116            entity.OnShapeUpdated -= SetEventColliders;
 38117            entity.OnShapeUpdated += SetEventColliders;
 118
 38119            DataStore.i.sceneBoundariesChecker.Add(entity,this);
 38120        }
 121
 122        public WebInterface.ACTION_BUTTON GetActionButton()
 123        {
 16214124            return ((Model) this.model).GetActionButton();
 125        }
 126
 127        public void SetHoverState(bool hoverState)
 128        {
 16218129            Model model = (Model) this.model;
 16218130            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 16218131        }
 132
 133        void SetEventColliders(IDCLEntity entity)
 134        {
 77135            pointerEventHandler.SetColliders(entity);
 77136        }
 137
 138        public bool IsVisible()
 139        {
 16227140            if (entity == null)
 0141                return false;
 142
 16227143            bool isVisible = false;
 144
 16227145            if (entity.meshesInfo != null &&
 146                entity.meshesInfo.renderers != null &&
 147                entity.meshesInfo.renderers.Length > 0)
 148            {
 16227149                isVisible = entity.meshesInfo.renderers[0].enabled;
 150            }
 151
 16227152            return isVisible;
 153        }
 154
 155        public bool IsAtHoverDistance(float distance)
 156        {
 16223157            Model model = this.model as Model;
 16223158            return distance <= model.distance;
 159        }
 160
 161        public override IEnumerator ApplyChanges(BaseModel newModel)
 162        {
 54163            this.model = newModel ?? new Model();
 54164            return null;
 165        }
 166
 167        public bool ShouldShowHoverFeedback()
 168        {
 16212169            Model model = this.model as Model;
 16212170            return model.showFeedback;
 171        }
 172
 173        void OnDestroy()
 174        {
 38175            if (entity != null)
 38176                entity.OnShapeUpdated -= SetEventColliders;
 177
 38178            DataStore.i.sceneBoundariesChecker.Remove(entity,this);
 179
 38180            pointerEventHandler.Dispose();
 38181        }
 182
 183        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit)
 184        {
 0185        }
 186
 187        public virtual PointerInputEventType GetEventType()
 188        {
 0189            return PointerInputEventType.NONE;
 190        }
 191
 192        public void UpdateOutOfBoundariesState(bool enable)
 193        {
 1194            pointerEventHandler.UpdateCollidersEnabledBasedOnRenderers(entity);
 1195        }
 196    }
 197}