< 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:86
Uncovered lines:24
Coverable lines:110
Total lines:243
Line coverage:78.1% (86 of 110)
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%8.125050%
ForceFocus()0%110100%
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
 56            public override BaseModel GetDataFromJSON(string json)
 57            {
 1258                Model model = Utils.SafeFromJson<Model>(json);
 1259                return model;
 60            }
 61        }
 62
 1063        public override string referencesContainerPrefabName => "UIInputText";
 64
 065        public TMP_Text tmpText => referencesContainer.text;
 066        public TMP_InputField inputField => referencesContainer.inputField;
 067        public RectTransform rectTransform => referencesContainer.rectTransform;
 68
 3069        public UIInputText() { model = new Model(); }
 70
 071        public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; }
 72
 73        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 74        {
 075            Debug.LogError(
 76                "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 077        }
 78
 079        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 80
 81        public override IEnumerator ApplyChanges(BaseModel newModel)
 82        {
 83            //NOTE(Brian): We have to serialize twice now, but in the future we should fix the
 84            //             client data structure to be like this, so we can serialize all of it in one shot.
 1285            model = (Model) newModel;
 86
 1287            inputField.textViewport = referencesContainer.rectTransform;
 88
 1289            UnsuscribeFromEvents();
 90
 1291            inputField.onSelect.AddListener(OnFocus);
 1292            inputField.onDeselect.AddListener(OnBlur);
 1293            inputField.onSubmit.AddListener(OnSubmit);
 1294            inputField.onValueChanged.AddListener(OnChanged);
 95
 96            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 1297            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 98            {
 099                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 100            }
 101
 12102            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 12103            ApplyModelChanges(tmpText, model);
 104
 12105            inputField.text = model.placeholder;
 12106            inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g,
 107                model.placeholderColor.b, model.placeholderColor.a);
 12108            referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g,
 109                model.focusedBackground.b, model.focusedBackground.a);
 12110        }
 111
 112        public void OnFocus(string call)
 113        {
 3114            if (inputField.text == model.placeholder)
 115            {
 2116                inputField.text = "";
 117            }
 118
 3119            inputField.customCaretColor = true;
 3120            inputField.caretColor = Color.white;
 3121            Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.id, model.onFocus);
 3122        }
 123
 34124        public void OnChanged(string changedText) { Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData
 125
 126        public void OnBlur(string call)
 127        {
 1128            HideCaret();
 1129            Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.id, model.onBlur);
 1130        }
 131
 132        public void HideCaret()
 133        {
 1134            if (string.IsNullOrEmpty(inputField.text))
 135            {
 1136                inputField.text = model.placeholder;
 137            }
 138
 1139            inputField.customCaretColor = true;
 1140            inputField.caretColor = Color.clear;
 1141            inputField.selectionAnchorPosition = 0;
 1142            inputField.selectionFocusPosition = 0;
 1143            inputField.selectionStringAnchorPosition = 0;
 1144            inputField.selectionStringFocusPosition = 0;
 1145        }
 146
 147        public void OnSubmit(string call)
 148        {
 1149            bool validString = !string.IsNullOrEmpty(tmpText.text);
 150
 1151            if (tmpText.text.Length == 1 && (byte) tmpText.text[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNu
 152            {
 0153                validString = false;
 154            }
 155
 1156            if (validString)
 157            {
 1158                Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.id, model.onTextSubmit, tmpText.text);
 159
 1160                ForceFocus();
 1161            }
 0162            else if (scene.isPersistent) // DCL UI Chat text input
 163            {
 0164                inputField.DeactivateInputField();
 0165                referencesContainer.mouseCatcher.LockCursor();
 166
 167                // To avoid focusing the chat in the same frame we unfocused it
 0168                referencesContainer.inputDetectionPausedFrames = 1;
 169            }
 0170        }
 171
 172        public void ForceFocus()
 173        {
 1174            inputField.text = "";
 1175            inputField.caretColor = Color.white;
 1176            inputField.Select();
 1177            inputField.ActivateInputField();
 1178        }
 179
 180        void UnsuscribeFromEvents()
 181        {
 25182            inputField.onSelect.RemoveAllListeners();
 25183            inputField.onDeselect.RemoveAllListeners();
 25184            inputField.onSubmit.RemoveAllListeners();
 25185            inputField.onValueChanged.RemoveAllListeners();
 25186        }
 187
 188        public override void Dispose()
 189        {
 13190            UnsuscribeFromEvents();
 13191            base.Dispose();
 13192        }
 193
 194        private void ApplyModelChanges(TMP_Text text, Model model)
 195        {
 12196            text.text = model.value;
 197
 12198            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 12199            text.fontSize = (int) model.fontSize;
 12200            text.richText = true;
 12201            text.overflowMode = TextOverflowModes.Overflow;
 202
 12203            text.margin =
 204                new Vector4
 205                (
 206                    (int) model.paddingLeft,
 207                    (int) model.paddingTop,
 208                    (int) model.paddingRight,
 209                    (int) model.paddingBottom
 210                );
 211
 12212            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 12213            text.lineSpacing = 0f;
 214
 12215            text.maxVisibleLines = int.MaxValue;
 216
 217
 12218            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 219
 12220            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 221            {
 0222                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0223                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0224                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 0225            }
 12226            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 227            {
 0228                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 229            }
 230
 12231            if (model.outlineWidth > 0f)
 232            {
 0233                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0234                text.outlineWidth = model.outlineWidth;
 0235                text.outlineColor = model.outlineColor;
 0236            }
 12237            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 238            {
 0239                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 240            }
 12241        }
 242    }
 243}