< Summary

Class:SceneCardView
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/Views/SceneCardView.cs
Covered lines:70
Uncovered lines:34
Coverable lines:104
Total lines:325
Line coverage:67.3% (70 of 104)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SceneCardView()0%110100%
get_searchInfo()0%110100%
SceneCardView()0%110100%
get_SceneData()0%110100%
get_contextMenuButtonPosition()0%2100%
Awake()0%220100%
JumpInButtonPressed()0%6200%
EditorButtonPressed()0%6200%
OnEnable()0%330100%
Setup(...)0%33092.86%
SetParent(...)0%2.032080%
SetName(...)0%110100%
SetCoords(...)0%110100%
SetSize(...)0%220100%
SetThumbnail(...)0%31.286011.11%
SetThumbnail(...)0%20400%
SetDeployed(...)0%220100%
SetUserRole(...)0%3.013088.89%
SetActive(...)0%110100%
SetSiblingIndex(...)0%110100%
SetToDefaultParent()0%110100%
ConfigureDefaultParent(...)0%110100%
SetEditable(...)0%110100%
Dispose()0%220100%
OnDestroy()0%110100%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using System;
 4using DCL.Builder;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9/// <summary>
 10/// This interface represent a scene card
 11/// </summary>
 12internal interface ISceneCardView : IDisposable
 13{
 14    /// <summary>
 15    /// Jump button pressed
 16    /// </summary>
 17    event Action<Vector2Int> OnJumpInPressed;
 18
 19    /// <summary>
 20    /// Editor button pressed
 21    /// </summary>
 22    event Action<Vector2Int> OnEditorPressed;
 23
 24    /// <summary>
 25    /// Setting button presed
 26    /// </summary>
 27    event Action<ISceneData> OnSettingsPressed;
 28
 29    /// <summary>
 30    /// Context button present
 31    /// </summary>
 32    event Action<ISceneData, ISceneCardView> OnContextMenuPressed;
 33
 34    /// <summary>
 35    /// Data of the scene
 36    /// </summary>
 37    ISceneData SceneData { get; }
 38
 39    /// <summary>
 40    /// Info of the search
 41    /// </summary>
 42    ISearchInfo searchInfo { get; }
 43
 44    /// <summary>
 45    /// Position of the context menu
 46    /// </summary>
 47    Vector3 contextMenuButtonPosition { get; }
 48
 49    /// <summary>
 50    /// This set the data of the card
 51    /// </summary>
 52    /// <param name="sceneData">data of the card</param>
 53    void Setup(ISceneData sceneData);
 54
 55    /// <summary>
 56    /// Set Parent of the card
 57    /// </summary>
 58    /// <param name="parent"></param>
 59    void SetParent(Transform parent);
 60
 61    /// <summary>
 62    /// Reset to default parent
 63    /// </summary>
 64    void SetToDefaultParent();
 65
 66    /// <summary>
 67    /// Configure the default parent
 68    /// </summary>
 69    /// <param name="parent">default parent to apply</param>
 70    void ConfigureDefaultParent(Transform parent);
 71
 72    /// <summary>
 73    /// This set the order of the card
 74    /// </summary>
 75    /// <param name="index"></param>
 76    void SetSiblingIndex(int index);
 77
 78    void SetName(string name);
 79    void SetCoords(Vector2Int coords);
 80    void SetSize(Vector2Int size);
 81    void SetThumbnail(string thumbnailUrl);
 82    void SetThumbnail(Texture2D thumbnailTexture);
 83    void SetDeployed(bool deployed);
 84    void SetEditable(bool isEditable);
 85    void SetUserRole(bool isOwner, bool isOperator, bool isContributor);
 86    void SetActive(bool active);
 87}
 88
 89internal class SceneCardView : MonoBehaviour, ISceneCardView
 90{
 91    const int THMBL_MARKETPLACE_WIDTH = 196;
 92    const int THMBL_MARKETPLACE_HEIGHT = 143;
 93    const int THMBL_MARKETPLACE_SIZEFACTOR = 50;
 94
 195    static readonly Vector3 CONTEXT_MENU_OFFSET = new Vector3(6.24f, 12f, 0);
 96
 97    public event Action<Vector2Int> OnJumpInPressed;
 98    public event Action<Vector2Int> OnEditorPressed;
 99    public event Action<ISceneData> OnSettingsPressed;
 100    public event Action<ISceneData, ISceneCardView> OnContextMenuPressed;
 101
 102    [SerializeField] private Texture2D defaultThumbnail;
 103
 104    [Space]
 105    [SerializeField] private RawImageFillParent thumbnail;
 106    [SerializeField] private GameObject thumbnailGameObject;
 107    [SerializeField] private GameObject thumbnailLoading;
 108
 109    [SerializeField] private TextMeshProUGUI sceneName;
 110
 111    [Space]
 112    [SerializeField] private TextMeshProUGUI coordsText;
 113
 114    [Space]
 115    [SerializeField] internal GameObject sizeContainer;
 116
 117    [SerializeField] private TextMeshProUGUI sizeText;
 118
 119    [Space]
 120    [SerializeField] internal Button jumpInButton;
 121
 122    [SerializeField] internal Button editorButton;
 123    [SerializeField] internal Button contextMenuButton;
 124    [SerializeField] internal Button settingsButton;
 125
 126    [Space]
 127    [SerializeField] internal GameObject roleOwnerGO;
 128
 129    [SerializeField] internal GameObject roleOperatorGO;
 130
 131    [Space]
 132    [SerializeField] internal GameObject editorLockedGO;
 133
 134    [SerializeField] internal GameObject editorLockedTooltipGO;
 135
 136    [SerializeField] internal Animator loadingAnimator;
 137
 1138    private static readonly int isLoadingAnimation = Animator.StringToHash("isLoading");
 139
 153140    ISearchInfo ISceneCardView.searchInfo { get; } = new SearchInfo();
 46141    ISceneData ISceneCardView.SceneData => sceneData;
 0142    Vector3 ISceneCardView.contextMenuButtonPosition => contextMenuButton.transform.position + CONTEXT_MENU_OFFSET;
 143
 144    private ISceneData sceneData;
 145    private string thumbnailUrl = null;
 146    private AssetPromise_Texture thumbnailPromise;
 147    private bool isDestroyed = false;
 148    private Transform defaultParent;
 149    private bool isLoadingThumbnail = false;
 150
 151    private void Awake()
 152    {
 18153        jumpInButton.onClick.AddListener( JumpInButtonPressed );
 18154        editorButton.onClick.AddListener( EditorButtonPressed );
 18155        contextMenuButton.onClick.AddListener(() => OnContextMenuPressed?.Invoke(sceneData, this));
 18156        settingsButton?.onClick.AddListener(() => OnSettingsPressed?.Invoke(sceneData));
 157
 18158        editorLockedGO.SetActive(false);
 18159        editorLockedTooltipGO.SetActive(false);
 18160    }
 161
 162    private void JumpInButtonPressed()
 163    {
 0164        OnJumpInPressed?.Invoke(sceneData.coords);
 0165        BIWAnalytics.PlayerJumpOrEdit("Scene", "JumpIn", sceneData.coords, "Scene Owner");
 0166    }
 167
 168    private void EditorButtonPressed()
 169    {
 0170        OnEditorPressed?.Invoke(sceneData.coords);
 0171        BIWAnalytics.PlayerJumpOrEdit("Scene", "Editor", sceneData.coords, "Scene Owner");
 0172    }
 173
 174    private void OnEnable()
 175    {
 28176        if(loadingAnimator != null)
 26177            loadingAnimator?.SetBool(isLoadingAnimation, isLoadingThumbnail);
 28178    }
 179
 180    void ISceneCardView.Setup(ISceneData sceneData)
 181    {
 25182        this.sceneData = sceneData;
 183
 25184        string sceneThumbnailUrl = sceneData.thumbnailUrl;
 25185        if (string.IsNullOrEmpty(sceneThumbnailUrl) && sceneData.parcels != null)
 186        {
 0187            sceneThumbnailUrl = MapUtils.GetMarketPlaceThumbnailUrl(sceneData.parcels,
 188                THMBL_MARKETPLACE_WIDTH, THMBL_MARKETPLACE_HEIGHT, THMBL_MARKETPLACE_SIZEFACTOR);
 189        }
 190
 25191        ISceneCardView thisView = this;
 25192        thisView.SetThumbnail(sceneThumbnailUrl);
 25193        thisView.SetName(sceneData.name);
 25194        thisView.SetCoords(sceneData.coords);
 25195        thisView.SetSize(sceneData.size);
 25196        thisView.SetDeployed(sceneData.isDeployed);
 25197        thisView.SetUserRole(sceneData.isOwner, sceneData.isOperator, sceneData.isContributor);
 25198        thisView.SetEditable(sceneData.isEditable);
 199
 25200        thisView.searchInfo.SetId(sceneData.id);
 25201    }
 202
 203    void ISceneCardView.SetParent(Transform parent)
 204    {
 10205        if (transform.parent == parent)
 0206            return;
 207
 10208        transform.SetParent(parent);
 10209        transform.ResetLocalTRS();
 10210    }
 211
 212    void ISceneCardView.SetName(string name)
 213    {
 25214        sceneName.text = name;
 25215        ((ISceneCardView)this).searchInfo.SetName(name);
 25216    }
 217
 218    void ISceneCardView.SetCoords(Vector2Int coords)
 219    {
 25220        string coordStr = $"{coords.x},{coords.y}";
 25221        coordsText.text = coordStr;
 25222        ((ISceneCardView)this).searchInfo.SetCoords(coordStr);
 25223    }
 224
 225    void ISceneCardView.SetSize(Vector2Int size)
 226    {
 25227        if(sizeText == null)
 2228            return;
 23229        sizeText.text = $"{size.x},{size.y}m";
 23230        ((ISceneCardView)this).searchInfo.SetSize(size.x * size.y);
 23231    }
 232
 233    void ISceneCardView.SetThumbnail(string thumbnailUrl)
 234    {
 25235        if (this.thumbnailUrl == thumbnailUrl)
 25236            return;
 237
 0238        this.thumbnailUrl = thumbnailUrl;
 239
 0240        isLoadingThumbnail = true;
 0241        thumbnailLoading.SetActive(true);
 0242        if(loadingAnimator != null)
 0243            loadingAnimator?.SetBool(isLoadingAnimation, isLoadingThumbnail);
 244
 0245        if (thumbnailPromise != null)
 246        {
 0247            AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 0248            thumbnailPromise = null;
 249        }
 250
 251
 0252        if (string.IsNullOrEmpty(thumbnailUrl))
 253        {
 0254            ((ISceneCardView)this).SetThumbnail((Texture2D) null);
 0255            return;
 256        }
 257
 0258        thumbnailPromise = new AssetPromise_Texture(thumbnailUrl);
 0259        thumbnailPromise.OnSuccessEvent += texture => ((ISceneCardView)this).SetThumbnail(texture.texture);
 0260        thumbnailPromise.OnFailEvent += (texture, error) => ((ISceneCardView)this).SetThumbnail((Texture2D) null);
 261
 0262        AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise);
 0263    }
 264
 265    void ISceneCardView.SetThumbnail(Texture2D thumbnailTexture)
 266    {
 0267        thumbnail.texture = thumbnailTexture ?? defaultThumbnail;
 0268        isLoadingThumbnail = false;
 0269        thumbnail.enabled = true;
 0270        thumbnailGameObject.SetActive(true);
 0271        thumbnailLoading.SetActive(false);
 0272        if(loadingAnimator != null)
 0273            loadingAnimator?.SetBool(isLoadingAnimation, isLoadingThumbnail);
 0274    }
 275
 276    void ISceneCardView.SetDeployed(bool deployed)
 277    {
 25278        if(sizeContainer != null)
 23279            sizeContainer.SetActive(!deployed);
 25280        jumpInButton.gameObject.SetActive(deployed);
 25281    }
 282
 283    void ISceneCardView.SetUserRole(bool isOwner, bool isOperator, bool isContributor)
 284    {
 25285        roleOwnerGO.SetActive(false);
 25286        roleOperatorGO.SetActive(false);
 25287        ((ISceneCardView)this).searchInfo.SetRole(isOwner);
 288
 25289        if (isOwner)
 290        {
 1291            roleOwnerGO.SetActive(true);
 1292        }
 24293        else if (isOperator)
 294        {
 0295            roleOperatorGO.SetActive(true);
 296        }
 24297    }
 298
 44299    void ISceneCardView.SetActive(bool active) { gameObject.SetActive(active); }
 300
 20301    void ISceneCardView.SetSiblingIndex(int index) { transform.SetSiblingIndex(index); }
 4302    void ISceneCardView.SetToDefaultParent() { transform.SetParent(defaultParent); }
 303
 4304    void ISceneCardView.ConfigureDefaultParent(Transform parent) { defaultParent = parent; }
 305
 306    void ISceneCardView.SetEditable(bool isEditable)
 307    {
 25308        editorButton.gameObject.SetActive(isEditable);
 25309        editorLockedGO.SetActive(!isEditable);
 25310    }
 311
 312    public void Dispose()
 313    {
 12314        if (!isDestroyed)
 315        {
 12316            Destroy(gameObject);
 317        }
 12318    }
 319
 320    private void OnDestroy()
 321    {
 18322        AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise);
 18323        isDestroyed = true;
 18324    }
 325}