| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Builder; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Builder |
| | 10 | | { |
| | 11 | |
|
| | 12 | |
|
| | 13 | | internal interface IProjectSceneCardView : IDisposable |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Setting button pressed |
| | 17 | | /// </summary> |
| | 18 | | event Action<ProjectData> OnSettingsPressed; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Data of the project card |
| | 22 | | /// </summary> |
| | 23 | | Scene scene { get; } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Position of the context menu button |
| | 27 | | /// </summary> |
| | 28 | | Vector3 contextMenuButtonPosition { get; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// This setup the scene data |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="scene"></param> |
| | 34 | | /// <param name="outdated">If the scene is sync with the project</param> |
| | 35 | | void Setup(Scene scene, bool outdated); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Active the card |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="active"></param> |
| | 41 | | void SetActive(bool active); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public class ProjectSceneCardView : MonoBehaviour, IProjectSceneCardView |
| | 45 | | { |
| | 46 | | const int THMBL_MARKETPLACE_WIDTH = 196; |
| | 47 | | const int THMBL_MARKETPLACE_HEIGHT = 143; |
| | 48 | | const int THMBL_MARKETPLACE_SIZEFACTOR = 50; |
| | 49 | |
|
| | 50 | | public event Action<ProjectData> OnSettingsPressed; |
| | 51 | |
|
| 0 | 52 | | public Scene scene => sceneData; |
| 0 | 53 | | public Vector3 contextMenuButtonPosition { get; } |
| | 54 | |
|
| | 55 | | [Header("Design Variables")] |
| 3 | 56 | | [SerializeField] private float animationSpeed = 6f; |
| | 57 | |
|
| | 58 | | [Header("Project References")] |
| | 59 | | [SerializeField] private Texture2D defaultThumbnail; |
| | 60 | |
|
| | 61 | | [Header("Prefab references")] |
| | 62 | | [SerializeField] private GameObject loadingImgGameObject; |
| | 63 | | [SerializeField] internal GameObject outdatedGameObject; |
| | 64 | |
|
| | 65 | | [SerializeField] internal TextMeshProUGUI sceneNameTxt; |
| | 66 | | [SerializeField] internal TextMeshProUGUI sceneCoordsTxt; |
| | 67 | |
|
| | 68 | | [SerializeField] internal CanvasGroup canvasGroup; |
| | 69 | |
|
| | 70 | | [Space] |
| | 71 | | [SerializeField] private RawImageFillParent thumbnail; |
| | 72 | |
|
| | 73 | | private string thumbnailUrl; |
| | 74 | | private AssetPromise_Texture thumbnailPromise; |
| | 75 | | private Scene sceneData; |
| | 76 | |
|
| | 77 | | public void Setup(Scene scene, bool outdated) |
| | 78 | | { |
| 2 | 79 | | sceneData = scene; |
| | 80 | |
|
| 2 | 81 | | sceneNameTxt.text = scene.title; |
| 2 | 82 | | sceneCoordsTxt.text = scene.@base.x + "," + scene.@base.y; |
| | 83 | |
|
| 2 | 84 | | string sceneThumbnailUrl = scene.navmapThumbnail; |
| 2 | 85 | | if (string.IsNullOrEmpty(sceneThumbnailUrl) && scene.parcels != null) |
| | 86 | | { |
| 0 | 87 | | sceneThumbnailUrl = MapUtils.GetMarketPlaceThumbnailUrl(scene.parcels, |
| | 88 | | THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLACE_HEIGHT, THMBL_MARKETPLACE_SIZEFACTOR); |
| | 89 | | } |
| | 90 | |
|
| 2 | 91 | | SetThumbnail(sceneThumbnailUrl); |
| 2 | 92 | | outdatedGameObject.SetActive(outdated); |
| 2 | 93 | | } |
| | 94 | |
|
| | 95 | | private void OnDestroy() |
| | 96 | | { |
| 2 | 97 | | Dispose(); |
| 2 | 98 | | } |
| | 99 | |
|
| | 100 | | public void Dispose() |
| | 101 | | { |
| 2 | 102 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| 2 | 103 | | } |
| | 104 | |
|
| | 105 | | public void SetActive(bool active) |
| | 106 | | { |
| 1 | 107 | | float from = active ? 0 : 1; |
| 1 | 108 | | float to = active ? 1 : 0; |
| 1 | 109 | | CoroutineStarter.Start( SetActiveAnimation(from,to)); |
| 1 | 110 | | } |
| | 111 | |
|
| | 112 | | private void SetThumbnail(string thumbnailUrl) |
| | 113 | | { |
| 2 | 114 | | if (this.thumbnailUrl == thumbnailUrl) |
| 0 | 115 | | return; |
| | 116 | |
|
| 2 | 117 | | this.thumbnailUrl = thumbnailUrl; |
| | 118 | |
|
| 2 | 119 | | if (thumbnailPromise != null) |
| | 120 | | { |
| 0 | 121 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| 0 | 122 | | thumbnailPromise = null; |
| | 123 | | } |
| | 124 | |
|
| 2 | 125 | | if (string.IsNullOrEmpty(thumbnailUrl)) |
| | 126 | | { |
| 2 | 127 | | SetThumbnail((Texture2D) null); |
| 2 | 128 | | return; |
| | 129 | | } |
| | 130 | |
|
| 0 | 131 | | loadingImgGameObject.SetActive(true); |
| | 132 | |
|
| 0 | 133 | | thumbnailPromise = new AssetPromise_Texture(thumbnailUrl); |
| 0 | 134 | | thumbnailPromise.OnSuccessEvent += texture => SetThumbnail(texture.texture); |
| 0 | 135 | | thumbnailPromise.OnFailEvent += (texture, error) => SetThumbnail((Texture2D) null); |
| 0 | 136 | | thumbnail.enabled = false; |
| | 137 | |
|
| 0 | 138 | | AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private void SetThumbnail(Texture2D thumbnailTexture) |
| | 142 | | { |
| 2 | 143 | | loadingImgGameObject.SetActive(false); |
| 2 | 144 | | thumbnail.texture = thumbnailTexture ?? defaultThumbnail; |
| 2 | 145 | | thumbnail.enabled = true; |
| 2 | 146 | | } |
| | 147 | |
|
| | 148 | | IEnumerator SetActiveAnimation(float from, float to) |
| | 149 | | { |
| 1 | 150 | | gameObject.SetActive(true); |
| | 151 | |
|
| 1 | 152 | | float time = 0; |
| | 153 | |
|
| 2 | 154 | | while (time < 1) |
| | 155 | | { |
| 2 | 156 | | time += Time.deltaTime * animationSpeed; |
| 2 | 157 | | canvasGroup.alpha = Mathf.Lerp(from,to,time); |
| 1 | 158 | | yield return null; |
| | 159 | | } |
| 0 | 160 | | gameObject.SetActive(to >= 0.99f); |
| 0 | 161 | | } |
| | 162 | | } |
| | 163 | | } |