< Summary

Class:DCL.Builder.ProjectSceneCardView
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/Views/Projects/ProjectSceneCardView.cs
Covered lines:36
Uncovered lines:16
Coverable lines:52
Total lines:171
Line coverage:69.2% (36 of 52)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProjectSceneCardView()0%110100%
ProjectSceneCardView()0%110100%
Awake()0%110100%
Setup(...)0%3.013088.89%
OnDestroy()0%110100%
Dispose()0%110100%
SetActive(...)0%550100%
ContextMenuSettingsPressed()0%6200%
SetThumbnail(...)0%7.914037.5%
SetThumbnail(...)0%220100%
SetActiveAnimation()0%4.254075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/Views/Projects/ProjectSceneCardView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Builder;
 5using DCL.Helpers;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.UI;
 9
 10namespace DCL.Builder
 11{
 12
 13    public interface IProjectSceneCardView : IDisposable
 14    {
 15        /// <summary>
 16        ///  Setting button pressed
 17        /// </summary>
 18        event Action<IProjectSceneCardView> 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="isSync">If the scene is sync with the project</param>
 35        void Setup(Scene scene, bool isSync);
 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;
 149        static readonly Vector3 CONTEXT_MENU_OFFSET = new Vector3(6.24f, 12f, 0);
 50        public event Action<IProjectSceneCardView> OnSettingsPressed;
 51
 052        public Scene scene => sceneData;
 053        public Vector3 contextMenuButtonPosition => contextSettingButton.transform.position + CONTEXT_MENU_OFFSET;
 54
 55        [Header("Design Variables")]
 356        [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        [SerializeField] internal Button contextSettingButton;
 68
 69        [SerializeField] internal CanvasGroup canvasGroup;
 70
 71        [Space]
 72        [SerializeField] private RawImageFillParent thumbnail;
 73
 74        private string thumbnailUrl;
 75        private AssetPromise_Texture thumbnailPromise;
 76        private Scene sceneData;
 77
 78        private Coroutine animCoroutine;
 79
 480        private void Awake() { contextSettingButton.onClick.AddListener(ContextMenuSettingsPressed); }
 81
 82        public void Setup(Scene scene, bool isSync)
 83        {
 284            sceneData = scene;
 85
 286            sceneNameTxt.text = scene.title;
 287            sceneCoordsTxt.text = scene.@base.x + "," + scene.@base.y;
 88
 289            string sceneThumbnailUrl = scene.navmapThumbnail;
 290            if (string.IsNullOrEmpty(sceneThumbnailUrl) && scene.parcels != null)
 91            {
 092                sceneThumbnailUrl = MapUtils.GetMarketPlaceThumbnailUrl(scene.parcels,
 93                    THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLACE_HEIGHT, THMBL_MARKETPLACE_SIZEFACTOR);
 94            }
 95
 296            SetThumbnail(sceneThumbnailUrl);
 297            outdatedGameObject.SetActive(!isSync);
 298        }
 99
 4100        private void OnDestroy() { Dispose(); }
 101
 102        public void Dispose()
 103        {
 2104            contextSettingButton.onClick.RemoveAllListeners();
 105
 2106            CoroutineStarter.Stop(animCoroutine);
 2107            AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 2108        }
 109
 110        public void SetActive(bool active)
 111        {
 1112            float from = active ? 0 : 1;
 1113            float to = active ? 1 : 0;
 114
 1115            animCoroutine = CoroutineStarter.Start( SetActiveAnimation(from, to));
 1116        }
 117
 0118        public void ContextMenuSettingsPressed() { OnSettingsPressed?.Invoke(this); }
 119
 120        private void SetThumbnail(string thumbnailUrl)
 121        {
 2122            if (this.thumbnailUrl == thumbnailUrl)
 0123                return;
 124
 2125            this.thumbnailUrl = thumbnailUrl;
 126
 2127            if (thumbnailPromise != null)
 128            {
 0129                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 0130                thumbnailPromise = null;
 131            }
 132
 2133            if (string.IsNullOrEmpty(thumbnailUrl))
 134            {
 2135                SetThumbnail((Texture2D) null);
 2136                return;
 137            }
 138
 0139            loadingImgGameObject.SetActive(true);
 140
 0141            thumbnailPromise = new AssetPromise_Texture(thumbnailUrl);
 0142            thumbnailPromise.OnSuccessEvent += texture => SetThumbnail(texture.texture);
 0143            thumbnailPromise.OnFailEvent += (texture, error) => SetThumbnail((Texture2D) null);
 0144            thumbnail.enabled = false;
 145
 0146            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 0147        }
 148
 149        private void SetThumbnail(Texture2D thumbnailTexture)
 150        {
 2151            loadingImgGameObject.SetActive(false);
 2152            thumbnail.texture = thumbnailTexture ?? defaultThumbnail;
 2153            thumbnail.enabled = true;
 2154        }
 155
 156        IEnumerator SetActiveAnimation(float from, float to)
 157        {
 1158            gameObject.SetActive(true);
 159
 1160            float time = 0;
 161
 1162            while (time < 1)
 163            {
 1164                time += Time.deltaTime * animationSpeed;
 1165                canvasGroup.alpha = Mathf.Lerp(from, to, time);
 1166                yield return null;
 167            }
 0168            gameObject.SetActive(to >= 0.99f);
 0169        }
 170    }
 171}