< Summary

Class:ItemToggle
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ItemToggle.cs
Covered lines:0
Uncovered lines:90
Coverable lines:90
Total lines:218
Line coverage:0% (0 of 90)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:29
Method coverage:0% (0 of 29)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ItemToggle()0%2100%
SetSelection(...)0%6200%
SetLoadingSpinner(...)0%6200%
Awake()0%6200%
OnClick()0%6200%
Initialize(...)0%2100%
OnPointerEnter(...)0%20400%
IsIncompatible()0%6200%
IsReplacingWearables()0%6200%
IsHidingWearables()0%6200%
OnPointerExit(...)0%2100%
SetHideOtherWerablesToastStrategy(...)0%2100%
SetBodyShapeCompatibilityStrategy(...)0%2100%
SetReplaceOtherWearablesToastStrategy(...)0%2100%
OnEnable()0%2100%
OnDestroy()0%2100%
CallOnSellClicked()0%6200%
SetColorScale()0%12300%
UpdateThumbnail()0%30500%
OnThumbnailReady(...)0%12300%
SetLoadingAnimation(...)0%6200%
Hide()0%2100%
SetCallbacks(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/ItemToggle.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using DCL.Configuration;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.EventSystems;
 7using UnityEngine.UI;
 8
 9public class ItemToggle : UIButton, IPointerEnterHandler, IPointerExitHandler
 10{
 011    private static readonly string ANIMATION_LOADED = "Loaded";
 012    private static readonly string ANIMATION_LOADING_IDLE = "LoadingIdle";
 13
 14    public event Action<ItemToggle> OnClicked;
 15    public event Action<ItemToggle> OnSellClicked;
 16
 017    public WearableItem wearableItem { get; private set; }
 18
 19    public Image thumbnail;
 20    public Image selectionHighlight;
 21    [SerializeField] private GameObject warningPanel;
 22    [SerializeField] private GameObject incompatibleWarningPanel;
 23    [SerializeField] private GameObject hideWarningPanel;
 24    [SerializeField] private GameObject loadingSpinner;
 25    [SerializeField] internal RectTransform amountContainer;
 26    [SerializeField] internal Animation loadingAnimation;
 27    [SerializeField] internal TextMeshProUGUI amountText;
 28    [SerializeField] internal GameObject root;
 29    [SerializeField] private GameObject disabledOverlay;
 30    [SerializeField] internal Material grayScaleMaterial;
 31    [SerializeField] internal Button selectButton;
 32
 33    private bool selectedValue;
 34
 35    private AvatarEditorHUDView view;
 36
 37    private Func<WearableItem, bool> getEquippedWearablesReplacedByFunc;
 38    private Func<WearableItem, bool> getEquippedWearablesHiddenBy;
 39    private Func<WearableItem, bool> getBodyShapeCompatibility;
 40
 041    public string collectionId { get; set; }
 42
 43    public bool selected
 44    {
 045        get { return selectedValue; }
 46        set
 47        {
 048            selectedValue = value;
 049            SetSelection(selectedValue);
 050        }
 51    }
 52
 53    protected virtual void SetSelection(bool isSelected)
 54    {
 055        if (selectionHighlight != null)
 056            selectionHighlight.enabled = isSelected;
 057    }
 58
 059    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 60
 61    protected new virtual void Awake()
 62    {
 063        base.Awake();
 064        thumbnail.sprite = null;
 065        warningPanel.SetActive(false);
 066        hideWarningPanel.SetActive(false);
 067        incompatibleWarningPanel.SetActive(false);
 068        if (!EnvironmentSettings.RUNNING_TESTS)
 069            view = GetComponentInParent<AvatarEditorHUDView>();
 070    }
 71
 72    protected override void OnClick()
 73    {
 074        OnClicked?.Invoke(this);
 075        warningPanel.SetActive(false);
 076    }
 77
 78    public virtual void Initialize(WearableItem w, bool isSelected, int amount, NFTItemToggleSkin skin)
 79    {
 080        root.gameObject.SetActive(true);
 81
 082        wearableItem = w;
 083        selected = isSelected;
 084        amountContainer.gameObject.SetActive(amount > 1);
 085        amountText.text = $"x{amount.ToString()}";
 86
 087        UpdateThumbnail();
 088    }
 89
 90    public void OnPointerEnter(PointerEventData eventData)
 91    {
 092        incompatibleWarningPanel.SetActive(false);
 093        warningPanel.SetActive(false);
 094        hideWarningPanel.SetActive(false);
 095        if(IsIncompatible())
 96        {
 097            incompatibleWarningPanel.SetActive(true);
 98        }
 099        else if(IsReplacingWearables())
 100        {
 0101            warningPanel.SetActive(true);
 102        }
 0103        else if(IsHidingWearables())
 104        {
 0105            hideWarningPanel.SetActive(true);
 106        }
 0107    }
 108
 109    private bool IsIncompatible()
 110    {
 0111        if(getBodyShapeCompatibility == null) return false;
 0112        return getBodyShapeCompatibility(wearableItem);
 113    }
 114
 115    private bool IsReplacingWearables()
 116    {
 0117        if (getEquippedWearablesReplacedByFunc == null) return false;
 0118        return getEquippedWearablesReplacedByFunc(wearableItem);
 119    }
 120
 121    private bool IsHidingWearables()
 122    {
 0123        if (getEquippedWearablesHiddenBy == null) return false;
 0124        return getEquippedWearablesHiddenBy(wearableItem);
 125    }
 126
 127    public void OnPointerExit(PointerEventData eventData)
 128    {
 0129        warningPanel.SetActive(false);
 0130        hideWarningPanel.SetActive(false);
 0131        incompatibleWarningPanel.SetActive(false);
 0132    }
 133
 134    public void SetHideOtherWerablesToastStrategy(Func<WearableItem, bool> function) =>
 0135        getEquippedWearablesHiddenBy = function;
 136
 137    public void SetBodyShapeCompatibilityStrategy(Func<WearableItem, bool> function) =>
 0138        getBodyShapeCompatibility = function;
 139
 140    public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) =>
 0141        getEquippedWearablesReplacedByFunc = function;
 142
 143    private void OnEnable()
 144    {
 0145        UpdateThumbnail();
 0146    }
 147
 148    protected virtual void OnDestroy()
 149    {
 0150        OnClicked = null;
 0151    }
 152
 0153    protected void CallOnSellClicked() { OnSellClicked?.Invoke(this); }
 154
 155    private void SetColorScale()
 156    {
 0157        if(getBodyShapeCompatibility != null && getBodyShapeCompatibility(wearableItem))
 158        {
 0159            thumbnail.material = grayScaleMaterial;
 0160            thumbnail.SetMaterialDirty();
 0161            disabledOverlay.SetActive(true);
 0162            selectButton.interactable = false;
 163        }
 164        else
 165        {
 0166            thumbnail.material = null;
 0167            disabledOverlay.SetActive(false);
 0168            selectButton.interactable = true;
 169        }
 0170    }
 171
 172    private void UpdateThumbnail()
 173    {
 0174        string url = wearableItem?.ComposeThumbnailUrl();
 175
 0176        if (wearableItem == null || string.IsNullOrEmpty(url))
 177        {
 0178            SetLoadingAnimation(ANIMATION_LOADED);
 0179            return;
 180        }
 181
 0182        SetLoadingAnimation(ANIMATION_LOADING_IDLE);
 183
 0184        ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 0185    }
 186
 187    private void OnThumbnailReady(Asset_Texture texture)
 188    {
 0189        SetLoadingAnimation(ANIMATION_LOADED);
 190
 0191        thumbnail.sprite = ThumbnailsManager.GetOrCreateSpriteFromTexture(texture.texture, out var wasCreated);
 192
 0193        if (view != null && wasCreated)
 194        {
 0195            if (view.avatarEditorCanvas.enabled)
 0196                AudioScriptableObjects.listItemAppear.Play(true);
 197        }
 0198        SetColorScale();
 0199    }
 200
 201    private void SetLoadingAnimation(string id)
 202    {
 0203        if (!loadingAnimation.isActiveAndEnabled)
 0204            return;
 205
 0206        loadingAnimation.Play(id);
 0207    }
 208
 209    public void Hide()
 210    {
 0211        root.gameObject.SetActive(false);
 0212    }
 213    public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked)
 214    {
 0215        OnClicked = toggleClicked;
 0216        OnSellClicked = sellClicked;
 0217    }
 218}