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