-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLineInput.cs
More file actions
208 lines (182 loc) · 5.98 KB
/
LineInput.cs
File metadata and controls
208 lines (182 loc) · 5.98 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using JetBrains.Annotations;
using Raylib_cs;
using SharpEngine.Core.Input;
using SharpEngine.Core.Manager;
using SharpEngine.Core.Math;
using SharpEngine.Core.Renderer;
using SharpEngine.Core.Utils.EventArgs;
using Color = SharpEngine.Core.Utils.Color;
using MouseButton = SharpEngine.Core.Input.MouseButton;
namespace SharpEngine.Core.Widget;
/// <summary>
/// Class which represents Line Input
/// </summary>
/// <param name="position">Line Edit Position</param>
/// <param name="text">Line Edit Text ("")</param>
/// <param name="font">Line Edit Font ("")</param>
/// <param name="size">Line Edit Size (Vec2(300, 50))</param>
/// <param name="fontSize">Line Edit Font Size (null)</param>
/// <param name="secret">If Line Edit is Secret (false)</param>
/// <param name="zLayer">Z Layer</param>
public class LineInput(
Vec2 position,
string text = "",
string font = "",
Vec2? size = null,
int? fontSize = null,
bool secret = false,
int zLayer = 0
) : Widget(position, zLayer)
{
/// <summary>
/// Current Text of Line Input
/// </summary>
[UsedImplicitly]
public string Text { get; set; } = text;
/// <summary>
/// Font of Line Input
/// </summary>
[UsedImplicitly]
public string Font { get; set; } = font;
/// <summary>
/// Size of Line Input
/// </summary>
[UsedImplicitly]
public Vec2 Size { get; set; } = size ?? new Vec2(300, 50);
/// <summary>
/// If Line Input is Focused
/// </summary>
[UsedImplicitly]
public bool Focused { get; private set; }
/// <summary>
/// Font Size of Line Input (or null)
/// </summary>
[UsedImplicitly]
public int? FontSize { get; set; } = fontSize;
/// <summary>
/// If Line Input is Secret
/// </summary>
[UsedImplicitly]
public bool Secret { get; set; } = secret;
/// <summary>
/// Event trigger when the value is changed
/// </summary>
[UsedImplicitly]
public event EventHandler<ValueEventArgs<string>>? ValueChanged;
/// <summary>
/// Invoke ValueChanged Event
/// </summary>
/// <param name="args">Event Args</param>
protected void InvokeValueChanged(ValueEventArgs<string> args) => ValueChanged?.Invoke(this, args);
/// <inheritdoc />
public override void Update(float delta)
{
if (!RealDisplayed || !Active)
{
Focused = false;
return;
}
if (InputManager.IsMouseButtonPressed(MouseButton.Left))
Focused = InputManager.IsMouseInRectangle(new Rect(RealPosition - Size / 2, Size));
if (!Focused)
return;
#region Text Processing
if (InputManager.IsKeyPressed(Key.Backspace) && Text.Length >= 1)
{
var old = Text;
Text = Text[..^1];
ValueChanged?.Invoke(
this,
new ValueEventArgs<string> { OldValue = old, NewValue = Text }
);
}
var finalFont = Scene?.Window?.FontManager.GetFont(Font);
if (finalFont != null)
{
foreach (var pressedChar in InputManager.GetPressedChars())
{
if (
char.IsSymbol(pressedChar)
|| char.IsWhiteSpace(pressedChar)
|| char.IsLetterOrDigit(pressedChar)
|| char.IsPunctuation(pressedChar)
)
{
Text += pressedChar;
ValueChanged?.Invoke(
this,
new ValueEventArgs<string> { OldValue = Text, NewValue = Text[..^1] }
);
}
}
}
#endregion
}
/// <inheritdoc />
public override void Draw()
{
base.Draw();
if (!Displayed || Scene == null)
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 + 2, RealPosition.Y + 2, Size.X - 4, Size.Y - 4),
Size / 2,
0,
Color.White,
InstructionSource.Ui,
ZLayer + 0.00001f
);
var finalFont = Scene?.Window?.FontManager.GetFont(Font);
if (Font.Length <= 0 || finalFont == null)
return;
var text = Secret ? new string('*', Text.Length) : Text;
var finalFontSize = FontSize ?? finalFont.Value.BaseSize;
var textSize = Raylib.MeasureTextEx(finalFont.Value, text, finalFontSize, 2);
var offset = textSize.X - (Size.X - 20);
if (text.Length > 0)
{
var finalPosition = new Vec2(RealPosition.X - Size.X / 2 + 4, RealPosition.Y - textSize.Y / 2);
SERender.ScissorMode(
finalPosition.X,
finalPosition.Y,
Size.X - 8,
textSize.Y,
InstructionSource.Ui,
ZLayer + 0.00002f,
() =>
{
SERender.DrawText(
finalFont.Value,
text,
new Vec2(finalPosition.X - (offset > 0 ? offset : 0), finalPosition.Y),
finalFontSize,
2,
Color.Black,
InstructionSource.Ui,
0
);
}
);
}
if (!Focused) return;
var potentielSize = Raylib.MeasureTextEx(finalFont.Value, "A", finalFontSize, 2);
SERender.DrawRectangle(
(RealPosition.X - Size.X / 2 + 10 + textSize.X - (offset > 0 ? offset : 0)),
(RealPosition.Y - potentielSize.Y / 2 + 2),
5,
potentielSize.Y - 4,
Color.Black,
InstructionSource.Ui,
ZLayer + 0.00003f
);
}
}