< 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:15
Coverable lines:57
Total lines:207
Line coverage:73.6% (42 of 57)
Covered branches:0
Total branches:0
Covered methods:14
Total methods:17
Method coverage:82.3% (14 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
OnPointerEvent()0%110100%
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetDataFromPb(...)0%72800%
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;
 10using Decentraland.Sdk.Ecs6;
 11
 12namespace DCL.Components
 13{
 14    public class OnPointerEventHandler : IDisposable
 15    {
 16        public static bool enableInteractionHoverFeedback = true;
 17        public OnPointerEventColliders eventColliders { get; private set; }
 18
 19        private IDCLEntity entity;
 20
 21        public OnPointerEventHandler()
 22        {
 23            eventColliders = new OnPointerEventColliders();
 24        }
 25
 26        public void SetColliders(IDCLEntity entity)
 27        {
 28            this.entity = entity;
 29            eventColliders.Initialize(entity);
 30        }
 31
 32        public void UpdateCollidersEnabledBasedOnRenderers(IDCLEntity entity)
 33        {
 34            this.entity = entity;
 35            eventColliders.UpdateCollidersEnabledBasedOnRenderers(entity);
 36        }
 37
 38        public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 39        {
 40            if (!enableInteractionHoverFeedback)
 41                return;
 42
 43            var cursorData = DataStore.i.Get<DataStore_Cursor>();
 44            cursorData.hoverFeedbackEnabled.Set(showFeedback);
 45
 46            if (showFeedback)
 47            {
 48                if (hoverState)
 49                {
 50                    cursorData.hoverFeedbackButton.Set(button);
 51                    cursorData.hoverFeedbackText.Set(hoverText);
 52                }
 53
 54                cursorData.hoverFeedbackHoverState.Set(hoverState);
 55            }
 56        }
 57
 58        public string GetMeshName(Collider collider)
 59        {
 60            if (collider == null || eventColliders == null)
 61                return null;
 62
 63            return eventColliders.GetMeshName(collider);
 64        }
 65
 66        public void Dispose()
 67        {
 68            eventColliders.Dispose();
 69        }
 70    }
 71
 72    public class OnPointerEvent : UUIDComponent, IPointerInputEvent, IOutOfSceneBoundariesHandler
 73    {
 174        public static bool enableInteractionHoverFeedback = true;
 75
 76        [Serializable]
 77        public new class Model : UUIDComponent.Model
 78        {
 93479            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 93480            public string hoverText = "Interact";
 93481            public float distance = 10f;
 93482            public bool showFeedback = true;
 83
 84            public override BaseModel GetDataFromJSON(string json) =>
 12485                Utils.SafeFromJson<Model>(json);
 86
 87            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 88            {
 089                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UuidCallback)
 090                    return Utils.SafeUnimplemented<OnPointerEvent, Model>(expected: ComponentBodyPayload.PayloadOneofCas
 91
 092                var pb = new Model();
 93
 094                if (pbModel.UuidCallback.HasUuid) pb.uuid = pbModel.UuidCallback.Uuid;
 095                if (pbModel.UuidCallback.HasType) pb.type = pbModel.UuidCallback.Type;
 096                if (pbModel.UuidCallback.HasButton) pb.button = pbModel.UuidCallback.Button;
 097                if (pbModel.UuidCallback.HasHoverText) pb.hoverText = pbModel.UuidCallback.HoverText;
 098                if (pbModel.UuidCallback.HasDistance) pb.distance = pbModel.UuidCallback.Distance;
 099                if (pbModel.UuidCallback.HasShowFeedback) pb.showFeedback = pbModel.UuidCallback.ShowFeedback;
 100
 0101                return pb;
 102            }
 103
 104            public WebInterface.ACTION_BUTTON GetActionButton() =>
 6499105                button switch
 106                {
 1107                    "PRIMARY" => WebInterface.ACTION_BUTTON.PRIMARY,
 0108                    "SECONDARY" => WebInterface.ACTION_BUTTON.SECONDARY,
 0109                    "POINTER" => WebInterface.ACTION_BUTTON.POINTER,
 6498110                    _ => WebInterface.ACTION_BUTTON.ANY,
 111                };
 112        }
 113
 114        public OnPointerEventHandler pointerEventHandler;
 115
 116        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 117        {
 38118            base.Initialize(scene, entity);
 119
 38120            if (model == null)
 38121                model = new OnPointerEvent.Model();
 122
 38123            pointerEventHandler = new OnPointerEventHandler();
 38124            SetEventColliders(entity);
 125
 38126            entity.OnShapeUpdated -= SetEventColliders;
 38127            entity.OnShapeUpdated += SetEventColliders;
 128
 38129            DataStore.i.sceneBoundariesChecker.Add(entity,this);
 38130        }
 131
 132        public WebInterface.ACTION_BUTTON GetActionButton()
 133        {
 6499134            return ((Model) this.model).GetActionButton();
 135        }
 136
 137        public void SetHoverState(bool hoverState)
 138        {
 6503139            Model model = (Model) this.model;
 6503140            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 6503141        }
 142
 143        void SetEventColliders(IDCLEntity entity)
 144        {
 77145            pointerEventHandler.SetColliders(entity);
 77146        }
 147
 148        public bool IsVisible()
 149        {
 6512150            if (entity == null)
 0151                return false;
 152
 6512153            bool isVisible = false;
 154
 6512155            if (entity.meshesInfo != null &&
 156                entity.meshesInfo.renderers != null &&
 157                entity.meshesInfo.renderers.Length > 0)
 158            {
 6512159                isVisible = entity.meshesInfo.renderers[0].enabled;
 160            }
 161
 6512162            return isVisible;
 163        }
 164
 165        public bool IsAtHoverDistance(float distance)
 166        {
 6508167            Model model = this.model as Model;
 6508168            return distance <= model.distance;
 169        }
 170
 171        public override IEnumerator ApplyChanges(BaseModel newModel)
 172        {
 54173            this.model = newModel ?? new Model();
 54174            return null;
 175        }
 176
 177        public bool ShouldShowHoverFeedback()
 178        {
 6497179            Model model = this.model as Model;
 6497180            return model.showFeedback;
 181        }
 182
 183        void OnDestroy()
 184        {
 38185            if (entity != null)
 38186                entity.OnShapeUpdated -= SetEventColliders;
 187
 38188            DataStore.i.sceneBoundariesChecker.Remove(entity,this);
 189
 38190            pointerEventHandler.Dispose();
 38191        }
 192
 193        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit)
 194        {
 0195        }
 196
 197        public virtual PointerInputEventType GetEventType()
 198        {
 0199            return PointerInputEventType.NONE;
 200        }
 201
 202        public void UpdateOutOfBoundariesState(bool enable)
 203        {
 1204            pointerEventHandler.UpdateCollidersEnabledBasedOnRenderers(entity);
 1205        }
 206    }
 207}