| | 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<PBPointerHoverFeedback> 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 int sceneNumber; |
| | 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<PBPointerHoverFeedback> pointerEvents, |
| | 50 | | IECSInteractionHoverCanvas interactionHoverCanvas, |
| | 51 | | IWorldState worldState, |
| | 52 | | DataStore_ECS7 dataStoreEcs) |
| | 53 | | { |
| 16 | 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 | | }; |
| 39 | 67 | | return () => Update(state); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | private static void Update(State state) |
| | 71 | | { |
| 23 | 72 | | DataStore_ECS7.PointerEvent currentPointerInput = state.dataStoreEcs7.lastPointerInputEvent; |
| | 73 | |
|
| 23 | 74 | | bool isHit = state.dataStoreEcs7.lastPointerRayHit.hasValue && state.dataStoreEcs7.lastPointerRayHit.didHit; |
| 23 | 75 | | bool isPointerDown = currentPointerInput.hasValue && currentPointerInput.isButtonDown; |
| 23 | 76 | | bool isPointerUp = state.isLastInputPointerDown && currentPointerInput.hasValue && !currentPointerInput.isBu |
| | 77 | |
|
| 23 | 78 | | DataStore_ECS7.RaycastEvent.Hit raycastHit = state.dataStoreEcs7.lastPointerRayHit.hit; |
| 23 | 79 | | Ray raycastRay = state.dataStoreEcs7.lastPointerRayHit.ray; |
| | 80 | |
|
| 23 | 81 | | IECSReadOnlyComponentData<InternalColliders> colliderData = isHit |
| | 82 | | ? state.pointerColliderComponent |
| | 83 | | .GetEntityWithCollider(raycastHit.collider) |
| | 84 | | : null; |
| | 85 | |
|
| 23 | 86 | | bool isRaycastHitValidEntity = colliderData != null; |
| 23 | 87 | | bool isHoveringInput = !isPointerDown && !isPointerUp && !state.isLastInputPointerDown; |
| 23 | 88 | | bool isHoveringExit = !isRaycastHitValidEntity && state.lastInputHover.hasValue; |
| | 89 | |
|
| 23 | 90 | | IList<PBPointerHoverFeedback.Types.Entry> hoverEvents = isRaycastHitValidEntity |
| | 91 | | ? state.pointerEvents.GetPointerEventsForEntity(colliderData.scene, colliderData.entity) |
| | 92 | | : null; |
| | 93 | |
|
| | 94 | | // show hover tooltip for pointer down |
| 23 | 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.sceneNumber = colliderData.scene.sceneData.sceneNumber; |
| | 104 | | } |
| | 105 | | } |
| | 106 | | // show hover tooltip for pointer up |
| 20 | 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, (InputAction)curre |
| 3 | 112 | | state.lastHoverFeedback.hasValue = true; |
| 3 | 113 | | state.lastHoverFeedback.entity = colliderData.entity; |
| 3 | 114 | | state.lastHoverFeedback.scene = colliderData.scene; |
| 3 | 115 | | state.lastHoverFeedback.sceneNumber = colliderData.scene.sceneData.sceneNumber; |
| | 116 | | } |
| | 117 | | } |
| | 118 | | // hide hover tooltip |
| 17 | 119 | | else if (state.lastHoverFeedback.hasValue) |
| | 120 | | { |
| 0 | 121 | | state.interactionHoverCanvas.Hide(); |
| 0 | 122 | | state.lastHoverFeedback.hasValue = false; |
| | 123 | | } |
| | 124 | |
|
| 23 | 125 | | if (isRaycastHitValidEntity) |
| | 126 | | { |
| 21 | 127 | | InputHitType inputHitType = isPointerDown ? InputHitType.PointerDown |
| | 128 | | : isPointerUp ? InputHitType.PointerUp |
| | 129 | | : isHoveringInput ? InputHitType.PointerHover |
| | 130 | | : InputHitType.None; |
| | 131 | |
|
| | 132 | | // process entity hit with input |
| | 133 | | switch (inputHitType) |
| | 134 | | { |
| | 135 | | case InputHitType.PointerDown: |
| 9 | 136 | | state.lastInputDown.entity = colliderData.entity; |
| 9 | 137 | | state.lastInputDown.scene = colliderData.scene; |
| 9 | 138 | | state.lastInputDown.sceneNumber = colliderData.scene.sceneData.sceneNumber; |
| 9 | 139 | | state.lastInputDown.hasValue = true; |
| | 140 | |
|
| 9 | 141 | | AddInputResultEvent( |
| | 142 | | state, |
| | 143 | | currentPointerInput, |
| | 144 | | colliderData.scene, |
| | 145 | | colliderData.entity.entityId, |
| | 146 | | raycastRay, |
| | 147 | | raycastHit, |
| | 148 | | PointerEventType.PetDown |
| | 149 | | ); |
| 9 | 150 | | break; |
| | 151 | |
|
| | 152 | | case InputHitType.PointerUp: |
| 2 | 153 | | bool validInputDownExist = state.lastInputDown.hasValue; |
| 2 | 154 | | EntityInput lastInputDownData = state.lastInputDown; |
| | 155 | |
|
| | 156 | | // did it hit same entity as pointer down hit? |
| 2 | 157 | | if (validInputDownExist && colliderData.entity.entityId == lastInputDownData.entity.entityId |
| | 158 | | && colliderData.scene.sceneData.sceneNumber == lastInputDownData.sceneNu |
| | 159 | | { |
| 1 | 160 | | AddInputResultEvent( |
| | 161 | | state, |
| | 162 | | currentPointerInput, |
| | 163 | | colliderData.scene, |
| | 164 | | colliderData.entity.entityId, |
| | 165 | | raycastRay, |
| | 166 | | raycastHit, |
| | 167 | | PointerEventType.PetUp |
| | 168 | | ); |
| | 169 | | } |
| 1 | 170 | | else if (validInputDownExist) // did it hit different entity as pointer down hit? |
| | 171 | | { |
| 1 | 172 | | bool isEntityFromSameScene = colliderData.scene.sceneData.sceneNumber == lastInputDownData.s |
| 1 | 173 | | bool isValidScene = isEntityFromSameScene || state.worldState.ContainsScene(lastInputDownDat |
| | 174 | |
|
| 1 | 175 | | if (isValidScene) |
| | 176 | | { |
| 1 | 177 | | AddInputResultEvent( |
| | 178 | | state, |
| | 179 | | currentPointerInput, |
| | 180 | | lastInputDownData.scene, |
| | 181 | | -1, |
| | 182 | | raycastRay, |
| | 183 | | raycastHit, |
| | 184 | | PointerEventType.PetUp |
| | 185 | | ); |
| | 186 | | } |
| | 187 | | } |
| 2 | 188 | | state.lastInputDown.hasValue = false; |
| 2 | 189 | | break; |
| | 190 | |
|
| | 191 | | case InputHitType.PointerHover: |
| 7 | 192 | | bool isPreviouslyHoveredEntity = state.lastInputHover.hasValue; |
| 7 | 193 | | bool isHoveringNewEntity = !isPreviouslyHoveredEntity |
| | 194 | | || state.lastInputHover.entity.entityId != colliderData.entity.entity |
| | 195 | | || state.lastInputHover.sceneNumber != colliderData.scene.sceneData.s |
| | 196 | |
|
| | 197 | | // was other entity previously hovered? |
| 7 | 198 | | if (isPreviouslyHoveredEntity && isHoveringNewEntity) |
| | 199 | | { |
| 1 | 200 | | bool isValidScene = colliderData.scene.sceneData.sceneNumber == state.lastInputHover.sceneNu |
| | 201 | | || state.worldState.ContainsScene(state.lastInputHover.sceneNumber); |
| | 202 | |
|
| 1 | 203 | | if (isValidScene) |
| | 204 | | { |
| 1 | 205 | | AddInputResultEvent( |
| | 206 | | state, |
| | 207 | | currentPointerInput, |
| | 208 | | state.lastInputHover.scene, |
| | 209 | | state.lastInputHover.entity.entityId, |
| | 210 | | raycastRay, |
| | 211 | | raycastHit, |
| | 212 | | PointerEventType.PetHoverLeave |
| | 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.sceneNumber = colliderData.scene.sceneData.sceneNumber; |
| 7 | 223 | | state.lastInputHover.hasValue = true; |
| | 224 | |
|
| 7 | 225 | | AddInputResultEvent( |
| | 226 | | state, |
| | 227 | | currentPointerInput, |
| | 228 | | colliderData.scene, |
| | 229 | | colliderData.entity.entityId, |
| | 230 | | raycastRay, |
| | 231 | | raycastHit, |
| | 232 | | PointerEventType.PetHoverEnter |
| | 233 | | ); |
| | 234 | | } |
| 7 | 235 | | break; |
| | 236 | | } |
| | 237 | | } |
| | 238 | | else // no entity hit |
| | 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.sceneNumber); |
| 1 | 246 | | if (isValidScene) |
| | 247 | | { |
| 1 | 248 | | AddInputResultEvent( |
| | 249 | | state, |
| | 250 | | currentPointerInput, |
| | 251 | | state.lastInputDown.scene, |
| | 252 | | -1, |
| | 253 | | raycastRay, |
| | 254 | | raycastHit, |
| | 255 | | PointerEventType.PetUp |
| | 256 | | ); |
| | 257 | | } |
| | 258 | | } |
| 1 | 259 | | state.lastInputDown.hasValue = false; |
| | 260 | | } |
| | 261 | |
|
| 2 | 262 | | if (isHoveringExit) |
| | 263 | | { |
| 1 | 264 | | bool isValidScene = state.worldState.ContainsScene(state.lastInputHover.sceneNumber); |
| 1 | 265 | | if (isValidScene) |
| | 266 | | { |
| 1 | 267 | | AddInputResultEvent( |
| | 268 | | state, |
| | 269 | | currentPointerInput, |
| | 270 | | state.lastInputHover.scene, |
| | 271 | | state.lastInputHover.entity.entityId, |
| | 272 | | raycastRay, |
| | 273 | | raycastHit, |
| | 274 | | PointerEventType.PetHoverLeave |
| | 275 | | ); |
| | 276 | | } |
| 1 | 277 | | state.lastInputHover.hasValue = false; |
| | 278 | | } |
| | 279 | | } |
| | 280 | |
|
| 23 | 281 | | state.dataStoreEcs7.lastPointerInputEvent.hasValue = false; |
| 23 | 282 | | state.dataStoreEcs7.lastPointerRayHit.hasValue = false; |
| 23 | 283 | | state.isLastInputPointerDown = isPointerDown || (!isPointerUp && state.isLastInputPointerDown); |
| 23 | 284 | | } |
| | 285 | |
|
| | 286 | | private static void AddInputResultEvent(State state, DataStore_ECS7.PointerEvent pointerEvent, IParcelScene scen |
| | 287 | | long entityId, Ray ray, DataStore_ECS7.RaycastEvent.Hit raycastHit, PointerEventType pointerEventType) |
| | 288 | | { |
| 21 | 289 | | raycastHit.point = WorldStateUtils.ConvertUnityToScenePosition(raycastHit.point, scene); |
| 21 | 290 | | ray.origin = WorldStateUtils.ConvertUnityToScenePosition(ray.origin, scene); |
| | 291 | |
|
| 21 | 292 | | state.inputResultComponent.AddEvent(scene, new InternalInputEventResults.EventData() |
| | 293 | | { |
| | 294 | | analog = 1, |
| | 295 | | button = (InputAction)pointerEvent.buttonId, |
| | 296 | | hit = ProtoConvertUtils.ToPBRaycasHit(entityId, null, |
| | 297 | | ray, raycastHit.distance, raycastHit.point, raycastHit.normal, entityId != -1), |
| | 298 | | type = pointerEventType |
| | 299 | | }); |
| 21 | 300 | | } |
| | 301 | |
|
| | 302 | | private static IECSReadOnlyComponentData<InternalColliders> GetEntityWithCollider( |
| | 303 | | this IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 304 | | Collider collider) |
| | 305 | | { |
| 21 | 306 | | var collidersData = pointerColliderComponent.GetForAll(); |
| 54 | 307 | | for (int i = 0; i < collidersData.Count; i++) |
| | 308 | | { |
| 27 | 309 | | var colliderData = collidersData[i].value; |
| 27 | 310 | | if (colliderData.model.colliders.Contains(collider)) |
| | 311 | | { |
| 21 | 312 | | return colliderData; |
| | 313 | | } |
| | 314 | | } |
| 0 | 315 | | return null; |
| | 316 | | } |
| | 317 | |
|
| | 318 | | private static IList<PBPointerHoverFeedback.Types.Entry> GetPointerEventsForEntity(this ECSComponent<PBPointerHo |
| | 319 | | IParcelScene scene, IDCLEntity entity) |
| | 320 | | { |
| 21 | 321 | | var componentData = component.Get(scene, entity); |
| 21 | 322 | | return componentData?.model.PointerEvents; |
| | 323 | | } |
| | 324 | |
|
| | 325 | | private static void ShowPointerDownHover(this IECSInteractionHoverCanvas canvas, |
| | 326 | | IList<PBPointerHoverFeedback.Types.Entry> entityEvents, float distance) |
| | 327 | | { |
| 3 | 328 | | canvas.ShowHoverTooltips(entityEvents, (pointerEvent) => |
| 4 | 329 | | pointerEvent.EventType == PointerEventType.PetDown |
| | 330 | | && pointerEvent.EventInfo.GetShowFeedback() |
| | 331 | | && distance <= pointerEvent.EventInfo.GetMaxDistance() |
| | 332 | | ); |
| 3 | 333 | | } |
| | 334 | |
|
| | 335 | | private static void ShowPointerUpHover(this IECSInteractionHoverCanvas canvas, |
| | 336 | | IList<PBPointerHoverFeedback.Types.Entry> entityEvents, float distance, InputAction expectedButton) |
| | 337 | | { |
| 3 | 338 | | canvas.ShowHoverTooltips(entityEvents, (pointerEvent) => |
| 3 | 339 | | pointerEvent.EventType == PointerEventType.PetUp |
| | 340 | | && pointerEvent.EventInfo.GetShowFeedback() |
| | 341 | | && distance <= pointerEvent.EventInfo.GetMaxDistance() |
| | 342 | | && (pointerEvent.EventInfo.GetButton() == expectedButton || pointerEvent.EventInfo.GetButton() == InputA |
| | 343 | | ); |
| 3 | 344 | | } |
| | 345 | |
|
| | 346 | | private static void ShowHoverTooltips(this IECSInteractionHoverCanvas canvas, |
| | 347 | | IList<PBPointerHoverFeedback.Types.Entry> entityEvents, Func<PBPointerHoverFeedback.Types.Entry, bool> filte |
| | 348 | | { |
| 6 | 349 | | if (entityEvents is null) |
| 0 | 350 | | return; |
| | 351 | |
|
| 6 | 352 | | bool anyTooltipAdded = false; |
| 6 | 353 | | int eventIndex = 0; |
| 36 | 354 | | for (int i = 0; i < canvas.tooltipsCount; i++) |
| | 355 | | { |
| 12 | 356 | | PBPointerHoverFeedback.Types.Entry pointerEvent = null; |
| 18 | 357 | | for (; eventIndex < entityEvents.Count; eventIndex++) |
| | 358 | | { |
| 7 | 359 | | pointerEvent = entityEvents[eventIndex]; |
| 7 | 360 | | if (filter(pointerEvent)) |
| | 361 | | { |
| 4 | 362 | | eventIndex++; |
| 4 | 363 | | break; |
| | 364 | | } |
| 3 | 365 | | pointerEvent = null; |
| | 366 | | } |
| | 367 | |
|
| 12 | 368 | | if (!(pointerEvent is null)) |
| | 369 | | { |
| 4 | 370 | | anyTooltipAdded = true; |
| 4 | 371 | | canvas.SetTooltipText(i, pointerEvent.EventInfo.GetHoverText()); |
| 4 | 372 | | canvas.SetTooltipInput(i, pointerEvent.EventInfo.GetButton()); |
| 4 | 373 | | canvas.SetTooltipActive(i, true); |
| | 374 | | } |
| | 375 | | else |
| | 376 | | { |
| 8 | 377 | | canvas.SetTooltipActive(i, false); |
| | 378 | | } |
| | 379 | | } |
| | 380 | |
|
| 6 | 381 | | if (anyTooltipAdded) |
| | 382 | | { |
| 3 | 383 | | canvas.Show(); |
| | 384 | | } |
| 6 | 385 | | } |
| | 386 | | } |
| | 387 | | } |