mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 03:56:31 +00:00
wiring up the input listeners to the gui manager
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended.InputListeners;
|
||||
|
||||
namespace MonoGame.Extended.Gui
|
||||
{
|
||||
@@ -22,7 +23,7 @@ namespace MonoGame.Extended.Gui
|
||||
public bool IsPressed { get; private set; }
|
||||
public bool IsHovered { get; private set; }
|
||||
|
||||
public event EventHandler Clicked;
|
||||
public event EventHandler<MouseEventArgs> Clicked;
|
||||
|
||||
public override GuiControlStyle CurrentStyle
|
||||
{
|
||||
@@ -38,16 +39,29 @@ namespace MonoGame.Extended.Gui
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnMouseDown(object sender, MouseEventArgs args)
|
||||
{
|
||||
IsPressed = true;
|
||||
base.OnMouseDown(sender, args);
|
||||
}
|
||||
|
||||
public override void OnMouseUp(object sender, MouseEventArgs args)
|
||||
{
|
||||
if(IsPressed)
|
||||
Clicked.Raise(this, args);
|
||||
|
||||
IsPressed = false;
|
||||
base.OnMouseUp(sender, args);
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
var mouseState = Mouse.GetState();
|
||||
var previouslyPressed = IsPressed;
|
||||
|
||||
IsHovered = Shape.Contains(mouseState.X, mouseState.Y);
|
||||
IsPressed = IsHovered && mouseState.LeftButton == ButtonState.Pressed;
|
||||
//IsHovered = Contains(mouseState.X, mouseState.Y);
|
||||
//IsPressed = IsHovered && mouseState.LeftButton == ButtonState.Pressed;
|
||||
|
||||
if(previouslyPressed && !IsPressed && IsHovered)
|
||||
Clicked.Raise(this, EventArgs.Empty);
|
||||
//if(previouslyPressed && !IsPressed && IsHovered)
|
||||
// Clicked.Raise(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using MonoGame.Extended.InputListeners;
|
||||
using MonoGame.Extended.Shapes;
|
||||
|
||||
namespace MonoGame.Extended.Gui
|
||||
@@ -6,6 +7,28 @@ namespace MonoGame.Extended.Gui
|
||||
public abstract class GuiControl : IUpdate
|
||||
{
|
||||
public abstract IShapeF Shape { get; set; }
|
||||
|
||||
public virtual void Update(GameTime gameTime) { }
|
||||
|
||||
public bool Contains(Vector2 point)
|
||||
{
|
||||
return Shape.Contains(point);
|
||||
}
|
||||
|
||||
public bool Contains(Point point)
|
||||
{
|
||||
return Shape.Contains(point.ToVector2());
|
||||
}
|
||||
|
||||
public bool Contains(int x, int y)
|
||||
{
|
||||
return Shape.Contains(x, y);
|
||||
}
|
||||
|
||||
public virtual void OnMouseEnter(object sender, MouseEventArgs args) { }
|
||||
public virtual void OnMouseLeave(object sender, MouseEventArgs args) { }
|
||||
public virtual void OnMouseMoved(object sender, MouseEventArgs args) { }
|
||||
public virtual void OnMouseDown(object sender, MouseEventArgs args) { }
|
||||
public virtual void OnMouseUp(object sender, MouseEventArgs args) { }
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.ViewportAdapters;
|
||||
|
||||
namespace MonoGame.Extended.Gui
|
||||
{
|
||||
public class GuiLayout : IDraw, IUpdate
|
||||
{
|
||||
private readonly ViewportAdapter _viewportAdapter;
|
||||
|
||||
public GuiLayout(ViewportAdapter viewportAdapter, GraphicsDevice graphicsDevice)
|
||||
{
|
||||
_viewportAdapter = viewportAdapter;
|
||||
_spriteBatch = new SpriteBatch(graphicsDevice);
|
||||
|
||||
Controls = new List<GuiControl>();
|
||||
}
|
||||
|
||||
private readonly SpriteBatch _spriteBatch;
|
||||
|
||||
public List<GuiControl> Controls { get; private set; }
|
||||
|
||||
public void Draw(GameTime gameTime)
|
||||
{
|
||||
_spriteBatch.Begin(transformMatrix: _viewportAdapter.GetScaleMatrix());
|
||||
|
||||
foreach (var control in Controls.OfType<GuiDrawableControl>())
|
||||
control.Draw(_spriteBatch);
|
||||
|
||||
_spriteBatch.End();
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
foreach (var control in Controls)
|
||||
control.Update(gameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,60 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.InputListeners;
|
||||
using MonoGame.Extended.ViewportAdapters;
|
||||
|
||||
namespace MonoGame.Extended.Gui
|
||||
{
|
||||
public class GuiManager : IDraw, IUpdate
|
||||
{
|
||||
private readonly ViewportAdapter _viewportAdapter;
|
||||
private readonly InputListenerManager _inputManager;
|
||||
private readonly SpriteBatch _spriteBatch;
|
||||
|
||||
public GuiManager(ViewportAdapter viewportAdapter, GraphicsDevice graphicsDevice)
|
||||
{
|
||||
_layout = new GuiLayout(viewportAdapter, graphicsDevice);
|
||||
_viewportAdapter = viewportAdapter;
|
||||
_inputManager = new InputListenerManager(viewportAdapter);
|
||||
_spriteBatch = new SpriteBatch(graphicsDevice);
|
||||
|
||||
Controls = new List<GuiControl>();
|
||||
|
||||
var mouseListener = _inputManager.AddListener<MouseListener>();
|
||||
mouseListener.MouseMoved += (sender, args) => DelegateMouseEvent(args, c => c.OnMouseMoved(this, args));
|
||||
mouseListener.MouseDown += (sender, args) => DelegateMouseEvent(args, c => c.OnMouseDown(this, args));
|
||||
mouseListener.MouseUp += (sender, args) => DelegateMouseEvent(args, c => c.OnMouseUp(this, args));
|
||||
}
|
||||
|
||||
private readonly GuiLayout _layout;
|
||||
public GuiLayout Layout { get { return _layout; } }
|
||||
public List<GuiControl> Controls { get; private set; }
|
||||
|
||||
private void DelegateMouseEvent(MouseEventArgs args, Action<GuiControl> action)
|
||||
{
|
||||
foreach (var control in Controls
|
||||
.Where(control => control.Contains(args.Position)))
|
||||
{
|
||||
action(control);
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(GameTime gameTime)
|
||||
{
|
||||
Layout.Draw(gameTime);
|
||||
_spriteBatch.Begin(transformMatrix: _viewportAdapter.GetScaleMatrix());
|
||||
|
||||
foreach (var control in Controls.OfType<GuiDrawableControl>())
|
||||
control.Draw(_spriteBatch);
|
||||
|
||||
_spriteBatch.End();
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
Layout.Update(gameTime);
|
||||
_inputManager.Update(gameTime);
|
||||
|
||||
foreach (var control in Controls)
|
||||
control.Update(gameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using MonoGame.Extended.ViewportAdapters;
|
||||
|
||||
namespace MonoGame.Extended.InputListeners
|
||||
{
|
||||
@@ -8,6 +9,8 @@ namespace MonoGame.Extended.InputListeners
|
||||
{
|
||||
}
|
||||
|
||||
internal ViewportAdapter ViewportAdapter { get; set; }
|
||||
|
||||
internal abstract void Update(GameTime gameTime);
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,24 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.Xna.Framework;
|
||||
using MonoGame.Extended.ViewportAdapters;
|
||||
|
||||
namespace MonoGame.Extended.InputListeners
|
||||
{
|
||||
public class InputListenerManager : IUpdate
|
||||
{
|
||||
public InputListenerManager()
|
||||
: this(null)
|
||||
{
|
||||
}
|
||||
|
||||
public InputListenerManager(ViewportAdapter viewportAdapter)
|
||||
{
|
||||
_viewportAdapter = viewportAdapter;
|
||||
_listeners = new List<InputListener>();
|
||||
}
|
||||
|
||||
private readonly ViewportAdapter _viewportAdapter;
|
||||
private readonly List<InputListener> _listeners;
|
||||
|
||||
public IEnumerable<InputListener> Listeners
|
||||
@@ -40,6 +48,7 @@ namespace MonoGame.Extended.InputListeners
|
||||
throw new InvalidOperationException(string.Format("No parameterless constructor defined for type {0}", typeof(T).Name));
|
||||
|
||||
var listener = (T)constructors[0].Invoke(new object[0]);
|
||||
listener.ViewportAdapter = _viewportAdapter;
|
||||
_listeners.Add(listener);
|
||||
return listener;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended.ViewportAdapters;
|
||||
|
||||
namespace MonoGame.Extended.InputListeners
|
||||
{
|
||||
public class MouseEventArgs : EventArgs
|
||||
{
|
||||
internal MouseEventArgs(TimeSpan time, MouseState previousState, MouseState currentState,
|
||||
internal MouseEventArgs(ViewportAdapter viewportAdapter, TimeSpan time, MouseState previousState, MouseState currentState,
|
||||
MouseButton button = MouseButton.None)
|
||||
{
|
||||
PreviousState = previousState;
|
||||
CurrentState = currentState;
|
||||
Position = new Point(currentState.X, currentState.Y);
|
||||
Position = viewportAdapter != null
|
||||
? viewportAdapter.PointToScreen(currentState.X, currentState.Y)
|
||||
: new Point(currentState.X, currentState.Y);
|
||||
Button = button;
|
||||
ScrollWheelValue = currentState.ScrollWheelValue;
|
||||
ScrollWheelDelta = currentState.ScrollWheelValue - previousState.ScrollWheelValue;
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace MonoGame.Extended.InputListeners
|
||||
if (getButtonState(_currentState) == ButtonState.Pressed &&
|
||||
getButtonState(_previousState) == ButtonState.Released)
|
||||
{
|
||||
var args = new MouseEventArgs(_gameTime.TotalGameTime, _previousState, _currentState, button);
|
||||
var args = new MouseEventArgs(ViewportAdapter, _gameTime.TotalGameTime, _previousState, _currentState, button);
|
||||
|
||||
MouseDown.Raise(this, args);
|
||||
_mouseDownArgs = args;
|
||||
@@ -66,7 +66,7 @@ namespace MonoGame.Extended.InputListeners
|
||||
if (getButtonState(_currentState) == ButtonState.Released &&
|
||||
getButtonState(_previousState) == ButtonState.Pressed)
|
||||
{
|
||||
var args = new MouseEventArgs(_gameTime.TotalGameTime, _previousState, _currentState, button);
|
||||
var args = new MouseEventArgs(ViewportAdapter, _gameTime.TotalGameTime, _previousState, _currentState, button);
|
||||
|
||||
if (_mouseDownArgs.Button == args.Button)
|
||||
{
|
||||
@@ -110,11 +110,11 @@ namespace MonoGame.Extended.InputListeners
|
||||
|
||||
// Check for any sort of mouse movement.
|
||||
if (_previousState.X != _currentState.X || _previousState.Y != _currentState.Y)
|
||||
MouseMoved.Raise(this, new MouseEventArgs(gameTime.TotalGameTime, _previousState, _currentState));
|
||||
MouseMoved.Raise(this, new MouseEventArgs(ViewportAdapter, gameTime.TotalGameTime, _previousState, _currentState));
|
||||
|
||||
// Handle mouse wheel events.
|
||||
if (_previousState.ScrollWheelValue != _currentState.ScrollWheelValue)
|
||||
MouseWheelMoved.Raise(this, new MouseEventArgs(gameTime.TotalGameTime, _previousState, _currentState));
|
||||
MouseWheelMoved.Raise(this, new MouseEventArgs(ViewportAdapter, gameTime.TotalGameTime, _previousState, _currentState));
|
||||
|
||||
_previousState = _currentState;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,6 @@
|
||||
<Compile Include="Gui\GuiControl.cs" />
|
||||
<Compile Include="Gui\GuiControlStyle.cs" />
|
||||
<Compile Include="Gui\GuiDrawableControl.cs" />
|
||||
<Compile Include="Gui\GuiLayout.cs" />
|
||||
<Compile Include="Gui\GuiManager.cs" />
|
||||
<Compile Include="Gui\GuiSpriteStyle.cs" />
|
||||
<Compile Include="IDraw.cs" />
|
||||
|
||||
@@ -68,9 +68,10 @@ namespace SpaceGame
|
||||
};
|
||||
button.Clicked += (sender, args) =>
|
||||
{
|
||||
Explode(_player.Position, 3);
|
||||
_player.Destroy();
|
||||
};
|
||||
_guiManager.Layout.Controls.Add(button);
|
||||
_guiManager.Controls.Add(button);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user