< Summary

Class:AttributeXYZ
Assembly:BuilderInWorldEntityInformation
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/EntityInformation/AttributeXYZ.cs
Covered lines:24
Uncovered lines:36
Coverable lines:60
Total lines:128
Line coverage:40% (24 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
OnDestroy()0%110100%
ConfigureField(...)0%330100%
UnsubscribeField(...)0%220100%
SetValues(...)0%6200%
ChangeXValue(...)0%30500%
ChangeYValue(...)0%30500%
ChangeZValue(...)0%30500%
InputSelected(...)0%2100%
InputDeselected(...)0%2100%
SetTextboxActive(...)0%2.062075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/EntityInformation/AttributeXYZ.cs

#LineLine coverage
 1using System;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.Events;
 5using UnityEngine.EventSystems;
 6using UnityEngine.UI;
 7
 8public class AttributeXYZ : MonoBehaviour
 9{
 10    public TMP_InputField xField;
 11    public Image xTextBoxImage;
 12    public TMP_InputField yField;
 13    public Image yTextBoxImage;
 14    public TMP_InputField zField;
 15    public Image zTextBoxImage;
 16
 17    public event Action<Vector3> OnChanged;
 18
 19    Vector3 currentValue;
 20
 21    bool isSelected = false;
 22
 23    private void Awake()
 24    {
 6625        ConfigureField(xField, xTextBoxImage, ChangeXValue);
 6626        ConfigureField(yField, yTextBoxImage, ChangeYValue);
 6627        ConfigureField(zField, zTextBoxImage, ChangeZValue);
 6628    }
 29
 30    private void OnDestroy()
 31    {
 6632        UnsubscribeField(xField);
 6633        UnsubscribeField(yField);
 6634        UnsubscribeField(zField);
 6635    }
 36
 37    private void ConfigureField(TMP_InputField field, Image textBoxImage, UnityAction<string> onChangeAction)
 38    {
 19839        if (field != null)
 40        {
 19841            field.onValueChanged.AddListener(onChangeAction);
 42
 19843            field.onSelect.AddListener((currentText) =>
 44            {
 045                InputSelected(currentText);
 046                SetTextboxActive(textBoxImage, true);
 047            });
 48
 19849            field.onEndEdit.AddListener((newText) =>
 50            {
 051                SetTextboxActive(textBoxImage, false);
 052                InputDeselected(newText);
 53
 054                if (EventSystem.current != null && !EventSystem.current.alreadySelecting)
 055                    EventSystem.current.SetSelectedGameObject(null);
 056            });
 57
 19858            field.onSubmit.AddListener((newText) => EventSystem.current?.SetSelectedGameObject(null));
 59        }
 60
 19861        SetTextboxActive(textBoxImage, false);
 19862    }
 63
 64    private void UnsubscribeField(TMP_InputField field)
 65    {
 19866        if (field != null)
 67        {
 19868            field.onValueChanged.RemoveAllListeners();
 19869            field.onSelect.RemoveAllListeners();
 19870            field.onEndEdit.RemoveAllListeners();
 19871            field.onSubmit.RemoveAllListeners();
 72        }
 19873    }
 74
 75    public void SetValues(Vector3 value)
 76    {
 077        if (isSelected)
 078            return;
 79
 080        currentValue = value;
 081        xField.SetTextWithoutNotify(value.x.ToString("0.##"));
 082        yField.SetTextWithoutNotify(value.y.ToString("0.##"));
 083        zField.SetTextWithoutNotify(value.z.ToString("0.##"));
 84
 085    }
 86
 87    public void ChangeXValue(string value)
 88    {
 089        if (!isSelected || string.IsNullOrEmpty(value))
 090            return;
 91
 092        value = value.Replace(".", ",");
 093        if (float.TryParse(value, out currentValue.x))
 094            OnChanged?.Invoke(currentValue);
 095    }
 96
 97    public void ChangeYValue(string value)
 98    {
 099        if (!isSelected || string.IsNullOrEmpty(value))
 0100            return;
 101
 0102        value = value.Replace(".", ",");
 0103        if (float.TryParse(value, out currentValue.y))
 0104            OnChanged?.Invoke(currentValue);
 0105    }
 106
 107    public void ChangeZValue(string value)
 108    {
 0109        if (!isSelected || string.IsNullOrEmpty(value))
 0110            return;
 111
 0112        value = value.Replace(".", ",");
 0113        if (float.TryParse(value, out currentValue.z))
 0114            OnChanged?.Invoke(currentValue);
 0115    }
 116
 0117    public void InputSelected(string text) { isSelected = true; }
 118
 0119    public void InputDeselected(string text) { isSelected = false; }
 120
 121    private void SetTextboxActive(Image textBoxImage, bool isActive)
 122    {
 198123        if (textBoxImage == null)
 0124            return;
 125
 198126        textBoxImage.enabled = isActive;
 198127    }
 128}