< Summary

Class:EntityListAdapter
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/GodMode/Inspector/EntityListAdapter.cs
Covered lines:8
Uncovered lines:91
Coverable lines:99
Total lines:206
Line coverage:8% (8 of 99)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Start()0%4.123050%
OnDestroy()0%5.673033.33%
SetContent(...)0%6200%
SelectOrDeselect()0%12300%
ShowOrHide()0%6200%
LockOrUnlock()0%6200%
DeleteEntity()0%6200%
SetInfo(...)0%30500%
GetThumbnail(...)0%12300%
SetThumbnail(...)0%6200%
Rename(...)0%12300%
AllowNameEdition(...)0%2100%
DeleteAdapter(...)0%12300%
SetEntityError(...)0%6200%
SetTextboxActive(...)0%2.52050%
CheckEntityNameColor(...)0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/GodMode/Inspector/EntityListAdapter.cs

#LineLine coverage
 1using DCL;
 2using DCL.Models;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.EventSystems;
 6using UnityEngine.UI;
 7
 8public class EntityListAdapter : MonoBehaviour
 9{
 10    public Color entitySelectedColor;
 11    public Color entityUnselectedColor;
 12    public Color entityWithoutErrorsColor;
 13    public Color entityErrorColor;
 14    public Color iconsSelectedColor;
 15    public Color iconsUnselectedColor;
 16    public TMP_InputField nameInputField;
 17    public TextMeshProUGUI nameInputField_Text;
 18    public Image selectedImg;
 19    public RawImage entityThumbnailImg;
 20    public Button unlockButton;
 21    public Button lockButton;
 22    public Image showImg;
 23    public Image textBoxImage;
 24    public System.Action<EntityAction, BIWEntity, EntityListAdapter> OnActionInvoked;
 25    public System.Action<BIWEntity, string> OnEntityRename;
 26    BIWEntity currentEntity;
 27    internal AssetPromise_Texture loadedThumbnailPromise;
 28
 29    private void Start()
 30    {
 131        if (nameInputField != null)
 32        {
 033            nameInputField.onSelect.AddListener((currentText) => SetTextboxActive(true));
 34
 035            nameInputField.onEndEdit.AddListener((newText) =>
 36            {
 037                Rename(newText);
 038                SetTextboxActive(false);
 39
 040                if (EventSystem.current != null && !EventSystem.current.alreadySelecting)
 041                    EventSystem.current.SetSelectedGameObject(null);
 042            });
 43
 044            nameInputField.onSubmit.AddListener((newText) => EventSystem.current?.SetSelectedGameObject(null));
 45        }
 46
 147        SetTextboxActive(false);
 148    }
 49
 50    private void OnDestroy()
 51    {
 152        if (nameInputField != null)
 53        {
 054            nameInputField.onSelect.RemoveAllListeners();
 055            nameInputField.onEndEdit.RemoveAllListeners();
 056            nameInputField.onSubmit.RemoveAllListeners();
 57        }
 58
 159        if (currentEntity != null)
 60        {
 061            currentEntity.OnStatusUpdate -= SetInfo;
 062            currentEntity.OnDelete -= DeleteAdapter;
 063            currentEntity.OnErrorStatusChange -= SetEntityError;
 64        }
 165    }
 66
 67    public void SetContent(BIWEntity decentrelandEntity)
 68    {
 069        if (currentEntity != null)
 70        {
 071            currentEntity.OnStatusUpdate -= SetInfo;
 072            currentEntity.OnDelete -= DeleteAdapter;
 073            currentEntity.OnErrorStatusChange -= SetEntityError;
 74        }
 75
 076        currentEntity = decentrelandEntity;
 077        currentEntity.OnStatusUpdate += SetInfo;
 078        currentEntity.OnDelete += DeleteAdapter;
 079        currentEntity.OnErrorStatusChange += SetEntityError;
 80
 081        AllowNameEdition(false);
 082        SetInfo(decentrelandEntity);
 83
 084        entityThumbnailImg.enabled = false;
 085        CatalogItem entitySceneObject = decentrelandEntity.GetCatalogItemAssociated();
 086        GetThumbnail(entitySceneObject);
 087    }
 88
 89    public void SelectOrDeselect()
 90    {
 091        if (currentEntity.isVisible)
 092            OnActionInvoked?.Invoke(EntityAction.SELECT, currentEntity, this);
 093    }
 94
 095    public void ShowOrHide() { OnActionInvoked?.Invoke(EntityAction.SHOW, currentEntity, this); }
 96
 097    public void LockOrUnlock() { OnActionInvoked?.Invoke(EntityAction.LOCK, currentEntity, this); }
 98
 099    public void DeleteEntity() { OnActionInvoked?.Invoke(EntityAction.DELETE, currentEntity, this); }
 100
 101    void SetInfo(BIWEntity entityToEdit)
 102    {
 0103        if (this == null)
 0104            return;
 105
 0106        if (string.IsNullOrEmpty(entityToEdit.GetDescriptiveName()))
 0107            nameInputField.text = entityToEdit.rootEntity.entityId;
 108        else
 0109            nameInputField.text = entityToEdit.GetDescriptiveName();
 110
 111        //NOTE (Adrian): this is done to force the text component to update, otherwise it won't show the text, seems lik
 0112        nameInputField.textComponent.enabled = true;
 113
 114
 0115        if (entityToEdit.isVisible)
 0116            showImg.color = iconsSelectedColor;
 117        else
 0118            showImg.color = iconsUnselectedColor;
 119
 0120        CheckEntityNameColor(entityToEdit);
 121
 0122        unlockButton.gameObject.SetActive(!entityToEdit.isLocked);
 0123        lockButton.gameObject.SetActive(entityToEdit.isLocked);
 124
 0125        if (entityToEdit.isSelected)
 126        {
 0127            AllowNameEdition(true);
 0128            selectedImg.color = entitySelectedColor;
 0129        }
 130        else
 131        {
 0132            AllowNameEdition(false);
 0133            selectedImg.color = entityUnselectedColor;
 134        }
 135
 0136        SetEntityError(entityToEdit);
 0137    }
 138
 139    internal void GetThumbnail(CatalogItem catalogItem)
 140    {
 0141        if (catalogItem == null)
 0142            return;
 143
 0144        var url = catalogItem.thumbnailURL;
 145
 0146        if (string.IsNullOrEmpty(url))
 0147            return;
 148
 0149        var newLoadedThumbnailPromise = new AssetPromise_Texture(url);
 0150        newLoadedThumbnailPromise.OnSuccessEvent += SetThumbnail;
 0151        newLoadedThumbnailPromise.OnFailEvent += (x, error) => { Debug.Log($"Error downloading: {url}, Exception: {error
 0152        AssetPromiseKeeper_Texture.i.Keep(newLoadedThumbnailPromise);
 0153        AssetPromiseKeeper_Texture.i.Forget(loadedThumbnailPromise);
 0154        loadedThumbnailPromise = newLoadedThumbnailPromise;
 0155    }
 156
 157    internal void SetThumbnail(Asset_Texture texture)
 158    {
 0159        if (entityThumbnailImg == null)
 0160            return;
 0161        entityThumbnailImg.enabled = true;
 0162        entityThumbnailImg.texture = texture.texture;
 0163    }
 164
 165    public void Rename(string newName)
 166    {
 0167        if (!string.IsNullOrEmpty(newName))
 0168            OnEntityRename?.Invoke(currentEntity, newName);
 169        else
 0170            nameInputField.text = currentEntity.GetDescriptiveName();
 0171    }
 172
 0173    public void AllowNameEdition(bool isAllowed) { nameInputField.enabled = isAllowed; }
 174
 175    void DeleteAdapter(BIWEntity entityToEdit)
 176    {
 0177        if (this != null && entityToEdit.entityUniqueId == currentEntity.entityUniqueId)
 0178            Destroy(gameObject);
 0179    }
 180
 181    private void SetEntityError(BIWEntity entity)
 182    {
 0183        if (entity != currentEntity)
 0184            return;
 185
 0186        CheckEntityNameColor(entity);
 0187    }
 188
 189    private void SetTextboxActive(bool isActive)
 190    {
 1191        if (textBoxImage == null)
 1192            return;
 193
 0194        textBoxImage.enabled = isActive;
 0195    }
 196
 197    private void CheckEntityNameColor(BIWEntity entity)
 198    {
 0199        if (entity.hasError)
 0200            nameInputField_Text.color = entityErrorColor;
 0201        else if (!entity.isVisible || entity.isLocked)
 0202            nameInputField_Text.color = iconsUnselectedColor;
 203        else
 0204            nameInputField_Text.color = entityWithoutErrorsColor;
 0205    }
 206}