mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
make the mess I made a bit nicer :^)
This commit is contained in:
parent
5a4d90043e
commit
334dea8b39
5 changed files with 56 additions and 40 deletions
|
@ -38,7 +38,7 @@ namespace MLEM.Ui.Elements {
|
|||
this.InputRule = rule ?? DefaultRule;
|
||||
this.font = font;
|
||||
|
||||
if (InputHandler.TextInputSupported) {
|
||||
if (WindowExtensions.SupportsTextInput()) {
|
||||
this.OnTextInput += (element, key, character) => {
|
||||
if (!this.IsSelected)
|
||||
return;
|
||||
|
|
|
@ -16,6 +16,7 @@ namespace MLEM.Ui {
|
|||
public class UiSystem {
|
||||
|
||||
public readonly GraphicsDevice GraphicsDevice;
|
||||
public readonly GameWindow Window;
|
||||
public Rectangle Viewport { get; private set; }
|
||||
private readonly List<RootElement> rootElements = new List<RootElement>();
|
||||
|
||||
|
@ -57,6 +58,7 @@ namespace MLEM.Ui {
|
|||
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
|
||||
this.Controls = new UiControls(this, inputHandler);
|
||||
this.GraphicsDevice = device;
|
||||
this.Window = window;
|
||||
this.style = style;
|
||||
this.Viewport = device.Viewport.Bounds;
|
||||
this.AutoScaleReferenceSize = this.Viewport.Size;
|
||||
|
@ -67,14 +69,10 @@ namespace MLEM.Ui {
|
|||
root.Element.ForceUpdateArea();
|
||||
};
|
||||
|
||||
if (InputHandler.TextInputSupported) {
|
||||
// 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) => {
|
||||
window.AddTextInputListener((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) => {
|
||||
if (!this.Controls.SelectedLastElementWithMouse && element.SelectionIndicator != null) {
|
||||
|
|
49
MLEM/Extensions/WindowExtensions.cs
Normal file
49
MLEM/Extensions/WindowExtensions.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace MLEM.Extensions {
|
||||
public static class WindowExtensions {
|
||||
|
||||
private static readonly EventInfo TextInput = typeof(GameWindow).GetEvent("TextInput");
|
||||
|
||||
public static bool AddTextInputListener(this GameWindow window, TextInputCallback callback) {
|
||||
return new TextInputReflector(callback).AddToWindow(window);
|
||||
}
|
||||
|
||||
public static bool SupportsTextInput() {
|
||||
return TextInput != null;
|
||||
}
|
||||
|
||||
public delegate void TextInputCallback(object sender, Keys key, char character);
|
||||
|
||||
private class TextInputReflector {
|
||||
|
||||
private readonly TextInputCallback callback;
|
||||
|
||||
public TextInputReflector(TextInputCallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public bool AddToWindow(GameWindow window) {
|
||||
if (TextInput == null)
|
||||
return false;
|
||||
var handler = this.GetType().GetMethod(nameof(this.OnTextInput), new[] {typeof(object), typeof(EventArgs)});
|
||||
if (handler == null)
|
||||
return false;
|
||||
TextInput.AddEventHandler(window, Delegate.CreateDelegate(TextInput.EventHandlerType, this, handler));
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -11,7 +11,6 @@ namespace MLEM.Input {
|
|||
public class InputHandler {
|
||||
|
||||
public static MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray();
|
||||
public static readonly bool TextInputSupported = typeof(GameWindow).GetEvent("TextInput") != null;
|
||||
|
||||
public KeyboardState LastKeyboardState { get; private set; }
|
||||
public KeyboardState KeyboardState { get; private set; }
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue