| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSComponents.UIAbstractElements; |
| | 4 | | using DCL.ECSComponents.Utils; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using DCL.Models; |
| | 7 | | using DCL.UIElements; |
| | 8 | | using System; |
| | 9 | | using UnityEngine.UIElements; |
| | 10 | |
|
| | 11 | | namespace 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 | |
|
| 0 | 22 | | internal TextField uiElement { get; private set; } |
| | 23 | |
|
| 0 | 24 | | internal TextFieldPlaceholder placeholder { get; private set; } |
| | 25 | |
|
| | 26 | | public UIInputHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, |
| | 27 | | int resultComponentId, |
| | 28 | | IInternalECSComponent<InternalUIInputResults> inputResults, |
| 0 | 29 | | AssetPromiseKeeper_Font fontPromiseKeeper, int componentId) : base(internalUiContainer, componentId) |
| | 30 | | { |
| 0 | 31 | | this.resultComponentId = resultComponentId; |
| 0 | 32 | | this.inputResults = inputResults; |
| 0 | 33 | | this.fontPromiseKeeper = fontPromiseKeeper; |
| 0 | 34 | | } |
| | 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 |
| 0 | 40 | | uiElement = new TextField(); |
| 0 | 41 | | uiElement.style.flexGrow = 1f; |
| | 42 | |
|
| 0 | 43 | | placeholder = new TextFieldPlaceholder(uiElement); |
| | 44 | |
|
| 0 | 45 | | AddElementToRoot(scene, entity, uiElement); |
| 0 | 46 | | fontUpdater = new UIFontUpdater(uiElement, fontPromiseKeeper); |
| | 47 | |
|
| 0 | 48 | | onValueChanged = UIPointerEventsUtils |
| | 49 | | .RegisterFeedback<ChangeEvent<string>, PBUiInputResult> |
| | 50 | | (inputResults, |
| | 51 | | CreateInputResult, |
| | 52 | | scene, |
| | 53 | | entity, |
| | 54 | | uiElement, |
| | 55 | | resultComponentId); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | private static PBUiInputResult CreateInputResult(ChangeEvent<string> onValueChange) => |
| 0 | 59 | | new () { Value = onValueChange.newValue }; |
| | 60 | |
|
| | 61 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 62 | | { |
| 0 | 63 | | uiElement.UnregisterFeedback(onValueChanged); |
| 0 | 64 | | RemoveElementFromRoot(scene, entity, uiElement); |
| 0 | 65 | | uiElement = null; |
| 0 | 66 | | fontUpdater.Dispose(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiInput model) |
| | 70 | | { |
| 0 | 71 | | placeholder.SetPlaceholder(model.Placeholder); |
| 0 | 72 | | placeholder.SetPlaceholderColor(model.GetPlaceholderColor()); |
| 0 | 73 | | placeholder.SetNormalColor(model.GetColor()); |
| | 74 | |
|
| 0 | 75 | | var isReadonly = !model.IsInteractable(); |
| | 76 | |
|
| 0 | 77 | | placeholder.SetReadOnly(isReadonly); |
| 0 | 78 | | uiElement.isReadOnly = isReadonly; |
| 0 | 79 | | uiElement.style.fontSize = model.GetFontSize(); |
| 0 | 80 | | uiElement.style.unityTextAlign = model.GetTextAlign().ToUnityTextAlign(); |
| | 81 | |
|
| 0 | 82 | | fontUpdater.Update(model.GetFont()); |
| 0 | 83 | | } |
| | 84 | | } |
| | 85 | | } |