< 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:83
Uncovered lines:24
Coverable lines:107
Total lines:250
Line coverage:77.5% (83 of 107)
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%44093.33%
OnFocus(...)0%220100%
OnChanged(...)0%110100%
OnBlur(...)0%110100%
HideCaret()0%220100%
OnSubmit(...)0%10.56050%
UnsuscribeFromEvents()0%110100%
Dispose()0%110100%
ApplyModelChanges(...)0%14.189060%

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 float outlineWidth = 0f;
 3416            public Color outlineColor = Color.white;
 17
 3418            public Color color = Color.white;
 19
 20            //These values exist in the SDK but we are doing nothing with these values
 3421            public float thickness = 1f;
 3422            public Color placeHolderColor = Color.white;
 3423            public float maxWidth = 100f;
 3424            public bool autoStretchWidth = true;
 3425            public Color background = Color.black;
 26
 3427            public string fontWeight = "normal";
 28            //
 29
 3430            public float fontSize = 100f;
 31            public string font;
 3432            public string value = "";
 3433            public string hTextAlign = "bottom";
 3434            public string vTextAlign = "left";
 35            public bool textWrapping = false;
 36
 37            public float shadowBlur = 0f;
 38            public float shadowOffsetX = 0f;
 39            public float shadowOffsetY = 0f;
 3440            public Color shadowColor = new Color(1, 1, 1);
 41
 42            public float paddingTop = 0f;
 43            public float paddingRight = 0f;
 44            public float paddingBottom = 0f;
 45            public float paddingLeft = 0f;
 46
 47            public string placeholder;
 3448            public Color placeholderColor = Color.white;
 3449            public Color focusedBackground = Color.black;
 50
 51            public string onTextSubmit;
 52            public string onChanged;
 53            public string onFocus;
 54            public string onBlur;
 55            public string onTextChanged;
 56
 57            public override BaseModel GetDataFromJSON(string json)
 58            {
 1259                Model model = Utils.SafeFromJson<Model>(json);
 1260                return model;
 61            }
 62        }
 63
 1064        public override string referencesContainerPrefabName => "UIInputText";
 65
 066        public TMP_Text tmpText => referencesContainer.text;
 067        public TMP_InputField inputField => referencesContainer.inputField;
 068        public RectTransform rectTransform => referencesContainer.rectTransform;
 69
 3070        public UIInputText() { model = new Model(); }
 71
 072        public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; }
 73
 74        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 75        {
 076            Debug.LogError(
 77                "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 078        }
 79
 080        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 81
 82        public override IEnumerator ApplyChanges(BaseModel newModel)
 83        {
 84            //NOTE(Brian): We have to serialize twice now, but in the future we should fix the
 85            //             client data structure to be like this, so we can serialize all of it in one shot.
 1286            model = (Model) newModel;
 87
 1288            inputField.textViewport = referencesContainer.rectTransform;
 89
 1290            UnsuscribeFromEvents();
 91
 1292            inputField.onSelect.AddListener(OnFocus);
 1293            inputField.onDeselect.AddListener(OnBlur);
 1294            inputField.onSubmit.AddListener(OnSubmit);
 1295            inputField.onValueChanged.AddListener(OnChanged);
 96
 97            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 1298            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 99            {
 0100                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 101            }
 102
 12103            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 12104            ApplyModelChanges(tmpText, model);
 105
 12106            inputField.text = model.placeholder;
 12107            inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g,
 108                model.placeholderColor.b, model.placeholderColor.a);
 12109            referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g,
 110                model.focusedBackground.b, model.focusedBackground.a);
 12111        }
 112
 113        public void OnFocus(string call)
 114        {
 2115            if (inputField.text == model.placeholder)
 116            {
 2117                inputField.text = "";
 118            }
 119
 2120            inputField.customCaretColor = true;
 2121            inputField.caretColor = Color.white;
 2122            Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.id, model.onFocus);
 2123        }
 124
 125        public void OnChanged(string changedText)
 126        {
 127            // NOTE: OSX is adding the ESC character at the end of the string when ESC is pressed
 128            #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
 129            if (changedText.Length > 0 && changedText[changedText.Length - 1] == 27)
 130            {
 131                changedText = changedText.Substring(0, changedText.Length - 1);
 132                inputField.SetTextWithoutNotify(changedText);
 133            }
 134            #endif
 135
 136            // NOTE: we keep `ReportOnTextInputChangedEvent` for backward compatibility (it won't be called for scenes u
 16137            Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData.id, model.onChanged, changedText);
 16138            Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.id, model.onTextChanged, changedTex
 16139        }
 140
 141        public void OnBlur(string call)
 142        {
 1143            HideCaret();
 1144            Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.id, model.onBlur);
 1145        }
 146
 147        public void HideCaret()
 148        {
 1149            if (string.IsNullOrEmpty(inputField.text))
 150            {
 1151                inputField.text = model.placeholder;
 152            }
 153
 1154            inputField.customCaretColor = true;
 1155            inputField.caretColor = Color.clear;
 1156            inputField.selectionAnchorPosition = 0;
 1157            inputField.selectionFocusPosition = 0;
 1158            inputField.selectionStringAnchorPosition = 0;
 1159            inputField.selectionStringFocusPosition = 0;
 1160        }
 161
 162        public void OnSubmit(string call)
 163        {
 2164            bool validString = !string.IsNullOrEmpty(call);
 165
 2166            if (call?.Length == 1 && (byte) call[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNullOrWhitespace.
 167            {
 0168                validString = false;
 169            }
 170
 2171            if (validString)
 172            {
 173                // NOTE: we keep `ReportOnTextSubmitEvent` for backward compatibility (it won't be called for scenes usi
 2174                Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.id, model.onTextSubmit, call);
 2175                Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.id, model.onTextChanged, call, 
 2176            }
 0177            else if (scene.isPersistent) // DCL UI Chat text input
 178            {
 0179                inputField.DeactivateInputField();
 0180                referencesContainer.mouseCatcher.LockCursor();
 181
 182                // To avoid focusing the chat in the same frame we unfocused it
 0183                referencesContainer.inputDetectionPausedFrames = 1;
 184            }
 0185        }
 186
 187        void UnsuscribeFromEvents()
 188        {
 25189            inputField.onSelect.RemoveAllListeners();
 25190            inputField.onDeselect.RemoveAllListeners();
 25191            inputField.onSubmit.RemoveAllListeners();
 25192            inputField.onValueChanged.RemoveAllListeners();
 25193        }
 194
 195        public override void Dispose()
 196        {
 13197            UnsuscribeFromEvents();
 13198            base.Dispose();
 13199        }
 200
 201        private void ApplyModelChanges(TMP_Text text, Model model)
 202        {
 12203            text.text = model.value;
 204
 12205            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 12206            text.fontSize = (int) model.fontSize;
 12207            text.richText = true;
 12208            text.overflowMode = TextOverflowModes.Overflow;
 209
 12210            text.margin =
 211                new Vector4
 212                (
 213                    (int) model.paddingLeft,
 214                    (int) model.paddingTop,
 215                    (int) model.paddingRight,
 216                    (int) model.paddingBottom
 217                );
 218
 12219            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 12220            text.lineSpacing = 0f;
 221
 12222            text.maxVisibleLines = int.MaxValue;
 223
 224
 12225            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 226
 12227            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 228            {
 0229                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0230                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0231                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 0232            }
 12233            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 234            {
 0235                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 236            }
 237
 12238            if (model.outlineWidth > 0f)
 239            {
 0240                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0241                text.outlineWidth = model.outlineWidth;
 0242                text.outlineColor = model.outlineColor;
 0243            }
 12244            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 245            {
 0246                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 247            }
 12248        }
 249    }
 250}