< Summary

Class:DCL.Builder.ProjectSceneCardView
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/Views/ProjectSceneCardView.cs
Covered lines:33
Uncovered lines:15
Coverable lines:48
Total lines:163
Line coverage:68.7% (33 of 48)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ProjectSceneCardView()0%110100%
Setup(...)0%3.013088.89%
OnDestroy()0%110100%
Dispose()0%110100%
SetActive(...)0%550100%
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/ProjectSceneCardView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Builder;
 5using DCL.Helpers;
 6using TMPro;
 7using UnityEngine;
 8
 9namespace 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
 052        public Scene scene => sceneData;
 053        public Vector3 contextMenuButtonPosition { get; }
 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
 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        {
 279            sceneData = scene;
 80
 281            sceneNameTxt.text = scene.title;
 282            sceneCoordsTxt.text = scene.@base.x + "," + scene.@base.y;
 83
 284            string sceneThumbnailUrl = scene.navmapThumbnail;
 285            if (string.IsNullOrEmpty(sceneThumbnailUrl) && scene.parcels != null)
 86            {
 087                sceneThumbnailUrl = MapUtils.GetMarketPlaceThumbnailUrl(scene.parcels,
 88                    THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLACE_HEIGHT, THMBL_MARKETPLACE_SIZEFACTOR);
 89            }
 90
 291            SetThumbnail(sceneThumbnailUrl);
 292            outdatedGameObject.SetActive(outdated);
 293        }
 94
 95        private void OnDestroy()
 96        {
 297            Dispose();
 298        }
 99
 100        public void Dispose()
 101        {
 2102            AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 2103        }
 104
 105        public void SetActive(bool active)
 106        {
 1107            float from = active ? 0 : 1;
 1108            float to = active ? 1 : 0;
 1109            CoroutineStarter.Start( SetActiveAnimation(from,to));
 1110        }
 111
 112        private void SetThumbnail(string thumbnailUrl)
 113        {
 2114            if (this.thumbnailUrl == thumbnailUrl)
 0115                return;
 116
 2117            this.thumbnailUrl = thumbnailUrl;
 118
 2119            if (thumbnailPromise != null)
 120            {
 0121                AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 0122                thumbnailPromise = null;
 123            }
 124
 2125            if (string.IsNullOrEmpty(thumbnailUrl))
 126            {
 2127                SetThumbnail((Texture2D) null);
 2128                return;
 129            }
 130
 0131            loadingImgGameObject.SetActive(true);
 132
 0133            thumbnailPromise = new AssetPromise_Texture(thumbnailUrl);
 0134            thumbnailPromise.OnSuccessEvent += texture => SetThumbnail(texture.texture);
 0135            thumbnailPromise.OnFailEvent += (texture, error) => SetThumbnail((Texture2D) null);
 0136            thumbnail.enabled = false;
 137
 0138            AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 0139        }
 140
 141        private void SetThumbnail(Texture2D thumbnailTexture)
 142        {
 2143            loadingImgGameObject.SetActive(false);
 2144            thumbnail.texture = thumbnailTexture ?? defaultThumbnail;
 2145            thumbnail.enabled = true;
 2146        }
 147
 148        IEnumerator SetActiveAnimation(float from, float to)
 149        {
 1150            gameObject.SetActive(true);
 151
 1152            float time = 0;
 153
 2154            while (time < 1)
 155            {
 2156                time += Time.deltaTime * animationSpeed;
 2157                canvasGroup.alpha = Mathf.Lerp(from,to,time);
 1158                yield return null;
 159            }
 0160            gameObject.SetActive(to >= 0.99f);
 0161        }
 162    }
 163}