-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckbox.cs
More file actions
97 lines (86 loc) · 2.58 KB
/
Checkbox.cs
File metadata and controls
97 lines (86 loc) · 2.58 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using JetBrains.Annotations;
using SharpEngine.Core.Manager;
using SharpEngine.Core.Math;
using SharpEngine.Core.Renderer;
using SharpEngine.Core.Utils.EventArgs;
using SharpEngine.Core.Utils;
using SharpEngine.Core.Input;
namespace SharpEngine.Core.Widget;
/// <summary>
/// Class which display Checkbox
/// </summary>
/// <param name="position">Checkbox Position</param>
/// <param name="size">Checkbox Size</param>
/// <param name="isChecked">If Checkbox is Checked</param>
/// <param name="zLayer">Z Layer</param>
[UsedImplicitly]
public class Checkbox(Vec2 position, Vec2? size = null, bool isChecked = false, int zLayer = 0)
: Widget(position, zLayer)
{
/// <summary>
/// Size of Checkbox
/// </summary>
[UsedImplicitly]
public Vec2 Size { get; set; } = size ?? new Vec2(20);
/// <summary>
/// If Checkbox is Checked
/// </summary>
[UsedImplicitly]
public bool IsChecked { get; set; } = isChecked;
/// <summary>
/// Event trigger when value is changed
/// </summary>
[UsedImplicitly]
public event EventHandler<ValueEventArgs<bool>>? ValueChanged;
/// <inheritdoc />
public override void Update(float delta)
{
base.Update(delta);
if (!RealDisplayed || !Active)
return;
if (
InputManager.IsMouseButtonPressed(MouseButton.Left)
&& InputManager.IsMouseInRectangle(new Rect(RealPosition - Size / 2, Size))
)
{
IsChecked = !IsChecked;
ValueChanged?.Invoke(
this,
new ValueEventArgs<bool> { OldValue = !IsChecked, NewValue = IsChecked }
);
}
}
/// <inheritdoc />
public override void Draw()
{
base.Draw();
if (!Displayed || Size == Vec2.Zero)
return;
SERender.DrawRectangle(
new Rect(RealPosition.X, RealPosition.Y, Size.X, Size.Y),
Size / 2,
0,
Color.Black,
InstructionSource.Ui,
ZLayer
);
SERender.DrawRectangle(
new Rect(RealPosition.X, RealPosition.Y, Size.X - 4, Size.Y - 4),
(Size - 4) / 2,
0,
Color.White,
InstructionSource.Ui,
ZLayer + 0.00001f
);
if (IsChecked)
SERender.DrawRectangle(
new Rect(RealPosition.X, RealPosition.Y, Size.X - 6, Size.Y - 6),
(Size - 6) / 2,
0,
Color.Black,
InstructionSource.Ui,
ZLayer + 0.00002f
);
}
}