< Summary

Class:DCL.Components.UIInputText
Assembly:DCL.Components.UI
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UI/UIInputText/UIInputText.cs
Covered lines:78
Uncovered lines:20
Coverable lines:98
Total lines:240
Line coverage:79.5% (78 of 98)
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%11.846045.45%
UnsuscribeFromEvents()0%110100%
Dispose()0%110100%
ApplyModelChanges(...)0%12.419065.22%

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
 3420            public float fontSize = 100f;
 21            public string font;
 3422            public string value = "";
 3423            public string hTextAlign = "bottom";
 3424            public string vTextAlign = "left";
 25            public bool textWrapping = false;
 26
 27            public float shadowBlur = 0f;
 28            public float shadowOffsetX = 0f;
 29            public float shadowOffsetY = 0f;
 3430            public Color shadowColor = new Color(1, 1, 1);
 31
 32            public float paddingTop = 0f;
 33            public float paddingRight = 0f;
 34            public float paddingBottom = 0f;
 35            public float paddingLeft = 0f;
 36
 37            public string placeholder;
 3438            public Color placeholderColor = Color.white;
 3439            public Color focusedBackground = Color.black;
 40
 41            public string onTextSubmit;
 42            public string onChanged;
 43            public string onFocus;
 44            public string onBlur;
 45            public string onTextChanged;
 46
 47            public override BaseModel GetDataFromJSON(string json)
 48            {
 1249                Model model = Utils.SafeFromJson<Model>(json);
 1250                return model;
 51            }
 52        }
 53
 1054        public override string referencesContainerPrefabName => "UIInputText";
 55
 1256        public TMP_Text tmpText => referencesContainer.text;
 17757        public TMP_InputField inputField => referencesContainer.inputField;
 058        public RectTransform rectTransform => referencesContainer.rectTransform;
 59
 3060        public UIInputText() { model = new Model(); }
 61
 062        public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; }
 63
 64        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 65        {
 066            Debug.LogError(
 67                "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 068        }
 69
 070        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 71
 72        public override IEnumerator ApplyChanges(BaseModel newModel)
 73        {
 74            //NOTE(Brian): We have to serialize twice now, but in the future we should fix the
 75            //             client data structure to be like this, so we can serialize all of it in one shot.
 1276            model = (Model) newModel;
 77
 1278            inputField.textViewport = referencesContainer.rectTransform;
 79
 1280            UnsuscribeFromEvents();
 81
 1282            inputField.onSelect.AddListener(OnFocus);
 1283            inputField.onDeselect.AddListener(OnBlur);
 1284            inputField.onSubmit.AddListener(OnSubmit);
 1285            inputField.onValueChanged.AddListener(OnChanged);
 86
 87            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 1288            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 89            {
 090                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 91            }
 92
 1293            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 1294            ApplyModelChanges(tmpText, model);
 95
 1296            inputField.text = model.placeholder;
 1297            inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g,
 98                model.placeholderColor.b, model.placeholderColor.a);
 1299            referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g,
 100                model.focusedBackground.b, model.focusedBackground.a);
 12101        }
 102
 103        public void OnFocus(string call)
 104        {
 2105            if (inputField.text == model.placeholder)
 106            {
 2107                inputField.text = "";
 108            }
 109
 2110            inputField.customCaretColor = true;
 2111            inputField.caretColor = Color.white;
 2112            Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.sceneNumber, model.onFocus);
 2113        }
 114
 115        public void OnChanged(string changedText)
 116        {
 117            // NOTE: OSX is adding the ESC character at the end of the string when ESC is pressed
 118            #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
 119            if (changedText.Length > 0 && changedText[changedText.Length - 1] == 27)
 120            {
 121                changedText = changedText.Substring(0, changedText.Length - 1);
 122                inputField.SetTextWithoutNotify(changedText);
 123            }
 124            #endif
 125
 126            // NOTE: we keep `ReportOnTextInputChangedEvent` for backward compatibility (it won't be called for scenes u
 17127            Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData.sceneNumber, model.onChanged, changedTe
 17128            Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.sceneNumber, model.onTextChanged, c
 17129        }
 130
 131        public void OnBlur(string call)
 132        {
 2133            HideCaret();
 2134            Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.sceneNumber, model.onBlur);
 2135        }
 136
 137        public void HideCaret()
 138        {
 2139            if (string.IsNullOrEmpty(inputField.text))
 140            {
 2141                inputField.text = model.placeholder;
 142            }
 143
 2144            inputField.customCaretColor = true;
 2145            inputField.caretColor = Color.clear;
 2146            inputField.selectionAnchorPosition = 0;
 2147            inputField.selectionFocusPosition = 0;
 2148            inputField.selectionStringAnchorPosition = 0;
 2149            inputField.selectionStringFocusPosition = 0;
 2150        }
 151
 152        public void OnSubmit(string call)
 153        {
 2154            bool validString = !string.IsNullOrEmpty(call);
 155
 2156            if (call?.Length == 1 && (byte) call[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNullOrWhitespace.
 157            {
 0158                validString = false;
 159            }
 160
 2161            if (validString)
 162            {
 163                // NOTE: we keep `ReportOnTextSubmitEvent` for backward compatibility (it won't be called for scenes usi
 2164                Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.sceneNumber, model.onTextSubmit, call);
 2165                Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.sceneNumber, model.onTextChange
 166            }
 0167            else if (scene.isPersistent) // DCL UI Chat text input
 168            {
 0169                inputField.DeactivateInputField();
 0170                referencesContainer.mouseCatcher.LockCursor();
 171
 172                // To avoid focusing the chat in the same frame we unfocused it
 0173                referencesContainer.inputDetectionPausedFrames = 1;
 174            }
 0175        }
 176
 177        void UnsuscribeFromEvents()
 178        {
 15179            inputField.onSelect.RemoveAllListeners();
 15180            inputField.onDeselect.RemoveAllListeners();
 15181            inputField.onSubmit.RemoveAllListeners();
 15182            inputField.onValueChanged.RemoveAllListeners();
 15183        }
 184
 185        public override void Dispose()
 186        {
 3187            UnsuscribeFromEvents();
 3188            base.Dispose();
 3189        }
 190
 191        private void ApplyModelChanges(TMP_Text text, Model model)
 192        {
 12193            text.text = model.value;
 194
 12195            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 12196            text.fontSize = (int) model.fontSize;
 12197            text.richText = true;
 12198            text.overflowMode = TextOverflowModes.Overflow;
 199
 12200            text.margin =
 201                new Vector4
 202                (
 203                    (int) model.paddingLeft,
 204                    (int) model.paddingTop,
 205                    (int) model.paddingRight,
 206                    (int) model.paddingBottom
 207                );
 208
 12209            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 12210            text.lineSpacing = 0f;
 211
 12212            text.maxVisibleLines = int.MaxValue;
 213
 214
 12215            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 216
 12217            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 218            {
 0219                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0220                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0221                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 222            }
 12223            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 224            {
 0225                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 226            }
 227
 12228            if (model.outlineWidth > 0f)
 229            {
 0230                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0231                text.outlineWidth = model.outlineWidth;
 0232                text.outlineColor = model.outlineColor;
 233            }
 12234            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 235            {
 0236                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 237            }
 12238        }
 239    }
 240}