| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | internal interface ISceneCardView : IDisposable |
| | 9 | | { |
| | 10 | | event Action<Vector2Int> OnJumpInPressed; |
| | 11 | | event Action<Vector2Int> OnEditorPressed; |
| | 12 | | event Action<ISceneData> OnSettingsPressed; |
| | 13 | | event Action<ISceneData, ISceneCardView> OnContextMenuPressed; |
| | 14 | | ISceneData sceneData { get; } |
| | 15 | | ISearchInfo searchInfo { get; } |
| | 16 | | Vector3 contextMenuButtonPosition { get; } |
| | 17 | | void Setup(ISceneData sceneData); |
| | 18 | | void SetParent(Transform parent); |
| | 19 | | void SetToDefaultParent(); |
| | 20 | | void ConfigureDefaultParent(Transform parent); |
| | 21 | | void SetName(string name); |
| | 22 | | void SetCoords(Vector2Int coords); |
| | 23 | | void SetSize(Vector2Int size); |
| | 24 | | void SetThumbnail(string thumbnailUrl); |
| | 25 | | void SetThumbnail(Texture2D thumbnailTexture); |
| | 26 | | void SetDeployed(bool deployed); |
| | 27 | | void SetEditable(bool isEditable); |
| | 28 | | void SetUserRole(bool isOwner, bool isOperator, bool isContributor); |
| | 29 | | void SetActive(bool active); |
| | 30 | | void SetSiblingIndex(int index); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | internal class SceneCardView : MonoBehaviour, ISceneCardView |
| | 34 | | { |
| | 35 | | const int THMBL_MARKETPLACE_WIDTH = 196; |
| | 36 | | const int THMBL_MARKETPLACE_HEIGHT = 143; |
| | 37 | | const int THMBL_MARKETPLACE_SIZEFACTOR = 50; |
| | 38 | |
|
| 1 | 39 | | static readonly Vector3 CONTEXT_MENU_OFFSET = new Vector3(6.24f, 12f, 0); |
| | 40 | |
|
| | 41 | | public event Action<Vector2Int> OnJumpInPressed; |
| | 42 | | public event Action<Vector2Int> OnEditorPressed; |
| | 43 | | public event Action<ISceneData> OnSettingsPressed; |
| | 44 | | public event Action<ISceneData, ISceneCardView> OnContextMenuPressed; |
| | 45 | |
|
| | 46 | | [SerializeField] private Texture2D defaultThumbnail; |
| | 47 | |
|
| | 48 | | [Space] |
| | 49 | | [SerializeField] private RawImageFillParent thumbnail; |
| | 50 | |
|
| | 51 | | [SerializeField] private TextMeshProUGUI sceneName; |
| | 52 | |
|
| | 53 | | [Space] |
| | 54 | | [SerializeField] internal GameObject coordsContainer; |
| | 55 | |
|
| | 56 | | [SerializeField] private TextMeshProUGUI coordsText; |
| | 57 | |
|
| | 58 | | [Space] |
| | 59 | | [SerializeField] internal GameObject sizeContainer; |
| | 60 | |
|
| | 61 | | [SerializeField] private TextMeshProUGUI sizeText; |
| | 62 | |
|
| | 63 | | [Space] |
| | 64 | | [SerializeField] internal Button jumpInButton; |
| | 65 | |
|
| | 66 | | [SerializeField] internal Button editorButton; |
| | 67 | | [SerializeField] internal Button contextMenuButton; |
| | 68 | | [SerializeField] internal Button settingsButton; |
| | 69 | |
|
| | 70 | | [Space] |
| | 71 | | [SerializeField] internal GameObject roleOwnerGO; |
| | 72 | |
|
| | 73 | | [SerializeField] internal GameObject roleOperatorGO; |
| | 74 | | [SerializeField] internal GameObject roleContributorGO; |
| | 75 | |
|
| | 76 | | [Space] |
| | 77 | | [SerializeField] internal GameObject editorLockedGO; |
| | 78 | |
|
| | 79 | | [SerializeField] internal GameObject editorLockedTooltipGO; |
| | 80 | |
|
| | 81 | | [SerializeField] internal Animator loadingAnimator; |
| | 82 | |
|
| 1 | 83 | | private static readonly int isLoadingAnimation = Animator.StringToHash("isLoading"); |
| | 84 | |
|
| 321 | 85 | | ISearchInfo ISceneCardView.searchInfo { get; } = new SearchInfo(); |
| 93 | 86 | | ISceneData ISceneCardView.sceneData => sceneData; |
| 0 | 87 | | Vector3 ISceneCardView.contextMenuButtonPosition => contextMenuButton.transform.position + CONTEXT_MENU_OFFSET; |
| | 88 | |
|
| | 89 | | private ISceneData sceneData; |
| | 90 | | private string thumbnailUrl = null; |
| | 91 | | private AssetPromise_Texture thumbnailPromise; |
| | 92 | | private bool isDestroyed = false; |
| | 93 | | private Transform defaultParent; |
| | 94 | | private bool isLoadingThumbnail = false; |
| | 95 | |
|
| | 96 | | private void Awake() |
| | 97 | | { |
| 33 | 98 | | jumpInButton.onClick.AddListener( JumpInButtonPressed ); |
| 33 | 99 | | editorButton.onClick.AddListener( EditorButtonPressed ); |
| 33 | 100 | | contextMenuButton.onClick.AddListener(() => OnContextMenuPressed?.Invoke(sceneData, this)); |
| 33 | 101 | | settingsButton.onClick.AddListener(() => OnSettingsPressed?.Invoke(sceneData)); |
| | 102 | |
|
| 33 | 103 | | editorLockedGO.SetActive(false); |
| 33 | 104 | | editorLockedTooltipGO.SetActive(false); |
| 33 | 105 | | } |
| | 106 | |
|
| | 107 | | private void JumpInButtonPressed() |
| | 108 | | { |
| 0 | 109 | | OnJumpInPressed?.Invoke(sceneData.coords); |
| 0 | 110 | | BIWAnalytics.PlayerJumpOrEdit("Scene", "JumpIn", sceneData.coords, "Scene Owner"); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | private void EditorButtonPressed() |
| | 114 | | { |
| 0 | 115 | | OnEditorPressed?.Invoke(sceneData.coords); |
| 0 | 116 | | BIWAnalytics.PlayerJumpOrEdit("Scene", "Editor", sceneData.coords, "Scene Owner"); |
| 0 | 117 | | } |
| | 118 | |
|
| 146 | 119 | | private void OnEnable() { loadingAnimator.SetBool(isLoadingAnimation, isLoadingThumbnail); } |
| | 120 | |
|
| | 121 | | void ISceneCardView.Setup(ISceneData sceneData) |
| | 122 | | { |
| 51 | 123 | | this.sceneData = sceneData; |
| | 124 | |
|
| 51 | 125 | | string sceneThumbnailUrl = sceneData.thumbnailUrl; |
| 51 | 126 | | if (string.IsNullOrEmpty(sceneThumbnailUrl) && sceneData.parcels != null) |
| | 127 | | { |
| 0 | 128 | | sceneThumbnailUrl = MapUtils.GetMarketPlaceThumbnailUrl(sceneData.parcels, |
| | 129 | | THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLACE_HEIGHT, THMBL_MARKETPLACE_SIZEFACTOR); |
| | 130 | | } |
| | 131 | |
|
| 51 | 132 | | ISceneCardView thisView = this; |
| 51 | 133 | | thisView.SetThumbnail(sceneThumbnailUrl); |
| 51 | 134 | | thisView.SetName(sceneData.name); |
| 51 | 135 | | thisView.SetCoords(sceneData.coords); |
| 51 | 136 | | thisView.SetSize(sceneData.size); |
| 51 | 137 | | thisView.SetDeployed(sceneData.isDeployed); |
| 51 | 138 | | thisView.SetUserRole(sceneData.isOwner, sceneData.isOperator, sceneData.isContributor); |
| 51 | 139 | | thisView.SetEditable(sceneData.isEditable); |
| | 140 | |
|
| 51 | 141 | | thisView.searchInfo.SetId(sceneData.id); |
| 51 | 142 | | } |
| | 143 | |
|
| | 144 | | void ISceneCardView.SetParent(Transform parent) |
| | 145 | | { |
| 41 | 146 | | if (transform.parent == parent) |
| 15 | 147 | | return; |
| | 148 | |
|
| 26 | 149 | | transform.SetParent(parent); |
| 26 | 150 | | transform.ResetLocalTRS(); |
| 26 | 151 | | } |
| | 152 | |
|
| | 153 | | void ISceneCardView.SetName(string name) |
| | 154 | | { |
| 51 | 155 | | sceneName.text = name; |
| 51 | 156 | | ((ISceneCardView)this).searchInfo.SetName(name); |
| 51 | 157 | | } |
| | 158 | |
|
| | 159 | | void ISceneCardView.SetCoords(Vector2Int coords) |
| | 160 | | { |
| 51 | 161 | | string coordStr = $"{coords.x},{coords.y}"; |
| 51 | 162 | | coordsText.text = coordStr; |
| 51 | 163 | | ((ISceneCardView)this).searchInfo.SetCoords(coordStr); |
| 51 | 164 | | } |
| | 165 | |
|
| | 166 | | void ISceneCardView.SetSize(Vector2Int size) |
| | 167 | | { |
| 51 | 168 | | sizeText.text = $"{size.x},{size.y}m"; |
| 51 | 169 | | ((ISceneCardView)this).searchInfo.SetSize(size.x * size.y); |
| 51 | 170 | | } |
| | 171 | |
|
| | 172 | | void ISceneCardView.SetThumbnail(string thumbnailUrl) |
| | 173 | | { |
| 51 | 174 | | if (this.thumbnailUrl == thumbnailUrl) |
| 51 | 175 | | return; |
| | 176 | |
|
| 0 | 177 | | this.thumbnailUrl = thumbnailUrl; |
| | 178 | |
|
| 0 | 179 | | isLoadingThumbnail = true; |
| 0 | 180 | | loadingAnimator.SetBool(isLoadingAnimation, isLoadingThumbnail); |
| | 181 | |
|
| 0 | 182 | | if (thumbnailPromise != null) |
| | 183 | | { |
| 0 | 184 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| 0 | 185 | | thumbnailPromise = null; |
| | 186 | | } |
| | 187 | |
|
| | 188 | |
|
| 0 | 189 | | if (string.IsNullOrEmpty(thumbnailUrl)) |
| | 190 | | { |
| 0 | 191 | | ((ISceneCardView)this).SetThumbnail((Texture2D) null); |
| 0 | 192 | | return; |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | thumbnailPromise = new AssetPromise_Texture(thumbnailUrl); |
| 0 | 196 | | thumbnailPromise.OnSuccessEvent += texture => ((ISceneCardView)this).SetThumbnail(texture.texture); |
| 0 | 197 | | thumbnailPromise.OnFailEvent += texture => ((ISceneCardView)this).SetThumbnail((Texture2D) null); |
| | 198 | |
|
| 0 | 199 | | AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise); |
| 0 | 200 | | } |
| | 201 | |
|
| | 202 | | void ISceneCardView.SetThumbnail(Texture2D thumbnailTexture) |
| | 203 | | { |
| 0 | 204 | | thumbnail.texture = thumbnailTexture ?? defaultThumbnail; |
| 0 | 205 | | isLoadingThumbnail = false; |
| 0 | 206 | | loadingAnimator.SetBool(isLoadingAnimation, isLoadingThumbnail); |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | void ISceneCardView.SetDeployed(bool deployed) |
| | 210 | | { |
| 51 | 211 | | coordsContainer.SetActive(deployed); |
| 51 | 212 | | sizeContainer.SetActive(!deployed); |
| 51 | 213 | | jumpInButton.gameObject.SetActive(deployed); |
| 51 | 214 | | } |
| | 215 | |
|
| | 216 | | void ISceneCardView.SetUserRole(bool isOwner, bool isOperator, bool isContributor) |
| | 217 | | { |
| 51 | 218 | | roleOwnerGO.SetActive(false); |
| 51 | 219 | | roleOperatorGO.SetActive(false); |
| 51 | 220 | | roleContributorGO.SetActive(false); |
| 51 | 221 | | ((ISceneCardView)this).searchInfo.SetRole(isOwner); |
| | 222 | |
|
| 51 | 223 | | if (isOwner) |
| | 224 | | { |
| 1 | 225 | | roleOwnerGO.SetActive(true); |
| 1 | 226 | | } |
| 50 | 227 | | else if (isOperator) |
| | 228 | | { |
| 0 | 229 | | roleOperatorGO.SetActive(true); |
| 0 | 230 | | } |
| 50 | 231 | | else if (isContributor) |
| | 232 | | { |
| 3 | 233 | | roleContributorGO.SetActive(true); |
| | 234 | | } |
| 50 | 235 | | } |
| | 236 | |
|
| 136 | 237 | | void ISceneCardView.SetActive(bool active) { gameObject.SetActive(active); } |
| | 238 | |
|
| 82 | 239 | | void ISceneCardView.SetSiblingIndex(int index) { transform.SetSiblingIndex(index); } |
| 26 | 240 | | void ISceneCardView.SetToDefaultParent() { transform.SetParent(defaultParent); } |
| | 241 | |
|
| 14 | 242 | | void ISceneCardView.ConfigureDefaultParent(Transform parent) { defaultParent = parent; } |
| | 243 | |
|
| | 244 | | void ISceneCardView.SetEditable(bool isEditable) |
| | 245 | | { |
| 51 | 246 | | editorButton.gameObject.SetActive(isEditable); |
| 51 | 247 | | editorLockedGO.SetActive(!isEditable); |
| 51 | 248 | | } |
| | 249 | |
|
| | 250 | | public void Dispose() |
| | 251 | | { |
| 27 | 252 | | if (!isDestroyed) |
| | 253 | | { |
| 27 | 254 | | Destroy(gameObject); |
| | 255 | | } |
| 27 | 256 | | } |
| | 257 | |
|
| | 258 | | private void OnDestroy() |
| | 259 | | { |
| 33 | 260 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| 33 | 261 | | isDestroyed = true; |
| 33 | 262 | | } |
| | 263 | | } |