< Summary

Class:LimitInputField
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/NewProject/LimitInputField.cs
Covered lines:35
Uncovered lines:14
Coverable lines:49
Total lines:111
Line coverage:71.4% (35 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LimitInputField()0%110100%
Start()0%330100%
SetError()0%2100%
InputLostFocus(...)0%6200%
InputFocused(...)0%6200%
InputChanged(...)0%5.275077.78%
InputAvailable()0%550100%
Empty()0%3.073080%
LimitReached()0%4.034087.5%
GetValue()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/NewProject/LimitInputField.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8public class LimitInputField : MonoBehaviour
 9{
 10    public event System.Action OnLimitReached;
 11    public event System.Action OnEmptyValue;
 12    public event System.Action OnInputAvailable;
 13
 14    public event System.Action<string> OnInputChange;
 15
 16    [SerializeField] private int characterLimit;
 17    [SerializeField] private bool isMandatory = false;
 18    [SerializeField] private Color normalColor;
 19    [SerializeField] private Color errorColor;
 20
 21    [SerializeField] private Sprite inputFieldNormalBackgroundSprite;
 22    [SerializeField] private Sprite inputFieldErrorBackgroundSprite;
 23    [SerializeField] private Sprite inputFieldFocusBackgroundSprite;
 24
 25    [SerializeField] private Image inputFieldbackgroundImg;
 26    [SerializeField] private TMP_InputField inputField;
 27    [SerializeField] private TextMeshProUGUI limitText;
 28
 29    internal bool hasPassedLimit = false;
 30    internal bool hasBeenEmpty = false;
 5231    private string currentValue = "";
 32
 33    private void Start()
 34    {
 835        inputField.onSelect.AddListener(InputFocused);
 836        inputField.onDeselect.AddListener(InputLostFocus);
 837        inputField.onValueChanged.AddListener(InputChanged);
 838        limitText?.SetText( currentValue.Length + "/" + characterLimit);
 839        if (isMandatory)
 440            hasBeenEmpty = true;
 841    }
 42
 043    public void SetError() { LimitReached(); }
 44
 45    private void InputLostFocus(string value)
 46    {
 047        if (value.Length > characterLimit)
 048            inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite;
 49        else
 050            inputFieldbackgroundImg.sprite = inputFieldNormalBackgroundSprite;
 051    }
 52
 53    private void InputFocused(string value)
 54    {
 055        if (value.Length > characterLimit)
 056            inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite;
 57        else
 058            inputFieldbackgroundImg.sprite = inputFieldFocusBackgroundSprite;
 059    }
 60
 61    internal void InputChanged(string newValue)
 62    {
 163        currentValue = newValue;
 164        limitText?.SetText(newValue.Length + "/" + characterLimit);
 165        if (newValue.Length > characterLimit)
 066            LimitReached();
 167        else if (currentValue.Length == 0)
 068            Empty();
 69        else
 170            InputAvailable();
 71
 172        OnInputChange?.Invoke(newValue);
 173    }
 74
 75    internal void InputAvailable()
 76    {
 277        if (!hasPassedLimit && !hasBeenEmpty)
 178            return;
 79
 180        if (limitText != null)
 181            limitText.color = normalColor;
 182        inputFieldbackgroundImg.sprite = inputFieldFocusBackgroundSprite;
 183        OnInputAvailable?.Invoke();
 184        hasPassedLimit = false;
 185        hasBeenEmpty = false;
 186    }
 87
 88    internal void Empty()
 89    {
 190        if (hasBeenEmpty)
 091            return;
 92
 193        hasBeenEmpty = true;
 194        OnEmptyValue?.Invoke();
 195    }
 96
 97    internal void LimitReached()
 98    {
 199        if (hasPassedLimit)
 0100            return;
 101
 1102        if (limitText != null)
 1103            limitText.color = errorColor;
 1104        inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite;
 105
 1106        OnLimitReached?.Invoke();
 1107        hasPassedLimit = true;
 1108    }
 109
 0110    public string GetValue() { return currentValue; }
 111}