< 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:38
Uncovered lines:5
Coverable lines:43
Total lines:182
Line coverage:88.3% (38 of 43)
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%
ShouldShowHoverFeedback()0%110100%
OnDestroy()0%220100%
Report(...)0%2100%
GetEventType()0%2100%

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 SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 32        {
 33            if (!enableInteractionHoverFeedback)
 34                return;
 35
 36            var cursorData = DataStore.i.Get<DataStore_Cursor>();
 37            cursorData.hoverFeedbackEnabled.Set(showFeedback);
 38
 39            if (showFeedback)
 40            {
 41                if (hoverState)
 42                {
 43                    cursorData.hoverFeedbackButton.Set(button);
 44                    cursorData.hoverFeedbackText.Set(hoverText);
 45                }
 46
 47                cursorData.hoverFeedbackHoverState.Set(hoverState);
 48            }
 49        }
 50
 51        public string GetMeshName(Collider collider)
 52        {
 53            if (collider == null || eventColliders == null)
 54                return null;
 55
 56            return eventColliders.GetMeshName(collider);
 57        }
 58
 59        public void Dispose()
 60        {
 61            eventColliders.Dispose();
 62        }
 63    }
 64
 65    public class OnPointerEvent : UUIDComponent, IPointerInputEvent
 66    {
 167        public static bool enableInteractionHoverFeedback = true;
 68
 69        [System.Serializable]
 70        public new class Model : UUIDComponent.Model
 71        {
 20572            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 20573            public string hoverText = "Interact";
 20574            public float distance = 10f;
 20575            public bool showFeedback = true;
 76
 77            public override BaseModel GetDataFromJSON(string json)
 78            {
 5379                return Utils.SafeFromJson<Model>(json);
 80            }
 81
 82            public WebInterface.ACTION_BUTTON GetActionButton()
 83            {
 114684                switch (button)
 85                {
 86                    case "PRIMARY":
 187                        return WebInterface.ACTION_BUTTON.PRIMARY;
 88                    case "SECONDARY":
 089                        return WebInterface.ACTION_BUTTON.SECONDARY;
 90                    case "POINTER":
 091                        return WebInterface.ACTION_BUTTON.POINTER;
 92                    default:
 114593                        return WebInterface.ACTION_BUTTON.ANY;
 94                }
 95            }
 96        }
 97
 98        public OnPointerEventHandler pointerEventHandler;
 99
 100        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 101        {
 37102            base.Initialize(scene, entity);
 103
 37104            if (model == null)
 37105                model = new OnPointerEvent.Model();
 106
 37107            pointerEventHandler = new OnPointerEventHandler();
 37108            SetEventColliders(entity);
 109
 37110            entity.OnShapeUpdated -= SetEventColliders;
 37111            entity.OnShapeUpdated += SetEventColliders;
 37112        }
 113
 114        public WebInterface.ACTION_BUTTON GetActionButton()
 115        {
 1146116            return ((Model) this.model).GetActionButton();
 117        }
 118
 119        public void SetHoverState(bool hoverState)
 120        {
 1150121            Model model = (Model) this.model;
 1150122            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 1150123        }
 124
 125        void SetEventColliders(IDCLEntity entity)
 126        {
 75127            pointerEventHandler.SetColliders(entity);
 75128        }
 129
 130        public bool IsVisible()
 131        {
 1159132            if (entity == null)
 0133                return false;
 134
 1159135            bool isVisible = false;
 136
 1159137            if (entity.meshesInfo != null &&
 138                entity.meshesInfo.renderers != null &&
 139                entity.meshesInfo.renderers.Length > 0)
 140            {
 1159141                isVisible = entity.meshesInfo.renderers[0].enabled;
 142            }
 143
 1159144            return isVisible;
 145        }
 146
 147        public bool IsAtHoverDistance(float distance)
 148        {
 1155149            Model model = this.model as Model;
 1155150            return distance <= model.distance;
 151        }
 152
 153        public override IEnumerator ApplyChanges(BaseModel newModel)
 154        {
 53155            this.model = newModel ?? new Model();
 53156            return null;
 157        }
 158
 159        public bool ShouldShowHoverFeedback()
 160        {
 1144161            Model model = this.model as Model;
 1144162            return model.showFeedback;
 163        }
 164
 165        void OnDestroy()
 166        {
 37167            if (entity != null)
 37168                entity.OnShapeUpdated -= SetEventColliders;
 169
 37170            pointerEventHandler.Dispose();
 37171        }
 172
 173        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit)
 174        {
 0175        }
 176
 177        public virtual PointerInputEventType GetEventType()
 178        {
 0179            return PointerInputEventType.NONE;
 180        }
 181    }
 182}