Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 7539a59

Browse files
author
Alexis Huvier
committed
feat(widgets): Add LineInput
1 parent 2b31324 commit 7539a59

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

SharpEngine/Widget/LineInput.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using Raylib_cs;
3+
using SharpEngine.Manager;
4+
using SharpEngine.Math;
5+
using SharpEngine.Utils;
6+
using SharpEngine.Utils.EventArgs;
7+
using SharpEngine.Utils.Input;
8+
using Color = SharpEngine.Utils.Color;
9+
using MouseButton = SharpEngine.Utils.Input.MouseButton;
10+
11+
namespace SharpEngine.Widget;
12+
13+
/// <summary>
14+
/// Class which represents Line Input
15+
/// </summary>
16+
public class LineInput: Widget
17+
{
18+
/// <summary>
19+
/// Current Text of Line Input
20+
/// </summary>
21+
public string Text;
22+
23+
/// <summary>
24+
/// Font of Line Input
25+
/// </summary>
26+
public string Font;
27+
28+
/// <summary>
29+
/// Size of Line Input
30+
/// </summary>
31+
public Vec2 Size;
32+
33+
/// <summary>
34+
/// If Line Input is Focused
35+
/// </summary>
36+
public bool Focused { get; private set; }
37+
38+
/// <summary>
39+
/// Font Size of Line Input (or null)
40+
/// </summary>
41+
public int? FontSize;
42+
43+
/// <summary>
44+
/// Event trigger when value is changed
45+
/// </summary>
46+
public event EventHandler<ValueEventArgs<string>>? ValueChanged;
47+
48+
private float _timer;
49+
private bool _cursor;
50+
51+
/// <summary>
52+
/// Create Line Input
53+
/// </summary>
54+
/// <param name="position">Line Edit Position</param>
55+
/// <param name="text">Line Edit Text ("")</param>
56+
/// <param name="font">Line Edit Font ("")</param>
57+
/// <param name="size">Line Edit Size (Vec2(300, 50))</param>
58+
/// <param name="fontSize">Line Edit Font Size (null)</param>
59+
public LineInput(Vec2 position, string text = "", string font = "", Vec2? size = null, int? fontSize = null) : base(position)
60+
{
61+
Text = text;
62+
Font = font;
63+
Size = size ?? new Vec2(300, 50);
64+
FontSize = fontSize;
65+
Focused = false;
66+
_timer = 0.5f;
67+
}
68+
69+
/// <inheritdoc />
70+
public override void Update(float delta)
71+
{
72+
if (!Active)
73+
{
74+
_cursor = false;
75+
return;
76+
}
77+
78+
if (InputManager.IsMouseButtonPressed(MouseButton.Left))
79+
{
80+
Focused = InputManager.IsMouseInRectangle(new Rect(Position - Size / 2, Size));
81+
if (!Focused && _cursor)
82+
_cursor = false;
83+
}
84+
85+
if(!Focused) return;
86+
87+
#region Text Processing
88+
89+
if (InputManager.IsKeyPressed(Key.Backspace) && Text.Length >= 1)
90+
Text = Text[..^1];
91+
92+
var font = Scene?.Window?.FontManager.GetFont(Font);
93+
if (font != null)
94+
{
95+
var fontSize = FontSize ?? font.Value.baseSize;
96+
foreach (var pressedChar in InputManager.PressedChars)
97+
{
98+
if ((char.IsSymbol(pressedChar) || char.IsWhiteSpace(pressedChar) ||
99+
char.IsLetterOrDigit(pressedChar) || char.IsPunctuation(pressedChar)) &&
100+
Raylib.MeasureTextEx(font.Value, Text + pressedChar, fontSize, 2).X < Size.X - 8)
101+
{
102+
var old = Text;
103+
Text += pressedChar;
104+
ValueChanged?.Invoke(this, new ValueEventArgs<string>()
105+
{
106+
OldValue = old,
107+
NewValue = Text
108+
});
109+
}
110+
}
111+
}
112+
113+
#endregion
114+
115+
if (_timer <= 0)
116+
{
117+
_cursor = !_cursor;
118+
_timer = 0.5f;
119+
}
120+
121+
_timer -= delta;
122+
}
123+
124+
/// <inheritdoc />
125+
public override void Draw()
126+
{
127+
base.Draw();
128+
129+
if(!Displayed || Scene == null ) return;
130+
131+
var position = RealPosition;
132+
133+
Raylib.DrawRectanglePro(new Rectangle(position.X, position.Y, Size.X, Size.Y), Size / 2, 0, Color.Black);
134+
Raylib.DrawRectanglePro(new Rectangle(position.X + 2, position.Y + 2, Size.X - 4, Size.Y - 4), Size / 2, 0,
135+
Color.White);
136+
137+
var font = Scene?.Window?.FontManager.GetFont(Font);
138+
139+
if(Text.Length <= 0 || Font.Length <= 0 || font == null) return;
140+
141+
var fontSize = FontSize ?? font.Value.baseSize;
142+
if (_cursor)
143+
{
144+
var textSize = Raylib.MeasureTextEx(font.Value, Text + "I", fontSize, 2);
145+
Raylib.DrawTextEx(font.Value, Text + "I", new Vec2(position.X - Size.X / 2 + 4, position.Y - textSize.Y / 2),
146+
fontSize, 2, Color.Black);
147+
}
148+
else
149+
{
150+
var textSize = Raylib.MeasureTextEx(font.Value, Text, fontSize, 2);
151+
Raylib.DrawTextEx(font.Value, Text, new Vec2(position.X - Size.X / 2 + 4, position.Y - textSize.Y / 2),
152+
fontSize, 2, Color.Black);
153+
}
154+
155+
}
156+
}

0 commit comments

Comments
 (0)