added clicked event to gui button

This commit is contained in:
Dylan Wilson
2016-01-15 18:06:29 +10:00
parent a8a74f643a
commit 8b1c8a6ab0
2 changed files with 11 additions and 0 deletions
@@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
@@ -21,6 +22,8 @@ namespace MonoGame.Extended.Gui
public bool IsPressed { get; private set; }
public bool IsHovered { get; private set; }
public event EventHandler Clicked;
public override GuiControlStyle CurrentStyle
{
get
@@ -38,9 +41,13 @@ namespace MonoGame.Extended.Gui
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;
if(previouslyPressed && !IsPressed && IsHovered)
Clicked.Raise(this, EventArgs.Empty);
}
}
}
+4
View File
@@ -66,6 +66,10 @@ namespace SpaceGame
{
Position = new Vector2(400, 370)
};
button.Clicked += (sender, args) =>
{
_player.Destroy();
};
_guiManager.Layout.Controls.Add(button);