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