| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Interface; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | internal class LandElementView : MonoBehaviour, IDisposable |
| | 10 | | { |
| | 11 | | internal const string SIZE_TEXT_FORMAT = "{0} LAND"; |
| | 12 | |
|
| | 13 | | public event Action<Vector2Int> OnJumpInPressed; |
| | 14 | | public event Action<Vector2Int> OnEditorPressed; |
| | 15 | | public event Action<string> OnSettingsPressed; |
| | 16 | | public event Action<string> OnOpenInDappPressed; |
| | 17 | |
|
| | 18 | | [SerializeField] private Texture2D defaultThumbnail; |
| | 19 | | [SerializeField] private RawImageFillParent thumbnail; |
| | 20 | | [SerializeField] internal TextMeshProUGUI landName; |
| | 21 | | [SerializeField] private TextMeshProUGUI landCoords; |
| | 22 | | [SerializeField] internal TextMeshProUGUI landSize; |
| | 23 | | [SerializeField] internal GameObject landSizeGO; |
| | 24 | | [SerializeField] internal GameObject roleOwner; |
| | 25 | | [SerializeField] internal GameObject roleOperator; |
| | 26 | | [SerializeField] internal Button buttonSettings; |
| | 27 | | [SerializeField] internal Button buttonJumpIn; |
| | 28 | | [SerializeField] internal Button buttonEditor; |
| | 29 | | [SerializeField] internal Button buttonOpenInBuilderDapp; |
| | 30 | | [SerializeField] internal UIHoverTriggerShowHideAnimator editorLocked; |
| | 31 | | [SerializeField] private ShowHideAnimator editorLockedTooltipEstate; |
| | 32 | | [SerializeField] private ShowHideAnimator editorLockedTooltipSdkScene; |
| | 33 | | [SerializeField] internal Animator loadingAnimator; |
| | 34 | |
|
| 1 | 35 | | private static readonly int isLoadingAnimation = Animator.StringToHash("isLoading"); |
| | 36 | |
|
| 15 | 37 | | public ISearchInfo searchInfo { get; } = new SearchInfo(); |
| | 38 | |
|
| | 39 | | private bool isDestroyed = false; |
| | 40 | | private string landId; |
| | 41 | | private string landCoordinates; |
| | 42 | | private string thumbnailUrl; |
| | 43 | | private AssetPromise_Texture thumbnailPromise; |
| | 44 | | private Vector2Int coords; |
| | 45 | | private bool isLoadingThumbnail = false; |
| | 46 | |
|
| | 47 | | private LandWithAccess currentLand; |
| | 48 | | private void Awake() |
| | 49 | | { |
| 14 | 50 | | buttonSettings.onClick.AddListener(() => OnSettingsPressed?.Invoke(landId)); |
| 13 | 51 | | buttonJumpIn.onClick.AddListener(JumpInButtonPressed); |
| 13 | 52 | | buttonEditor.onClick.AddListener(EditorButtonPressed); |
| 14 | 53 | | buttonOpenInBuilderDapp.onClick.AddListener(() => OnOpenInDappPressed?.Invoke(landId)); |
| | 54 | |
|
| 13 | 55 | | editorLockedTooltipEstate.gameObject.SetActive(false); |
| 13 | 56 | | editorLockedTooltipSdkScene.gameObject.SetActive(false); |
| 13 | 57 | | } |
| | 58 | |
|
| | 59 | | private void JumpInButtonPressed() |
| | 60 | | { |
| 1 | 61 | | if (currentLand != null) |
| | 62 | | { |
| 0 | 63 | | string ownership = currentLand.role == LandRole.OWNER ? "Owner" : "Operator"; |
| 0 | 64 | | BIWAnalytics.PlayerJumpOrEdit("Lands", "JumpIn", coords, ownership); |
| | 65 | | } |
| 1 | 66 | | OnJumpInPressed?.Invoke(coords); |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | private void EditorButtonPressed() |
| | 70 | | { |
| 1 | 71 | | if (currentLand != null) |
| | 72 | | { |
| 0 | 73 | | string ownership = currentLand.role == LandRole.OWNER ? "Owner" : "Operator"; |
| 0 | 74 | | BIWAnalytics.PlayerJumpOrEdit("Lands", "Editor", coords, ownership); |
| | 75 | | } |
| 1 | 76 | | OnEditorPressed?.Invoke(coords); |
| 1 | 77 | | } |
| | 78 | |
|
| | 79 | | private void OnDestroy() |
| | 80 | | { |
| 13 | 81 | | isDestroyed = true; |
| | 82 | |
|
| 13 | 83 | | if (thumbnailPromise != null) |
| | 84 | | { |
| 3 | 85 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| 3 | 86 | | thumbnailPromise = null; |
| | 87 | | } |
| 13 | 88 | | } |
| | 89 | |
|
| 34 | 90 | | private void OnEnable() { loadingAnimator.SetBool(isLoadingAnimation, isLoadingThumbnail); } |
| | 91 | |
|
| 10 | 92 | | public void SetActive(bool active) { gameObject.SetActive(active); } |
| | 93 | |
|
| | 94 | | public void Setup(LandWithAccess land) |
| | 95 | | { |
| 5 | 96 | | currentLand = land; |
| 5 | 97 | | bool estate = land.type == LandType.ESTATE; |
| | 98 | |
|
| 5 | 99 | | SetId(land.id); |
| 5 | 100 | | SetName(land.name); |
| 5 | 101 | | SetCoords(land.baseCoords.x, land.baseCoords.y); |
| 5 | 102 | | SetSize(land.size); |
| 5 | 103 | | SetRole(land.role == LandRole.OWNER); |
| 5 | 104 | | SetEditable(!estate); |
| | 105 | |
|
| 5 | 106 | | if (estate) |
| | 107 | | { |
| 0 | 108 | | editorLocked.SetShowHideAnimator(editorLockedTooltipEstate); |
| 0 | 109 | | } |
| 5 | 110 | | else if (land.scenes != null && land.scenes.Count > 0 && land.scenes[0].source == DeployedScene.Source.SDK) |
| | 111 | | { |
| 0 | 112 | | editorLocked.SetShowHideAnimator(editorLockedTooltipSdkScene); |
| 0 | 113 | | SetEditable(false); |
| | 114 | | } |
| 5 | 115 | | } |
| | 116 | |
|
| | 117 | | public void SetId(string id) |
| | 118 | | { |
| 5 | 119 | | landId = id; |
| 5 | 120 | | searchInfo.SetId(id); |
| 5 | 121 | | } |
| | 122 | |
|
| 0 | 123 | | public string GetId() { return landId; } |
| | 124 | |
|
| | 125 | | public void SetName(string name) |
| | 126 | | { |
| 6 | 127 | | landName.text = name; |
| 6 | 128 | | searchInfo.SetName(name); |
| 6 | 129 | | } |
| | 130 | |
|
| | 131 | | public void SetCoords(int x, int y) |
| | 132 | | { |
| 5 | 133 | | landCoordinates = $"{x},{y}"; |
| 5 | 134 | | landCoords.text = landCoordinates; |
| 5 | 135 | | searchInfo.SetCoords(landCoordinates); |
| 5 | 136 | | coords.Set(x, y); |
| 5 | 137 | | } |
| | 138 | |
|
| | 139 | | public void SetSize(int size) |
| | 140 | | { |
| 7 | 141 | | landSizeGO.SetActive(size > 0); |
| 7 | 142 | | landSize.text = string.Format(SIZE_TEXT_FORMAT, size); |
| 7 | 143 | | searchInfo.SetSize(size); |
| 7 | 144 | | } |
| | 145 | |
|
| | 146 | | public void SetRole(bool isOwner) |
| | 147 | | { |
| 7 | 148 | | roleOwner.SetActive(isOwner); |
| 7 | 149 | | roleOperator.SetActive(!isOwner); |
| 7 | 150 | | searchInfo.SetRole(isOwner); |
| 7 | 151 | | } |
| | 152 | |
|
| 12 | 153 | | public Transform GetParent() { return transform.parent; } |
| | 154 | |
|
| 10 | 155 | | public void SetParent(Transform parent) { transform.SetParent(parent); } |
| | 156 | |
|
| | 157 | | public void SetThumbnail(string url) |
| | 158 | | { |
| 5 | 159 | | if (url == thumbnailUrl) |
| 2 | 160 | | return; |
| | 161 | |
|
| 3 | 162 | | isLoadingThumbnail = true; |
| 3 | 163 | | loadingAnimator.SetBool(isLoadingAnimation, isLoadingThumbnail); |
| 3 | 164 | | thumbnailUrl = url; |
| | 165 | |
|
| 3 | 166 | | var prevPromise = thumbnailPromise; |
| | 167 | |
|
| 3 | 168 | | if (string.IsNullOrEmpty(url)) |
| | 169 | | { |
| 0 | 170 | | SetThumbnail(defaultThumbnail); |
| 0 | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| 3 | 174 | | thumbnailPromise = new AssetPromise_Texture(url); |
| 3 | 175 | | thumbnailPromise.OnSuccessEvent += asset => SetThumbnail(asset.texture); |
| 3 | 176 | | thumbnailPromise.OnFailEvent += asset => SetThumbnail(defaultThumbnail); |
| 3 | 177 | | AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise); |
| | 178 | | } |
| | 179 | |
|
| 3 | 180 | | if (prevPromise != null) |
| | 181 | | { |
| 0 | 182 | | AssetPromiseKeeper_Texture.i.Forget(prevPromise); |
| | 183 | | } |
| 3 | 184 | | } |
| | 185 | |
|
| | 186 | | public void SetThumbnail(Texture thumbnailTexture) |
| | 187 | | { |
| 0 | 188 | | thumbnail.texture = thumbnailTexture; |
| 0 | 189 | | isLoadingThumbnail = false; |
| 0 | 190 | | loadingAnimator.SetBool(isLoadingAnimation, isLoadingThumbnail); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | public void SetEditable(bool isEditable) |
| | 194 | | { |
| 5 | 195 | | buttonEditor.gameObject.SetActive(isEditable); |
| 5 | 196 | | editorLocked.gameObject.SetActive(!isEditable); |
| 5 | 197 | | } |
| | 198 | |
|
| | 199 | | public void Dispose() |
| | 200 | | { |
| 3 | 201 | | if (!isDestroyed) |
| | 202 | | { |
| 3 | 203 | | Destroy(gameObject); |
| | 204 | | } |
| 3 | 205 | | } |
| | 206 | | } |