| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using DCL.Interface; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using UnityEngine; |
| | 10 | | using RaycastHit = DCL.ECSComponents.RaycastHit; |
| | 11 | |
|
| | 12 | | namespace ECSSystems.PointerInputSystem |
| | 13 | | { |
| | 14 | | public class ECSPointerInputSystem |
| | 15 | | { |
| 1 | 16 | | private static readonly InputAction[] INPUT_ACTION_ENUM = (InputAction[])Enum.GetValues(typeof(WebInterface.ACTI |
| | 17 | |
|
| | 18 | | private readonly IInternalECSComponent<InternalColliders> pointerColliderComponent; |
| | 19 | | private readonly IInternalECSComponent<InternalInputEventResults> inputResultComponent; |
| | 20 | | private readonly IInternalECSComponent<InternalPointerEvents> pointerEvents; |
| | 21 | | private readonly DataStore_ECS7 dataStoreEcs7; |
| | 22 | | private readonly EntityInput lastHoverFeedback; |
| | 23 | | private readonly IWorldState worldState; |
| | 24 | | private readonly IECSInteractionHoverCanvas interactionHoverCanvas; |
| | 25 | | private readonly bool[] inputActionState; |
| | 26 | |
|
| | 27 | | private class EntityInput |
| | 28 | | { |
| | 29 | | public long entityId; |
| | 30 | | public IParcelScene scene; |
| | 31 | | public int sceneNumber; |
| | 32 | | public bool hasValue; |
| | 33 | | public IReadOnlyList<InternalPointerEvents.Entry> pointerEvents; |
| | 34 | | } |
| | 35 | |
|
| 31 | 36 | | public ECSPointerInputSystem( |
| | 37 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 38 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 39 | | IInternalECSComponent<InternalPointerEvents> pointerEvents, |
| | 40 | | IECSInteractionHoverCanvas interactionHoverCanvas, |
| | 41 | | IWorldState worldState, |
| | 42 | | DataStore_ECS7 dataStoreEcs) |
| | 43 | | { |
| 31 | 44 | | this.pointerColliderComponent = pointerColliderComponent; |
| 31 | 45 | | this.inputResultComponent = inputResultComponent; |
| 31 | 46 | | this.pointerEvents = pointerEvents; |
| 31 | 47 | | this.worldState = worldState; |
| 31 | 48 | | this.interactionHoverCanvas = interactionHoverCanvas; |
| 31 | 49 | | this.dataStoreEcs7 = dataStoreEcs; |
| 31 | 50 | | this.lastHoverFeedback = new EntityInput() { hasValue = false }; |
| 31 | 51 | | this.inputActionState = new bool[INPUT_ACTION_ENUM.Length]; |
| 31 | 52 | | } |
| | 53 | |
|
| | 54 | | public void Update() |
| | 55 | | { |
| | 56 | | // Retrieve the last raycast hit |
| 43 | 57 | | bool doesRaycastHit = dataStoreEcs7.lastPointerRayHit.hasValue && dataStoreEcs7.lastPointerRayHit.didHit; |
| 43 | 58 | | DataStore_ECS7.RaycastEvent raycastEvent = dataStoreEcs7.lastPointerRayHit; |
| 43 | 59 | | DataStore_ECS7.RaycastEvent.Hit raycastHit = raycastEvent.hit; |
| 43 | 60 | | Ray raycastRay = raycastEvent.ray; |
| 43 | 61 | | IReadOnlyList<IParcelScene> loadedScenes = dataStoreEcs7.scenes; |
| | 62 | |
|
| | 63 | | // Get the collider that the raycast hit |
| 43 | 64 | | ECSComponentData<InternalColliders>? colliderData = doesRaycastHit |
| | 65 | | ? GetEntityWithCollider(pointerColliderComponent, raycastHit.collider) |
| | 66 | | : null; |
| | 67 | |
|
| 43 | 68 | | IParcelScene colliderScene = colliderData?.scene; |
| | 69 | |
|
| 43 | 70 | | IReadOnlyList<InternalPointerEvents.Entry> entityPointerEvents = colliderData != null |
| | 71 | | ? pointerEvents.GetFor(colliderData.Value.scene, colliderData.Value.entity)?.model.PointerEvents |
| | 72 | | : null; |
| | 73 | |
|
| 43 | 74 | | bool isAnyButtonDown = false; |
| | 75 | |
|
| | 76 | | // Emit command for button states |
| 43 | 77 | | bool[] curState = dataStoreEcs7.inputActionState; |
| 43 | 78 | | bool[] prevState = inputActionState; |
| | 79 | |
|
| 1290 | 80 | | for (int i = 0; i < curState.Length; i++) |
| | 81 | | { |
| 602 | 82 | | isAnyButtonDown |= curState[i]; |
| | 83 | |
|
| 602 | 84 | | if (curState[i] != prevState[i]) |
| | 85 | | { |
| 31 | 86 | | PointerEventType pointerEventType = curState[i] ? PointerEventType.PetDown : PointerEventType.PetUp; |
| 31 | 87 | | InputAction inputAction = INPUT_ACTION_ENUM[i]; |
| | 88 | |
|
| 31 | 89 | | if (colliderData != null) |
| | 90 | | { |
| 31 | 91 | | AddInputResultEvent( |
| | 92 | | inputResultComponent, |
| | 93 | | inputAction, |
| | 94 | | colliderData.Value.scene, |
| | 95 | | colliderData.Value.entity.entityId, |
| | 96 | | raycastRay, |
| | 97 | | raycastHit, |
| | 98 | | pointerEventType, |
| | 99 | | entityPointerEvents |
| | 100 | | ); |
| | 101 | | } |
| | 102 | |
|
| 31 | 103 | | BroadcastInputResultEvent( |
| | 104 | | inputResultComponent, |
| | 105 | | loadedScenes, |
| | 106 | | inputAction, |
| | 107 | | raycastRay, |
| | 108 | | raycastHit, |
| | 109 | | pointerEventType, |
| | 110 | | colliderScene |
| | 111 | | ); |
| | 112 | |
|
| | 113 | | // update |
| 31 | 114 | | prevState[i] = curState[i]; |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| | 118 | | // Check if the hovered entity has changed with three options: |
| | 119 | | // 1) We were hitting a collider A and now we're hitting a collider B |
| 43 | 120 | | if (IsColliderDifferent(lastHoverFeedback, colliderData)) |
| | 121 | | { |
| 5 | 122 | | HandleColliderChanged(colliderData.Value, lastHoverFeedback, raycastEvent, interactionHoverCanvas, input |
| | 123 | | } |
| | 124 | |
|
| | 125 | | // 2) We were hitting a collider A and now we're not hitting anything |
| 38 | 126 | | else if (IsColliderMissing(lastHoverFeedback, colliderData)) |
| | 127 | | { |
| 0 | 128 | | HandleMissingCollider(lastHoverFeedback, raycastEvent, interactionHoverCanvas, inputResultComponent, wor |
| | 129 | | } |
| | 130 | |
|
| | 131 | | // 3) We were not hitting anything and now we're hitting collider A |
| 38 | 132 | | else if (IsColliderAvailable(lastHoverFeedback, colliderData)) |
| | 133 | | { |
| 29 | 134 | | HandleAvailableCollider(colliderData.Value, lastHoverFeedback, raycastEvent, inputResultComponent, entit |
| | 135 | | } |
| | 136 | |
|
| 43 | 137 | | if (entityPointerEvents != null) |
| | 138 | | { |
| 30 | 139 | | ShowHoverTooltips(interactionHoverCanvas, entityPointerEvents, curState, raycastHit.distance, isAnyButto |
| | 140 | | } |
| 43 | 141 | | } |
| | 142 | |
|
| | 143 | | private static bool IsColliderDifferent(EntityInput lastHoverFeedback, ECSComponentData<InternalColliders>? coll |
| | 144 | | { |
| 43 | 145 | | return colliderData != null && // current collider |
| | 146 | | lastHoverFeedback.hasValue && // previous collider |
| | 147 | | (lastHoverFeedback.entityId != colliderData.Value.entity.entityId || |
| | 148 | | lastHoverFeedback.sceneNumber != colliderData.Value.scene.sceneData.sceneNumber); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | private static bool IsColliderMissing(EntityInput lastHoverFeedback, ECSComponentData<InternalColliders>? collid |
| | 152 | | { |
| 38 | 153 | | return colliderData == null && lastHoverFeedback.hasValue; |
| | 154 | | } |
| | 155 | |
|
| | 156 | | private static bool IsColliderAvailable(EntityInput lastHoverFeedback, ECSComponentData<InternalColliders>? coll |
| | 157 | | { |
| 38 | 158 | | return colliderData != null && !lastHoverFeedback.hasValue; |
| | 159 | | } |
| | 160 | |
|
| | 161 | | private static void HandleColliderChanged( |
| | 162 | | ECSComponentData<InternalColliders> colliderData, |
| | 163 | | EntityInput lastHoverFeedback, |
| | 164 | | DataStore_ECS7.RaycastEvent lastPointerRayHit, |
| | 165 | | IECSInteractionHoverCanvas interactionHoverCanvas, |
| | 166 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 167 | | IWorldState worldState, |
| | 168 | | IReadOnlyList<InternalPointerEvents.Entry> entityEvents) |
| | 169 | | { |
| 5 | 170 | | DataStore_ECS7.RaycastEvent.Hit raycastHit = lastPointerRayHit.hit; |
| 5 | 171 | | Ray raycastRay = lastPointerRayHit.ray; |
| | 172 | |
|
| 5 | 173 | | if (worldState.ContainsScene(lastHoverFeedback.sceneNumber)) |
| | 174 | | { |
| 5 | 175 | | AddInputResultEvent( |
| | 176 | | inputResultComponent, |
| | 177 | | InputAction.IaPointer, |
| | 178 | | lastHoverFeedback.scene, |
| | 179 | | lastHoverFeedback.entityId, |
| | 180 | | raycastRay, |
| | 181 | | raycastHit, |
| | 182 | | PointerEventType.PetHoverLeave, |
| | 183 | | lastHoverFeedback.pointerEvents |
| | 184 | | ); |
| | 185 | | } |
| | 186 | |
|
| 5 | 187 | | AddInputResultEvent( |
| | 188 | | inputResultComponent, |
| | 189 | | InputAction.IaPointer, |
| | 190 | | colliderData.scene, |
| | 191 | | colliderData.entity.entityId, |
| | 192 | | raycastRay, |
| | 193 | | raycastHit, |
| | 194 | | PointerEventType.PetHoverEnter, |
| | 195 | | entityEvents |
| | 196 | | ); |
| | 197 | |
|
| 5 | 198 | | interactionHoverCanvas.Hide(); |
| | 199 | |
|
| 5 | 200 | | lastHoverFeedback.hasValue = true; |
| 5 | 201 | | lastHoverFeedback.sceneNumber = colliderData.scene.sceneData.sceneNumber; |
| 5 | 202 | | lastHoverFeedback.scene = colliderData.scene; |
| 5 | 203 | | lastHoverFeedback.entityId = colliderData.entity.entityId; |
| 5 | 204 | | lastHoverFeedback.pointerEvents = entityEvents; |
| 5 | 205 | | } |
| | 206 | |
|
| | 207 | | private static void HandleMissingCollider( |
| | 208 | | EntityInput lastHoverFeedback, |
| | 209 | | DataStore_ECS7.RaycastEvent lastPointerRayHit, |
| | 210 | | IECSInteractionHoverCanvas interactionHoverCanvas, |
| | 211 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 212 | | IWorldState worldState) |
| | 213 | | { |
| 0 | 214 | | DataStore_ECS7.RaycastEvent.Hit raycastHit = lastPointerRayHit.hit; |
| 0 | 215 | | Ray raycastRay = lastPointerRayHit.ray; |
| | 216 | |
|
| 0 | 217 | | if (worldState.ContainsScene(lastHoverFeedback.sceneNumber)) |
| | 218 | | { |
| 0 | 219 | | AddInputResultEvent( |
| | 220 | | inputResultComponent, |
| | 221 | | InputAction.IaPointer, |
| | 222 | | lastHoverFeedback.scene, |
| | 223 | | lastHoverFeedback.entityId, |
| | 224 | | raycastRay, |
| | 225 | | raycastHit, |
| | 226 | | PointerEventType.PetHoverLeave, |
| | 227 | | lastHoverFeedback.pointerEvents |
| | 228 | | ); |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | interactionHoverCanvas.Hide(); |
| 0 | 232 | | lastHoverFeedback.hasValue = false; |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | private static void HandleAvailableCollider( |
| | 236 | | ECSComponentData<InternalColliders> colliderData, |
| | 237 | | EntityInput lastHoverFeedback, |
| | 238 | | DataStore_ECS7.RaycastEvent lastPointerRayHit, |
| | 239 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 240 | | IReadOnlyList<InternalPointerEvents.Entry> entityEvents) |
| | 241 | | { |
| 29 | 242 | | DataStore_ECS7.RaycastEvent.Hit raycastHit = lastPointerRayHit.hit; |
| 29 | 243 | | Ray raycastRay = lastPointerRayHit.ray; |
| | 244 | |
|
| 29 | 245 | | AddInputResultEvent( |
| | 246 | | inputResultComponent, |
| | 247 | | InputAction.IaPointer, |
| | 248 | | colliderData.scene, |
| | 249 | | colliderData.entity.entityId, |
| | 250 | | raycastRay, |
| | 251 | | raycastHit, |
| | 252 | | PointerEventType.PetHoverEnter, |
| | 253 | | entityEvents |
| | 254 | | ); |
| | 255 | |
|
| 29 | 256 | | lastHoverFeedback.hasValue = true; |
| 29 | 257 | | lastHoverFeedback.sceneNumber = colliderData.scene.sceneData.sceneNumber; |
| 29 | 258 | | lastHoverFeedback.scene = colliderData.scene; |
| 29 | 259 | | lastHoverFeedback.entityId = colliderData.entity.entityId; |
| 29 | 260 | | lastHoverFeedback.pointerEvents = entityEvents; |
| 29 | 261 | | } |
| | 262 | |
|
| | 263 | | // Sent input result to other scenes |
| | 264 | | private static void BroadcastInputResultEvent( |
| | 265 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 266 | | IReadOnlyList<IParcelScene> scenes, |
| | 267 | | InputAction buttonId, |
| | 268 | | Ray ray, |
| | 269 | | DataStore_ECS7.RaycastEvent.Hit raycastHit, |
| | 270 | | PointerEventType pointerEventType, |
| | 271 | | IParcelScene skipScene = null) |
| | 272 | | { |
| 146 | 273 | | for (int i = 0; i < scenes.Count; i++) |
| | 274 | | { |
| 42 | 275 | | if (scenes[i] != skipScene) |
| | 276 | | { |
| 13 | 277 | | AddInputResultEvent(inputResultComponent, buttonId, scenes[i], -1, ray, raycastHit, pointerEventType |
| | 278 | | } |
| | 279 | | } |
| 31 | 280 | | } |
| | 281 | |
|
| | 282 | | private static void AddInputResultEvent( |
| | 283 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 284 | | InputAction buttonId, |
| | 285 | | IParcelScene scene, |
| | 286 | | long entityId, |
| | 287 | | Ray ray, |
| | 288 | | DataStore_ECS7.RaycastEvent.Hit raycastHit, |
| | 289 | | PointerEventType pointerEventType, |
| | 290 | | IReadOnlyList<InternalPointerEvents.Entry> entityEvents) |
| | 291 | | { |
| 83 | 292 | | RaycastHit hitInfo = null; |
| | 293 | |
|
| | 294 | | // If entity has pointer event component for this `pointerEventType` we setup the `hit` data |
| | 295 | | // otherwise we leave it empty (global input) |
| 83 | 296 | | if (HasInputEvent(entityEvents, pointerEventType, buttonId, raycastHit.distance)) |
| | 297 | | { |
| 15 | 298 | | ray.origin = WorldStateUtils.ConvertUnityToScenePosition(ray.origin, scene); |
| | 299 | |
|
| 15 | 300 | | hitInfo = ProtoConvertUtils.ToPBRaycasHit( |
| | 301 | | entityId, |
| | 302 | | null, |
| | 303 | | ray, |
| | 304 | | raycastHit.distance, |
| | 305 | | WorldStateUtils.ConvertUnityToScenePosition(raycastHit.point, scene), |
| | 306 | | raycastHit.normal); |
| | 307 | | } |
| | 308 | |
|
| | 309 | | // If entity does not have pointer event component for this `pointerEventType` we ignore the event |
| | 310 | | // so it's not send to the scene |
| 68 | 311 | | else if (pointerEventType == PointerEventType.PetHoverEnter || pointerEventType == PointerEventType.PetHover |
| | 312 | | { |
| 37 | 313 | | return; |
| | 314 | | } |
| | 315 | |
|
| 46 | 316 | | inputResultComponent.AddEvent(scene, new InternalInputEventResults.EventData() |
| | 317 | | { |
| | 318 | | button = buttonId, |
| | 319 | | hit = hitInfo, |
| | 320 | | type = pointerEventType |
| | 321 | | }); |
| 46 | 322 | | } |
| | 323 | |
|
| | 324 | | private static ECSComponentData<InternalColliders>? GetEntityWithCollider( |
| | 325 | | IInternalECSComponent<InternalColliders> pointerColliderComponent, |
| | 326 | | Collider collider) |
| | 327 | | { |
| 43 | 328 | | var collidersData = pointerColliderComponent.GetForAll(); |
| | 329 | |
|
| 104 | 330 | | for (int i = 0; i < collidersData.Count; i++) |
| | 331 | | { |
| 52 | 332 | | var colliderData = collidersData[i].value; |
| | 333 | |
|
| 52 | 334 | | if (colliderData.model.colliders.ContainsKey(collider)) |
| | 335 | | { |
| 43 | 336 | | return colliderData; |
| | 337 | | } |
| | 338 | | } |
| | 339 | |
|
| 0 | 340 | | return null; |
| | 341 | | } |
| | 342 | |
|
| | 343 | | private static void ShowHoverTooltips(IECSInteractionHoverCanvas canvas, |
| | 344 | | IReadOnlyList<InternalPointerEvents.Entry> entityEvents, bool[] buttonState, float distance, bool isAnyButto |
| | 345 | | { |
| 30 | 346 | | if (entityEvents is null) |
| 0 | 347 | | return; |
| | 348 | |
|
| 30 | 349 | | int tooltipIndex = -1; |
| 30 | 350 | | bool shouldAdd = false; |
| | 351 | | InternalPointerEvents.Entry pointerEvent; |
| | 352 | |
|
| 134 | 353 | | for (int eventIndex = 0; eventIndex < entityEvents.Count; eventIndex++) |
| | 354 | | { |
| 37 | 355 | | shouldAdd = false; |
| 37 | 356 | | pointerEvent = entityEvents[eventIndex]; |
| | 357 | |
|
| 37 | 358 | | if (!pointerEvent.EventInfo.ShowFeedback || distance > pointerEvent.EventInfo.MaxDistance) |
| | 359 | | continue; |
| | 360 | |
|
| 9 | 361 | | int buttonId = (int)pointerEvent.EventInfo.Button; |
| | 362 | |
|
| 9 | 363 | | if (buttonId == (int)InputAction.IaAny) |
| | 364 | | { |
| 2 | 365 | | if ( |
| | 366 | | (isAnyButtonDown && pointerEvent.EventType == PointerEventType.PetUp) || |
| | 367 | | (!isAnyButtonDown && pointerEvent.EventType == PointerEventType.PetDown)) |
| | 368 | | { |
| 1 | 369 | | shouldAdd = true; |
| | 370 | | } |
| | 371 | | } |
| 7 | 372 | | else if (buttonId >= 0 && buttonId < buttonState.Length) |
| | 373 | | { |
| 7 | 374 | | bool buttonIsDown = buttonState[buttonId]; |
| | 375 | |
|
| 7 | 376 | | if ((pointerEvent.EventType == PointerEventType.PetDown && !buttonIsDown) |
| | 377 | | || (pointerEvent.EventType == PointerEventType.PetUp && buttonIsDown)) |
| | 378 | | { |
| 4 | 379 | | shouldAdd = true; |
| | 380 | | } |
| | 381 | | } |
| | 382 | |
|
| 9 | 383 | | if (shouldAdd) |
| | 384 | | { |
| 5 | 385 | | tooltipIndex++; |
| 5 | 386 | | canvas.SetTooltipText(tooltipIndex, pointerEvent.EventInfo.HoverText); |
| 5 | 387 | | canvas.SetTooltipInput(tooltipIndex, pointerEvent.EventInfo.Button); |
| 5 | 388 | | canvas.SetTooltipActive(tooltipIndex, true); |
| | 389 | | } |
| | 390 | | } |
| | 391 | |
|
| | 392 | | // first tooltip free |
| 86 | 393 | | for (int i = tooltipIndex + 1; i < canvas.tooltipsCount; i++) |
| | 394 | | { |
| 13 | 395 | | canvas.SetTooltipActive(i, false); |
| | 396 | | } |
| | 397 | |
|
| 30 | 398 | | if (tooltipIndex != -1) |
| | 399 | | { |
| 5 | 400 | | canvas.Show(); |
| | 401 | | } |
| | 402 | | else |
| | 403 | | { |
| 25 | 404 | | canvas.Hide(); |
| | 405 | | } |
| 25 | 406 | | } |
| | 407 | |
|
| | 408 | | private static bool IsValidInputForUnlockingUiPrompts(InputAction inputAction) |
| | 409 | | { |
| 0 | 410 | | return inputAction == InputAction.IaPointer |
| | 411 | | || inputAction == InputAction.IaPrimary |
| | 412 | | || inputAction == InputAction.IaSecondary |
| | 413 | | || inputAction == InputAction.IaAction3 |
| | 414 | | || inputAction == InputAction.IaAction4 |
| | 415 | | || inputAction == InputAction.IaAction5 |
| | 416 | | || inputAction == InputAction.IaAction6; |
| | 417 | | } |
| | 418 | |
|
| | 419 | | private static bool HasInputEvent( |
| | 420 | | IReadOnlyList<InternalPointerEvents.Entry> entityEvents, |
| | 421 | | PointerEventType pointerEventType, |
| | 422 | | InputAction actionButton, |
| | 423 | | float distance) |
| | 424 | | { |
| 83 | 425 | | if (entityEvents == null) |
| 35 | 426 | | return false; |
| | 427 | |
|
| 178 | 428 | | for (int i = 0; i < entityEvents.Count; i++) |
| | 429 | | { |
| 56 | 430 | | var inputEventEntry = entityEvents[i]; |
| | 431 | |
|
| 56 | 432 | | if (inputEventEntry.EventInfo.Button != actionButton |
| | 433 | | && inputEventEntry.EventInfo.Button != InputAction.IaAny) |
| | 434 | | continue; |
| | 435 | |
|
| 33 | 436 | | if (inputEventEntry.EventType != pointerEventType) |
| | 437 | | continue; |
| | 438 | |
|
| 20 | 439 | | if (distance <= inputEventEntry.EventInfo.MaxDistance) |
| 15 | 440 | | return true; |
| | 441 | | } |
| | 442 | |
|
| 33 | 443 | | return false; |
| | 444 | | } |
| | 445 | | } |
| | 446 | | } |