| | 1 | | using System; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using DCL.Models; |
| | 12 | | using DCLPlugins.UUIDEventComponentsPlugin.UUIDComponent.Interfaces; |
| | 13 | | using Ray = UnityEngine.Ray; |
| | 14 | |
|
| | 15 | | namespace DCL |
| | 16 | | { |
| | 17 | | public class PointerEventsController |
| | 18 | | { |
| 0 | 19 | | private static bool renderingEnabled => CommonScriptableObjects.rendererState.Get(); |
| | 20 | | public System.Action OnPointerHoverStarts; |
| | 21 | | public System.Action OnPointerHoverEnds; |
| | 22 | |
|
| | 23 | | RaycastHitInfo lastPointerDownEventHitInfo; |
| | 24 | | IPointerInputEvent pointerInputUpEvent; |
| 60 | 25 | | IRaycastHandler raycastHandler = new RaycastHandler(); |
| | 26 | |
|
| | 27 | | Camera charCamera; |
| | 28 | |
|
| | 29 | | GameObject lastHoveredObject = null; |
| | 30 | | GameObject newHoveredGO = null; |
| | 31 | |
|
| | 32 | | IPointerEvent newHoveredInputEvent = null; |
| | 33 | | IList<IPointerEvent> lastHoveredEventList = null; |
| | 34 | |
|
| | 35 | | RaycastHit hitInfo; |
| 60 | 36 | | PointerEventData uiGraphicRaycastPointerEventData = new PointerEventData(null); |
| 60 | 37 | | List<RaycastResult> uiGraphicRaycastResults = new List<RaycastResult>(); |
| | 38 | | GraphicRaycaster uiGraphicRaycaster; |
| | 39 | |
|
| | 40 | | private IRaycastPointerClickHandler clickHandler; |
| | 41 | | private InputController_Legacy inputControllerLegacy; |
| | 42 | | private InteractionHoverCanvasController hoverCanvas; |
| | 43 | |
|
| 60 | 44 | | public PointerEventsController(InputController_Legacy inputControllerLegacy, |
| | 45 | | InteractionHoverCanvasController hoverCanvas) |
| | 46 | | { |
| 60 | 47 | | this.inputControllerLegacy = inputControllerLegacy; |
| 60 | 48 | | this.hoverCanvas = hoverCanvas; |
| | 49 | |
|
| 1800 | 50 | | for (int i = 0; i < Enum.GetValues(typeof(WebInterface.ACTION_BUTTON)).Length; i++) |
| | 51 | | { |
| 840 | 52 | | var buttonId = (WebInterface.ACTION_BUTTON) i; |
| | 53 | |
|
| 840 | 54 | | if (buttonId == WebInterface.ACTION_BUTTON.ANY) |
| | 55 | | continue; |
| | 56 | |
|
| 780 | 57 | | inputControllerLegacy.AddListener(buttonId, OnButtonEvent); |
| | 58 | | } |
| | 59 | |
|
| 60 | 60 | | OnPointerHoverStarts += SetHoverCursor; |
| 60 | 61 | | OnPointerHoverEnds += SetNormalCursor; |
| | 62 | |
|
| 60 | 63 | | RetrieveCamera(); |
| | 64 | |
|
| 60 | 65 | | Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| 60 | 66 | | Utils.OnCursorLockChanged += HandleCursorLockChanges; |
| | 67 | |
|
| 60 | 68 | | HideOrShowCursor(Utils.IsCursorLocked); |
| 60 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Update() |
| | 72 | | { |
| 18018 | 73 | | if (charCamera == null) |
| 5 | 74 | | RetrieveCamera(); |
| | 75 | |
|
| 18018 | 76 | | if (!CommonScriptableObjects.rendererState.Get() || charCamera == null) |
| 0 | 77 | | return; |
| | 78 | |
|
| 18018 | 79 | | if (!Utils.IsCursorLocked) |
| 865 | 80 | | return; |
| | 81 | |
|
| 17153 | 82 | | IWorldState worldState = Environment.i.world.state; |
| | 83 | |
|
| | 84 | | // We use Physics.Raycast() instead of our raycastHandler.Raycast() as that one is slower, sometimes 2x, bec |
| 17153 | 85 | | bool didHit = Physics.Raycast(GetRayFromCamera(), out hitInfo, Mathf.Infinity, |
| | 86 | | PhysicsLayers.physicsCastLayerMaskWithoutCharacter); |
| | 87 | |
|
| 17153 | 88 | | bool uiIsBlocking = false; |
| 17153 | 89 | | string currentSceneId = worldState.GetCurrentSceneId(); |
| | 90 | |
|
| 17153 | 91 | | bool validCurrentSceneId = !string.IsNullOrEmpty(currentSceneId); |
| 17153 | 92 | | bool validCurrentScene = validCurrentSceneId && worldState.ContainsScene(currentSceneId); |
| | 93 | |
|
| | 94 | | // NOTE: in case of a single scene loaded (preview or builder) sceneId is set to null when stepping outside |
| 17153 | 95 | | if (didHit && validCurrentSceneId && validCurrentScene) |
| | 96 | | { |
| 16679 | 97 | | DataStore_World worldData = DataStore.i.Get<DataStore_World>(); |
| 16679 | 98 | | GraphicRaycaster raycaster = worldData.currentRaycaster.Get(); |
| | 99 | |
|
| 16679 | 100 | | if (raycaster) |
| | 101 | | { |
| 6 | 102 | | uiGraphicRaycastPointerEventData.position = new Vector2(Screen.width / 2, Screen.height / 2); |
| 6 | 103 | | uiGraphicRaycastResults.Clear(); |
| 6 | 104 | | raycaster.Raycast(uiGraphicRaycastPointerEventData, uiGraphicRaycastResults); |
| 6 | 105 | | uiIsBlocking = uiGraphicRaycastResults.Count > 0; |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| 17153 | 109 | | if (!didHit || uiIsBlocking) |
| | 110 | | { |
| 476 | 111 | | clickHandler = null; |
| 476 | 112 | | UnhoverLastHoveredObject(); |
| | 113 | |
|
| 476 | 114 | | return; |
| | 115 | | } |
| | 116 | |
|
| 16677 | 117 | | var raycastHandlerTarget = hitInfo.collider.GetComponent<IRaycastPointerHandler>(); |
| | 118 | |
|
| 16677 | 119 | | if (raycastHandlerTarget != null) |
| | 120 | | { |
| 0 | 121 | | ResolveGenericRaycastHandlers(raycastHandlerTarget); |
| 0 | 122 | | UnhoverLastHoveredObject(); |
| | 123 | |
|
| 0 | 124 | | return; |
| | 125 | | } |
| | 126 | |
|
| 16677 | 127 | | if (CollidersManager.i.GetColliderInfo(hitInfo.collider, out ColliderInfo info)) |
| 16677 | 128 | | newHoveredInputEvent = GetPointerEvent(info.entity); |
| | 129 | | else |
| 0 | 130 | | newHoveredInputEvent = hitInfo.collider.GetComponentInChildren<IPointerEvent>(); |
| | 131 | |
|
| 16677 | 132 | | clickHandler = null; |
| | 133 | |
|
| 16677 | 134 | | if (!EventObjectCanBeHovered(info, hitInfo.distance)) |
| | 135 | | { |
| 17 | 136 | | UnhoverLastHoveredObject(); |
| | 137 | |
|
| 17 | 138 | | return; |
| | 139 | | } |
| | 140 | |
|
| 16660 | 141 | | newHoveredGO = newHoveredInputEvent.GetTransform().gameObject; |
| | 142 | |
|
| 16660 | 143 | | if (newHoveredGO != lastHoveredObject) |
| | 144 | | { |
| 18 | 145 | | UnhoverLastHoveredObject(); |
| | 146 | |
|
| 18 | 147 | | lastHoveredObject = newHoveredGO; |
| | 148 | |
|
| 18 | 149 | | lastHoveredEventList = GetPointerEventList(newHoveredInputEvent.entity); |
| | 150 | |
|
| | 151 | | // NOTE: this case is for the Avatar, since it hierarchy differs from other ECS components |
| 18 | 152 | | if (lastHoveredEventList?.Count == 0) |
| | 153 | | { |
| 0 | 154 | | lastHoveredEventList = newHoveredGO.GetComponents<IPointerEvent>(); |
| | 155 | | } |
| | 156 | |
|
| 18 | 157 | | OnPointerHoverStarts?.Invoke(); |
| | 158 | | } |
| | 159 | |
|
| | 160 | | // OnPointerDown/OnClick and OnPointerUp should display their hover feedback at different moments |
| 16660 | 161 | | if (lastHoveredEventList != null && lastHoveredEventList.Count > 0) |
| | 162 | | { |
| 16660 | 163 | | bool isEntityShowingHoverFeedback = false; |
| | 164 | |
|
| 66642 | 165 | | for (int i = 0; i < lastHoveredEventList.Count; i++) |
| | 166 | | { |
| 16661 | 167 | | if (lastHoveredEventList[i] is IPointerInputEvent e) |
| | 168 | | { |
| 16653 | 169 | | bool eventButtonIsPressed = inputControllerLegacy.IsPressed(e.GetActionButton()); |
| | 170 | |
|
| 16653 | 171 | | bool isClick = e.GetEventType() == PointerInputEventType.CLICK; |
| 16653 | 172 | | bool isDown = e.GetEventType() == PointerInputEventType.DOWN; |
| 16653 | 173 | | bool isUp = e.GetEventType() == PointerInputEventType.UP; |
| | 174 | |
|
| 16653 | 175 | | if (isUp && eventButtonIsPressed) |
| | 176 | | { |
| 0 | 177 | | e.SetHoverState(true); |
| 0 | 178 | | isEntityShowingHoverFeedback = isEntityShowingHoverFeedback || e.ShouldShowHoverFeedback(); |
| | 179 | | } |
| 16653 | 180 | | else if ((isDown || isClick) && !eventButtonIsPressed) |
| | 181 | | { |
| 16651 | 182 | | e.SetHoverState(true); |
| 16651 | 183 | | isEntityShowingHoverFeedback = isEntityShowingHoverFeedback || e.ShouldShowHoverFeedback(); |
| | 184 | | } |
| 2 | 185 | | else if (!isEntityShowingHoverFeedback) |
| | 186 | | { |
| 2 | 187 | | e.SetHoverState(false); |
| | 188 | | } |
| | 189 | | } |
| | 190 | | else |
| | 191 | | { |
| 8 | 192 | | lastHoveredEventList[i].SetHoverState(true); |
| | 193 | | } |
| | 194 | | } |
| | 195 | | } |
| | 196 | |
|
| 16660 | 197 | | newHoveredGO = null; |
| 16660 | 198 | | newHoveredInputEvent = null; |
| 16660 | 199 | | } |
| | 200 | |
|
| | 201 | | private IList<IPointerEvent> GetPointerEventList(IDCLEntity entity) |
| | 202 | | { |
| | 203 | | // If an event exist in the new ECS, we got that value, if not it is ECS 6, so we continue as before |
| 18 | 204 | | if (DataStore.i.ecs7.entityEvents.TryGetValue(entity.entityId, out List<IPointerInputEvent> pointerInputEven |
| | 205 | | { |
| 0 | 206 | | return pointerInputEvent.Cast<IPointerEvent>().ToList(); |
| | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| 18 | 210 | | var lastHoveredEventList = newHoveredInputEvent.entity.gameObject.transform.Cast<Transform>() |
| 37 | 211 | | .Select(child => child.GetComponent<IPointerEvent>()) |
| 37 | 212 | | .Where(pointerComponent => pointerComponent != null) |
| | 213 | | .ToArray(); |
| | 214 | |
|
| 18 | 215 | | return lastHoveredEventList; |
| | 216 | | } |
| | 217 | | } |
| | 218 | |
|
| | 219 | | private IPointerEvent GetPointerEvent(IDCLEntity entity) |
| | 220 | | { |
| | 221 | | // If an event exist in the new ECS, we got that value, if not it is ECS 6, so we continue as before |
| 16677 | 222 | | if (DataStore.i.ecs7.entityEvents.TryGetValue(entity.entityId, out List<IPointerInputEvent> pointerInputEven |
| 0 | 223 | | return pointerInputEvent.First(); |
| | 224 | | else |
| 16677 | 225 | | return entity.gameObject.GetComponentInChildren<IPointerEvent>(); |
| | 226 | | } |
| | 227 | |
|
| | 228 | | private IList<IPointerInputEvent> GetPointerInputEvents(IDCLEntity entity, GameObject hitGameObject) |
| | 229 | | { |
| | 230 | | // If an event exist in the new ECS, we got that value, if not it is ECS 6, so we continue as before |
| 10 | 231 | | if (entity != null && DataStore.i.ecs7.entityEvents.TryGetValue(entity.entityId, out List<IPointerInputEvent |
| 0 | 232 | | return pointerInputEvent; |
| | 233 | | else |
| 10 | 234 | | return hitGameObject.GetComponentsInChildren<IPointerInputEvent>(); |
| | 235 | | } |
| | 236 | |
|
| | 237 | | private bool EventObjectCanBeHovered(ColliderInfo colliderInfo, float distance) |
| | 238 | | { |
| 16677 | 239 | | return newHoveredInputEvent != null && |
| | 240 | | newHoveredInputEvent.IsAtHoverDistance(distance) && |
| | 241 | | newHoveredInputEvent.IsVisible() && |
| | 242 | | AreSameEntity(newHoveredInputEvent, colliderInfo); |
| | 243 | | } |
| | 244 | |
|
| | 245 | | private void ResolveGenericRaycastHandlers(IRaycastPointerHandler raycastHandlerTarget) |
| | 246 | | { |
| 0 | 247 | | if (Utils.LockedThisFrame()) |
| 0 | 248 | | return; |
| | 249 | |
|
| 0 | 250 | | var mouseIsDown = Input.GetMouseButtonDown(0); |
| 0 | 251 | | var mouseIsUp = Input.GetMouseButtonUp(0); |
| | 252 | |
|
| 0 | 253 | | if (raycastHandlerTarget is IRaycastPointerDownHandler down) |
| | 254 | | { |
| 0 | 255 | | if (mouseIsDown) |
| 0 | 256 | | down.OnPointerDown(); |
| | 257 | | } |
| | 258 | |
|
| 0 | 259 | | if (raycastHandlerTarget is IRaycastPointerUpHandler up) |
| | 260 | | { |
| 0 | 261 | | if (mouseIsUp) |
| 0 | 262 | | up.OnPointerUp(); |
| | 263 | | } |
| | 264 | |
|
| 0 | 265 | | if (raycastHandlerTarget is IRaycastPointerClickHandler click) |
| | 266 | | { |
| 0 | 267 | | if (mouseIsDown) |
| 0 | 268 | | clickHandler = click; |
| | 269 | |
|
| 0 | 270 | | if (mouseIsUp) |
| | 271 | | { |
| 0 | 272 | | if (clickHandler == click) |
| 0 | 273 | | click.OnPointerClick(); |
| | 274 | |
|
| 0 | 275 | | clickHandler = null; |
| | 276 | | } |
| | 277 | | } |
| 0 | 278 | | } |
| | 279 | |
|
| | 280 | | void UnhoverLastHoveredObject() |
| | 281 | | { |
| 511 | 282 | | if (lastHoveredObject == null) |
| | 283 | | { |
| 506 | 284 | | if (hoverCanvas != null) |
| 506 | 285 | | hoverCanvas.SetHoverState(false); |
| | 286 | |
|
| 506 | 287 | | return; |
| | 288 | | } |
| | 289 | |
|
| 5 | 290 | | OnPointerHoverEnds?.Invoke(); |
| | 291 | |
|
| 22 | 292 | | for (int i = 0; i < lastHoveredEventList.Count; i++) |
| | 293 | | { |
| 6 | 294 | | if (lastHoveredEventList[i] == null) |
| | 295 | | continue; |
| | 296 | |
|
| 6 | 297 | | lastHoveredEventList[i].SetHoverState(false); |
| | 298 | | } |
| | 299 | |
|
| 5 | 300 | | lastHoveredEventList = null; |
| 5 | 301 | | lastHoveredObject = null; |
| 5 | 302 | | } |
| | 303 | |
|
| | 304 | | public void Dispose() |
| | 305 | | { |
| 1800 | 306 | | for (int i = 0; i < Enum.GetValues(typeof(WebInterface.ACTION_BUTTON)).Length; i++) |
| | 307 | | { |
| 840 | 308 | | var buttonId = (WebInterface.ACTION_BUTTON) i; |
| | 309 | |
|
| 840 | 310 | | if (buttonId == WebInterface.ACTION_BUTTON.ANY) |
| | 311 | | continue; |
| | 312 | |
|
| 780 | 313 | | inputControllerLegacy.RemoveListener(buttonId, OnButtonEvent); |
| | 314 | | } |
| | 315 | |
|
| 60 | 316 | | lastHoveredObject = null; |
| 60 | 317 | | newHoveredGO = null; |
| 60 | 318 | | newHoveredInputEvent = null; |
| 60 | 319 | | lastHoveredEventList = null; |
| | 320 | |
|
| 60 | 321 | | OnPointerHoverStarts -= SetHoverCursor; |
| 60 | 322 | | OnPointerHoverEnds -= SetNormalCursor; |
| | 323 | |
|
| 60 | 324 | | Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 60 | 325 | | Utils.OnCursorLockChanged -= HandleCursorLockChanges; |
| 60 | 326 | | } |
| | 327 | |
|
| | 328 | | void RetrieveCamera() |
| | 329 | | { |
| 65 | 330 | | if (charCamera == null) |
| | 331 | | { |
| 65 | 332 | | charCamera = Camera.main; |
| | 333 | | } |
| 65 | 334 | | } |
| | 335 | |
|
| 17167 | 336 | | public Ray GetRayFromCamera() { return charCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / |
| | 337 | |
|
| | 338 | | void OnButtonEvent(WebInterface.ACTION_BUTTON buttonId, InputController_Legacy.EVENT evt, bool useRaycast, |
| | 339 | | bool enablePointerEvent) |
| | 340 | | { |
| | 341 | | //TODO(Brian): We should remove this when we get a proper initialization layer |
| 14 | 342 | | if (!EnvironmentSettings.RUNNING_TESTS) |
| | 343 | | { |
| 0 | 344 | | if (Utils.LockedThisFrame()) |
| 0 | 345 | | return; |
| | 346 | |
|
| 0 | 347 | | if (!Utils.IsCursorLocked || !renderingEnabled) |
| 0 | 348 | | return; |
| | 349 | | } |
| | 350 | |
|
| 14 | 351 | | if (charCamera == null) |
| | 352 | | { |
| 0 | 353 | | RetrieveCamera(); |
| | 354 | |
|
| 0 | 355 | | if (charCamera == null) |
| 0 | 356 | | return; |
| | 357 | | } |
| | 358 | |
|
| 14 | 359 | | var pointerEventLayer = |
| | 360 | | PhysicsLayers.physicsCastLayerMaskWithoutCharacter; //Ensure characterController is being filtered |
| | 361 | |
|
| 14 | 362 | | var globalLayer = pointerEventLayer & ~PhysicsLayers.physicsCastLayerMask; |
| | 363 | |
|
| 14 | 364 | | if (evt == InputController_Legacy.EVENT.BUTTON_DOWN) |
| | 365 | | { |
| 11 | 366 | | ProcessButtonDown(buttonId, useRaycast, enablePointerEvent, pointerEventLayer, globalLayer); |
| | 367 | | } |
| 3 | 368 | | else if (evt == InputController_Legacy.EVENT.BUTTON_UP) |
| | 369 | | { |
| 3 | 370 | | ProcessButtonUp(buttonId, useRaycast, enablePointerEvent, pointerEventLayer, globalLayer); |
| | 371 | | } |
| 3 | 372 | | } |
| | 373 | |
|
| | 374 | | private void ProcessButtonUp(WebInterface.ACTION_BUTTON buttonId, bool useRaycast, bool enablePointerEvent, |
| | 375 | | LayerMask pointerEventLayer, int globalLayer) |
| | 376 | | { |
| 3 | 377 | | IWorldState worldState = Environment.i.world.state; |
| | 378 | |
|
| 3 | 379 | | string currentSceneId = worldState.GetCurrentSceneId(); |
| 3 | 380 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 0 | 381 | | return; |
| | 382 | |
|
| | 383 | | RaycastHitInfo raycastGlobalLayerHitInfo; |
| 3 | 384 | | Ray ray = GetRayFromCamera(); |
| | 385 | |
|
| | 386 | | // Raycast for global pointer events |
| 3 | 387 | | worldState.TryGetScene(currentSceneId, out var loadedScene); |
| | 388 | |
|
| 3 | 389 | | RaycastResultInfo raycastInfoGlobalLayer = raycastHandler.Raycast(ray, charCamera.farClipPlane, globalLayer, |
| | 390 | | loadedScene); |
| | 391 | |
|
| 3 | 392 | | raycastGlobalLayerHitInfo = raycastInfoGlobalLayer.hitInfo; |
| | 393 | |
|
| 3 | 394 | | if (pointerInputUpEvent != null) |
| | 395 | | { |
| | 396 | | // Raycast for pointer event components |
| 3 | 397 | | RaycastResultInfo raycastInfoPointerEventLayer = raycastHandler.Raycast(ray, charCamera.farClipPlane, |
| | 398 | | pointerEventLayer, loadedScene); |
| | 399 | |
|
| 3 | 400 | | bool isOnClickComponentBlocked = |
| | 401 | | IsBlockingOnClick(raycastInfoPointerEventLayer.hitInfo, raycastGlobalLayerHitInfo); |
| | 402 | |
|
| 3 | 403 | | bool isSameEntityThatWasPressed = AreCollidersFromSameEntity(raycastInfoPointerEventLayer.hitInfo, |
| | 404 | | lastPointerDownEventHitInfo); |
| | 405 | |
|
| 3 | 406 | | if (!isOnClickComponentBlocked && isSameEntityThatWasPressed && enablePointerEvent) |
| | 407 | | { |
| 3 | 408 | | pointerInputUpEvent.Report(buttonId, ray, raycastInfoPointerEventLayer.hitInfo.hit); |
| | 409 | | } |
| | 410 | |
|
| 3 | 411 | | pointerInputUpEvent = null; |
| | 412 | | } |
| | 413 | |
|
| 3 | 414 | | ReportGlobalPointerUpEvent(buttonId, useRaycast, raycastGlobalLayerHitInfo, raycastInfoGlobalLayer, |
| | 415 | | currentSceneId); |
| | 416 | |
|
| | 417 | | // Raycast for global pointer events (for each PE scene) |
| 3 | 418 | | List<string> currentPortableExperienceIds = DataStore.i.Get<DataStore_World>().portableExperienceIds.Get().T |
| | 419 | |
|
| 6 | 420 | | for (int i = 0; i < currentPortableExperienceIds.Count; i++) |
| | 421 | | { |
| 0 | 422 | | if (worldState.TryGetScene(currentPortableExperienceIds[i], out var portableScene)) |
| | 423 | | { |
| 0 | 424 | | raycastInfoGlobalLayer = raycastHandler.Raycast(ray, charCamera.farClipPlane, globalLayer, |
| | 425 | | portableScene); |
| | 426 | |
|
| 0 | 427 | | raycastGlobalLayerHitInfo = raycastInfoGlobalLayer.hitInfo; |
| | 428 | |
|
| 0 | 429 | | ReportGlobalPointerUpEvent(buttonId, useRaycast, raycastGlobalLayerHitInfo, raycastInfoGlobalLayer, |
| | 430 | | currentPortableExperienceIds[i]); |
| | 431 | | } |
| | 432 | | } |
| 3 | 433 | | } |
| | 434 | |
|
| | 435 | | private void ProcessButtonDown(WebInterface.ACTION_BUTTON buttonId, bool useRaycast, bool enablePointerEvent, |
| | 436 | | LayerMask pointerEventLayer, int globalLayer) |
| | 437 | | { |
| 11 | 438 | | IWorldState worldState = Environment.i.world.state; |
| | 439 | |
|
| 11 | 440 | | string currentSceneId = worldState.GetCurrentSceneId(); |
| 11 | 441 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 0 | 442 | | return; |
| | 443 | |
|
| | 444 | | RaycastHitInfo raycastGlobalLayerHitInfo; |
| 11 | 445 | | Ray ray = GetRayFromCamera(); |
| 11 | 446 | | worldState.TryGetScene(currentSceneId, out var loadedScene); |
| | 447 | |
|
| | 448 | | // Raycast for pointer event components |
| 11 | 449 | | RaycastResultInfo raycastInfoPointerEventLayer = raycastHandler.Raycast(ray, charCamera.farClipPlane, pointe |
| | 450 | |
|
| | 451 | | // Raycast for global pointer events |
| 11 | 452 | | RaycastResultInfo raycastInfoGlobalLayer = raycastHandler.Raycast(ray, charCamera.farClipPlane, globalLayer, |
| | 453 | |
|
| 11 | 454 | | raycastGlobalLayerHitInfo = raycastInfoGlobalLayer.hitInfo; |
| | 455 | |
|
| 11 | 456 | | bool isOnClickComponentBlocked = |
| | 457 | | IsBlockingOnClick(raycastInfoPointerEventLayer.hitInfo, raycastGlobalLayerHitInfo); |
| | 458 | |
|
| 11 | 459 | | if (!isOnClickComponentBlocked && raycastInfoPointerEventLayer.hitInfo.hit.collider) |
| | 460 | | { |
| 10 | 461 | | Collider collider = raycastInfoPointerEventLayer.hitInfo.hit.collider; |
| | 462 | |
|
| | 463 | | GameObject hitGameObject; |
| | 464 | |
|
| 10 | 465 | | if (CollidersManager.i.GetColliderInfo(collider, out ColliderInfo info)) |
| 10 | 466 | | hitGameObject = info.entity.gameObject; |
| | 467 | | else |
| 0 | 468 | | hitGameObject = collider.gameObject; |
| | 469 | |
|
| 10 | 470 | | IList<IPointerInputEvent> events = GetPointerInputEvents(info.entity, hitGameObject); |
| | 471 | |
|
| 36 | 472 | | for (var i = 0; i < events.Count; i++) |
| | 473 | | { |
| 8 | 474 | | IPointerInputEvent e = events[i]; |
| 8 | 475 | | bool areSameEntity = AreSameEntity(e, info); |
| | 476 | |
|
| 8 | 477 | | switch (e.GetEventType()) |
| | 478 | | { |
| | 479 | | case PointerInputEventType.CLICK: |
| 1 | 480 | | if (areSameEntity && enablePointerEvent) |
| 1 | 481 | | e.Report(buttonId, ray, raycastInfoPointerEventLayer.hitInfo.hit); |
| | 482 | |
|
| 1 | 483 | | break; |
| | 484 | | case PointerInputEventType.DOWN: |
| 4 | 485 | | if (areSameEntity && enablePointerEvent) |
| 4 | 486 | | e.Report(buttonId, ray, raycastInfoPointerEventLayer.hitInfo.hit); |
| | 487 | |
|
| 4 | 488 | | break; |
| | 489 | | case PointerInputEventType.UP: |
| 3 | 490 | | if (areSameEntity && enablePointerEvent) |
| 3 | 491 | | pointerInputUpEvent = e; |
| | 492 | | else |
| 0 | 493 | | pointerInputUpEvent = null; |
| | 494 | |
|
| | 495 | | break; |
| | 496 | | } |
| | 497 | | } |
| | 498 | |
|
| 10 | 499 | | lastPointerDownEventHitInfo = raycastInfoPointerEventLayer.hitInfo; |
| | 500 | | } |
| | 501 | |
|
| 11 | 502 | | ReportGlobalPointerDownEvent(buttonId, useRaycast, raycastGlobalLayerHitInfo, raycastInfoGlobalLayer, curren |
| | 503 | |
|
| | 504 | | // Raycast for global pointer events (for each PE scene) |
| 11 | 505 | | IEnumerable<string> currentPortableExperienceIds = DataStore.i.world.portableExperienceIds.Get(); |
| | 506 | |
|
| 22 | 507 | | foreach (var pexId in currentPortableExperienceIds) |
| | 508 | | { |
| 0 | 509 | | if (worldState.TryGetScene(pexId, out var portableScene)) |
| | 510 | | { |
| 0 | 511 | | raycastInfoGlobalLayer = raycastHandler.Raycast(ray, charCamera.farClipPlane, globalLayer, portableS |
| | 512 | |
|
| 0 | 513 | | raycastGlobalLayerHitInfo = raycastInfoGlobalLayer.hitInfo; |
| | 514 | |
|
| 0 | 515 | | ReportGlobalPointerDownEvent(buttonId, useRaycast, raycastGlobalLayerHitInfo, raycastInfoGlobalLayer |
| | 516 | | pexId); |
| | 517 | | } |
| | 518 | |
|
| | 519 | | } |
| 11 | 520 | | } |
| | 521 | |
|
| | 522 | | private void ReportGlobalPointerUpEvent( |
| | 523 | | WebInterface.ACTION_BUTTON buttonId, |
| | 524 | | bool useRaycast, |
| | 525 | | RaycastHitInfo raycastGlobalLayerHitInfo, |
| | 526 | | RaycastResultInfo raycastInfoGlobalLayer, |
| | 527 | | string sceneId) |
| | 528 | | { |
| 3 | 529 | | if (useRaycast && raycastGlobalLayerHitInfo.isValid) |
| | 530 | | { |
| 3 | 531 | | CollidersManager.i.GetColliderInfo(raycastGlobalLayerHitInfo.hit.collider, |
| | 532 | | out ColliderInfo colliderInfo); |
| | 533 | |
|
| 3 | 534 | | string entityId = SpecialEntityIdLegacyLiteral.SCENE_ROOT_ENTITY; |
| | 535 | |
|
| 3 | 536 | | if (colliderInfo.entity != null) |
| 3 | 537 | | entityId = |
| | 538 | | Environment.i.world.sceneController.entityIdHelper.GetOriginalId(colliderInfo.entity.entityId); |
| | 539 | |
|
| 3 | 540 | | WebInterface.ReportGlobalPointerUpEvent( |
| | 541 | | buttonId, |
| | 542 | | raycastInfoGlobalLayer.ray, |
| | 543 | | raycastGlobalLayerHitInfo.hit.point, |
| | 544 | | raycastGlobalLayerHitInfo.hit.normal, |
| | 545 | | raycastGlobalLayerHitInfo.hit.distance, |
| | 546 | | sceneId, |
| | 547 | | entityId, |
| | 548 | | colliderInfo.meshName, |
| | 549 | | isHitInfoValid: true); |
| | 550 | | } |
| | 551 | | else |
| | 552 | | { |
| 0 | 553 | | WebInterface.ReportGlobalPointerUpEvent(buttonId, raycastInfoGlobalLayer.ray, Vector3.zero, |
| | 554 | | Vector3.zero, 0, sceneId); |
| | 555 | | } |
| 0 | 556 | | } |
| | 557 | |
|
| | 558 | | private void ReportGlobalPointerDownEvent( |
| | 559 | | WebInterface.ACTION_BUTTON buttonId, |
| | 560 | | bool useRaycast, |
| | 561 | | RaycastHitInfo raycastGlobalLayerHitInfo, |
| | 562 | | RaycastResultInfo raycastInfoGlobalLayer, |
| | 563 | | string sceneId) |
| | 564 | | { |
| 11 | 565 | | if (useRaycast && raycastGlobalLayerHitInfo.isValid) |
| | 566 | | { |
| 10 | 567 | | CollidersManager.i.GetColliderInfo(raycastGlobalLayerHitInfo.hit.collider, |
| | 568 | | out ColliderInfo colliderInfo); |
| | 569 | |
|
| 10 | 570 | | string entityId = SpecialEntityIdLegacyLiteral.SCENE_ROOT_ENTITY; |
| | 571 | |
|
| 10 | 572 | | if (colliderInfo.entity != null) |
| 10 | 573 | | entityId = |
| | 574 | | Environment.i.world.sceneController.entityIdHelper.GetOriginalId(colliderInfo.entity.entityId); |
| | 575 | |
|
| 10 | 576 | | WebInterface.ReportGlobalPointerDownEvent( |
| | 577 | | buttonId, |
| | 578 | | raycastInfoGlobalLayer.ray, |
| | 579 | | raycastGlobalLayerHitInfo.hit.point, |
| | 580 | | raycastGlobalLayerHitInfo.hit.normal, |
| | 581 | | raycastGlobalLayerHitInfo.hit.distance, |
| | 582 | | sceneId, |
| | 583 | | entityId, |
| | 584 | | colliderInfo.meshName, |
| | 585 | | isHitInfoValid: true); |
| | 586 | | } |
| | 587 | | else |
| | 588 | | { |
| 1 | 589 | | WebInterface.ReportGlobalPointerDownEvent(buttonId, raycastInfoGlobalLayer.ray, Vector3.zero, |
| | 590 | | Vector3.zero, 0, sceneId); |
| | 591 | | } |
| 1 | 592 | | } |
| | 593 | |
|
| | 594 | | bool AreSameEntity(IPointerEvent pointerInputEvent, ColliderInfo colliderInfo) |
| | 595 | | { |
| 16669 | 596 | | return pointerInputEvent != null && colliderInfo.entity != null && |
| | 597 | | pointerInputEvent.entity == colliderInfo.entity; |
| | 598 | | } |
| | 599 | |
|
| | 600 | | bool IsBlockingOnClick(RaycastHitInfo targetOnClickHit, RaycastHitInfo potentialBlockerHit) |
| | 601 | | { |
| 14 | 602 | | return |
| | 603 | | potentialBlockerHit.hit.collider != null // Does a potential blocker hit exist? |
| | 604 | | && targetOnClickHit.hit.collider != null // Was a target entity with a pointer event component hit? |
| | 605 | | && potentialBlockerHit.hit.distance <= |
| | 606 | | targetOnClickHit.hit.distance // Is potential blocker nearer than target entity? |
| | 607 | | && !AreCollidersFromSameEntity(potentialBlockerHit, |
| | 608 | | targetOnClickHit); // Does potential blocker belong to other entity rather than target entity? |
| | 609 | | } |
| | 610 | |
|
| | 611 | | bool EntityHasPointerEvent(IDCLEntity entity) |
| | 612 | | { |
| 32 | 613 | | var componentsManager = entity.scene.componentsManagerLegacy; |
| | 614 | |
|
| 32 | 615 | | return componentsManager.HasComponent(entity, Models.CLASS_ID_COMPONENT.UUID_CALLBACK) || |
| | 616 | | componentsManager.HasComponent(entity, Models.CLASS_ID_COMPONENT.UUID_ON_UP) || |
| | 617 | | componentsManager.HasComponent(entity, Models.CLASS_ID_COMPONENT.UUID_ON_DOWN) || |
| | 618 | | componentsManager.HasComponent(entity, Models.CLASS_ID_COMPONENT.UUID_ON_CLICK); |
| | 619 | | } |
| | 620 | |
|
| | 621 | | bool AreCollidersFromSameEntity(RaycastHitInfo hitInfoA, RaycastHitInfo hitInfoB) |
| | 622 | | { |
| 16 | 623 | | CollidersManager.i.GetColliderInfo(hitInfoA.hit.collider, out ColliderInfo colliderInfoA); |
| 16 | 624 | | CollidersManager.i.GetColliderInfo(hitInfoB.hit.collider, out ColliderInfo colliderInfoB); |
| | 625 | |
|
| 16 | 626 | | var entityA = colliderInfoA.entity; |
| 16 | 627 | | var entityB = colliderInfoB.entity; |
| | 628 | |
|
| 16 | 629 | | bool entityAHasEvent = entityA != null && EntityHasPointerEvent(entityA); |
| 16 | 630 | | bool entityBHasEvent = entityB != null && EntityHasPointerEvent(entityB); |
| | 631 | |
|
| | 632 | | // If both entities has OnClick/PointerEvent component |
| 16 | 633 | | if (entityAHasEvent && entityBHasEvent) |
| | 634 | | { |
| 14 | 635 | | return entityA == entityB; |
| | 636 | | } |
| | 637 | | // If only one of them has OnClick/PointerEvent component |
| 2 | 638 | | else if (entityAHasEvent ^ entityBHasEvent) |
| | 639 | | { |
| 0 | 640 | | return false; |
| | 641 | | } |
| | 642 | | // None of them has OnClick/PointerEvent component |
| | 643 | | else |
| | 644 | | { |
| 2 | 645 | | return colliderInfoA.entity == colliderInfoB.entity; |
| | 646 | | } |
| | 647 | | } |
| | 648 | |
|
| | 649 | | private void HandleCursorLockChanges(bool isLocked) |
| | 650 | | { |
| 0 | 651 | | HideOrShowCursor(isLocked); |
| | 652 | |
|
| 0 | 653 | | if (!isLocked) |
| 0 | 654 | | UnhoverLastHoveredObject(); |
| 0 | 655 | | } |
| | 656 | |
|
| 120 | 657 | | private void HideOrShowCursor(bool isCursorLocked) { DataStore.i.Get<DataStore_Cursor>().cursorVisible.Set(isCur |
| | 658 | |
|
| 36 | 659 | | private void SetHoverCursor() { DataStore.i.Get<DataStore_Cursor>().cursorType.Set(DataStore_Cursor.CursorType.H |
| | 660 | |
|
| 10 | 661 | | private void SetNormalCursor() { DataStore.i.Get<DataStore_Cursor>().cursorType.Set(DataStore_Cursor.CursorType. |
| | 662 | | } |
| | 663 | | } |