< Summary

Class:DCL.Components.UIInputText
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIInputText/UIInputText.cs
Covered lines:57
Uncovered lines:13
Coverable lines:70
Total lines:157
Line coverage:81.4% (57 of 70)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
UIInputText()0%110100%
GetClassId()0%2100%
AttachTo(...)0%2100%
DetachFrom(...)0%2100%
ApplyChanges()0%330100%
OnFocus(...)0%220100%
OnChanged(...)0%110100%
OnBlur(...)0%110100%
HideCaret()0%220100%
OnSubmit(...)0%8.125050%
ForceFocus()0%110100%
UnsuscribeFromEvents()0%110100%
Dispose()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIInputText/UIInputText.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Models;
 3using System.Collections;
 4using DCL.Helpers;
 5using TMPro;
 6using UnityEngine;
 7
 8namespace DCL.Components
 9{
 10    public class UIInputText : UIShape<UIInputTextRefContainer, UIInputText.Model>
 11    {
 12        [System.Serializable]
 13        new public class Model : UIShape.Model
 14        {
 15            public TextShape.Model textModel;
 16            public string placeholder;
 3417            public Color placeholderColor = Color.white;
 3418            public Color focusedBackground = Color.black;
 19
 20            public string onTextSubmit;
 21            public string onChanged;
 22            public string onFocus;
 23            public string onBlur;
 24
 25            public override BaseModel GetDataFromJSON(string json)
 26            {
 1227                Model model = Utils.SafeFromJson<Model>(json);
 1228                model.textModel = Utils.SafeFromJson<TextShape.Model>(json);
 1229                return model;
 30            }
 31        }
 32
 1033        public override string referencesContainerPrefabName => "UIInputText";
 34
 035        public TMP_Text tmpText => referencesContainer.text;
 036        public TMP_InputField inputField => referencesContainer.inputField;
 037        public RectTransform rectTransform => referencesContainer.rectTransform;
 38
 3039        public UIInputText() { model = new Model(); }
 40
 041        public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; }
 42
 43        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 44        {
 045            Debug.LogError(
 46                "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 047        }
 48
 049        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 50
 51        public override IEnumerator ApplyChanges(BaseModel newModel)
 52        {
 53            //NOTE(Brian): We have to serialize twice now, but in the future we should fix the
 54            //             client data structure to be like this, so we can serialize all of it in one shot.
 1255            model = (Model) newModel;
 56
 1257            inputField.textViewport = referencesContainer.rectTransform;
 58
 1259            UnsuscribeFromEvents();
 60
 1261            inputField.onSelect.AddListener(OnFocus);
 1262            inputField.onDeselect.AddListener(OnBlur);
 1263            inputField.onSubmit.AddListener(OnSubmit);
 1264            inputField.onValueChanged.AddListener(OnChanged);
 65
 1266            yield return DCL.Components.TextShape.ApplyModelChanges(scene, tmpText, model.textModel);
 67
 1268            inputField.text = model.placeholder;
 1269            inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g,
 70                model.placeholderColor.b, model.placeholderColor.a);
 1271            referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g,
 72                model.focusedBackground.b, model.focusedBackground.a);
 1273        }
 74
 75        public void OnFocus(string call)
 76        {
 377            if (inputField.text == model.placeholder)
 78            {
 279                inputField.text = "";
 80            }
 81
 382            inputField.customCaretColor = true;
 383            inputField.caretColor = Color.white;
 384            Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.id, model.onFocus);
 385        }
 86
 3487        public void OnChanged(string changedText) { Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData
 88
 89        public void OnBlur(string call)
 90        {
 191            HideCaret();
 192            Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.id, model.onBlur);
 193        }
 94
 95        public void HideCaret()
 96        {
 197            if (string.IsNullOrEmpty(inputField.text))
 98            {
 199                inputField.text = model.placeholder;
 100            }
 101
 1102            inputField.customCaretColor = true;
 1103            inputField.caretColor = Color.clear;
 1104            inputField.selectionAnchorPosition = 0;
 1105            inputField.selectionFocusPosition = 0;
 1106            inputField.selectionStringAnchorPosition = 0;
 1107            inputField.selectionStringFocusPosition = 0;
 1108        }
 109
 110        public void OnSubmit(string call)
 111        {
 1112            bool validString = !string.IsNullOrEmpty(tmpText.text);
 113
 1114            if (tmpText.text.Length == 1 && (byte) tmpText.text[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNu
 115            {
 0116                validString = false;
 117            }
 118
 1119            if (validString)
 120            {
 1121                Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.id, model.onTextSubmit, tmpText.text);
 122
 1123                ForceFocus();
 1124            }
 0125            else if (scene.isPersistent) // DCL UI Chat text input
 126            {
 0127                inputField.DeactivateInputField();
 0128                referencesContainer.mouseCatcher.LockCursor();
 129
 130                // To avoid focusing the chat in the same frame we unfocused it
 0131                referencesContainer.inputDetectionPausedFrames = 1;
 132            }
 0133        }
 134
 135        public void ForceFocus()
 136        {
 1137            inputField.text = "";
 1138            inputField.caretColor = Color.white;
 1139            inputField.Select();
 1140            inputField.ActivateInputField();
 1141        }
 142
 143        void UnsuscribeFromEvents()
 144        {
 25145            inputField.onSelect.RemoveAllListeners();
 25146            inputField.onDeselect.RemoveAllListeners();
 25147            inputField.onSubmit.RemoveAllListeners();
 25148            inputField.onValueChanged.RemoveAllListeners();
 25149        }
 150
 151        public override void Dispose()
 152        {
 13153            UnsuscribeFromEvents();
 13154            base.Dispose();
 13155        }
 156    }
 157}