< Summary

Class:DCL.Components.OnPointerEventHandler
Assembly:DCL.Components.Events
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/UUIDEventComponentsPlugin/UUIDComponent/OnPointerEvent.cs
Covered lines:21
Uncovered lines:2
Coverable lines:23
Total lines:182
Line coverage:91.3% (21 of 23)
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.024090%
GetMeshName(...)0%3.333066.67%
Dispose()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    {
 115        public static bool enableInteractionHoverFeedback = true;
 3716        public OnPointerEventColliders eventColliders { get; private set; }
 17
 18        private IDCLEntity entity;
 19
 3720        public OnPointerEventHandler()
 21        {
 3722            eventColliders = new OnPointerEventColliders();
 3723        }
 24
 25        public void SetColliders(IDCLEntity entity)
 26        {
 7527            this.entity = entity;
 7528            eventColliders.Initialize(entity);
 7529        }
 30
 31        public void SetFeedbackState(bool showFeedback, bool hoverState, string button, string hoverText)
 32        {
 115033            if (!enableInteractionHoverFeedback)
 034                return;
 35
 115036            var cursorData = DataStore.i.Get<DataStore_Cursor>();
 115037            cursorData.hoverFeedbackEnabled.Set(showFeedback);
 38
 115039            if (showFeedback)
 40            {
 115041                if (hoverState)
 42                {
 114443                    cursorData.hoverFeedbackButton.Set(button);
 114444                    cursorData.hoverFeedbackText.Set(hoverText);
 45                }
 46
 115047                cursorData.hoverFeedbackHoverState.Set(hoverState);
 48            }
 115049        }
 50
 51        public string GetMeshName(Collider collider)
 52        {
 653            if (collider == null || eventColliders == null)
 054                return null;
 55
 656            return eventColliders.GetMeshName(collider);
 57        }
 58
 59        public void Dispose()
 60        {
 3761            eventColliders.Dispose();
 3762        }
 63    }
 64
 65    public class OnPointerEvent : UUIDComponent, IPointerInputEvent
 66    {
 67        public static bool enableInteractionHoverFeedback = true;
 68
 69        [System.Serializable]
 70        public new class Model : UUIDComponent.Model
 71        {
 72            public string button = WebInterface.ACTION_BUTTON.ANY.ToString();
 73            public string hoverText = "Interact";
 74            public float distance = 10f;
 75            public bool showFeedback = true;
 76
 77            public override BaseModel GetDataFromJSON(string json)
 78            {
 79                return Utils.SafeFromJson<Model>(json);
 80            }
 81
 82            public WebInterface.ACTION_BUTTON GetActionButton()
 83            {
 84                switch (button)
 85                {
 86                    case "PRIMARY":
 87                        return WebInterface.ACTION_BUTTON.PRIMARY;
 88                    case "SECONDARY":
 89                        return WebInterface.ACTION_BUTTON.SECONDARY;
 90                    case "POINTER":
 91                        return WebInterface.ACTION_BUTTON.POINTER;
 92                    default:
 93                        return WebInterface.ACTION_BUTTON.ANY;
 94                }
 95            }
 96        }
 97
 98        public OnPointerEventHandler pointerEventHandler;
 99
 100        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 101        {
 102            base.Initialize(scene, entity);
 103
 104            if (model == null)
 105                model = new OnPointerEvent.Model();
 106
 107            pointerEventHandler = new OnPointerEventHandler();
 108            SetEventColliders(entity);
 109
 110            entity.OnShapeUpdated -= SetEventColliders;
 111            entity.OnShapeUpdated += SetEventColliders;
 112        }
 113
 114        public WebInterface.ACTION_BUTTON GetActionButton()
 115        {
 116            return ((Model) this.model).GetActionButton();
 117        }
 118
 119        public void SetHoverState(bool hoverState)
 120        {
 121            Model model = (Model) this.model;
 122            pointerEventHandler.SetFeedbackState(model.showFeedback, hoverState, model.button, model.hoverText);
 123        }
 124
 125        void SetEventColliders(IDCLEntity entity)
 126        {
 127            pointerEventHandler.SetColliders(entity);
 128        }
 129
 130        public bool IsVisible()
 131        {
 132            if (entity == null)
 133                return false;
 134
 135            bool isVisible = false;
 136
 137            if (entity.meshesInfo != null &&
 138                entity.meshesInfo.renderers != null &&
 139                entity.meshesInfo.renderers.Length > 0)
 140            {
 141                isVisible = entity.meshesInfo.renderers[0].enabled;
 142            }
 143
 144            return isVisible;
 145        }
 146
 147        public bool IsAtHoverDistance(float distance)
 148        {
 149            Model model = this.model as Model;
 150            return distance <= model.distance;
 151        }
 152
 153        public override IEnumerator ApplyChanges(BaseModel newModel)
 154        {
 155            this.model = newModel ?? new Model();
 156            return null;
 157        }
 158
 159        public bool ShouldShowHoverFeedback()
 160        {
 161            Model model = this.model as Model;
 162            return model.showFeedback;
 163        }
 164
 165        void OnDestroy()
 166        {
 167            if (entity != null)
 168                entity.OnShapeUpdated -= SetEventColliders;
 169
 170            pointerEventHandler.Dispose();
 171        }
 172
 173        public virtual void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit)
 174        {
 175        }
 176
 177        public virtual PointerInputEventType GetEventType()
 178        {
 179            return PointerInputEventType.NONE;
 180        }
 181    }
 182}