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