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

make the mess I made a bit nicer :^)

This commit is contained in:
Ellpeck 2019-09-01 19:33:33 +02:00
parent 5a4d90043e
commit 334dea8b39
5 changed files with 56 additions and 40 deletions

View file

@ -38,7 +38,7 @@ namespace MLEM.Ui.Elements {
this.InputRule = rule ?? DefaultRule; this.InputRule = rule ?? DefaultRule;
this.font = font; this.font = font;
if (InputHandler.TextInputSupported) { if (WindowExtensions.SupportsTextInput()) {
this.OnTextInput += (element, key, character) => { this.OnTextInput += (element, key, character) => {
if (!this.IsSelected) if (!this.IsSelected)
return; return;

View file

@ -16,6 +16,7 @@ namespace MLEM.Ui {
public class UiSystem { public class UiSystem {
public readonly GraphicsDevice GraphicsDevice; public readonly GraphicsDevice GraphicsDevice;
public readonly GameWindow Window;
public Rectangle Viewport { get; private set; } public Rectangle Viewport { get; private set; }
private readonly List<RootElement> rootElements = new List<RootElement>(); 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) { public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
this.Controls = new UiControls(this, inputHandler); this.Controls = new UiControls(this, inputHandler);
this.GraphicsDevice = device; this.GraphicsDevice = device;
this.Window = window;
this.style = style; this.style = style;
this.Viewport = device.Viewport.Bounds; this.Viewport = device.Viewport.Bounds;
this.AutoScaleReferenceSize = this.Viewport.Size; this.AutoScaleReferenceSize = this.Viewport.Size;
@ -67,14 +69,10 @@ namespace MLEM.Ui {
root.Element.ForceUpdateArea(); root.Element.ForceUpdateArea();
}; };
if (InputHandler.TextInputSupported) { window.AddTextInputListener((sender, 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) foreach (var root in this.rootElements)
root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character)); root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character));
}).AddToWindow(window); });
}
this.OnSelectedElementDrawn = (element, time, batch, alpha, offset) => { this.OnSelectedElementDrawn = (element, time, batch, alpha, offset) => {
if (!this.Controls.SelectedLastElementWithMouse && element.SelectionIndicator != null) { if (!this.Controls.SelectedLastElementWithMouse && element.SelectionIndicator != null) {

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

View file

@ -11,7 +11,6 @@ namespace MLEM.Input {
public class InputHandler { public class InputHandler {
public static MouseButton[] MouseButtons = EnumHelper.GetValues<MouseButton>().ToArray(); 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 LastKeyboardState { get; private set; }
public KeyboardState KeyboardState { get; private set; } public KeyboardState KeyboardState { get; private set; }

View file

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