1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

made text input not test the release build of android :V reflection is a pain

This commit is contained in:
Ellpeck 2019-09-01 18:34:19 +02:00
parent e1ee92ad9b
commit cfcd54dbe0
2 changed files with 35 additions and 6 deletions

View file

@ -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<Keys, char> func) {
window.TextInput += (sender, args) => func(args.Key, args.Character);
}
public void Update(GameTime time) {
this.Controls.Update();

View file

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