mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
made input handler and ui system gamecomponents
This commit is contained in:
parent
f86944ee9a
commit
ff42233222
4 changed files with 12 additions and 11 deletions
|
@ -36,14 +36,14 @@ namespace MLEM.Startup {
|
||||||
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.Components.Add(this.InputHandler);
|
||||||
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
|
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch), this.InputHandler);
|
||||||
|
this.Components.Add(this.UiSystem);
|
||||||
this.OnLoadContent?.Invoke(this);
|
this.OnLoadContent?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected sealed override void Update(GameTime gameTime) {
|
protected sealed override void Update(GameTime gameTime) {
|
||||||
this.InputHandler.Update();
|
|
||||||
this.DoUpdate(gameTime);
|
this.DoUpdate(gameTime);
|
||||||
this.UiSystem.Update(gameTime);
|
|
||||||
this.OnUpdate?.Invoke(this, gameTime);
|
this.OnUpdate?.Invoke(this, gameTime);
|
||||||
CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
|
CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
|
||||||
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
|
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
|
||||||
|
|
|
@ -76,7 +76,6 @@ namespace MLEM.Ui {
|
||||||
// KEYBOARD INPUT
|
// KEYBOARD INPUT
|
||||||
if (this.HandleKeyboard) {
|
if (this.HandleKeyboard) {
|
||||||
if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) {
|
if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) {
|
||||||
this.IsAutoNavMode = true;
|
|
||||||
if (this.SelectedElement?.Root != null) {
|
if (this.SelectedElement?.Root != null) {
|
||||||
if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
|
if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
|
||||||
// secondary action on element using space or enter
|
// secondary action on element using space or enter
|
||||||
|
@ -117,11 +116,9 @@ namespace MLEM.Ui {
|
||||||
// GAMEPAD INPUT
|
// GAMEPAD INPUT
|
||||||
if (this.HandleGamepad) {
|
if (this.HandleGamepad) {
|
||||||
if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) {
|
if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) {
|
||||||
this.IsAutoNavMode = true;
|
|
||||||
if (this.SelectedElement?.Root != null)
|
if (this.SelectedElement?.Root != null)
|
||||||
this.System.OnElementPressed?.Invoke(this.SelectedElement);
|
this.System.OnElementPressed?.Invoke(this.SelectedElement);
|
||||||
} else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) {
|
} else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) {
|
||||||
this.IsAutoNavMode = true;
|
|
||||||
if (this.SelectedElement?.Root != null)
|
if (this.SelectedElement?.Root != null)
|
||||||
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
|
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
|
||||||
} else if (this.DownButtons.Any(this.IsGamepadPressed)) {
|
} else if (this.DownButtons.Any(this.IsGamepadPressed)) {
|
||||||
|
|
|
@ -13,7 +13,7 @@ using MLEM.Ui.Elements;
|
||||||
using MLEM.Ui.Style;
|
using MLEM.Ui.Style;
|
||||||
|
|
||||||
namespace MLEM.Ui {
|
namespace MLEM.Ui {
|
||||||
public class UiSystem {
|
public class UiSystem : GameComponent {
|
||||||
|
|
||||||
public readonly GraphicsDevice GraphicsDevice;
|
public readonly GraphicsDevice GraphicsDevice;
|
||||||
public readonly GameWindow Window;
|
public readonly GameWindow Window;
|
||||||
|
@ -67,7 +67,7 @@ namespace MLEM.Ui {
|
||||||
public RootCallback OnRootAdded;
|
public RootCallback OnRootAdded;
|
||||||
public RootCallback OnRootRemoved;
|
public RootCallback OnRootRemoved;
|
||||||
|
|
||||||
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) {
|
public UiSystem(GameWindow window, GraphicsDevice device, UiStyle style, InputHandler inputHandler = null) : base(null) {
|
||||||
this.Controls = new UiControls(this, inputHandler);
|
this.Controls = new UiControls(this, inputHandler);
|
||||||
this.GraphicsDevice = device;
|
this.GraphicsDevice = device;
|
||||||
this.Window = window;
|
this.Window = window;
|
||||||
|
@ -95,7 +95,7 @@ namespace MLEM.Ui {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(GameTime time) {
|
public override void Update(GameTime time) {
|
||||||
this.Controls.Update();
|
this.Controls.Update();
|
||||||
|
|
||||||
for (var i = this.rootElements.Count - 1; i >= 0; i--) {
|
for (var i = this.rootElements.Count - 1; i >= 0; i--) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ using MLEM.Extensions;
|
||||||
using MLEM.Misc;
|
using MLEM.Misc;
|
||||||
|
|
||||||
namespace MLEM.Input {
|
namespace MLEM.Input {
|
||||||
public class InputHandler {
|
public class InputHandler : GameComponent {
|
||||||
|
|
||||||
public KeyboardState LastKeyboardState { get; private set; }
|
public KeyboardState LastKeyboardState { get; private set; }
|
||||||
public KeyboardState KeyboardState { get; private set; }
|
public KeyboardState KeyboardState { get; private set; }
|
||||||
|
@ -50,7 +50,7 @@ namespace MLEM.Input {
|
||||||
private readonly bool[] triggerGamepadButtonRepeat = new bool[GamePad.MaximumGamePadCount];
|
private readonly bool[] triggerGamepadButtonRepeat = new bool[GamePad.MaximumGamePadCount];
|
||||||
private readonly Buttons?[] heldGamepadButtons = new Buttons?[GamePad.MaximumGamePadCount];
|
private readonly Buttons?[] heldGamepadButtons = new Buttons?[GamePad.MaximumGamePadCount];
|
||||||
|
|
||||||
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) {
|
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) : base(null) {
|
||||||
this.HandleKeyboard = handleKeyboard;
|
this.HandleKeyboard = handleKeyboard;
|
||||||
this.HandleMouse = handleMouse;
|
this.HandleMouse = handleMouse;
|
||||||
this.HandleGamepads = handleGamepads;
|
this.HandleGamepads = handleGamepads;
|
||||||
|
@ -113,7 +113,7 @@ namespace MLEM.Input {
|
||||||
if (this.HandleGamepadRepeats) {
|
if (this.HandleGamepadRepeats) {
|
||||||
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
this.triggerGamepadButtonRepeat[i] = false;
|
this.triggerGamepadButtonRepeat[i] = false;
|
||||||
|
|
||||||
if (!this.heldGamepadButtons[i].HasValue) {
|
if (!this.heldGamepadButtons[i].HasValue) {
|
||||||
foreach (var b in EnumHelper.Buttons) {
|
foreach (var b in EnumHelper.Buttons) {
|
||||||
if (this.IsGamepadButtonDown(b, i)) {
|
if (this.IsGamepadButtonDown(b, i)) {
|
||||||
|
@ -151,6 +151,10 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void Update(GameTime gameTime) {
|
||||||
|
this.Update();
|
||||||
|
}
|
||||||
|
|
||||||
public GamePadState GetLastGamepadState(int index) {
|
public GamePadState GetLastGamepadState(int index) {
|
||||||
return this.lastGamepads[index];
|
return this.lastGamepads[index];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue