< Summary

Class:ECSSystems.PointerInputSystem.PointerInputResult
Assembly:ECS7Plugin.Systems.PointerInput
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/Systems/PointerInputSystem/PointerDownUpProcessor.cs
Covered lines:6
Uncovered lines:0
Coverable lines:6
Total lines:113
Line coverage:100% (6 of 6)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PointerInputResult(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/Systems/PointerInputSystem/PointerDownUpProcessor.cs

#LineLine coverage
 1using DCL;
 2using DCL.ECS7.InternalComponents;
 3using DCL.ECSComponents;
 4using DCL.ECSRuntime;
 5
 6namespace 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        {
 2017            this.entityId = entityId;
 2018            this.sceneId = sceneId;
 2019            this.hasValue = hasValue;
 2020            this.shouldTriggerEvent = shouldTriggerEvent;
 2021        }
 22
 523        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}