From 3862f78c9b270ae450ca7a15491c4c92500d6510 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 30 Aug 2019 19:05:27 +0200 Subject: [PATCH] made text fields work on mobile too --- Demos/Demos.csproj | 4 ++-- MLEM.Extended/MLEM.Extended.csproj | 2 +- MLEM.Startup/MLEM.Startup.csproj | 2 +- MLEM.Ui/Elements/TextField.cs | 37 +++++++++++++++++++++--------- MLEM.Ui/MLEM.Ui.csproj | 2 +- MLEM.Ui/UiSystem.cs | 22 ++++++------------ MLEM/Input/InputHandler.cs | 2 +- MLEM/MLEM.csproj | 2 +- Test/GameImpl.cs | 2 ++ 9 files changed, 42 insertions(+), 33 deletions(-) diff --git a/Demos/Demos.csproj b/Demos/Demos.csproj index 61edf6a..b0fecce 100644 --- a/Demos/Demos.csproj +++ b/Demos/Demos.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.0 + net462 @@ -14,7 +14,7 @@ - + diff --git a/MLEM.Extended/MLEM.Extended.csproj b/MLEM.Extended/MLEM.Extended.csproj index 2eda7c6..6cadefa 100644 --- a/MLEM.Extended/MLEM.Extended.csproj +++ b/MLEM.Extended/MLEM.Extended.csproj @@ -17,7 +17,7 @@ all - + all diff --git a/MLEM.Startup/MLEM.Startup.csproj b/MLEM.Startup/MLEM.Startup.csproj index 0ca7638..018f433 100644 --- a/MLEM.Startup/MLEM.Startup.csproj +++ b/MLEM.Startup/MLEM.Startup.csproj @@ -17,7 +17,7 @@ - + all diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 9f9bda2..037ea24 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MLEM.Extensions; using MLEM.Font; +using MLEM.Input; using MLEM.Textures; using MLEM.Ui.Style; @@ -30,21 +31,35 @@ namespace MLEM.Ui.Elements { private double caretBlinkTimer; private int textStartIndex; public Rule InputRule; + public string MobileTitle; + public string MobileDescription; public TextField(Anchor anchor, Vector2 size, Rule rule = null, IGenericFont font = null) : base(anchor, size) { this.InputRule = rule ?? DefaultRule; this.font = font; - this.OnTextInput += (element, key, character) => { - if (!this.IsSelected) - return; - if (key == Keys.Back) { - if (this.text.Length > 0) { - this.RemoveText(this.text.Length - 1, 1); + + if (InputHandler.TextInputSupported) { + this.OnTextInput += (element, key, character) => { + if (!this.IsSelected) + return; + if (key == Keys.Back) { + if (this.text.Length > 0) { + this.RemoveText(this.text.Length - 1, 1); + } + } else { + this.AppendText(character); } - } else { - this.AppendText(character); - } - }; + }; + } else { + this.OnPressed += async e => { + if (!KeyboardInput.IsVisible) { + var title = this.MobileTitle ?? this.PlaceholderText; + var result = await KeyboardInput.Show(title, this.MobileDescription, this.Text); + if (result != null) + this.SetText(result.Replace('\n', ' ')); + } + }; + } } private void HandleTextChange() { @@ -106,7 +121,7 @@ namespace MLEM.Ui.Elements { } public void AppendText(object text) { - if (!this.InputRule(this, text.ToString())) + if (!this.InputRule(this, text.ToString())) return; this.text.Append(text); this.HandleTextChange(); diff --git a/MLEM.Ui/MLEM.Ui.csproj b/MLEM.Ui/MLEM.Ui.csproj index cddddde..bd234d1 100644 --- a/MLEM.Ui/MLEM.Ui.csproj +++ b/MLEM.Ui/MLEM.Ui.csproj @@ -14,7 +14,7 @@ - + all diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index c749bdf..578ec8f 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -48,7 +48,6 @@ namespace MLEM.Ui { public BlendState BlendState; public SamplerState SamplerState = SamplerState.PointClamp; public UiControls Controls; - public readonly bool SupportsTextInput; public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) { this.Controls = new UiControls(this, inputHandler); @@ -62,18 +61,19 @@ namespace MLEM.Ui { foreach (var root in this.rootElements) root.Element.ForceUpdateArea(); }; - - try { - NativeTextInput.AddToTextInput(window, (key, character) => { + + if (InputHandler.TextInputSupported) { + AddToTextInput(window, (key, character) => { foreach (var root in this.rootElements) root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character)); }); - this.SupportsTextInput = true; - } catch (TypeLoadException) { - this.SupportsTextInput = false; } } + private static void AddToTextInput(GameWindow window, Action func) { + window.TextInput += (sender, args) => func(args.Key, args.Character); + } + public void Update(GameTime time) { this.Controls.Update(); @@ -146,14 +146,6 @@ namespace MLEM.Ui { root.Element.Propagate(action); } - private static class NativeTextInput { - - internal static void AddToTextInput(GameWindow window, Action func) { - window.TextInput += (sender, args) => func(args.Key, args.Character); - } - - } - } public class RootElement { diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index 23b0d0f..4a64811 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -11,6 +11,7 @@ namespace MLEM.Input { public class InputHandler { public static MouseButton[] MouseButtons = EnumHelper.GetValues().ToArray(); + public static readonly bool TextInputSupported = typeof(GameWindow).GetEvent("TextInput") != null; public KeyboardState LastKeyboardState { get; private set; } public KeyboardState KeyboardState { get; private set; } @@ -40,7 +41,6 @@ namespace MLEM.Input { this.HandleMouse = handleMouse; this.HandleGamepads = handleGamepads; this.HandleTouch = handleTouch; - this.Gestures = this.gestures.AsReadOnly(); } diff --git a/MLEM/MLEM.csproj b/MLEM/MLEM.csproj index 3f2b1f2..16badd2 100644 --- a/MLEM/MLEM.csproj +++ b/MLEM/MLEM.csproj @@ -14,7 +14,7 @@ - + all diff --git a/Test/GameImpl.cs b/Test/GameImpl.cs index fa24ba2..cc71da9 100644 --- a/Test/GameImpl.cs +++ b/Test/GameImpl.cs @@ -39,6 +39,8 @@ namespace AndroidTests { panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Toggle Image") { OnPressed = element => image.IsHidden = !image.IsHidden }); + + panel.AddChild(new TextField(Anchor.AutoLeft, new Vector2(1, 10)) {PlaceholderText = "Tap to type"}); } protected override void DoDraw(GameTime gameTime) {