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

Commit 234c4a5

Browse files
author
Alexis Huvier
committed
feat(multilineinput): Remove Cursor, use Scissor
1 parent b2be172 commit 234c4a5

File tree

1 file changed

+15
-59
lines changed

1 file changed

+15
-59
lines changed

SharpEngine/Widget/MultiLineInput.cs

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Numerics;
23
using System.Text;
34
using Raylib_cs;
45
using SharpEngine.Manager;
@@ -40,11 +41,7 @@ public class MultiLineInput: Widget
4041
/// <summary>
4142
/// Event trigger when value is changed
4243
/// </summary>
43-
public event EventHandler<ValueEventArgs<string>>? ValueChanged;
44-
45-
private float _timer;
46-
private bool _cursor;
47-
private string? _displayText;
44+
public event EventHandler<ValueEventArgs<string>>? ValueChanged;
4845

4946
/// <summary>
5047
/// Create Multi Line Input
@@ -61,27 +58,20 @@ public MultiLineInput(Vec2 position, string text = "", string font = "", Vec2? s
6158
Size = size ?? new Vec2(500, 200);
6259
FontSize = fontSize;
6360
Focused = false;
64-
_timer = 0.5f;
65-
66-
ValueChanged += (_, _) => UpdateDisplayText();
6761
}
6862

6963
/// <inheritdoc />
7064
public override void Update(float delta)
7165
{
7266
if (!Active)
7367
{
74-
_cursor = false;
68+
Focused = false;
7569
return;
7670
}
7771

7872
if (InputManager.IsMouseButtonPressed(MouseButton.Left))
79-
{
8073
Focused = InputManager.IsMouseInRectangle(new Rect(RealPosition - Size / 2, Size));
81-
if (!Focused && _cursor)
82-
_cursor = false;
83-
}
84-
74+
8575
if(!Focused) return;
8676

8777
#region Text Processing
@@ -126,14 +116,6 @@ public override void Update(float delta)
126116
}
127117

128118
#endregion
129-
130-
if (_timer <= 0)
131-
{
132-
_cursor = !_cursor;
133-
_timer = 0.5f;
134-
}
135-
136-
_timer -= delta;
137119
}
138120

139121
/// <inheritdoc />
@@ -154,54 +136,28 @@ public override void Draw()
154136
if(Text.Length <= 0 || Font.Length <= 0 || font == null) return;
155137

156138
var fontSize = FontSize ?? font.Value.baseSize;
157-
158-
if(_displayText == null)
159-
UpdateDisplayText();
160139

161-
var textSize = Raylib.MeasureTextEx(font.Value, _displayText.Split("\n")[^1], fontSize, 2);
140+
var textSize = Raylib.MeasureTextEx(font.Value, Text.Split("\n")[^1], fontSize, 2);
162141

163142
var finalPosition = new Vec2(position.X - Size.X / 2 + 4, position.Y - Size.Y / 2 + 4);
164143

165-
var lines = _displayText.Split("\n");
144+
var lines = Text.Split("\n");
145+
var offsetX = textSize.X - (Size.X - 20);
146+
var offsetY = textSize.Y * lines.Length - (Size.Y - 8);
147+
148+
Raylib.BeginScissorMode((int)finalPosition.X, (int)finalPosition.Y, (int)Size.X - 8, (int)Size.Y - 8);
166149
for (var i = 0; i < lines.Length; i++)
167150
{
168151
var lineSize = Raylib.MeasureTextEx(font.Value, lines[i], fontSize, 2);
169-
var pos = new Vec2(finalPosition.X,finalPosition.Y + i * lineSize.Y);
152+
var pos = new Vec2(finalPosition.X - (offsetX > 0 ? offsetX : 0),finalPosition.Y + i * lineSize.Y - (offsetY > 0 ? offsetY : 0));
170153
Raylib.DrawTextEx(font.Value, lines[i], pos, fontSize, 2, Color.Black);
171154

172155
}
156+
Raylib.EndScissorMode();
173157

174-
if (_cursor)
175-
Raylib.DrawRectangle((int)(finalPosition.X + 6 + textSize.X),
176-
(int)(finalPosition.Y + textSize.Y * (lines.Length - 1)),
158+
if (Focused)
159+
Raylib.DrawRectangle((int)(finalPosition.X + 6 + textSize.X - (offsetX > 0 ? offsetX : 0)),
160+
(int)(finalPosition.Y + textSize.Y * (lines.Length - 1) - (offsetY > 0 ? offsetY : 0)),
177161
5, (int)textSize.Y, Color.Black);
178162
}
179-
180-
/// <summary>
181-
/// Update Displayed Text
182-
/// </summary>
183-
public void UpdateDisplayText()
184-
{
185-
var fontLocal = Scene?.Window?.FontManager.GetFont(Font);
186-
if (fontLocal == null) return;
187-
188-
var fontSizeLocal = FontSize ?? fontLocal.Value.baseSize;
189-
_displayText = GetDisplayedText(fontLocal, fontSizeLocal, Size.X - 18, Size.Y, Text);
190-
}
191-
192-
private string GetDisplayedText(Font? font, int fontSize, float sizeX, float sizeY, string text)
193-
{
194-
var current = new StringBuilder();
195-
var index = text.Length - 1;
196-
var textSize = Raylib.MeasureTextEx(font.Value, current.ToString(), fontSize, 2);
197-
198-
while (index >= 0 && textSize.X <= sizeX && textSize.Y <= sizeY)
199-
{
200-
current.Insert(0, text[index]);
201-
textSize = Raylib.MeasureTextEx(font.Value, current.ToString(), fontSize, 2);
202-
index--;
203-
}
204-
205-
return index == -1 ? text : current.Remove(0, 1).ToString();
206-
}
207163
}

0 commit comments

Comments
 (0)