-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelector.cs
More file actions
169 lines (148 loc) · 4.24 KB
/
Selector.cs
File metadata and controls
169 lines (148 loc) · 4.24 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Raylib_cs;
using SharpEngine.Core.Math;
using SharpEngine.Core.Utils.EventArgs;
namespace SharpEngine.Core.Widget;
/// <summary>
/// Class, which display a selector
/// </summary>
[UsedImplicitly]
public class Selector : Widget
{
/// <summary>
/// Values of Selector
/// </summary>
[UsedImplicitly]
public List<string> Values { get; }
/// <summary>
/// Selected Index of Selector
/// </summary>
[UsedImplicitly]
public int SelectedIndex { get; set; }
/// <summary>
/// Left Button of Selector
/// </summary>
[UsedImplicitly]
public Button LeftButton { get; }
/// <summary>
/// Right Button of Selector
/// </summary>
[UsedImplicitly]
public Button RightButton { get; }
/// <summary>
/// Label of Selector
/// </summary>
[UsedImplicitly]
public Label LabelValue { get; }
/// <summary>
/// Font of Selector
/// </summary>
[UsedImplicitly]
public string Font { get; set; }
/// <summary>
/// Font Size of Selector
/// </summary>
[UsedImplicitly]
public int? FontSize { get; set; }
/// <summary>
/// Event trigger when the value is changed
/// </summary>
[UsedImplicitly]
public event EventHandler<ValueEventArgs<string>>? ValueChanged;
/// <summary>
/// Selected Value
/// </summary>
[UsedImplicitly]
public string Selected => Values[SelectedIndex];
/// <summary>
/// Create Selector
/// </summary>
/// <param name="position">Position</param>
/// <param name="values">Values of Selector</param>
/// <param name="font">Font of Selector</param>
/// <param name="currentIndex">Current Index of Selector</param>
/// <param name="fontSize">Font Size of Selector</param>
/// <param name="zLayer">Z Layer</param>
public Selector(
Vec2 position,
List<string>? values = null,
string font = "",
int currentIndex = 0,
int? fontSize = null,
int zLayer = 0
)
: base(position, zLayer)
{
Values = values ?? [];
SelectedIndex = currentIndex;
Font = font;
FontSize = fontSize;
LabelValue = new Label(Vec2.Zero, Selected, font, fontSize: fontSize, zLayer: zLayer);
LeftButton = new Button(
Vec2.Zero,
"<",
font,
new Vec2(30),
fontSize: fontSize,
zLayer: zLayer
);
RightButton = new Button(
Vec2.Zero,
">",
font,
new Vec2(30),
fontSize: fontSize,
zLayer: zLayer
);
AddChild(LabelValue);
AddChild(LeftButton).Clicked += LeftButtonClick;
AddChild(RightButton).Clicked += RightButtonClick;
}
private void LeftButtonClick(object? sender, EventArgs e)
{
var old = Selected;
if (SelectedIndex == 0)
SelectedIndex = Values.Count - 1;
else
SelectedIndex--;
LabelValue.Text = Selected;
ValueChanged?.Invoke(
this,
new ValueEventArgs<string> { OldValue = old, NewValue = Selected }
);
}
private void RightButtonClick(object? sender, EventArgs e)
{
var old = Selected;
if (SelectedIndex == Values.Count - 1)
SelectedIndex = 0;
else
SelectedIndex++;
LabelValue.Text = Selected;
ValueChanged?.Invoke(
this,
new ValueEventArgs<string> { OldValue = old, NewValue = Selected }
);
}
/// <inheritdoc />
public override void Load()
{
base.Load();
var font = Scene?.Window?.FontManager.GetFont(Font);
if (font != null)
{
var fontSize = FontSize ?? font.Value.BaseSize;
var maxSizeX = 0f;
foreach (var value in Values)
{
var size = Raylib.MeasureTextEx(font.Value, value, fontSize, 2);
if (maxSizeX < size.X)
maxSizeX = size.X;
}
LeftButton.Position = new Vec2(-maxSizeX / 2 - 30, 0);
RightButton.Position = new Vec2(maxSizeX / 2 + 30, 0);
}
}
}