| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.Models; |
| | 4 | | using Google.Protobuf; |
| | 5 | | using JetBrains.Annotations; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine.UIElements; |
| | 9 | |
|
| | 10 | | namespace 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) => |
| 0 | 34 | | 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 | | } |