< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CreateSubscription(...)0%2100%

File(s)

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

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7.InternalComponents;
 3using DCL.Models;
 4using Google.Protobuf;
 5using JetBrains.Annotations;
 6using System;
 7using System.Collections.Generic;
 8using UnityEngine.UIElements;
 9
 10namespace DCL.ECSComponents
 11{
 12    public static class UIPointerEventsUtils
 13    {
 14        internal abstract class UIEventSubscriptionFactory
 15        {
 16            internal abstract IDisposable CreateSubscription(VisualElement uiElement,
 17                IParcelScene parcelScene,
 18                IDCLEntity dclEntity,
 19                PointerEventType pointerEventType,
 20                PBPointerEvents.Types.Info requestInfo,
 21                IInternalECSComponent<InternalInputEventResults> results);
 22        }
 23
 24        internal class UIEventSubscriptionFactory<TEvent> : UIEventSubscriptionFactory
 25            where TEvent: PointerEventBase<TEvent>, new()
 26        {
 27            internal override IDisposable CreateSubscription(
 28                VisualElement uiElement,
 29                IParcelScene parcelScene,
 30                IDCLEntity dclEntity,
 31                PointerEventType pointerEventType,
 32                PBPointerEvents.Types.Info requestInfo,
 33                IInternalECSComponent<InternalInputEventResults> results) =>
 034                new UIEventSubscription<TEvent>(uiElement, parcelScene, dclEntity, pointerEventType, requestInfo, result
 35        }
 36
 37        private static readonly Dictionary<PointerEventType, UIEventSubscriptionFactory> FACTORIES = new ()
 38        {
 39            { PointerEventType.PetDown, new UIEventSubscriptionFactory<PointerDownEvent>() },
 40            { PointerEventType.PetUp, new UIEventSubscriptionFactory<PointerUpEvent>() },
 41            { PointerEventType.PetHoverEnter, new UIEventSubscriptionFactory<PointerEnterEvent>() },
 42            { PointerEventType.PetHoverLeave, new UIEventSubscriptionFactory<PointerLeaveEvent>() }
 43        };
 44
 45        [CanBeNull]
 46        public static UIEventsSubscriptions AddCommonInteractivity(
 47            VisualElement uiElement,
 48            IParcelScene parcelScene,
 49            IDCLEntity dclEntity,
 50            IReadOnlyList<PBPointerEvents.Types.Entry> requestedInteraction,
 51            IInternalECSComponent<InternalInputEventResults> results)
 52        {
 53            if (requestedInteraction is null or { Count: 0 })
 54                return null;
 55
 56            var subscriptions = new UIEventsSubscriptions();
 57
 58            foreach (var entry in requestedInteraction)
 59            {
 60                if (!FACTORIES.TryGetValue(entry.EventType, out var factory))
 61                    continue;
 62
 63                subscriptions.list.Add(factory.CreateSubscription(uiElement, parcelScene, dclEntity,
 64                    entry.EventType, entry.EventInfo, results));
 65            }
 66
 67            return subscriptions;
 68        }
 69
 70        public static EventCallback<TEvent> RegisterFeedback<TEvent, TResultMessage>(
 71            IInternalECSComponent<InternalUIInputResults> inputResults,
 72            Func<TEvent, TResultMessage> createResult,
 73            IParcelScene scene,
 74            IDCLEntity entity,
 75            VisualElement uiElement,
 76            int resultComponentId)
 77            where TResultMessage : class, IMessage
 78            where TEvent : EventBase<TEvent>, new()
 79        {
 80            EventCallback<TEvent> callback = evt =>
 81            {
 82                var model = inputResults.GetFor(scene, entity)?.model ?? new InternalUIInputResults();
 83                model.Results.Enqueue(new InternalUIInputResults.Result(createResult(evt), resultComponentId));
 84                inputResults.PutFor(scene, entity, model);
 85            };
 86
 87            uiElement.RegisterCallback(callback);
 88            return callback;
 89        }
 90
 91        public static void UnregisterFeedback<TEvent>(
 92            this VisualElement uiElement,
 93            EventCallback<TEvent> callback
 94            ) where TEvent : EventBase<TEvent>, new()
 95        {
 96            uiElement.UnregisterCallback(callback);
 97        }
 98    }
 99}