From cfcd54dbe0f5a66582ae8835967f5e0a3395eb1a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 1 Sep 2019 18:34:19 +0200 Subject: [PATCH] made text input not test the release build of android :V reflection is a pain --- MLEM.Ui/UiSystem.cs | 11 +++++------ MLEM/Misc/TextInputReflector.cs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 MLEM/Misc/TextInputReflector.cs diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index d247dd9..19d73af 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -7,6 +7,7 @@ using Microsoft.Xna.Framework.Input; using MLEM.Extensions; using MLEM.Font; using MLEM.Input; +using MLEM.Misc; using MLEM.Textures; using MLEM.Ui.Elements; using MLEM.Ui.Style; @@ -67,10 +68,12 @@ namespace MLEM.Ui { }; if (InputHandler.TextInputSupported) { - AddToTextInput(window, (key, character) => { + // 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); } this.OnSelectedElementDrawn = (element, time, batch, alpha, offset) => { @@ -80,10 +83,6 @@ namespace MLEM.Ui { }; } - 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(); diff --git a/MLEM/Misc/TextInputReflector.cs b/MLEM/Misc/TextInputReflector.cs new file mode 100644 index 0000000..01f34d2 --- /dev/null +++ b/MLEM/Misc/TextInputReflector.cs @@ -0,0 +1,30 @@ +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