< 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:85
Uncovered lines:24
Coverable lines:109
Total lines:240
Line coverage:77.9% (85 of 109)
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%
ApplyModelChanges()0%21.7412059.26%

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;
 3426            public string fontWeight = "normal";
 27            //
 28
 3429            public float fontSize = 100f;
 30            public string font;
 3431            public string value = "";
 3432            public string hTextAlign = "bottom";
 3433            public string vTextAlign = "left";
 34            public bool textWrapping = false;
 35
 36            public float shadowBlur = 0f;
 37            public float shadowOffsetX = 0f;
 38            public float shadowOffsetY = 0f;
 3439            public Color shadowColor = new Color(1, 1, 1);
 40
 41            public float paddingTop = 0f;
 42            public float paddingRight = 0f;
 43            public float paddingBottom = 0f;
 44            public float paddingLeft = 0f;
 45
 46            public string placeholder;
 3447            public Color placeholderColor = Color.white;
 3448            public Color focusedBackground = Color.black;
 49
 50            public string onTextSubmit;
 51            public string onChanged;
 52            public string onFocus;
 53            public string onBlur;
 54
 55            public override BaseModel GetDataFromJSON(string json)
 56            {
 1257                Model model = Utils.SafeFromJson<Model>(json);
 1258                return model;
 59            }
 60        }
 61
 1062        public override string referencesContainerPrefabName => "UIInputText";
 63
 064        public TMP_Text tmpText => referencesContainer.text;
 065        public TMP_InputField inputField => referencesContainer.inputField;
 066        public RectTransform rectTransform => referencesContainer.rectTransform;
 67
 3068        public UIInputText() { model = new Model(); }
 69
 070        public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; }
 71
 72        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 73        {
 074            Debug.LogError(
 75                "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 076        }
 77
 078        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 79
 80        public override IEnumerator ApplyChanges(BaseModel newModel)
 81        {
 82            //NOTE(Brian): We have to serialize twice now, but in the future we should fix the
 83            //             client data structure to be like this, so we can serialize all of it in one shot.
 1284            model = (Model) newModel;
 85
 1286            inputField.textViewport = referencesContainer.rectTransform;
 87
 1288            UnsuscribeFromEvents();
 89
 1290            inputField.onSelect.AddListener(OnFocus);
 1291            inputField.onDeselect.AddListener(OnBlur);
 1292            inputField.onSubmit.AddListener(OnSubmit);
 1293            inputField.onValueChanged.AddListener(OnChanged);
 94
 1295            yield return ApplyModelChanges(scene, tmpText, model);
 96
 1297            inputField.text = model.placeholder;
 1298            inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g,
 99                model.placeholderColor.b, model.placeholderColor.a);
 12100            referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g,
 101                model.focusedBackground.b, model.focusedBackground.a);
 12102        }
 103
 104        public void OnFocus(string call)
 105        {
 3106            if (inputField.text == model.placeholder)
 107            {
 2108                inputField.text = "";
 109            }
 110
 3111            inputField.customCaretColor = true;
 3112            inputField.caretColor = Color.white;
 3113            Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.id, model.onFocus);
 3114        }
 115
 34116        public void OnChanged(string changedText) { Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData
 117
 118        public void OnBlur(string call)
 119        {
 1120            HideCaret();
 1121            Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.id, model.onBlur);
 1122        }
 123
 124        public void HideCaret()
 125        {
 1126            if (string.IsNullOrEmpty(inputField.text))
 127            {
 1128                inputField.text = model.placeholder;
 129            }
 130
 1131            inputField.customCaretColor = true;
 1132            inputField.caretColor = Color.clear;
 1133            inputField.selectionAnchorPosition = 0;
 1134            inputField.selectionFocusPosition = 0;
 1135            inputField.selectionStringAnchorPosition = 0;
 1136            inputField.selectionStringFocusPosition = 0;
 1137        }
 138
 139        public void OnSubmit(string call)
 140        {
 1141            bool validString = !string.IsNullOrEmpty(tmpText.text);
 142
 1143            if (tmpText.text.Length == 1 && (byte) tmpText.text[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNu
 144            {
 0145                validString = false;
 146            }
 147
 1148            if (validString)
 149            {
 1150                Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.id, model.onTextSubmit, tmpText.text);
 151
 1152                ForceFocus();
 1153            }
 0154            else if (scene.isPersistent) // DCL UI Chat text input
 155            {
 0156                inputField.DeactivateInputField();
 0157                referencesContainer.mouseCatcher.LockCursor();
 158
 159                // To avoid focusing the chat in the same frame we unfocused it
 0160                referencesContainer.inputDetectionPausedFrames = 1;
 161            }
 0162        }
 163
 164        public void ForceFocus()
 165        {
 1166            inputField.text = "";
 1167            inputField.caretColor = Color.white;
 1168            inputField.Select();
 1169            inputField.ActivateInputField();
 1170        }
 171
 172        void UnsuscribeFromEvents()
 173        {
 25174            inputField.onSelect.RemoveAllListeners();
 25175            inputField.onDeselect.RemoveAllListeners();
 25176            inputField.onSubmit.RemoveAllListeners();
 25177            inputField.onValueChanged.RemoveAllListeners();
 25178        }
 179
 180        public override void Dispose()
 181        {
 13182            UnsuscribeFromEvents();
 13183            base.Dispose();
 13184        }
 185
 186        private IEnumerator ApplyModelChanges(IParcelScene scene, TMP_Text text, Model model)
 187        {
 12188            if (!string.IsNullOrEmpty(model.font))
 189            {
 0190                yield return DCLFont.SetFontFromComponent(scene, model.font, text);
 191            }
 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);
 0222            }
 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;
 0233            }
 12234            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 235            {
 0236                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 237            }
 12238        }
 239    }
 240}