1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +01:00

fixed WindowExtensions not working on Release

This commit is contained in:
Ellpeck 2020-02-10 23:52:09 +01:00
parent 2083868874
commit 9366890aaa

View file

@ -6,25 +6,42 @@ using Microsoft.Xna.Framework.Input;
namespace MLEM.Extensions { namespace MLEM.Extensions {
public static class WindowExtensions { public static class WindowExtensions {
private static readonly bool TextInputSupported = typeof(GameWindow).GetEvent("TextInput") != null; private static readonly EventInfo TextInput = typeof(GameWindow).GetEvent("TextInput");
public static bool AddTextInputListener(this GameWindow window, TextInputCallback callback) { public static bool AddTextInputListener(this GameWindow window, TextInputCallback callback) {
if (!SupportsTextInput()) return new TextInputReflector(callback).AddToWindow(window);
return false;
TextInputAdder.Add(window, callback);
return true;
} }
public static bool SupportsTextInput() { public static bool SupportsTextInput() {
return TextInputSupported; return TextInput != null;
} }
public delegate void TextInputCallback(object sender, Keys key, char character); public delegate void TextInputCallback(object sender, Keys key, char character);
private static class TextInputAdder { private class TextInputReflector {
public static void Add(GameWindow window, TextInputCallback callback) { private static readonly MethodInfo Callback = typeof(TextInputReflector).GetMethod(nameof(OnTextInput), BindingFlags.Instance | BindingFlags.NonPublic);
window.TextInput += (sender, args) => callback(sender, args.Key, args.Character); private static PropertyInfo key;
private static PropertyInfo character;
private readonly TextInputCallback callback;
public TextInputReflector(TextInputCallback callback) {
this.callback = callback;
}
public bool AddToWindow(GameWindow window) {
if (TextInput == null)
return false;
TextInput.AddEventHandler(window, Delegate.CreateDelegate(TextInput.EventHandlerType, this, Callback));
return true;
}
private void OnTextInput(object sender, EventArgs args) {
if (key == null)
key = args.GetType().GetProperty("Key");
if (character == null)
character = args.GetType().GetProperty("Character");
this.callback.Invoke(sender, (Keys) key.GetValue(args), (char) character.GetValue(args));
} }
} }