-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIButton.cs
60 lines (51 loc) · 1.84 KB
/
UIButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ShogiClient
{
/// <summary>
/// UI Object that renders a button the user can click.
/// </summary>
public class UIButton
{
public Vector2 Position { get; set; }
public Vector2 Size { get; set; }
public string Text { get; set; }
public event Action OnClick;
private bool isBeingClicked = false;
private Rectangle RectOnScreen => new Rectangle((Position - Size / 2).ToPoint(), Size.ToPoint());
private GameResources resources;
public UIButton(GameResources resources)
{
this.resources = resources;
}
public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState, MouseState prevMouseState)
{
if (isBeingClicked)
{
if (!RectOnScreen.Contains(mouseState.Position))
{
isBeingClicked = false;
}
if (mouseState.LeftButton == ButtonState.Released)
{
OnClick.Invoke();
isBeingClicked = false;
}
}
if (mouseState.LeftButton == ButtonState.Pressed && RectOnScreen.Contains(mouseState.Position))
{
if (prevMouseState.LeftButton == ButtonState.Released)
{
isBeingClicked = true;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(resources.UIButton, RectOnScreen, null, Color.White);
spriteBatch.DrawString(resources.PieceFont, Text, RectOnScreen.Center.ToVector2() - resources.PieceFont.MeasureString(Text) / 2, Color.White);
}
}
}