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