| | 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, IPointerEvent, IPoolLifecycleHandler, IAvatarOnPointerDownCollider |
| | 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 | |
|
| 671 | 22 | | private bool passportEnabled = true; |
| 671 | 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 | | { |
| 672 | 43 | | if (!isHovering) |
| 672 | 44 | | return; |
| 0 | 45 | | isHovering = false; |
| 0 | 46 | | OnPointerExitReport?.Invoke(); |
| 0 | 47 | | } |
| | 48 | |
|
| 1340 | 49 | | void Awake() { CommonScriptableObjects.playerInfoCardVisibleState.OnChange += ReEnableOnInfoCardClosed; } |
| | 50 | |
|
| | 51 | | void OnDestroy() |
| | 52 | | { |
| 670 | 53 | | CommonScriptableObjects.playerInfoCardVisibleState.OnChange -= ReEnableOnInfoCardClosed; |
| 670 | 54 | | eventHandler?.Dispose(); |
| 670 | 55 | | CollidersManager.i.RemoveEntityCollider(entity, collider); |
| 670 | 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 PointerEventType GetEventType() { return PointerEventType.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 | | { |
| 2 | 121 | | passportEnabled = newEnabledState; |
| 2 | 122 | | eventHandler?.SetFeedbackState(model.showFeedback, false, model.button, model.hoverText); |
| 2 | 123 | | isHovering = false; |
| 2 | 124 | | } |
| | 125 | |
|
| | 126 | | public void SetOnClickReportEnabled(bool newEnabledState) |
| | 127 | | { |
| 0 | 128 | | onClickReportEnabled = newEnabledState; |
| 0 | 129 | | } |
| | 130 | |
|
| 0 | 131 | | public Transform GetTransform() { return transform; } |
| | 132 | |
|
| | 133 | | public void OnPoolRelease() |
| | 134 | | { |
| 0 | 135 | | eventHandler.Dispose(); |
| 0 | 136 | | avatarPlayer = null; |
| 0 | 137 | | } |
| | 138 | |
|
| 0 | 139 | | public void OnPoolGet() { } |
| | 140 | |
|
| | 141 | | private bool ShouldReportOnClickEvent(WebInterface.ACTION_BUTTON buttonId, out IParcelScene playerScene) |
| | 142 | | { |
| 0 | 143 | | playerScene = null; |
| | 144 | |
|
| 0 | 145 | | if (buttonId != WebInterface.ACTION_BUTTON.POINTER) |
| | 146 | | { |
| 0 | 147 | | return false; |
| | 148 | | } |
| | 149 | |
|
| 0 | 150 | | if (avatarPlayer == null) |
| | 151 | | { |
| 0 | 152 | | return false; |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | string playerSceneId = CommonScriptableObjects.sceneID.Get(); |
| | 156 | |
|
| 0 | 157 | | if (string.IsNullOrEmpty(playerSceneId)) |
| | 158 | | { |
| 0 | 159 | | return false; |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | playerScene = WorldStateUtils.GetCurrentScene(); |
| 0 | 163 | | return playerScene?.IsInsideSceneBoundaries(PositionUtils.UnityToWorldPosition(avatarPlayer.worldPosition)) |
| | 164 | | } |
| | 165 | | } |
| | 166 | | } |