| | 1 | | using AvatarSystem; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using DCLPlugins.UUIDEventComponentsPlugin.UUIDComponent.Interfaces; |
| | 5 | | using System; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Complementary class to detect avatar outlining only, |
| | 12 | | /// It does not live on its own and should coexist with `AvatarOnPointerDown` |
| | 13 | | /// </summary> |
| | 14 | | public class AvatarOutlineOnHoverEvent : MonoBehaviour, IUnlockedCursorInputEvent, IPoolLifecycleHandler |
| | 15 | | { |
| | 16 | | private IAvatar avatar; |
| | 17 | | private OnPointerEvent.Model model; |
| | 18 | | private bool isHovered; |
| | 19 | |
|
| 655 | 20 | | public bool ShouldBeHoveredWhenMouseIsLocked { get; set; } = true; |
| | 21 | |
|
| | 22 | | public event Action OnPointerEnterReport; |
| | 23 | | public event Action OnPointerExitReport; |
| | 24 | |
|
| | 25 | | private bool isInitialized; |
| | 26 | |
|
| | 27 | | public void Initialize(IDCLEntity entity, IAvatar avatar) |
| | 28 | | { |
| 0 | 29 | | if (isInitialized) return; |
| 0 | 30 | | this.entity = entity; |
| 0 | 31 | | this.avatar = avatar; |
| 0 | 32 | | model = new OnPointerEvent.Model(); |
| | 33 | |
|
| 0 | 34 | | CommonScriptableObjects.allUIHidden.OnChange += AllUIHiddenChanged; |
| 0 | 35 | | isInitialized = true; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | private void OnDestroy() |
| | 39 | | { |
| 634 | 40 | | isInitialized = false; |
| 634 | 41 | | CommonScriptableObjects.allUIHidden.OnChange -= AllUIHiddenChanged; |
| 634 | 42 | | } |
| | 43 | |
|
| | 44 | | public Transform GetTransform() => |
| 0 | 45 | | transform; |
| | 46 | |
|
| 0 | 47 | | public IDCLEntity entity { get; private set; } |
| | 48 | |
|
| | 49 | | public void SetHoverState(bool state) |
| | 50 | | { |
| 0 | 51 | | if (!enabled) return; |
| 0 | 52 | | if (isHovered == state) return; |
| | 53 | |
|
| 0 | 54 | | isHovered = state; |
| | 55 | |
|
| 0 | 56 | | if (isHovered) |
| | 57 | | { |
| 0 | 58 | | SetAvatarOutlined(); |
| 0 | 59 | | OnPointerEnterReport?.Invoke(); |
| | 60 | | } |
| | 61 | | else |
| | 62 | | { |
| 0 | 63 | | ResetAvatarOutlined(); |
| 0 | 64 | | OnPointerExitReport?.Invoke(); |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public bool IsAtHoverDistance(float distance) |
| | 69 | | { |
| 0 | 70 | | bool isCursorLocked = Utils.IsCursorLocked; |
| 0 | 71 | | if (!ShouldBeHoveredWhenMouseIsLocked && isCursorLocked) return false; |
| 0 | 72 | | return !isCursorLocked || distance <= model.distance; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public bool IsVisible() => |
| 0 | 76 | | true; |
| | 77 | |
|
| | 78 | | public void OnPoolRelease() |
| | 79 | | { |
| 0 | 80 | | avatar = null; |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | public void OnPoolGet() { } |
| | 84 | |
|
| | 85 | | private void AllUIHiddenChanged(bool isAllUIHidden, bool _) |
| | 86 | | { |
| 0 | 87 | | if (isAllUIHidden) |
| 0 | 88 | | ResetAvatarOutlined(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void ResetAvatarOutlined() |
| | 92 | | { |
| 0 | 93 | | DataStore.i.outliner.avatarOutlined.Set((null, -1, -1)); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private void SetAvatarOutlined() |
| | 97 | | { |
| 0 | 98 | | if (avatar.status == IAvatar.Status.Loaded) |
| | 99 | | { |
| 0 | 100 | | var renderer = avatar.GetMainRenderer(); |
| | 101 | |
|
| 0 | 102 | | if (renderer != null && !CommonScriptableObjects.allUIHidden.Get()) |
| 0 | 103 | | DataStore.i.outliner.avatarOutlined.Set((renderer, renderer.GetComponent<MeshFilter>().sharedMesh.su |
| | 104 | | } |
| 0 | 105 | | } |
| | 106 | | } |
| | 107 | | } |