| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.Models; |
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine.UIElements; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 0 | 19 | | public UIEventSubscription(VisualElement uiElement, |
| | 20 | | IParcelScene parcelScene, |
| | 21 | | IDCLEntity dclEntity, |
| | 22 | | PointerEventType pointerEventType, |
| | 23 | | PBPointerEvents.Types.Info requestInfo, |
| | 24 | | IInternalECSComponent<InternalInputEventResults> results |
| | 25 | | ) |
| | 26 | | { |
| 0 | 27 | | this.uiElement = uiElement; |
| 0 | 28 | | this.parcelScene = parcelScene; |
| 0 | 29 | | this.dclEntity = dclEntity; |
| 0 | 30 | | this.pointerEventType = pointerEventType; |
| 0 | 31 | | this.results = results; |
| | 32 | |
|
| | 33 | | // every other field is irrelevant for UI |
| 0 | 34 | | inputAction = requestInfo.Button; |
| | 35 | |
|
| 0 | 36 | | uiElement.RegisterCallback<TEvent>(OnEventRaised); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private void OnEventRaised(TEvent evt) |
| | 40 | | { |
| | 41 | | // filter by button |
| 0 | 42 | | if (!IsInputAccepted(evt)) |
| 0 | 43 | | return; |
| | 44 | |
|
| 0 | 45 | | 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 | |
|
| 0 | 57 | | results.AddEvent(parcelScene, eventData); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | private bool IsInputAccepted(TEvent evt) => |
| 0 | 61 | | 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 | | { |
| 0 | 67 | | uiElement.UnregisterCallback<TEvent>(OnEventRaised); |
| 0 | 68 | | } |
| | 69 | | } |
| | 70 | | } |