1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

made input handler and ui system gamecomponents

This commit is contained in:
Ellpeck 2019-12-05 17:52:25 +01:00
parent f86944ee9a
commit ff42233222
4 changed files with 12 additions and 11 deletions

View file

@ -36,14 +36,14 @@ namespace MLEM.Startup {
protected override void LoadContent() {
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
this.InputHandler = new InputHandler();
this.Components.Add(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);
}
protected sealed override void Update(GameTime gameTime) {
this.InputHandler.Update();
this.DoUpdate(gameTime);
this.UiSystem.Update(gameTime);
this.OnUpdate?.Invoke(this, gameTime);
CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
CoroutineHandler.RaiseEvent(CoroutineEvents.Update);

View file

@ -76,7 +76,6 @@ namespace MLEM.Ui {
// KEYBOARD INPUT
if (this.HandleKeyboard) {
if (this.KeyboardButtons.Any(this.Input.IsKeyPressed)) {
this.IsAutoNavMode = true;
if (this.SelectedElement?.Root != null) {
if (this.Input.IsModifierKeyDown(ModifierKey.Shift)) {
// secondary action on element using space or enter
@ -117,11 +116,9 @@ namespace MLEM.Ui {
// GAMEPAD INPUT
if (this.HandleGamepad) {
if (this.GamepadButtons.Any(b => this.IsGamepadPressed(b))) {
this.IsAutoNavMode = true;
if (this.SelectedElement?.Root != null)
this.System.OnElementPressed?.Invoke(this.SelectedElement);
} else if (this.SecondaryGamepadButtons.Any(b => this.IsGamepadPressed(b))) {
this.IsAutoNavMode = true;
if (this.SelectedElement?.Root != null)
this.System.OnElementSecondaryPressed?.Invoke(this.SelectedElement);
} else if (this.DownButtons.Any(this.IsGamepadPressed)) {

View file

@ -13,7 +13,7 @@ using MLEM.Ui.Elements;
using MLEM.Ui.Style;
namespace MLEM.Ui {
public class UiSystem {
public class UiSystem : GameComponent {
public readonly GraphicsDevice GraphicsDevice;
public readonly GameWindow Window;
@ -67,7 +67,7 @@ namespace MLEM.Ui {
public RootCallback OnRootAdded;
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.GraphicsDevice = device;
this.Window = window;
@ -95,7 +95,7 @@ namespace MLEM.Ui {
};
}
public void Update(GameTime time) {
public override void Update(GameTime time) {
this.Controls.Update();
for (var i = this.rootElements.Count - 1; i >= 0; i--) {

View file

@ -9,7 +9,7 @@ using MLEM.Extensions;
using MLEM.Misc;
namespace MLEM.Input {
public class InputHandler {
public class InputHandler : GameComponent {
public KeyboardState LastKeyboardState { 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 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.HandleMouse = handleMouse;
this.HandleGamepads = handleGamepads;
@ -113,7 +113,7 @@ namespace MLEM.Input {
if (this.HandleGamepadRepeats) {
for (var i = 0; i < this.ConnectedGamepads; i++) {
this.triggerGamepadButtonRepeat[i] = false;
if (!this.heldGamepadButtons[i].HasValue) {
foreach (var b in EnumHelper.Buttons) {
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) {
return this.lastGamepads[index];
}