| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.ComponentWrapper; |
| | 3 | | using DCL.ECS7.ComponentWrapper.Generic; |
| | 4 | | using DCL.ECS7.InternalComponents; |
| | 5 | | using DCL.ECSComponents.UIAbstractElements; |
| | 6 | | using DCL.ECSComponents.Utils; |
| | 7 | | using DCL.ECSRuntime; |
| | 8 | | using DCL.Models; |
| | 9 | | using DCL.UIElements; |
| | 10 | | using UnityEngine; |
| | 11 | | using UnityEngine.UIElements; |
| | 12 | |
|
| | 13 | | namespace DCL.ECSComponents.UIInput |
| | 14 | | { |
| | 15 | | public class UIInputHandler : UIElementHandlerBase, IECSComponentHandler<PBUiInput> |
| | 16 | | { |
| | 17 | | // The 'DCL.UIInput.uss' stylesheet loaded in DCLDefaultRuntimeTheme scriptable object uses this class |
| | 18 | | private const string USS_CLASS = "dcl-input"; |
| | 19 | |
|
| | 20 | | private UIFontUpdater fontUpdater; |
| | 21 | | private readonly int resultComponentId; |
| | 22 | | private readonly IInternalECSComponent<InternalUIInputResults> inputResults; |
| | 23 | | private readonly AssetPromiseKeeper_Font fontPromiseKeeper; |
| | 24 | | private readonly WrappedComponentPool<IWrappedComponent<PBUiInputResult>> componentPool; |
| | 25 | |
|
| | 26 | | private EventCallback<ChangeEvent<string>> onValueChanged; |
| | 27 | | private EventCallback<NavigationSubmitEvent> onSubmit; |
| | 28 | |
|
| 74 | 29 | | public TextField uiElement { get; private set; } |
| | 30 | |
|
| 20 | 31 | | internal TextFieldPlaceholder placeholder { get; private set; } |
| | 32 | |
|
| | 33 | | public UIInputHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, |
| | 34 | | int resultComponentId, |
| | 35 | | IInternalECSComponent<InternalUIInputResults> inputResults, |
| | 36 | | AssetPromiseKeeper_Font fontPromiseKeeper, int componentId, |
| 4 | 37 | | WrappedComponentPool<IWrappedComponent<PBUiInputResult>> componentPool) : base(internalUiContainer, componen |
| | 38 | | { |
| 4 | 39 | | this.resultComponentId = resultComponentId; |
| 4 | 40 | | this.inputResults = inputResults; |
| 4 | 41 | | this.fontPromiseKeeper = fontPromiseKeeper; |
| 4 | 42 | | this.componentPool = componentPool; |
| 4 | 43 | | } |
| | 44 | |
|
| | 45 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 46 | | { |
| | 47 | | // `TextField` contains a label as well but |
| | 48 | | // passing a null string will actually make it invisible |
| 4 | 49 | | uiElement = new TextField(); |
| 4 | 50 | | uiElement.AddToClassList(USS_CLASS); |
| | 51 | |
|
| 4 | 52 | | placeholder = new TextFieldPlaceholder(uiElement); |
| | 53 | |
|
| 4 | 54 | | AddElementToRoot(scene, entity, uiElement); |
| 4 | 55 | | uiElement.pickingMode = PickingMode.Position; // force pointer blocking |
| 4 | 56 | | fontUpdater = new UIFontUpdater(uiElement, fontPromiseKeeper); |
| | 57 | |
|
| 4 | 58 | | onValueChanged = UIPointerEventsUtils |
| | 59 | | .RegisterFeedback<ChangeEvent<string>> |
| | 60 | | (inputResults, |
| | 61 | | CreateOnChangeInputResult, |
| | 62 | | scene, |
| | 63 | | entity, |
| | 64 | | uiElement, |
| | 65 | | resultComponentId); |
| | 66 | |
|
| | 67 | | // We don't use <KeyDownEvent> because that one is called a lot more and is |
| | 68 | | // triggered twice for some reason (probably a unity bug) |
| 4 | 69 | | onSubmit = UIPointerEventsUtils |
| | 70 | | .RegisterFeedback<NavigationSubmitEvent> |
| | 71 | | (inputResults, |
| | 72 | | CreateOnSubmitInputResult, |
| | 73 | | scene, |
| | 74 | | entity, |
| | 75 | | uiElement, |
| | 76 | | resultComponentId); |
| 4 | 77 | | } |
| | 78 | |
|
| | 79 | | private IPooledWrappedComponent CreateOnChangeInputResult(ChangeEvent<string> evt) |
| | 80 | | { |
| 1 | 81 | | evt.StopPropagation(); |
| | 82 | |
|
| 1 | 83 | | var componentPooled = componentPool.Get(); |
| 1 | 84 | | var componentModel = componentPooled.WrappedComponent.Model; |
| 1 | 85 | | componentModel.Value = uiElement.value; |
| 1 | 86 | | componentModel.IsSubmit = false; |
| 1 | 87 | | return componentPooled; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private IPooledWrappedComponent CreateOnSubmitInputResult(NavigationSubmitEvent evt) |
| | 91 | | { |
| 0 | 92 | | evt.StopPropagation(); |
| | 93 | |
|
| | 94 | | // Space-bar is also detected as a navigation "submit" event |
| 0 | 95 | | if (evt.shiftKey || evt.altKey || evt.ctrlKey || evt.commandKey |
| | 96 | | || (!Input.GetKeyDown(KeyCode.Return) && !Input.GetKeyDown(KeyCode.KeypadEnter))) |
| 0 | 97 | | return null; |
| | 98 | |
|
| 0 | 99 | | var componentPooled = componentPool.Get(); |
| 0 | 100 | | var componentModel = componentPooled.WrappedComponent.Model; |
| 0 | 101 | | componentModel.Value = uiElement.value; |
| 0 | 102 | | componentModel.IsSubmit = true; |
| | 103 | |
|
| | 104 | | // Clear text field without triggering its onChange event |
| 0 | 105 | | uiElement.SetValueWithoutNotify(string.Empty); |
| | 106 | |
|
| 0 | 107 | | return componentPooled; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 111 | | { |
| 4 | 112 | | uiElement.UnregisterFeedback(onValueChanged); |
| 4 | 113 | | uiElement.UnregisterFeedback(onSubmit); |
| 4 | 114 | | RemoveElementFromRoot(scene, entity, uiElement); |
| 4 | 115 | | uiElement = null; |
| 4 | 116 | | fontUpdater.Dispose(); |
| 4 | 117 | | } |
| | 118 | |
|
| | 119 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiInput model) |
| | 120 | | { |
| 4 | 121 | | placeholder.SetPlaceholder(model.Placeholder); |
| 4 | 122 | | placeholder.SetPlaceholderColor(model.GetPlaceholderColor()); |
| 4 | 123 | | placeholder.SetNormalColor(model.GetColor()); |
| | 124 | |
|
| 4 | 125 | | var isReadonly = !model.IsInteractable(); |
| | 126 | |
|
| 4 | 127 | | placeholder.SetReadOnly(isReadonly); |
| 4 | 128 | | uiElement.isReadOnly = isReadonly; |
| 4 | 129 | | uiElement.style.fontSize = model.GetFontSize(); |
| 4 | 130 | | uiElement.style.unityTextAlign = model.GetTextAlign().ToUnityTextAlign(); |
| | 131 | |
|
| 4 | 132 | | if (model.HasValue) |
| 1 | 133 | | uiElement.SetValueWithoutNotify(model.Value); |
| | 134 | |
|
| 4 | 135 | | fontUpdater.Update(model.GetFont()); |
| 4 | 136 | | } |
| | 137 | | } |
| | 138 | | } |