< Summary

Class:DCL.ECSComponents.UIEventSubscription[TEvent]
Assembly:DCL.ECSComponents.UIComponentsUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/UIComponentsUtils/UIEventSubscription.cs
Covered lines:0
Uncovered lines:17
Coverable lines:17
Total lines:70
Line coverage:0% (0 of 17)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UIEventSubscription(...)0%2100%
OnEventRaised(...)0%6200%
IsInputAccepted(...)0%12300%
Dispose()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/UIComponentsUtils/UIEventSubscription.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7.InternalComponents;
 3using DCL.Models;
 4using System;
 5using System.Linq;
 6using UnityEngine.UIElements;
 7
 8namespace DCL.ECSComponents
 9{
 10    public class UIEventSubscription<TEvent> : IDisposable where TEvent : PointerEventBase<TEvent>, new()
 11    {
 12        private readonly VisualElement uiElement;
 13        private readonly IParcelScene parcelScene;
 14        private readonly IDCLEntity dclEntity;
 15        private readonly PointerEventType pointerEventType;
 16        private readonly InputAction inputAction;
 17        private readonly IInternalECSComponent<InternalInputEventResults> results;
 18
 019        public UIEventSubscription(VisualElement uiElement,
 20            IParcelScene parcelScene,
 21            IDCLEntity dclEntity,
 22            PointerEventType pointerEventType,
 23            PBPointerEvents.Types.Info requestInfo,
 24            IInternalECSComponent<InternalInputEventResults> results
 25        )
 26        {
 027            this.uiElement = uiElement;
 028            this.parcelScene = parcelScene;
 029            this.dclEntity = dclEntity;
 030            this.pointerEventType = pointerEventType;
 031            this.results = results;
 32
 33            // every other field is irrelevant for UI
 034            inputAction = requestInfo.Button;
 35
 036            uiElement.RegisterCallback<TEvent>(OnEventRaised);
 037        }
 38
 39        private void OnEventRaised(TEvent evt)
 40        {
 41            // filter by button
 042            if (!IsInputAccepted(evt))
 043                return;
 44
 045            var eventData = new InternalInputEventResults.EventData
 46            {
 47                button = (InputAction) evt.button,
 48                type = pointerEventType,
 49                hit = new RaycastHit
 50                {
 51                    EntityId = dclEntity.entityId,
 52                    Length = 0,
 53                    Position = ProtoConvertUtils.UnityVectorToPBVector(evt.position)
 54                }
 55            };
 56
 057            results.AddEvent(parcelScene, eventData);
 058        }
 59
 60        private bool IsInputAccepted(TEvent evt) =>
 061            inputAction == InputAction.IaAny ||
 62            (UIButtonAcceptanceMap.ACCEPTED_INPUT_ACTIONS.Length > evt.button
 63             && UIButtonAcceptanceMap.ACCEPTED_INPUT_ACTIONS[evt.button] == inputAction);
 64
 65        public void Dispose()
 66        {
 067            uiElement.UnregisterCallback<TEvent>(OnEventRaised);
 068        }
 69    }
 70}