1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

fixed textinputwrapper not working on core

This commit is contained in:
Ellpeck 2020-04-25 23:30:17 +02:00
parent 36357f0253
commit 74f0007280

View file

@ -26,8 +26,8 @@ namespace MLEM.Misc {
public class DesktopGl<T> : TextInputWrapper { public class DesktopGl<T> : TextInputWrapper {
private PropertyInfo key; private MemberInfo key;
private PropertyInfo character; private MemberInfo character;
private readonly Action<GameWindow, EventHandler<T>> addListener; private readonly Action<GameWindow, EventHandler<T>> addListener;
public DesktopGl(Action<GameWindow, EventHandler<T>> addListener) { public DesktopGl(Action<GameWindow, EventHandler<T>> addListener) {
@ -40,14 +40,33 @@ namespace MLEM.Misc {
public override void AddListener(GameWindow window, TextInputCallback callback) { public override void AddListener(GameWindow window, TextInputCallback callback) {
this.addListener(window, (sender, args) => { this.addListener(window, (sender, args) => {
// the old versions of DesktopGL use a property here, while the
// core version uses a field. So much for "no breaking changes"
if (this.key == null) if (this.key == null)
this.key = args.GetType().GetProperty("Key"); this.key = GetMember(args, "Key");
if (this.character == null) if (this.character == null)
this.character = args.GetType().GetProperty("Character"); this.character = GetMember(args, "Character");
callback.Invoke(sender, (Keys) this.key.GetValue(args), (char) this.character.GetValue(args)); callback.Invoke(sender, GetValue<Keys>(this.key, args), GetValue<char>(this.character, args));
}); });
} }
private static MemberInfo GetMember(object args, string name) {
var ret = args.GetType().GetProperty(name);
if (ret != null)
return ret;
return args.GetType().GetField(name);
}
private static U GetValue<U>(MemberInfo member, object args) {
switch (member) {
case PropertyInfo p:
return (U) p.GetValue(args);
case FieldInfo f:
return (U) f.GetValue(args);
}
throw new ArgumentException();
}
} }
public class Mobile : TextInputWrapper { public class Mobile : TextInputWrapper {