| | 1 | | using DCL; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSComponents; |
| | 4 | | using DCL.ECSRuntime; |
| | 5 | |
|
| | 6 | | namespace ECSSystems.PointerInputSystem |
| | 7 | | { |
| | 8 | | internal readonly struct PointerInputResult |
| | 9 | | { |
| | 10 | | public readonly long entityId; |
| | 11 | | public readonly string sceneId; |
| | 12 | | public readonly bool hasValue; |
| | 13 | | public readonly bool shouldTriggerEvent; |
| | 14 | |
|
| | 15 | | public PointerInputResult(string sceneId, long entityId, bool shouldTriggerEvent, bool hasValue = true) |
| | 16 | | { |
| 20 | 17 | | this.entityId = entityId; |
| 20 | 18 | | this.sceneId = sceneId; |
| 20 | 19 | | this.hasValue = hasValue; |
| 20 | 20 | | this.shouldTriggerEvent = shouldTriggerEvent; |
| 20 | 21 | | } |
| | 22 | |
|
| 5 | 23 | | public static PointerInputResult empty => new PointerInputResult(null, -1, false, false); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | internal static class PointerDownUpProcessor |
| | 27 | | { |
| | 28 | | public static PointerInputResult ProcessPointerDownUp( |
| | 29 | | DataStore_ECS7.PointerEvent pointerEvent, |
| | 30 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 31 | | ECSComponent<PBOnPointerDown> pointerDownComponent, |
| | 32 | | ECSComponent<PBOnPointerUp> pointerUpComponent, |
| | 33 | | PointerInputResult lastPointerDownResult) |
| | 34 | | { |
| | 35 | | if (pointerEvent.isButtonDown) |
| | 36 | | { |
| | 37 | | return GetPointerDownEntity(pointerDownComponent, pointerColliderComponent, pointerEvent); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | PointerInputResult pointerUp = GetPointerUpEntity(pointerUpComponent, pointerColliderComponent, pointerEvent |
| | 41 | |
|
| | 42 | | if (lastPointerDownResult.IsSameEntity(pointerUp)) |
| | 43 | | { |
| | 44 | | return pointerUp; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | return PointerInputResult.empty; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | private static PointerInputResult GetPointerDownEntity( |
| | 51 | | ECSComponent<PBOnPointerDown> pointerDownComponent, |
| | 52 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 53 | | DataStore_ECS7.PointerEvent pointerEvent) |
| | 54 | | { |
| | 55 | | var colliders = pointerColliderComponent.GetForAll(); |
| | 56 | | for (int i = 0; i < colliders.Count; i++) |
| | 57 | | { |
| | 58 | | ECSComponentData<InternalColliders> collider = colliders[i].value; |
| | 59 | | if (!collider.model.colliders.Contains(pointerEvent.rayResult.hitInfo.hit.collider)) |
| | 60 | | continue; |
| | 61 | |
|
| | 62 | | var pointerDownData = pointerDownComponent.Get(collider.scene, collider.entity); |
| | 63 | | if (pointerDownData == null) |
| | 64 | | return new PointerInputResult(collider.scene.sceneData.id, collider.entity.entityId, false); |
| | 65 | |
|
| | 66 | | if (PointerInputHelper.IsValidInputForEntity(pointerEvent.buttonId, |
| | 67 | | pointerEvent.rayResult.hitInfo.hit.distance, |
| | 68 | | pointerDownData.model.GetMaxDistance(), |
| | 69 | | pointerDownData.model.GetButton())) |
| | 70 | | { |
| | 71 | | return new PointerInputResult(collider.scene.sceneData.id, collider.entity.entityId, true); |
| | 72 | | } |
| | 73 | | return new PointerInputResult(collider.scene.sceneData.id, collider.entity.entityId, false); |
| | 74 | | } |
| | 75 | | return PointerInputResult.empty; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private static PointerInputResult GetPointerUpEntity( |
| | 79 | | ECSComponent<PBOnPointerUp> pointerUpComponent, |
| | 80 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 81 | | DataStore_ECS7.PointerEvent pointerEvent) |
| | 82 | | { |
| | 83 | | var colliders = pointerColliderComponent.GetForAll(); |
| | 84 | | for (int i = 0; i < colliders.Count; i++) |
| | 85 | | { |
| | 86 | | ECSComponentData<InternalColliders> collider = colliders[i].value; |
| | 87 | | if (!collider.model.colliders.Contains(pointerEvent.rayResult.hitInfo.hit.collider)) |
| | 88 | | continue; |
| | 89 | |
|
| | 90 | | var pointerUpData = pointerUpComponent.Get(collider.scene, collider.entity); |
| | 91 | | if (pointerUpData == null) |
| | 92 | | return new PointerInputResult(collider.scene.sceneData.id, collider.entity.entityId, false); |
| | 93 | |
|
| | 94 | | if (PointerInputHelper.IsValidInputForEntity(pointerEvent.buttonId, |
| | 95 | | pointerEvent.rayResult.hitInfo.hit.distance, |
| | 96 | | pointerUpData.model.GetMaxDistance(), |
| | 97 | | pointerUpData.model.GetButton())) |
| | 98 | | { |
| | 99 | | return new PointerInputResult(collider.scene.sceneData.id, collider.entity.entityId, true); |
| | 100 | | } |
| | 101 | | return new PointerInputResult(collider.scene.sceneData.id, collider.entity.entityId, false); |
| | 102 | | } |
| | 103 | | return PointerInputResult.empty; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private static bool IsSameEntity(this PointerInputResult self, PointerInputResult other) |
| | 107 | | { |
| | 108 | | if (!self.hasValue || !other.hasValue) |
| | 109 | | return false; |
| | 110 | | return self.entityId == other.entityId && self.sceneId == other.sceneId; |
| | 111 | | } |
| | 112 | | } |
| | 113 | | } |