From 334dea8b396b0f110ddbec2c7374dfb7540f4497 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 1 Sep 2019 19:33:33 +0200 Subject: [PATCH] make the mess I made a bit nicer :^) --- MLEM.Ui/Elements/TextField.cs | 2 +- MLEM.Ui/UiSystem.cs | 14 ++++----- MLEM/Extensions/WindowExtensions.cs | 49 +++++++++++++++++++++++++++++ MLEM/Input/InputHandler.cs | 1 - MLEM/Misc/TextInputReflector.cs | 30 ------------------ 5 files changed, 56 insertions(+), 40 deletions(-) create mode 100644 MLEM/Extensions/WindowExtensions.cs delete mode 100644 MLEM/Misc/TextInputReflector.cs diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 037ea24..42fd6ed 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -38,7 +38,7 @@ namespace MLEM.Ui.Elements { this.InputRule = rule ?? DefaultRule; this.font = font; - if (InputHandler.TextInputSupported) { + if (WindowExtensions.SupportsTextInput()) { this.OnTextInput += (element, key, character) => { if (!this.IsSelected) return; diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 19d73af..2eaecec 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -16,6 +16,7 @@ namespace MLEM.Ui { public class UiSystem { public readonly GraphicsDevice GraphicsDevice; + public readonly GameWindow Window; public Rectangle Viewport { get; private set; } private readonly List rootElements = new List(); @@ -57,6 +58,7 @@ namespace MLEM.Ui { public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) { this.Controls = new UiControls(this, inputHandler); this.GraphicsDevice = device; + this.Window = window; this.style = style; this.Viewport = device.Viewport.Bounds; this.AutoScaleReferenceSize = this.Viewport.Size; @@ -67,14 +69,10 @@ namespace MLEM.Ui { root.Element.ForceUpdateArea(); }; - if (InputHandler.TextInputSupported) { - // this needs to be done using reflection because the event and - // its argument class don't exist on non-Desktop devices annoyingly - new TextInputReflector((sender, key, character) => { - foreach (var root in this.rootElements) - root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character)); - }).AddToWindow(window); - } + window.AddTextInputListener((sender, key, character) => { + foreach (var root in this.rootElements) + root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character)); + }); this.OnSelectedElementDrawn = (element, time, batch, alpha, offset) => { if (!this.Controls.SelectedLastElementWithMouse && element.SelectionIndicator != null) { diff --git a/MLEM/Extensions/WindowExtensions.cs b/MLEM/Extensions/WindowExtensions.cs new file mode 100644 index 0000000..7aa0bac --- /dev/null +++ b/MLEM/Extensions/WindowExtensions.cs @@ -0,0 +1,49 @@ +using System; +using System.Reflection; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace MLEM.Extensions { + public static class WindowExtensions { + + private static readonly EventInfo TextInput = typeof(GameWindow).GetEvent("TextInput"); + + public static bool AddTextInputListener(this GameWindow window, TextInputCallback callback) { + return new TextInputReflector(callback).AddToWindow(window); + } + + public static bool SupportsTextInput() { + return TextInput != null; + } + + public delegate void TextInputCallback(object sender, Keys key, char character); + + private class TextInputReflector { + + private readonly TextInputCallback callback; + + public TextInputReflector(TextInputCallback callback) { + this.callback = callback; + } + + public bool AddToWindow(GameWindow window) { + if (TextInput == null) + return false; + var handler = this.GetType().GetMethod(nameof(this.OnTextInput), new[] {typeof(object), typeof(EventArgs)}); + if (handler == null) + return false; + TextInput.AddEventHandler(window, Delegate.CreateDelegate(TextInput.EventHandlerType, this, handler)); + return true; + } + + public void OnTextInput(object sender, EventArgs args) { + var type = args.GetType(); + var key = (Keys) type.GetProperty("Key").GetValue(args); + var character = (char) type.GetProperty("Character").GetValue(args); + this.callback.Invoke(sender, key, character); + } + + } + + } +} \ No newline at end of file diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index d3521dd..2998f1a 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -11,7 +11,6 @@ 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; } diff --git a/MLEM/Misc/TextInputReflector.cs b/MLEM/Misc/TextInputReflector.cs deleted file mode 100644 index 01f34d2..0000000 --- a/MLEM/Misc/TextInputReflector.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Input; - -namespace MLEM.Misc { - public class TextInputReflector { - - private readonly TextInputCallback callback; - - public TextInputReflector(TextInputCallback callback) { - this.callback = callback; - } - - public void AddToWindow(GameWindow window) { - var evt = window.GetType().GetEvent("TextInput"); - var handler = this.GetType().GetMethod(nameof(this.OnTextInput), new[] {typeof(object), typeof(EventArgs)}); - evt.AddEventHandler(window, Delegate.CreateDelegate(evt.EventHandlerType, this, handler)); - } - - public void OnTextInput(object sender, EventArgs args) { - var type = args.GetType(); - var key = (Keys) type.GetProperty("Key").GetValue(args); - var character = (char) type.GetProperty("Character").GetValue(args); - this.callback.Invoke(sender, key, character); - } - - public delegate void TextInputCallback(object sender, Keys key, char character); - - } -} \ No newline at end of file