diff --git a/MLEM.Startup/MlemGame.cs b/MLEM.Startup/MlemGame.cs index 4d9c2e0..9a5d228 100644 --- a/MLEM.Startup/MlemGame.cs +++ b/MLEM.Startup/MlemGame.cs @@ -70,9 +70,9 @@ namespace MLEM.Startup { /// protected override void LoadContent() { this.SpriteBatch = new SpriteBatch(this.GraphicsDevice); - this.InputHandler = new InputHandler(); + this.InputHandler = new InputHandler(this); 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.OnLoadContent?.Invoke(this); } diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index 2549ef7..1b9adf7 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -123,7 +123,7 @@ namespace MLEM.Ui { /// The input handler to use for controlling, or null to create a new one. public UiControls(UiSystem system, InputHandler inputHandler = null) { this.System = system; - this.Input = inputHandler ?? new InputHandler(); + this.Input = inputHandler ?? new InputHandler(system.Game); this.IsInputOurs = inputHandler == null; this.Keybinds = typeof(UiControls).GetFields() .Where(f => f.FieldType == typeof(Keybind)) diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 2807d86..8ea0465 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -180,26 +180,26 @@ namespace MLEM.Ui { /// /// Creates a new ui system with the given settings. /// - /// The game's window + /// The game /// The graphics device that should be used for viewport calculations /// The style settings that this ui should have. Use for the default, untextured style. /// The input handler that this ui's should use. If none is supplied, a new input handler is created for this ui. - 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.GraphicsDevice = device; - this.Window = window; + this.Window = game.Window; 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; - window.ClientSizeChanged += (sender, args) => { - this.Viewport = new Rectangle(Point.Zero, window.ClientBounds.Size); + this.Window.ClientSizeChanged += (sender, args) => { + this.Viewport = new Rectangle(Point.Zero, this.Window.ClientBounds.Size); foreach (var root in this.rootElements) root.Element.ForceUpdateArea(); }; 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.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.Invoke(t, e)); this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e)); diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index b741989..ded3f3b 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -123,11 +123,12 @@ namespace MLEM.Input { /// /// Creates a new input handler with optional initial values. /// + /// The game instance that this input handler belongs to /// If keyboard input should be handled /// If mouse input should be handled /// If gamepad input should be handled /// If touch input should be handled - 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.HandleMouse = handleMouse; this.HandleGamepads = handleGamepads; @@ -140,9 +141,10 @@ namespace MLEM.Input { /// Call this in your method. /// public void Update() { + var active = this.Game.IsActive; if (this.HandleKeyboard) { this.LastKeyboardState = this.KeyboardState; - this.KeyboardState = Keyboard.GetState(); + this.KeyboardState = active ? Keyboard.GetState() : default; this.PressedKeys = this.KeyboardState.GetPressedKeys(); if (this.HandleKeyboardRepeats) { @@ -179,15 +181,15 @@ namespace MLEM.Input { if (this.HandleMouse) { this.LastMouseState = this.MouseState; - this.MouseState = Mouse.GetState(); + this.MouseState = active ? Mouse.GetState() : default; } if (this.HandleGamepads) { this.ConnectedGamepads = GamePad.MaximumGamePadCount; for (var i = 0; i < GamePad.MaximumGamePadCount; i++) { this.lastGamepads[i] = this.gamepads[i]; - this.gamepads[i] = GamePad.GetState(i); - if (this.ConnectedGamepads > i && !this.gamepads[i].IsConnected) + this.gamepads[i] = active ? GamePad.GetState(i) : default; + if (this.ConnectedGamepads > i && !GamePad.GetCapabilities(i).IsConnected) this.ConnectedGamepads = i; } @@ -224,10 +226,10 @@ namespace MLEM.Input { if (this.HandleTouch) { this.LastTouchState = this.TouchState; - this.TouchState = TouchPanel.GetState(); + this.TouchState = active ? TouchPanel.GetState() : default; this.gestures.Clear(); - while (TouchPanel.IsGestureAvailable) + while (active && TouchPanel.IsGestureAvailable) this.gestures.Add(TouchPanel.ReadGesture()); } }