< Summary

Class:DCL.ECSComponents.UIInput.UIInputHandler
Assembly:DCL.ECSComponents.UIInput
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/UIInput/UIInputHandler.cs
Covered lines:0
Uncovered lines:30
Coverable lines:30
Total lines:85
Line coverage:0% (0 of 30)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UIInputHandler(...)0%2100%
OnComponentCreated(...)0%2100%
CreateInputResult(...)0%2100%
OnComponentRemoved(...)0%2100%
OnComponentModelUpdated(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/UIInput/UIInputHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7.InternalComponents;
 3using DCL.ECSComponents.UIAbstractElements;
 4using DCL.ECSComponents.Utils;
 5using DCL.ECSRuntime;
 6using DCL.Models;
 7using DCL.UIElements;
 8using System;
 9using UnityEngine.UIElements;
 10
 11namespace DCL.ECSComponents.UIInput
 12{
 13    public class UIInputHandler : UIElementHandlerBase, IECSComponentHandler<PBUiInput>
 14    {
 15        private UIFontUpdater fontUpdater;
 16        private readonly int resultComponentId;
 17        private readonly IInternalECSComponent<InternalUIInputResults> inputResults;
 18        private readonly AssetPromiseKeeper_Font fontPromiseKeeper;
 19
 20        private EventCallback<ChangeEvent<string>> onValueChanged;
 21
 022        internal TextField uiElement { get; private set; }
 23
 024        internal TextFieldPlaceholder placeholder { get; private set; }
 25
 26        public UIInputHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer,
 27            int resultComponentId,
 28            IInternalECSComponent<InternalUIInputResults> inputResults,
 029            AssetPromiseKeeper_Font fontPromiseKeeper, int componentId) : base(internalUiContainer, componentId)
 30        {
 031            this.resultComponentId = resultComponentId;
 032            this.inputResults = inputResults;
 033            this.fontPromiseKeeper = fontPromiseKeeper;
 034        }
 35
 36        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 37        {
 38            // `TextField` contains a label as well but
 39            // passing a null string will actually make it invisible
 040            uiElement = new TextField();
 041            uiElement.style.flexGrow = 1f;
 42
 043            placeholder = new TextFieldPlaceholder(uiElement);
 44
 045            AddElementToRoot(scene, entity, uiElement);
 046            fontUpdater = new UIFontUpdater(uiElement, fontPromiseKeeper);
 47
 048            onValueChanged = UIPointerEventsUtils
 49               .RegisterFeedback<ChangeEvent<string>, PBUiInputResult>
 50                (inputResults,
 51                    CreateInputResult,
 52                    scene,
 53                    entity,
 54                    uiElement,
 55                    resultComponentId);
 056        }
 57
 58        private static PBUiInputResult CreateInputResult(ChangeEvent<string> onValueChange) =>
 059            new () { Value = onValueChange.newValue };
 60
 61        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 62        {
 063            uiElement.UnregisterFeedback(onValueChanged);
 064            RemoveElementFromRoot(scene, entity, uiElement);
 065            uiElement = null;
 066            fontUpdater.Dispose();
 067        }
 68
 69        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiInput model)
 70        {
 071            placeholder.SetPlaceholder(model.Placeholder);
 072            placeholder.SetPlaceholderColor(model.GetPlaceholderColor());
 073            placeholder.SetNormalColor(model.GetColor());
 74
 075            var isReadonly = !model.IsInteractable();
 76
 077            placeholder.SetReadOnly(isReadonly);
 078            uiElement.isReadOnly = isReadonly;
 079            uiElement.style.fontSize = model.GetFontSize();
 080            uiElement.style.unityTextAlign = model.GetTextAlign().ToUnityTextAlign();
 81
 082            fontUpdater.Update(model.GetFont());
 083        }
 84    }
 85}