-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUISlider.cs
75 lines (64 loc) · 2.48 KB
/
UISlider.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace ShogiClient
{
/// <summary>
/// UI Object that renders and allows the user to slide a value.
/// </summary>
public class UISlider
{
public Vector2 Position { get; set; }
public Vector2 Size { get; set; }
public float CurrentProgress { get; set; }
public float CurrentValue { get; private set; }
public float Min { get; set; } = 0;
public float Max { get; set; } = 1;
public event Action<float> OnChange;
public event Action<float> OnRelease;
private Rectangle RectOnScreen => new Rectangle((Position - Size / 2).ToPoint(), Size.ToPoint());
private bool isBeingClicked = false;
private GameResources resources;
public UISlider(GameResources resources)
{
this.resources = resources;
}
public void SetCurrentValue(float value)
{
CurrentValue = value;
CurrentProgress = (value - Min) / (Max - Min);
}
public void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState, MouseState prevMouseState)
{
if (isBeingClicked)
{
var progress = Math.Clamp((mouseState.Position.X - RectOnScreen.Left) / Size.X, 0, 1);
var value = Min + (Max - Min) * progress;
if (mouseState.LeftButton == ButtonState.Released)
{
OnRelease?.Invoke(value);
isBeingClicked = false;
}
else if (progress != CurrentProgress)
{
OnChange?.Invoke(value);
}
CurrentProgress = progress;
CurrentValue = value;
}
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.Draw(resources.UIButton, new Rectangle(RectOnScreen.Location, new Point((int)(RectOnScreen.Size.X * CurrentProgress), RectOnScreen.Size.Y)), null, Color.Black);
}
}
}