-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlider.cs
More file actions
59 lines (53 loc) · 1.87 KB
/
Slider.cs
File metadata and controls
59 lines (53 loc) · 1.87 KB
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
using System;
using JetBrains.Annotations;
using SharpEngine.Core.Input;
using SharpEngine.Core.Manager;
using SharpEngine.Core.Math;
using SharpEngine.Core.Utils;
using SharpEngine.Core.Utils.EventArgs;
namespace SharpEngine.Core.Widget;
/// <summary>
/// Class which represents Slider
/// </summary>
/// <param name="position">Position</param>
/// <param name="size">Size (Vec2(150, 60))</param>
/// <param name="color">Color (Color.Green)</param>
/// <param name="value">Value (0)</param>
/// <param name="horizontal">Horizontal (true)</param>
/// <param name="zLayer">ZLayer (0)</param>
[UsedImplicitly]
public class Slider(
Vec2 position,
Vec2? size = null,
Color? color = null,
float value = 0,
bool horizontal = true,
int zLayer = 0
) : ProgressBar(position, size, color, value, horizontal, zLayer)
{
/// <summary>
/// Event trigger when the value is changed
/// </summary>
[UsedImplicitly]
public event EventHandler<ValueEventArgs<float>>? ValueChanged;
/// <inheritdoc />
public override void Update(float delta)
{
base.Update(delta);
if (!RealDisplayed || !InputManager.IsMouseButtonDown(MouseButton.Left)) return;
var finalPosition = RealPosition - Size / 2;
if (!InputManager.IsMouseInRectangle(new Rect(finalPosition, Size)))
return;
var barSize = Horizontal ? Size.X : Size.Y;
var point = Horizontal ? InputManager.GetMousePosition().X - finalPosition.X : InputManager.GetMousePosition().Y - finalPosition.Y;
var temp = Value;
Value = (int)System.Math.Round(point * 100 / barSize, MidpointRounding.AwayFromZero);
if (System.Math.Abs(temp - Value) > 0.001f)
{
ValueChanged?.Invoke(
this,
new ValueEventArgs<float> { OldValue = temp, NewValue = Value }
);
}
}
}