| | 1 | | using DCL.Interface; |
| | 2 | | using UnityEngine; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using Ray = UnityEngine.Ray; |
| | 6 | |
|
| | 7 | | namespace DCL.Components |
| | 8 | | { |
| | 9 | | public class AvatarOnPointerDown : MonoBehaviour, IPointerEvent, IPoolLifecycleHandler, IAvatarOnPointerDownCollider |
| | 10 | | { |
| | 11 | | public new Collider collider; |
| | 12 | | private OnPointerEvent.Model model; |
| | 13 | | private OnPointerEventHandler eventHandler; |
| 0 | 14 | | public IDCLEntity entity { get; private set; } |
| | 15 | | public event System.Action OnPointerDownReport; |
| | 16 | | public event System.Action OnPointerEnterReport; |
| | 17 | | public event System.Action OnPointerExitReport; |
| | 18 | | private bool isHovering = false; |
| | 19 | |
|
| 0 | 20 | | public WebInterface.ACTION_BUTTON GetActionButton() { return model.GetActionButton(); } |
| | 21 | |
|
| | 22 | | public void SetHoverState(bool state) |
| | 23 | | { |
| 0 | 24 | | bool isHoveringDirty = state != isHovering; |
| 0 | 25 | | isHovering = state; |
| 0 | 26 | | eventHandler.SetFeedbackState(model.showFeedback, state, model.button, model.hoverText); |
| 0 | 27 | | if (!isHoveringDirty) |
| 0 | 28 | | return; |
| 0 | 29 | | if (isHovering) |
| 0 | 30 | | OnPointerEnterReport?.Invoke(); |
| | 31 | | else |
| 0 | 32 | | OnPointerExitReport?.Invoke(); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OnDisable() |
| | 36 | | { |
| 663 | 37 | | if (!isHovering) |
| 663 | 38 | | return; |
| 0 | 39 | | isHovering = false; |
| 0 | 40 | | OnPointerExitReport?.Invoke(); |
| 0 | 41 | | } |
| | 42 | |
|
| 1322 | 43 | | void Awake() { CommonScriptableObjects.playerInfoCardVisibleState.OnChange += ReEnableOnInfoCardClosed; } |
| | 44 | |
|
| | 45 | | void OnDestroy() |
| | 46 | | { |
| 661 | 47 | | CommonScriptableObjects.playerInfoCardVisibleState.OnChange -= ReEnableOnInfoCardClosed; |
| 661 | 48 | | eventHandler?.Dispose(); |
| 661 | 49 | | CollidersManager.i.RemoveEntityCollider(entity, collider); |
| 661 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Initialize(OnPointerEvent.Model model, IDCLEntity entity) |
| | 53 | | { |
| 0 | 54 | | this.model = model; |
| 0 | 55 | | this.entity = entity; |
| | 56 | |
|
| 0 | 57 | | if (eventHandler == null) |
| 0 | 58 | | eventHandler = new OnPointerEventHandler(); |
| | 59 | |
|
| 0 | 60 | | eventHandler?.SetColliders(entity); |
| 0 | 61 | | CollidersManager.i.AddOrUpdateEntityCollider(entity, collider); |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | public bool IsAtHoverDistance(float distance) { return distance <= model.distance; } |
| | 65 | |
|
| 0 | 66 | | public bool IsVisible() { return true; } |
| | 67 | |
|
| | 68 | | public bool ShouldReportEvent(WebInterface.ACTION_BUTTON buttonId, HitInfo hit) |
| | 69 | | { |
| 0 | 70 | | return IsAtHoverDistance(hit.distance) && |
| | 71 | | (model.button == "ANY" || buttonId.ToString() == model.button); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public void Report(WebInterface.ACTION_BUTTON buttonId, Ray ray, HitInfo hit) |
| | 75 | | { |
| 0 | 76 | | if (!enabled) |
| 0 | 77 | | return; |
| | 78 | |
|
| 0 | 79 | | if (ShouldReportEvent(buttonId, hit)) |
| | 80 | | { |
| 0 | 81 | | string meshName = null; |
| | 82 | |
|
| 0 | 83 | | if (hit.collider != null) |
| 0 | 84 | | meshName = eventHandler.GetMeshName(hit.collider); |
| | 85 | |
|
| 0 | 86 | | WebInterface.ReportOnPointerDownEvent( |
| | 87 | | buttonId, |
| | 88 | | entity.scene.sceneData.id, |
| | 89 | | model.uuid, |
| | 90 | | entity.entityId, |
| | 91 | | meshName, |
| | 92 | | ray, |
| | 93 | | hit.point, |
| | 94 | | hit.normal, |
| | 95 | | hit.distance); |
| | 96 | |
|
| 0 | 97 | | eventHandler.SetFeedbackState(model.showFeedback, false, model.button, model.hoverText); |
| 0 | 98 | | enabled = false; |
| | 99 | |
|
| 0 | 100 | | OnPointerDownReport?.Invoke(); |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | public PointerEventType GetEventType() { return PointerEventType.DOWN; } |
| | 105 | |
|
| | 106 | | void ReEnableOnInfoCardClosed(bool newState, bool prevState) |
| | 107 | | { |
| 4 | 108 | | if (enabled || newState) |
| 4 | 109 | | return; |
| | 110 | |
|
| 0 | 111 | | enabled = true; |
| 0 | 112 | | } |
| | 113 | | public void SetColliderEnabled(bool newEnabledState) |
| | 114 | | { |
| 2 | 115 | | collider.enabled = newEnabledState; |
| 2 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public Transform GetTransform() { return transform; } |
| | 119 | |
|
| 0 | 120 | | public void OnPoolRelease() { eventHandler.Dispose(); } |
| | 121 | |
|
| 0 | 122 | | public void OnPoolGet() { } |
| | 123 | | } |
| | 124 | | } |