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