1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-20 12:09:10 +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>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>
<ItemGroup>
@ -14,7 +14,7 @@
<ItemGroup>
<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>

View file

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

View file

@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="Coroutine" Version="1.0.1" />
<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>
</PackageReference>
</ItemGroup>

View file

@ -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();

View file

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

View file

@ -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<Keys, char> 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<Keys, char> func) {
window.TextInput += (sender, args) => func(args.Key, args.Character);
}
}
}
public class RootElement {

View file

@ -11,6 +11,7 @@ namespace MLEM.Input {
public class InputHandler {
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 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();
}

View file

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

View file

@ -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) {