mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
fixed the input handler querying input when the window is inactive
This commit is contained in:
parent
69d81da70c
commit
01b6168259
4 changed files with 19 additions and 17 deletions
|
@ -70,9 +70,9 @@ namespace MLEM.Startup {
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void LoadContent() {
|
protected override void LoadContent() {
|
||||||
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
|
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
|
||||||
this.InputHandler = new InputHandler();
|
this.InputHandler = new InputHandler(this);
|
||||||
this.Components.Add(this.InputHandler);
|
this.Components.Add(this.InputHandler);
|
||||||
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
|
this.UiSystem = new UiSystem(this, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
|
||||||
this.Components.Add(this.UiSystem);
|
this.Components.Add(this.UiSystem);
|
||||||
this.OnLoadContent?.Invoke(this);
|
this.OnLoadContent?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,7 +123,7 @@ namespace MLEM.Ui {
|
||||||
/// <param name="inputHandler">The input handler to use for controlling, or null to create a new one.</param>
|
/// <param name="inputHandler">The input handler to use for controlling, or null to create a new one.</param>
|
||||||
public UiControls(UiSystem system, InputHandler inputHandler = null) {
|
public UiControls(UiSystem system, InputHandler inputHandler = null) {
|
||||||
this.System = system;
|
this.System = system;
|
||||||
this.Input = inputHandler ?? new InputHandler();
|
this.Input = inputHandler ?? new InputHandler(system.Game);
|
||||||
this.IsInputOurs = inputHandler == null;
|
this.IsInputOurs = inputHandler == null;
|
||||||
this.Keybinds = typeof(UiControls).GetFields()
|
this.Keybinds = typeof(UiControls).GetFields()
|
||||||
.Where(f => f.FieldType == typeof(Keybind))
|
.Where(f => f.FieldType == typeof(Keybind))
|
||||||
|
|
|
@ -180,26 +180,26 @@ namespace MLEM.Ui {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new ui system with the given settings.
|
/// Creates a new ui system with the given settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="window">The game's window</param>
|
/// <param name="game">The game</param>
|
||||||
/// <param name="device">The graphics device that should be used for viewport calculations</param>
|
/// <param name="device">The graphics device that should be used for viewport calculations</param>
|
||||||
/// <param name="style">The style settings that this ui should have. Use <see cref="UntexturedStyle"/> for the default, untextured style.</param>
|
/// <param name="style">The style settings that this ui should have. Use <see cref="UntexturedStyle"/> for the default, untextured style.</param>
|
||||||
/// <param name="inputHandler">The input handler that this ui's <see cref="UiControls"/> should use. If none is supplied, a new input handler is created for this ui.</param>
|
/// <param name="inputHandler">The input handler that this ui's <see cref="UiControls"/> should use. If none is supplied, a new input handler is created for this ui.</param>
|
||||||
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) : base(null) {
|
public UiSystem(Game game, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) : base(game) {
|
||||||
this.Controls = new UiControls(this, inputHandler);
|
this.Controls = new UiControls(this, inputHandler);
|
||||||
this.GraphicsDevice = device;
|
this.GraphicsDevice = device;
|
||||||
this.Window = window;
|
this.Window = game.Window;
|
||||||
this.style = style;
|
this.style = style;
|
||||||
this.Viewport = new Rectangle(Point.Zero, window.ClientBounds.Size);
|
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size);
|
||||||
this.AutoScaleReferenceSize = this.Viewport.Size;
|
this.AutoScaleReferenceSize = this.Viewport.Size;
|
||||||
|
|
||||||
window.ClientSizeChanged += (sender, args) => {
|
this.Window.ClientSizeChanged += (sender, args) => {
|
||||||
this.Viewport = new Rectangle(Point.Zero, window.ClientBounds.Size);
|
this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size);
|
||||||
foreach (var root in this.rootElements)
|
foreach (var root in this.rootElements)
|
||||||
root.Element.ForceUpdateArea();
|
root.Element.ForceUpdateArea();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (TextInputWrapper.Current != null)
|
if (TextInputWrapper.Current != null)
|
||||||
TextInputWrapper.Current.AddListener(window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
|
TextInputWrapper.Current.AddListener(this.Window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
|
||||||
this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
|
this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
|
||||||
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.Invoke(t, e));
|
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.Invoke(t, e));
|
||||||
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));
|
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));
|
||||||
|
|
|
@ -123,11 +123,12 @@ namespace MLEM.Input {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new input handler with optional initial values.
|
/// Creates a new input handler with optional initial values.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="game">The game instance that this input handler belongs to</param>
|
||||||
/// <param name="handleKeyboard">If keyboard input should be handled</param>
|
/// <param name="handleKeyboard">If keyboard input should be handled</param>
|
||||||
/// <param name="handleMouse">If mouse input should be handled</param>
|
/// <param name="handleMouse">If mouse input should be handled</param>
|
||||||
/// <param name="handleGamepads">If gamepad input should be handled</param>
|
/// <param name="handleGamepads">If gamepad input should be handled</param>
|
||||||
/// <param name="handleTouch">If touch input should be handled</param>
|
/// <param name="handleTouch">If touch input should be handled</param>
|
||||||
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) : base(null) {
|
public InputHandler(Game game, bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) : base(game) {
|
||||||
this.HandleKeyboard = handleKeyboard;
|
this.HandleKeyboard = handleKeyboard;
|
||||||
this.HandleMouse = handleMouse;
|
this.HandleMouse = handleMouse;
|
||||||
this.HandleGamepads = handleGamepads;
|
this.HandleGamepads = handleGamepads;
|
||||||
|
@ -140,9 +141,10 @@ namespace MLEM.Input {
|
||||||
/// Call this in your <see cref="Game.Update"/> method.
|
/// Call this in your <see cref="Game.Update"/> method.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Update() {
|
public void Update() {
|
||||||
|
var active = this.Game.IsActive;
|
||||||
if (this.HandleKeyboard) {
|
if (this.HandleKeyboard) {
|
||||||
this.LastKeyboardState = this.KeyboardState;
|
this.LastKeyboardState = this.KeyboardState;
|
||||||
this.KeyboardState = Keyboard.GetState();
|
this.KeyboardState = active ? Keyboard.GetState() : default;
|
||||||
this.PressedKeys = this.KeyboardState.GetPressedKeys();
|
this.PressedKeys = this.KeyboardState.GetPressedKeys();
|
||||||
|
|
||||||
if (this.HandleKeyboardRepeats) {
|
if (this.HandleKeyboardRepeats) {
|
||||||
|
@ -179,15 +181,15 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
if (this.HandleMouse) {
|
if (this.HandleMouse) {
|
||||||
this.LastMouseState = this.MouseState;
|
this.LastMouseState = this.MouseState;
|
||||||
this.MouseState = Mouse.GetState();
|
this.MouseState = active ? Mouse.GetState() : default;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.HandleGamepads) {
|
if (this.HandleGamepads) {
|
||||||
this.ConnectedGamepads = GamePad.MaximumGamePadCount;
|
this.ConnectedGamepads = GamePad.MaximumGamePadCount;
|
||||||
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
|
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
|
||||||
this.lastGamepads[i] = this.gamepads[i];
|
this.lastGamepads[i] = this.gamepads[i];
|
||||||
this.gamepads[i] = GamePad.GetState(i);
|
this.gamepads[i] = active ? GamePad.GetState(i) : default;
|
||||||
if (this.ConnectedGamepads > i && !this.gamepads[i].IsConnected)
|
if (this.ConnectedGamepads > i && !GamePad.GetCapabilities(i).IsConnected)
|
||||||
this.ConnectedGamepads = i;
|
this.ConnectedGamepads = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,10 +226,10 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
if (this.HandleTouch) {
|
if (this.HandleTouch) {
|
||||||
this.LastTouchState = this.TouchState;
|
this.LastTouchState = this.TouchState;
|
||||||
this.TouchState = TouchPanel.GetState();
|
this.TouchState = active ? TouchPanel.GetState() : default;
|
||||||
|
|
||||||
this.gestures.Clear();
|
this.gestures.Clear();
|
||||||
while (TouchPanel.IsGestureAvailable)
|
while (active && TouchPanel.IsGestureAvailable)
|
||||||
this.gestures.Add(TouchPanel.ReadGesture());
|
this.gestures.Add(TouchPanel.ReadGesture());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue