< 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:77
Uncovered lines:59
Coverable lines:136
Total lines:285
Line coverage:56.6% (77 of 136)
Covered branches:0
Total branches:0
Covered methods:15
Total methods:20
Method coverage:75% (15 of 20)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
GetDataFromPb(...)0%14063700%
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;
 7using Decentraland.Sdk.Ecs6;
 8using MainScripts.DCL.Components;
 9
 10namespace DCL.Components
 11{
 12    public class UIInputText : UIShape<UIInputTextRefContainer, UIInputText.Model>
 13    {
 14        [System.Serializable]
 15        public new class Model : UIShape.Model
 16        {
 17            public float outlineWidth = 0f;
 3418            public Color outlineColor = Color.white;
 19
 3420            public Color color = Color.white;
 21
 3422            public float fontSize = 100f;
 23            public string font;
 3424            public string value = "";
 3425            public string hTextAlign = "bottom";
 3426            public string vTextAlign = "left";
 27            public bool textWrapping = false;
 28
 29            public float shadowBlur = 0f;
 30            public float shadowOffsetX = 0f;
 31            public float shadowOffsetY = 0f;
 3432            public Color shadowColor = new Color(1, 1, 1);
 33
 34            public float paddingTop = 0f;
 35            public float paddingRight = 0f;
 36            public float paddingBottom = 0f;
 37            public float paddingLeft = 0f;
 38
 39            public string placeholder;
 3440            public Color placeholderColor = Color.white;
 3441            public Color focusedBackground = Color.black;
 42
 43            public string onTextSubmit;
 44            public string onChanged;
 45            public string onFocus;
 46            public string onBlur;
 47            public string onTextChanged;
 48
 49            public override BaseModel GetDataFromJSON(string json) =>
 1250                Utils.SafeFromJson<Model>(json);
 51
 52            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 53            {
 054                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiInputText)
 055                    return Utils.SafeUnimplemented<UIInputText, Model>(expected: ComponentBodyPayload.PayloadOneofCase.U
 56
 057                var pb = new Model();
 058                if (pbModel.UiInputText.HasName) pb.name = pbModel.UiInputText.Name;
 059                if (pbModel.UiInputText.HasParentComponent) pb.parentComponent = pbModel.UiInputText.ParentComponent;
 060                if (pbModel.UiInputText.HasVisible) pb.visible = pbModel.UiInputText.Visible;
 061                if (pbModel.UiInputText.HasOpacity) pb.opacity = pbModel.UiInputText.Opacity;
 062                if (pbModel.UiInputText.HasHAlign) pb.hAlign = pbModel.UiInputText.HAlign;
 063                if (pbModel.UiInputText.HasVAlign) pb.vAlign = pbModel.UiInputText.VAlign;
 064                if (pbModel.UiInputText.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel.U
 065                if (pbModel.UiInputText.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbMode
 066                if (pbModel.UiInputText.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.position
 067                if (pbModel.UiInputText.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.position
 068                if (pbModel.UiInputText.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiInputText.IsPointerBlocker;
 69
 070                if (pbModel.UiInputText.HasOutlineWidth) pb.outlineWidth = pbModel.UiInputText.OutlineWidth;
 071                if (pbModel.UiInputText.OutlineColor != null) pb.outlineColor = pbModel.UiInputText.OutlineColor.AsUnity
 072                if (pbModel.UiInputText.Color != null) pb.color = pbModel.UiInputText.Color.AsUnityColor();
 073                if (pbModel.UiInputText.HasFontSize) pb.fontSize = pbModel.UiInputText.FontSize;
 074                if (pbModel.UiInputText.HasFont) pb.font = pbModel.UiInputText.Font;
 075                if (pbModel.UiInputText.HasValue) pb.value = pbModel.UiInputText.Value;
 076                if (pbModel.UiInputText.HasHTextAlign) pb.hTextAlign = pbModel.UiInputText.HTextAlign;
 077                if (pbModel.UiInputText.HasVTextAlign) pb.vTextAlign = pbModel.UiInputText.VTextAlign;
 078                if (pbModel.UiInputText.HasTextWrapping) pb.textWrapping = pbModel.UiInputText.TextWrapping;
 079                if (pbModel.UiInputText.HasShadowBlur) pb.shadowBlur = pbModel.UiInputText.ShadowBlur;
 080                if (pbModel.UiInputText.HasShadowOffsetX) pb.shadowOffsetX = pbModel.UiInputText.ShadowOffsetX;
 081                if (pbModel.UiInputText.HasShadowOffsetY) pb.shadowOffsetY = pbModel.UiInputText.ShadowOffsetY;
 082                if (pbModel.UiInputText.ShadowColor != null) pb.shadowColor = pbModel.UiInputText.ShadowColor.AsUnityCol
 083                if (pbModel.UiInputText.HasPaddingTop) pb.paddingTop = pbModel.UiInputText.PaddingTop;
 084                if (pbModel.UiInputText.HasPaddingRight) pb.paddingRight = pbModel.UiInputText.PaddingRight;
 085                if (pbModel.UiInputText.HasPaddingBottom) pb.paddingBottom = pbModel.UiInputText.PaddingBottom;
 086                if (pbModel.UiInputText.HasPaddingLeft) pb.paddingLeft = pbModel.UiInputText.PaddingLeft;
 087                if (pbModel.UiInputText.HasPlaceholder) pb.placeholder = pbModel.UiInputText.Placeholder;
 088                if (pbModel.UiInputText.FocusedBackground != null) pb.focusedBackground = pbModel.UiInputText.FocusedBac
 089                if (pbModel.UiInputText.HasOnTextSubmit) pb.onTextSubmit = pbModel.UiInputText.OnTextSubmit;
 090                if (pbModel.UiInputText.HasOnChanged) pb.onChanged = pbModel.UiInputText.OnChanged;
 091                if (pbModel.UiInputText.HasOnFocus) pb.onFocus = pbModel.UiInputText.OnFocus;
 092                if (pbModel.UiInputText.HasOnBlur) pb.onBlur = pbModel.UiInputText.OnBlur;
 093                if (pbModel.UiInputText.HasOnTextChanged) pb.onTextChanged = pbModel.UiInputText.OnTextChanged;
 94
 095                return pb;
 96            }
 97        }
 98
 1099        public override string referencesContainerPrefabName => "UIInputText";
 100
 12101        public TMP_Text tmpText => referencesContainer.text;
 177102        public TMP_InputField inputField => referencesContainer.inputField;
 0103        public RectTransform rectTransform => referencesContainer.rectTransform;
 104
 30105        public UIInputText() { model = new Model(); }
 106
 0107        public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; }
 108
 109        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 110        {
 0111            Debug.LogError(
 112                "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities.");
 0113        }
 114
 0115        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { }
 116
 117        public override IEnumerator ApplyChanges(BaseModel newModel)
 118        {
 119            //NOTE(Brian): We have to serialize twice now, but in the future we should fix the
 120            //             client data structure to be like this, so we can serialize all of it in one shot.
 12121            model = (Model) newModel;
 122
 12123            inputField.textViewport = referencesContainer.rectTransform;
 124
 12125            UnsuscribeFromEvents();
 126
 12127            inputField.onSelect.AddListener(OnFocus);
 12128            inputField.onDeselect.AddListener(OnBlur);
 12129            inputField.onSubmit.AddListener(OnSubmit);
 12130            inputField.onValueChanged.AddListener(OnChanged);
 131
 132            // We avoid using even yield break; as this instruction skips a frame and we don't want that.
 12133            if ( !DCLFont.IsFontLoaded(scene, model.font) )
 134            {
 0135                yield return DCLFont.WaitUntilFontIsReady(scene, model.font);
 136            }
 137
 12138            DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text);
 12139            ApplyModelChanges(tmpText, model);
 140
 12141            inputField.text = model.placeholder;
 12142            inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g,
 143                model.placeholderColor.b, model.placeholderColor.a);
 12144            referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g,
 145                model.focusedBackground.b, model.focusedBackground.a);
 12146        }
 147
 148        public void OnFocus(string call)
 149        {
 2150            if (inputField.text == model.placeholder)
 151            {
 2152                inputField.text = "";
 153            }
 154
 2155            inputField.customCaretColor = true;
 2156            inputField.caretColor = Color.white;
 2157            Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.sceneNumber, model.onFocus);
 2158        }
 159
 160        public void OnChanged(string changedText)
 161        {
 162            // NOTE: OSX is adding the ESC character at the end of the string when ESC is pressed
 163            #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
 164            if (changedText.Length > 0 && changedText[changedText.Length - 1] == 27)
 165            {
 166                changedText = changedText.Substring(0, changedText.Length - 1);
 167                inputField.SetTextWithoutNotify(changedText);
 168            }
 169            #endif
 170
 171            // NOTE: we keep `ReportOnTextInputChangedEvent` for backward compatibility (it won't be called for scenes u
 17172            Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData.sceneNumber, model.onChanged, changedTe
 17173            Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.sceneNumber, model.onTextChanged, c
 17174        }
 175
 176        public void OnBlur(string call)
 177        {
 2178            HideCaret();
 2179            Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.sceneNumber, model.onBlur);
 2180        }
 181
 182        public void HideCaret()
 183        {
 2184            if (string.IsNullOrEmpty(inputField.text))
 185            {
 2186                inputField.text = model.placeholder;
 187            }
 188
 2189            inputField.customCaretColor = true;
 2190            inputField.caretColor = Color.clear;
 2191            inputField.selectionAnchorPosition = 0;
 2192            inputField.selectionFocusPosition = 0;
 2193            inputField.selectionStringAnchorPosition = 0;
 2194            inputField.selectionStringFocusPosition = 0;
 2195        }
 196
 197        public void OnSubmit(string call)
 198        {
 2199            bool validString = !string.IsNullOrEmpty(call);
 200
 2201            if (call?.Length == 1 && (byte) call[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNullOrWhitespace.
 202            {
 0203                validString = false;
 204            }
 205
 2206            if (validString)
 207            {
 208                // NOTE: we keep `ReportOnTextSubmitEvent` for backward compatibility (it won't be called for scenes usi
 2209                Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.sceneNumber, model.onTextSubmit, call);
 2210                Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.sceneNumber, model.onTextChange
 211            }
 0212            else if (scene.isPersistent) // DCL UI Chat text input
 213            {
 0214                inputField.DeactivateInputField();
 0215                referencesContainer.mouseCatcher.LockCursor();
 216
 217                // To avoid focusing the chat in the same frame we unfocused it
 0218                referencesContainer.inputDetectionPausedFrames = 1;
 219            }
 0220        }
 221
 222        void UnsuscribeFromEvents()
 223        {
 15224            inputField.onSelect.RemoveAllListeners();
 15225            inputField.onDeselect.RemoveAllListeners();
 15226            inputField.onSubmit.RemoveAllListeners();
 15227            inputField.onValueChanged.RemoveAllListeners();
 15228        }
 229
 230        public override void Dispose()
 231        {
 3232            UnsuscribeFromEvents();
 3233            base.Dispose();
 3234        }
 235
 236        private void ApplyModelChanges(TMP_Text text, Model model)
 237        {
 12238            text.text = model.value;
 239
 12240            text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0);
 12241            text.fontSize = (int) model.fontSize;
 12242            text.richText = true;
 12243            text.overflowMode = TextOverflowModes.Overflow;
 244
 12245            text.margin =
 246                new Vector4
 247                (
 248                    (int) model.paddingLeft,
 249                    (int) model.paddingTop,
 250                    (int) model.paddingRight,
 251                    (int) model.paddingBottom
 252                );
 253
 12254            text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign);
 12255            text.lineSpacing = 0f;
 256
 12257            text.maxVisibleLines = int.MaxValue;
 258
 259
 12260            text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing;
 261
 12262            if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0)
 263            {
 0264                text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
 0265                text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor);
 0266                text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur);
 267            }
 12268            else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON"))
 269            {
 0270                text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
 271            }
 272
 12273            if (model.outlineWidth > 0f)
 274            {
 0275                text.fontSharedMaterial.EnableKeyword("OUTLINE_ON");
 0276                text.outlineWidth = model.outlineWidth;
 0277                text.outlineColor = model.outlineColor;
 278            }
 12279            else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON"))
 280            {
 0281                text.fontSharedMaterial.DisableKeyword("OUTLINE_ON");
 282            }
 12283        }
 284    }
 285}