| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECS7.InternalComponents; |
| | 6 | | using DCL.ECSComponents; |
| | 7 | | using DCL.ECSRuntime; |
| | 8 | | using DCL.Models; |
| | 9 | | using UnityEngine; |
| | 10 | | using Ray = UnityEngine.Ray; |
| | 11 | |
|
| | 12 | | namespace ECSSystems.PointerInputSystem |
| | 13 | | { |
| | 14 | | public static class ECSPointerInputSystem |
| | 15 | | { |
| | 16 | | private class State |
| | 17 | | { |
| | 18 | | public IInternalECSComponent<InternalColliders> pointerColliderComponent; |
| | 19 | | public IInternalECSComponent<InternalInputEventResults> inputResultComponent; |
| | 20 | | public ECSComponent<PBPointerEvents> pointerEvents; |
| | 21 | | public DataStore_ECS7 dataStoreEcs7; |
| | 22 | | public bool isLastInputPointerDown; |
| | 23 | | public IWorldState worldState; |
| | 24 | | public EntityInput lastInputDown; |
| | 25 | | public EntityInput lastInputHover; |
| | 26 | | public EntityInput lastHoverFeedback; |
| | 27 | | public IECSInteractionHoverCanvas interactionHoverCanvas; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | private class EntityInput |
| | 31 | | { |
| | 32 | | public IDCLEntity entity; |
| | 33 | | public IParcelScene scene; |
| | 34 | | public string sceneId; |
| | 35 | | public bool hasValue; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | private enum InputHitType |
| | 39 | | { |
| | 40 | | None, |
| | 41 | | PointerDown, |
| | 42 | | PointerUp, |
| | 43 | | PointerHover |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public static Action CreateSystem( |
| | 47 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 48 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 49 | | ECSComponent<PBPointerEvents> pointerEvents, |
| | 50 | | IECSInteractionHoverCanvas interactionHoverCanvas, |
| | 51 | | IWorldState worldState, |
| | 52 | | DataStore_ECS7 dataStoreEcs) |
| | 53 | | { |
| 14 | 54 | | var state = new State() |
| | 55 | | { |
| | 56 | | pointerColliderComponent = pointerColliderComponent, |
| | 57 | | inputResultComponent = inputResultComponent, |
| | 58 | | pointerEvents = pointerEvents, |
| | 59 | | interactionHoverCanvas = interactionHoverCanvas, |
| | 60 | | isLastInputPointerDown = false, |
| | 61 | | dataStoreEcs7 = dataStoreEcs, |
| | 62 | | worldState = worldState, |
| | 63 | | lastInputDown = new EntityInput() { hasValue = false }, |
| | 64 | | lastInputHover = new EntityInput() { hasValue = false }, |
| | 65 | | lastHoverFeedback = new EntityInput() { hasValue = false }, |
| | 66 | | }; |
| 35 | 67 | | return () => Update(state); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | private static void Update(State state) |
| | 71 | | { |
| 21 | 72 | | DataStore_ECS7.PointerEvent currentPointerInput = state.dataStoreEcs7.lastPointerInputEvent; |
| | 73 | |
|
| 21 | 74 | | bool isHit = state.dataStoreEcs7.lastPointerRayHit.hasValue && state.dataStoreEcs7.lastPointerRayHit.didHit; |
| 21 | 75 | | bool isPointerDown = currentPointerInput.hasValue && currentPointerInput.isButtonDown; |
| 21 | 76 | | bool isPointerUp = state.isLastInputPointerDown && currentPointerInput.hasValue && !currentPointerInput.isBu |
| | 77 | |
|
| 21 | 78 | | DataStore_ECS7.RaycastEvent.Hit raycastHit = state.dataStoreEcs7.lastPointerRayHit.hit; |
| 21 | 79 | | Ray raycastRay = state.dataStoreEcs7.lastPointerRayHit.ray; |
| | 80 | |
|
| 21 | 81 | | IECSReadOnlyComponentData<InternalColliders> colliderData = isHit |
| | 82 | | ? state.pointerColliderComponent |
| | 83 | | .GetEntityWithCollider(raycastHit.collider) |
| | 84 | | : null; |
| | 85 | |
|
| 21 | 86 | | bool isRaycastHitValidEntity = colliderData != null; |
| 21 | 87 | | bool isHoveringInput = !isPointerDown && !isPointerUp && !state.isLastInputPointerDown; |
| 21 | 88 | | bool isHoveringExit = !isRaycastHitValidEntity && state.lastInputHover.hasValue; |
| | 89 | |
|
| 21 | 90 | | IList<PBPointerEvents.Types.Entry> hoverEvents = isRaycastHitValidEntity |
| | 91 | | ? state.pointerEvents.GetPointerEventsForEntity(colliderData.scene, colliderData.entity) |
| | 92 | | : null; |
| | 93 | |
|
| | 94 | | // show hover tooltip for pointer down |
| 21 | 95 | | if (hoverEvents != null && isHoveringInput) |
| | 96 | | { |
| 3 | 97 | | if (!state.lastHoverFeedback.hasValue || state.lastHoverFeedback.entity != colliderData.entity) |
| | 98 | | { |
| 3 | 99 | | state.interactionHoverCanvas.ShowPointerDownHover(hoverEvents, raycastHit.distance); |
| 3 | 100 | | state.lastHoverFeedback.hasValue = true; |
| 3 | 101 | | state.lastHoverFeedback.entity = colliderData.entity; |
| 3 | 102 | | state.lastHoverFeedback.scene = colliderData.scene; |
| 3 | 103 | | state.lastHoverFeedback.sceneId = colliderData.scene.sceneData.id; |
| | 104 | | } |
| 3 | 105 | | } |
| | 106 | | // show hover tooltip for pointer up |
| 18 | 107 | | else if (hoverEvents != null && state.isLastInputPointerDown && !isPointerUp) |
| | 108 | | { |
| 3 | 109 | | if (!state.lastHoverFeedback.hasValue && state.lastInputDown.entity == colliderData.entity) |
| | 110 | | { |
| 3 | 111 | | state.interactionHoverCanvas.ShowPointerUpHover(hoverEvents, raycastHit.distance, (ActionButton)curr |
| 3 | 112 | | state.lastHoverFeedback.hasValue = true; |
| 3 | 113 | | state.lastHoverFeedback.entity = colliderData.entity; |
| 3 | 114 | | state.lastHoverFeedback.scene = colliderData.scene; |
| 3 | 115 | | state.lastHoverFeedback.sceneId = colliderData.scene.sceneData.id; |
| | 116 | | } |
| 3 | 117 | | } |
| | 118 | | // hide hover tooltip |
| 15 | 119 | | else if (state.lastHoverFeedback.hasValue) |
| | 120 | | { |
| 0 | 121 | | state.interactionHoverCanvas.Hide(); |
| 0 | 122 | | state.lastHoverFeedback.hasValue = false; |
| | 123 | | } |
| | 124 | |
|
| 21 | 125 | | InputHitType inputHitType = InputHitType.None; |
| | 126 | |
|
| 21 | 127 | | if (isRaycastHitValidEntity) |
| | 128 | | { |
| 19 | 129 | | inputHitType = isPointerDown ? InputHitType.PointerDown |
| | 130 | | : isPointerUp ? InputHitType.PointerUp |
| | 131 | | : isHoveringInput ? InputHitType.PointerHover |
| | 132 | | : InputHitType.None; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | // process entity hit with input |
| | 136 | | switch (inputHitType) |
| | 137 | | { |
| | 138 | | case InputHitType.PointerDown: |
| 7 | 139 | | state.lastInputDown.entity = colliderData.entity; |
| 7 | 140 | | state.lastInputDown.scene = colliderData.scene; |
| 7 | 141 | | state.lastInputDown.sceneId = colliderData.scene.sceneData.id; |
| 7 | 142 | | state.lastInputDown.hasValue = true; |
| | 143 | |
|
| 7 | 144 | | state.inputResultComponent.AddEvent(colliderData.scene, new InternalInputEventResults.EventData() |
| | 145 | | { |
| | 146 | | analog = 1, |
| | 147 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 148 | | hit = ProtoConvertUtils.ToPBRaycasHit(colliderData.entity.entityId, null, |
| | 149 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal), |
| | 150 | | type = PointerEventType.Down |
| | 151 | | }); |
| 7 | 152 | | break; |
| | 153 | |
|
| | 154 | | case InputHitType.PointerUp: |
| 2 | 155 | | bool validInputDownExist = state.lastInputDown.hasValue; |
| 2 | 156 | | EntityInput lastInputDownData = state.lastInputDown; |
| | 157 | |
|
| | 158 | | // did it hit same entity as pointer down hit? |
| 2 | 159 | | if (validInputDownExist && colliderData.entity.entityId == lastInputDownData.entity.entityId |
| | 160 | | && colliderData.scene.sceneData.id == lastInputDownData.sceneId) |
| | 161 | | { |
| 1 | 162 | | state.inputResultComponent.AddEvent(colliderData.scene, new InternalInputEventResults.EventData( |
| | 163 | | { |
| | 164 | | analog = 1, |
| | 165 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 166 | | hit = ProtoConvertUtils.ToPBRaycasHit(colliderData.entity.entityId, null, |
| | 167 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal), |
| | 168 | | type = PointerEventType.Up |
| | 169 | | }); |
| 1 | 170 | | } |
| | 171 | | // did it hit different entity as pointer down hit? |
| 1 | 172 | | else if (validInputDownExist) |
| | 173 | | { |
| 1 | 174 | | bool isEntityFromSameScene = colliderData.scene.sceneData.id == lastInputDownData.sceneId; |
| 1 | 175 | | bool isValidScene = isEntityFromSameScene || state.worldState.ContainsScene(lastInputDownData.sc |
| | 176 | |
|
| 1 | 177 | | if (isValidScene) |
| | 178 | | { |
| 1 | 179 | | state.inputResultComponent.AddEvent(lastInputDownData.scene, new InternalInputEventResults.E |
| | 180 | | { |
| | 181 | | analog = 1, |
| | 182 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 183 | | hit = ProtoConvertUtils.ToPBRaycasHit(-1, null, |
| | 184 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal, false), |
| | 185 | | type = PointerEventType.Up |
| | 186 | | }); |
| | 187 | | } |
| | 188 | | } |
| 2 | 189 | | state.lastInputDown.hasValue = false; |
| 2 | 190 | | break; |
| | 191 | |
|
| | 192 | | case InputHitType.PointerHover: |
| 7 | 193 | | bool isPreviouslyHoveredEntity = state.lastInputHover.hasValue; |
| 7 | 194 | | bool isHoveringNewEntity = !isPreviouslyHoveredEntity |
| | 195 | | || state.lastInputHover.entity.entityId != colliderData.entity.entityId |
| | 196 | | || state.lastInputHover.sceneId != colliderData.scene.sceneData.id; |
| | 197 | |
|
| | 198 | | // was other entity previously hovered? |
| 7 | 199 | | if (isPreviouslyHoveredEntity && isHoveringNewEntity) |
| | 200 | | { |
| 1 | 201 | | bool isValidScene = colliderData.scene.sceneData.id == state.lastInputHover.sceneId |
| | 202 | | || state.worldState.ContainsScene(state.lastInputHover.sceneId); |
| | 203 | |
|
| 1 | 204 | | if (isValidScene) |
| | 205 | | { |
| 1 | 206 | | state.inputResultComponent.AddEvent(state.lastInputHover.scene, new InternalInputEventResult |
| | 207 | | { |
| | 208 | | analog = 1, |
| | 209 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 210 | | hit = ProtoConvertUtils.ToPBRaycasHit(state.lastInputHover.entity.entityId, null, |
| | 211 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal), |
| | 212 | | type = PointerEventType.HoverLeave |
| | 213 | | }); |
| | 214 | | } |
| | 215 | | } |
| | 216 | |
|
| | 217 | | // hover enter |
| 7 | 218 | | if (isHoveringNewEntity) |
| | 219 | | { |
| 7 | 220 | | state.lastInputHover.entity = colliderData.entity; |
| 7 | 221 | | state.lastInputHover.scene = colliderData.scene; |
| 7 | 222 | | state.lastInputHover.sceneId = colliderData.scene.sceneData.id; |
| 7 | 223 | | state.lastInputHover.hasValue = true; |
| | 224 | |
|
| 7 | 225 | | state.inputResultComponent.AddEvent(colliderData.scene, new InternalInputEventResults.EventData( |
| | 226 | | { |
| | 227 | | analog = 1, |
| | 228 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 229 | | hit = ProtoConvertUtils.ToPBRaycasHit(colliderData.entity.entityId, null, |
| | 230 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal), |
| | 231 | | type = PointerEventType.HoverEnter |
| | 232 | | }); |
| | 233 | | } |
| | 234 | | break; |
| | 235 | | } |
| | 236 | |
|
| | 237 | | // no entity hit |
| 21 | 238 | | if (!isRaycastHitValidEntity) |
| | 239 | | { |
| 2 | 240 | | if (isPointerUp) |
| | 241 | | { |
| 1 | 242 | | if (state.lastInputDown.hasValue) |
| | 243 | | { |
| | 244 | | // input up without hit but with valid input down |
| 1 | 245 | | bool isValidScene = state.worldState.ContainsScene(state.lastInputDown.sceneId); |
| 1 | 246 | | if (isValidScene) |
| | 247 | | { |
| 1 | 248 | | state.inputResultComponent.AddEvent(state.lastInputDown.scene, new InternalInputEventResults |
| | 249 | | { |
| | 250 | | analog = 1, |
| | 251 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 252 | | hit = ProtoConvertUtils.ToPBRaycasHit(-1, null, |
| | 253 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal, false), |
| | 254 | | type = PointerEventType.Up |
| | 255 | | }); |
| | 256 | | } |
| | 257 | | } |
| | 258 | |
|
| 1 | 259 | | state.lastInputDown.hasValue = false; |
| | 260 | | } |
| 2 | 261 | | if (isHoveringExit) |
| | 262 | | { |
| 1 | 263 | | bool isValidScene = state.worldState.ContainsScene(state.lastInputHover.sceneId); |
| | 264 | |
|
| 1 | 265 | | if (isValidScene) |
| | 266 | | { |
| 1 | 267 | | state.inputResultComponent.AddEvent(state.lastInputHover.scene, new InternalInputEventResults.Ev |
| | 268 | | { |
| | 269 | | analog = 1, |
| | 270 | | button = (ActionButton)currentPointerInput.buttonId, |
| | 271 | | hit = ProtoConvertUtils.ToPBRaycasHit(state.lastInputHover.entity.entityId, null, |
| | 272 | | raycastRay, raycastHit.distance, raycastHit.point, raycastHit.normal), |
| | 273 | | type = PointerEventType.HoverLeave |
| | 274 | | }); |
| | 275 | | } |
| 1 | 276 | | state.lastInputHover.hasValue = false; |
| | 277 | | } |
| | 278 | | } |
| | 279 | |
|
| 21 | 280 | | state.dataStoreEcs7.lastPointerInputEvent.hasValue = false; |
| 21 | 281 | | state.dataStoreEcs7.lastPointerRayHit.hasValue = false; |
| 21 | 282 | | state.isLastInputPointerDown = isPointerDown || (!isPointerUp && state.isLastInputPointerDown); |
| 21 | 283 | | } |
| | 284 | |
|
| | 285 | | private static IECSReadOnlyComponentData<InternalColliders> GetEntityWithCollider( |
| | 286 | | this IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 287 | | Collider collider) |
| | 288 | | { |
| 19 | 289 | | var collidersData = pointerColliderComponent.GetForAll(); |
| 42 | 290 | | for (int i = 0; i < collidersData.Count; i++) |
| | 291 | | { |
| 21 | 292 | | var colliderData = collidersData[i].value; |
| 21 | 293 | | if (colliderData.model.colliders.Contains(collider)) |
| | 294 | | { |
| 19 | 295 | | return colliderData; |
| | 296 | | } |
| | 297 | | } |
| 0 | 298 | | return null; |
| | 299 | | } |
| | 300 | |
|
| | 301 | | private static IList<PBPointerEvents.Types.Entry> GetPointerEventsForEntity(this ECSComponent<PBPointerEvents> c |
| | 302 | | IParcelScene scene, IDCLEntity entity) |
| | 303 | | { |
| 19 | 304 | | var componentData = component.Get(scene, entity); |
| 19 | 305 | | return componentData?.model.PointerEvents; |
| | 306 | | } |
| | 307 | |
|
| | 308 | | private static void ShowPointerDownHover(this IECSInteractionHoverCanvas canvas, |
| | 309 | | IList<PBPointerEvents.Types.Entry> entityEvents, float distance) |
| | 310 | | { |
| 3 | 311 | | canvas.ShowHoverTooltips(entityEvents, (pointerEvent) => |
| 4 | 312 | | pointerEvent.EventType == PointerEventType.Down |
| | 313 | | && pointerEvent.EventInfo.GetShowFeedback() |
| | 314 | | && distance <= pointerEvent.EventInfo.GetMaxDistance() |
| | 315 | | ); |
| 3 | 316 | | } |
| | 317 | |
|
| | 318 | | private static void ShowPointerUpHover(this IECSInteractionHoverCanvas canvas, |
| | 319 | | IList<PBPointerEvents.Types.Entry> entityEvents, float distance, ActionButton expectedButton) |
| | 320 | | { |
| 3 | 321 | | canvas.ShowHoverTooltips(entityEvents, (pointerEvent) => |
| 3 | 322 | | pointerEvent.EventType == PointerEventType.Up |
| | 323 | | && pointerEvent.EventInfo.GetShowFeedback() |
| | 324 | | && distance <= pointerEvent.EventInfo.GetMaxDistance() |
| | 325 | | && (pointerEvent.EventInfo.GetButton() == expectedButton || pointerEvent.EventInfo.GetButton() == Action |
| | 326 | | ); |
| 3 | 327 | | } |
| | 328 | |
|
| | 329 | | private static void ShowHoverTooltips(this IECSInteractionHoverCanvas canvas, |
| | 330 | | IList<PBPointerEvents.Types.Entry> entityEvents, Func<PBPointerEvents.Types.Entry, bool> filter) |
| | 331 | | { |
| 6 | 332 | | if (entityEvents is null) |
| 0 | 333 | | return; |
| | 334 | |
|
| 6 | 335 | | bool anyTooltipAdded = false; |
| 6 | 336 | | int eventIndex = 0; |
| 36 | 337 | | for (int i = 0; i < canvas.tooltipsCount; i++) |
| | 338 | | { |
| 12 | 339 | | PBPointerEvents.Types.Entry pointerEvent = null; |
| 18 | 340 | | for (; eventIndex < entityEvents.Count; eventIndex++) |
| | 341 | | { |
| 7 | 342 | | pointerEvent = entityEvents[eventIndex]; |
| 7 | 343 | | if (filter(pointerEvent)) |
| | 344 | | { |
| 4 | 345 | | eventIndex++; |
| 4 | 346 | | break; |
| | 347 | | } |
| 3 | 348 | | pointerEvent = null; |
| | 349 | | } |
| | 350 | |
|
| 12 | 351 | | if (!(pointerEvent is null)) |
| | 352 | | { |
| 4 | 353 | | anyTooltipAdded = true; |
| 4 | 354 | | canvas.SetTooltipText(i, pointerEvent.EventInfo.GetHoverText()); |
| 4 | 355 | | canvas.SetTooltipInput(i, pointerEvent.EventInfo.GetButton()); |
| 4 | 356 | | canvas.SetTooltipActive(i, true); |
| 4 | 357 | | } |
| | 358 | | else |
| | 359 | | { |
| 8 | 360 | | canvas.SetTooltipActive(i, false); |
| | 361 | | } |
| | 362 | | } |
| | 363 | |
|
| 6 | 364 | | if (anyTooltipAdded) |
| | 365 | | { |
| 3 | 366 | | canvas.Show(); |
| | 367 | | } |
| 6 | 368 | | } |
| | 369 | | } |
| | 370 | | } |