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