< 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:47
Uncovered lines:43
Coverable lines:90
Total lines:218
Line coverage:52.2% (47 of 90)
Covered branches:0
Total branches:0

Metrics

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

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{
 111    private static readonly string ANIMATION_LOADED = "Loaded";
 112    private static readonly string ANIMATION_LOADING_IDLE = "LoadingIdle";
 13
 14    public event Action<ItemToggle> OnClicked;
 15    public event Action<ItemToggle> OnSellClicked;
 16
 1631217    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        {
 763548            selectedValue = value;
 763549            SetSelection(selectedValue);
 763550        }
 51    }
 52
 53    protected virtual void SetSelection(bool isSelected)
 54    {
 763555        if (selectionHighlight != null)
 763556            selectionHighlight.enabled = isSelected;
 763557    }
 58
 1004459    public void SetLoadingSpinner(bool isActive) { loadingSpinner?.SetActive(isActive); }
 60
 61    protected new virtual void Awake()
 62    {
 84663        base.Awake();
 84664        thumbnail.sprite = null;
 84665        warningPanel.SetActive(false);
 84666        hideWarningPanel.SetActive(false);
 84667        incompatibleWarningPanel.SetActive(false);
 84668        if (!EnvironmentSettings.RUNNING_TESTS)
 069            view = GetComponentInParent<AvatarEditorHUDView>();
 84670    }
 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    {
 365480        root.gameObject.SetActive(true);
 81
 365482        wearableItem = w;
 365483        selected = isSelected;
 365484        amountContainer.gameObject.SetActive(amount > 1);
 365485        amountText.text = $"x{amount.ToString()}";
 86
 365487        UpdateThumbnail();
 365488    }
 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) =>
 3654135        getEquippedWearablesHiddenBy = function;
 136
 137    public void SetBodyShapeCompatibilityStrategy(Func<WearableItem, bool> function) =>
 3654138        getBodyShapeCompatibility = function;
 139
 140    public void SetReplaceOtherWearablesToastStrategy(Func<WearableItem, bool> function) =>
 3654141        getEquippedWearablesReplacedByFunc = function;
 142
 143    private void OnEnable()
 144    {
 846145        UpdateThumbnail();
 846146    }
 147
 148    protected virtual void OnDestroy()
 149    {
 846150        OnClicked = null;
 846151    }
 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    {
 4500174        string url = wearableItem?.ComposeThumbnailUrl();
 175
 4500176        if (wearableItem == null || string.IsNullOrEmpty(url))
 177        {
 774178            SetLoadingAnimation(ANIMATION_LOADED);
 774179            return;
 180        }
 181
 3726182        SetLoadingAnimation(ANIMATION_LOADING_IDLE);
 183
 3726184        ThumbnailsManager.GetThumbnail(url, OnThumbnailReady);
 3726185    }
 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    {
 4500203        if (!loadingAnimation.isActiveAndEnabled)
 4385204            return;
 205
 115206        loadingAnimation.Play(id);
 115207    }
 208
 209    public void Hide()
 210    {
 49254211        root.gameObject.SetActive(false);
 49254212    }
 213    public void SetCallbacks(Action<ItemToggle> toggleClicked, Action<ItemToggle> sellClicked)
 214    {
 3654215        OnClicked = toggleClicked;
 3654216        OnSellClicked = sellClicked;
 3654217    }
 218}