| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using DCLPlugins.UUIDEventComponentsPlugin.UUIDComponent.Interfaces; |
| | 7 | | using Ray = UnityEngine.Ray; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class AvatarOnPointerDown : MonoBehaviour, IAvatarOnPointerDown, IPoolLifecycleHandler, |
| | 12 | | IAvatarOnPointerDownCollider, IUnlockedCursorInputEvent |
| | 13 | | { |
| | 14 | | private OnPointerEvent.Model model; |
| | 15 | | private OnPointerEventHandler eventHandler; |
| | 16 | | private bool isHovering; |
| 655 | 17 | | private bool passportEnabled = true; |
| 655 | 18 | | private bool onClickReportEnabled = true; |
| | 19 | | private Player avatarPlayer; |
| | 20 | |
|
| 634 | 21 | | public IDCLEntity entity { get; private set; } |
| | 22 | | public event System.Action OnPointerDownReport; |
| | 23 | | public event System.Action OnPointerEnterReport; |
| | 24 | | public event System.Action OnPointerExitReport; |
| | 25 | |
|
| 655 | 26 | | public bool ShouldBeInteractableWhenMouseIsLocked { get; set; } = true; |
| | 27 | | public new Collider collider; |
| | 28 | |
|
| | 29 | | public WebInterface.ACTION_BUTTON GetActionButton() => |
| 0 | 30 | | model.GetActionButton(); |
| | 31 | |
|
| | 32 | | public void SetHoverState(bool state) |
| | 33 | | { |
| 0 | 34 | | if (!enabled) return; |
| 0 | 35 | | bool isHoveringDirty = state != isHovering; |
| 0 | 36 | | isHovering = state; |
| 0 | 37 | | eventHandler?.SetFeedbackState(model.showFeedback, state && passportEnabled, model.button, model.hoverText); |
| | 38 | |
|
| 0 | 39 | | if (!isHoveringDirty) |
| 0 | 40 | | return; |
| | 41 | |
|
| 0 | 42 | | if (isHovering) |
| 0 | 43 | | OnPointerEnterReport?.Invoke(); |
| | 44 | | else |
| 0 | 45 | | OnPointerExitReport?.Invoke(); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | private void OnDisable() |
| | 49 | | { |
| 613 | 50 | | if (!isHovering) |
| 613 | 51 | | return; |
| | 52 | |
|
| 0 | 53 | | isHovering = false; |
| 0 | 54 | | OnPointerExitReport?.Invoke(); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | private void Awake() |
| | 58 | | { |
| 634 | 59 | | CommonScriptableObjects.playerInfoCardVisibleState.OnChange += ReEnableOnInfoCardClosed; |
| 634 | 60 | | } |
| | 61 | |
|
| | 62 | | private void OnDestroy() |
| | 63 | | { |
| 634 | 64 | | CommonScriptableObjects.playerInfoCardVisibleState.OnChange -= ReEnableOnInfoCardClosed; |
| 634 | 65 | | eventHandler?.Dispose(); |
| | 66 | |
|
| 634 | 67 | | if (entity != null) |
| 0 | 68 | | CollidersManager.i.RemoveEntityCollider(entity, collider); |
| 634 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Initialize(OnPointerEvent.Model model, IDCLEntity entity, Player player) |
| | 72 | | { |
| 0 | 73 | | this.model = model; |
| 0 | 74 | | this.entity = entity; |
| 0 | 75 | | this.avatarPlayer = player; |
| | 76 | |
|
| 0 | 77 | | if (eventHandler == null) |
| 0 | 78 | | eventHandler = new OnPointerEventHandler(); |
| | 79 | |
|
| 0 | 80 | | if (entity != null) |
| | 81 | | { |
| 0 | 82 | | eventHandler?.SetColliders(entity); |
| 0 | 83 | | CollidersManager.i.AddOrUpdateEntityCollider(entity, collider); |
| | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public bool IsAtHoverDistance(float distance) |
| | 88 | | { |
| 0 | 89 | | bool isCursorLocked = Utils.IsCursorLocked; |
| 0 | 90 | | if (!ShouldBeInteractableWhenMouseIsLocked && isCursorLocked) return false; |
| 0 | 91 | | return !isCursorLocked || distance <= model.distance; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | public bool IsVisible() => |
| 0 | 95 | | true; |
| | 96 | |
|
| | 97 | | private bool ShouldReportPassportInputEvent(WebInterface.ACTION_BUTTON buttonId, HitInfo hit) => |
| 0 | 98 | | isHovering && (model.button == "ANY" || buttonId.ToString() == model.button); |
| | 99 | |
|
| | 100 | | public void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) |
| | 101 | | { |
| 0 | 102 | | if (!enabled) return; |
| 0 | 103 | | if (!ShouldBeInteractableWhenMouseIsLocked && !isHovering) return; |
| | 104 | |
|
| 0 | 105 | | if (passportEnabled && ShouldReportPassportInputEvent(buttonId, hit)) |
| | 106 | | { |
| 0 | 107 | | eventHandler.SetFeedbackState(model.showFeedback, false, model.button, model.hoverText); |
| 0 | 108 | | passportEnabled = false; |
| | 109 | |
|
| 0 | 110 | | OnPointerDownReport?.Invoke(); |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | if (onClickReportEnabled && ShouldReportOnClickEvent(buttonId, out IParcelScene playerScene)) |
| | 114 | | { |
| 0 | 115 | | AudioScriptableObjects.buttonClick.Play(true); |
| | 116 | |
|
| 0 | 117 | | WebInterface.ReportAvatarClick( |
| | 118 | | playerScene.sceneData.sceneNumber, |
| | 119 | | avatarPlayer.id, |
| | 120 | | WorldStateUtils.ConvertUnityToScenePosition(ray.origin, playerScene), |
| | 121 | | ray.direction, |
| | 122 | | hit.distance); |
| | 123 | | } |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public PointerInputEventType GetEventType() => |
| 0 | 127 | | PointerInputEventType.DOWN; |
| | 128 | |
|
| | 129 | | void ReEnableOnInfoCardClosed(bool newState, bool prevState) |
| | 130 | | { |
| 25 | 131 | | if (passportEnabled || newState) |
| 25 | 132 | | return; |
| | 133 | |
|
| 0 | 134 | | passportEnabled = true; |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | public void SetColliderEnabled(bool newEnabledState) |
| | 138 | | { |
| 0 | 139 | | collider.enabled = newEnabledState; |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | public void SetPassportEnabled(bool newEnabledState) |
| | 143 | | { |
| 0 | 144 | | passportEnabled = newEnabledState; |
| 0 | 145 | | isHovering = false; |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | public void SetOnClickReportEnabled(bool newEnabledState) |
| | 149 | | { |
| 0 | 150 | | onClickReportEnabled = newEnabledState; |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public Transform GetTransform() => |
| 0 | 154 | | transform; |
| | 155 | |
|
| | 156 | | public void OnPoolRelease() |
| | 157 | | { |
| 0 | 158 | | eventHandler.Dispose(); |
| 0 | 159 | | avatarPlayer = null; |
| 0 | 160 | | } |
| | 161 | |
|
| 0 | 162 | | public void OnPoolGet() { } |
| | 163 | |
|
| | 164 | | private bool ShouldReportOnClickEvent(WebInterface.ACTION_BUTTON buttonId, out IParcelScene playerScene) |
| | 165 | | { |
| 0 | 166 | | playerScene = null; |
| | 167 | |
|
| 0 | 168 | | if (buttonId != WebInterface.ACTION_BUTTON.POINTER) { return false; } |
| | 169 | |
|
| 0 | 170 | | if (avatarPlayer == null) { return false; } |
| | 171 | |
|
| 0 | 172 | | int playerSceneNumber = CommonScriptableObjects.sceneNumber.Get(); |
| | 173 | |
|
| 0 | 174 | | if (playerSceneNumber <= 0) { return false; } |
| | 175 | |
|
| 0 | 176 | | playerScene = WorldStateUtils.GetCurrentScene(); |
| | 177 | |
|
| 0 | 178 | | return playerScene?.IsInsideSceneBoundaries( |
| | 179 | | PositionUtils.UnityToWorldPosition(avatarPlayer.worldPosition)) ?? false; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | public bool ShouldShowHoverFeedback() => |
| 0 | 183 | | enabled && model.showFeedback; |
| | 184 | | } |
| | 185 | | } |