< Summary

Class:SpinBoxPresetted
Assembly:UIHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/UIHelpers/SpinBoxPresetted.cs
Covered lines:13
Uncovered lines:20
Coverable lines:33
Total lines:90
Line coverage:39.3% (13 of 33)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
SetLabels(...)0%2100%
SetValue(...)0%6.226081.82%
OverrideCurrentLabel(...)0%2100%
IncreaseValue()0%12300%
DecreaseValue()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/UIHelpers/SpinBoxPresetted.cs

#LineLine coverage
 1using UnityEngine;
 2using UnityEngine.UI;
 3using UnityEngine.Events;
 4
 5public class SpinBoxPresetted : MonoBehaviour
 6{
 7    [System.Serializable]
 8    public class OnValueChanged : UnityEvent<int> { }
 9
 10    [Header("References")]
 11    [SerializeField] TMPro.TextMeshProUGUI textLabel = null;
 12
 13    [SerializeField] Button increaseButton = null;
 14    [SerializeField] Button decreaseButton = null;
 15
 16    [Header("Config")]
 17    [SerializeField] string[] labels = null;
 18
 19    [SerializeField] int startingValue = 0;
 20
 21    public bool loop;
 22    public OnValueChanged onValueChanged;
 23
 24    private int currentValue;
 25
 026    public int value { get { return currentValue; } set { SetValue(value); } }
 27
 028    public string label { get { return textLabel.text; } set { OverrideCurrentLabel(value); } }
 29
 30    void Awake()
 31    {
 632        increaseButton.onClick.AddListener(IncreaseValue);
 633        decreaseButton.onClick.AddListener(DecreaseValue);
 634        SetValue(startingValue, false);
 635    }
 36
 037    public void SetLabels(string[] labels) { this.labels = labels; }
 38
 39    public void SetValue(int value, bool raiseValueChangedEvent = true)
 40    {
 741        if (value >= labels.Length || value < 0)
 42        {
 243            return;
 44        }
 45
 546        textLabel.text = labels[value];
 547        currentValue = (int)value;
 548        startingValue = currentValue;
 549        if (raiseValueChangedEvent)
 150            onValueChanged?.Invoke(value);
 51
 552        if (!loop)
 53        {
 054            increaseButton.interactable = value < labels.Length - 1;
 055            decreaseButton.interactable = value > 0;
 56        }
 557    }
 58
 059    public void OverrideCurrentLabel(string text) { textLabel.text = text; }
 60
 61    public void IncreaseValue()
 62    {
 063        int newVal = currentValue + 1;
 64
 065        if (newVal >= labels.Length)
 66        {
 067            if (loop)
 068                newVal = 0;
 69            else
 070                newVal = labels.Length - 1;
 71        }
 72
 073        SetValue(newVal);
 074    }
 75
 76    public void DecreaseValue()
 77    {
 078        int newVal = currentValue - 1;
 79
 080        if (newVal < 0)
 81        {
 082            if (loop)
 083                newVal = labels.Length - 1;
 84            else
 085                newVal = 0;
 86        }
 87
 088        SetValue(newVal);
 089    }
 90}