mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-27 02:29:23 +01:00
fixed WindowExtensions not working on Release
This commit is contained in:
parent
2083868874
commit
9366890aaa
1 changed files with 26 additions and 9 deletions
|
@ -6,25 +6,42 @@ using Microsoft.Xna.Framework.Input;
|
|||
namespace MLEM.Extensions {
|
||||
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) {
|
||||
if (!SupportsTextInput())
|
||||
return false;
|
||||
TextInputAdder.Add(window, callback);
|
||||
return true;
|
||||
return new TextInputReflector(callback).AddToWindow(window);
|
||||
}
|
||||
|
||||
public static bool SupportsTextInput() {
|
||||
return TextInputSupported;
|
||||
return TextInput != null;
|
||||
}
|
||||
|
||||
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) {
|
||||
window.TextInput += (sender, args) => callback(sender, args.Key, args.Character);
|
||||
private static readonly MethodInfo Callback = typeof(TextInputReflector).GetMethod(nameof(OnTextInput), BindingFlags.Instance | BindingFlags.NonPublic);
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue