1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-09-28 09:01:06 +02:00

made text fields work on mobile too

This commit is contained in:
Ellpeck 2019-08-30 19:05:27 +02:00
parent 353afdef6f
commit 3862f78c9b
9 changed files with 42 additions and 33 deletions

View file

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>net462</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -14,7 +14,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MonoGame.Content.Builder" Version="3.7.0.9" /> <PackageReference Include="MonoGame.Content.Builder" Version="3.7.0.9" />
<PackageReference Include="MonoGame.Framework.DesktopGL.Core" Version="3.7.0.7" /> <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.7.0.1708" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -17,7 +17,7 @@
<PackageReference Include="MonoGame.Extended" Version="3.7.0"> <PackageReference Include="MonoGame.Extended" Version="3.7.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>

View file

@ -17,7 +17,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Coroutine" Version="1.0.1" /> <PackageReference Include="Coroutine" Version="1.0.1" />
<PackageReference Include="MonoGame.Extended" Version="3.7.0" /> <PackageReference Include="MonoGame.Extended" Version="3.7.0" />
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>

View file

@ -6,6 +6,7 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Font; using MLEM.Font;
using MLEM.Input;
using MLEM.Textures; using MLEM.Textures;
using MLEM.Ui.Style; using MLEM.Ui.Style;
@ -30,21 +31,35 @@ namespace MLEM.Ui.Elements {
private double caretBlinkTimer; private double caretBlinkTimer;
private int textStartIndex; private int textStartIndex;
public Rule InputRule; public Rule InputRule;
public string MobileTitle;
public string MobileDescription;
public TextField(Anchor anchor, Vector2 size, Rule rule = null, IGenericFont font = null) : base(anchor, size) { public TextField(Anchor anchor, Vector2 size, Rule rule = null, IGenericFont font = null) : base(anchor, size) {
this.InputRule = rule ?? DefaultRule; this.InputRule = rule ?? DefaultRule;
this.font = font; this.font = font;
this.OnTextInput += (element, key, character) => {
if (!this.IsSelected) if (InputHandler.TextInputSupported) {
return; this.OnTextInput += (element, key, character) => {
if (key == Keys.Back) { if (!this.IsSelected)
if (this.text.Length > 0) { return;
this.RemoveText(this.text.Length - 1, 1); 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() { private void HandleTextChange() {
@ -106,7 +121,7 @@ namespace MLEM.Ui.Elements {
} }
public void AppendText(object text) { public void AppendText(object text) {
if (!this.InputRule(this, text.ToString())) if (!this.InputRule(this, text.ToString()))
return; return;
this.text.Append(text); this.text.Append(text);
this.HandleTextChange(); this.HandleTextChange();

View file

@ -14,7 +14,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>

View file

@ -48,7 +48,6 @@ namespace MLEM.Ui {
public BlendState BlendState; public BlendState BlendState;
public SamplerState SamplerState = SamplerState.PointClamp; public SamplerState SamplerState = SamplerState.PointClamp;
public UiControls Controls; public UiControls Controls;
public readonly bool SupportsTextInput;
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) { public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
this.Controls = new UiControls(this, inputHandler); this.Controls = new UiControls(this, inputHandler);
@ -63,17 +62,18 @@ namespace MLEM.Ui {
root.Element.ForceUpdateArea(); root.Element.ForceUpdateArea();
}; };
try { if (InputHandler.TextInputSupported) {
NativeTextInput.AddToTextInput(window, (key, character) => { AddToTextInput(window, (key, character) => {
foreach (var root in this.rootElements) foreach (var root in this.rootElements)
root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character)); 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<Keys, char> func) {
window.TextInput += (sender, args) => func(args.Key, args.Character);
}
public void Update(GameTime time) { public void Update(GameTime time) {
this.Controls.Update(); this.Controls.Update();
@ -146,14 +146,6 @@ namespace MLEM.Ui {
root.Element.Propagate(action); root.Element.Propagate(action);
} }
private static class NativeTextInput {
internal static void AddToTextInput(GameWindow window, Action<Keys, char> func) {
window.TextInput += (sender, args) => func(args.Key, args.Character);
}
}
} }
public class RootElement { public class RootElement {

View file

@ -11,6 +11,7 @@ namespace MLEM.Input {
public class InputHandler { public class InputHandler {
public static MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray(); public static MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray();
public static readonly bool TextInputSupported = typeof(GameWindow).GetEvent("TextInput") != null;
public KeyboardState LastKeyboardState { get; private set; } public KeyboardState LastKeyboardState { get; private set; }
public KeyboardState KeyboardState { get; private set; } public KeyboardState KeyboardState { get; private set; }
@ -40,7 +41,6 @@ namespace MLEM.Input {
this.HandleMouse = handleMouse; this.HandleMouse = handleMouse;
this.HandleGamepads = handleGamepads; this.HandleGamepads = handleGamepads;
this.HandleTouch = handleTouch; this.HandleTouch = handleTouch;
this.Gestures = this.gestures.AsReadOnly(); this.Gestures = this.gestures.AsReadOnly();
} }

View file

@ -14,7 +14,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>

View file

@ -39,6 +39,8 @@ namespace AndroidTests {
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Toggle Image") { panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(0.5F, 10), "Toggle Image") {
OnPressed = element => image.IsHidden = !image.IsHidden 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) { protected override void DoDraw(GameTime gameTime) {