Skip to content

Commit 904c71a

Browse files
committed
Add Skia input fields to UI Layouts
1 parent 3b597dc commit 904c71a

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

Assets/Scripts/OpenTS2/UI/Skia/SkiaInputField.cs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,32 @@ namespace OpenTS2.UI.Skia
1212
{
1313
public class SkiaInputField : Selectable, IUpdateSelectedHandler
1414
{
15+
public string Text
16+
{
17+
get
18+
{
19+
return Label.Text;
20+
}
21+
22+
set
23+
{
24+
Label.Text = value;
25+
}
26+
}
1527
private bool Selected => EventSystem.current != null && EventSystem.current.currentSelectedGameObject == gameObject;
16-
[SerializeField]
17-
private RectTransform _caret;
18-
[SerializeField]
19-
private SkiaLabel _label;
28+
public RectTransform Caret;
29+
public SkiaLabel Label;
2030

2131
private int _selectedCharacter = -1;
2232
private float _caretTimer = 0f;
2333
private float CaretTime => WinUtils.GetCaretBlinkTimer();
2434
private Event _processingEvent = new Event();
35+
public Action OnTextEdited;
36+
37+
private void FireTextEdited()
38+
{
39+
OnTextEdited?.Invoke();
40+
}
2541

2642
private void Update()
2743
{
@@ -34,8 +50,8 @@ private void Update()
3450

3551
private void DeselectedUpdate()
3652
{
37-
if (_caret.gameObject.activeSelf)
38-
_caret.gameObject.SetActive(false);
53+
if (Caret.gameObject.activeSelf)
54+
Caret.gameObject.SetActive(false);
3955
}
4056

4157
private void OnGUI()
@@ -106,24 +122,25 @@ private void DoCaretAnimation()
106122

107123
if (_caretTimer > caretTime)
108124
{
109-
if (_caret.gameObject.activeSelf)
110-
_caret.gameObject.SetActive(false);
125+
if (Caret.gameObject.activeSelf)
126+
Caret.gameObject.SetActive(false);
111127
}
112128
else
113129
{
114-
if (!_caret.gameObject.activeSelf)
115-
_caret.gameObject.SetActive(true);
130+
if (!Caret.gameObject.activeSelf)
131+
Caret.gameObject.SetActive(true);
116132
}
117133
}
118134

119135
private void TypeString(string str)
120136
{
121137
_caretTimer = 0f;
122-
var strBuilder = new StringBuilder(_label.Text);
138+
var strBuilder = new StringBuilder(Label.Text);
123139
var pointInsertion = _selectedCharacter + 1;
124140
strBuilder.Insert(pointInsertion, str);
125-
_label.Text = strBuilder.ToString();
141+
Label.Text = strBuilder.ToString();
126142
_selectedCharacter += str.Length;
143+
FireTextEdited();
127144
}
128145

129146
private void PasteClipboard()
@@ -136,10 +153,11 @@ private void Backspace()
136153
{
137154
if (_selectedCharacter >= 0)
138155
{
139-
var strBuilder = new StringBuilder(_label.Text);
156+
var strBuilder = new StringBuilder(Label.Text);
140157
strBuilder.Remove(_selectedCharacter, 1);
141-
_label.Text = strBuilder.ToString();
158+
Label.Text = strBuilder.ToString();
142159
MoveCaretLeft();
160+
FireTextEdited();
143161
}
144162
}
145163

@@ -157,26 +175,32 @@ private void MoveCaretRight()
157175
{
158176
_caretTimer = 0f;
159177
_selectedCharacter++;
160-
if (_selectedCharacter >= _label.Text.Length)
161-
_selectedCharacter = _label.Text.Length - 1;
178+
if (_selectedCharacter >= Label.Text.Length)
179+
_selectedCharacter = Label.Text.Length - 1;
162180
}
163181

164182
private void UpdateCaretPosition()
165183
{
166-
_caret.sizeDelta = new Vector2(1, _label.FontSize + ((_label.LineSpacing - 1f) * _label.FontSize));
184+
Caret.sizeDelta = new Vector2(1, Label.FontSize + ((Label.LineSpacing - 1f) * Label.FontSize));
167185
if (_selectedCharacter < 0)
168186
{
169-
var firstLineY = -_label.GetLineY(0) + _label.FontSize;
170-
_caret.anchoredPosition = new Vector2(0f, firstLineY);
187+
var firstLineY = -Label.GetLineY(0) + Label.FontSize;
188+
Caret.anchoredPosition = new Vector2(0f, firstLineY);
171189
return;
172190
}
173-
var rect = _label.GetCharacterRect(_selectedCharacter);
174-
_caret.anchoredPosition = new Vector2(rect.Rect.x + rect.Rect.width, rect.Rect.y);
191+
var rect = Label.GetCharacterRect(_selectedCharacter);
192+
Caret.anchoredPosition = new Vector2(rect.Rect.x + rect.Rect.width, rect.Rect.y);
175193
}
176194

177195
private void ValidateSelection()
178196
{
179-
_selectedCharacter = Mathf.Clamp(_selectedCharacter, -1, _label.Text.Length - 1);
197+
_selectedCharacter = Mathf.Clamp(_selectedCharacter, -1, Label.Text.Length - 1);
198+
}
199+
200+
public override void OnSelect(BaseEventData eventData)
201+
{
202+
base.OnSelect(eventData);
203+
_selectedCharacter = Label.Text.Length - 1;
180204
}
181205

182206
public void OnUpdateSelected(BaseEventData eventData)
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OpenTS2.UI.Skia;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -10,32 +11,23 @@ namespace OpenTS2.UI
1011
{
1112
public class UITextEditComponent : UITextComponent
1213
{
13-
private string _originalText;
1414
public Action<string> OnTextEdited;
15-
public InputField Input => GetComponent<InputField>();
15+
public SkiaInputField Input => GetComponent<SkiaInputField>();
1616
public override string Text
1717
{
1818
get
1919
{
20-
return "";
21-
return Input.text;
20+
return Input.Text;
2221
}
2322
set
2423
{
25-
return;
26-
Input.text = value;
27-
_originalText = value;
24+
Input.Text = value;
2825
}
2926
}
3027

31-
public void CheckTextEdited()
28+
public void FireTextEdited()
3229
{
33-
if (Text != _originalText)
34-
{
35-
Debug.Log($"TEXT EDITED! ({_originalText}) => ({Text})");
36-
OnTextEdited?.Invoke(Text);
37-
_originalText = Text;
38-
}
30+
OnTextEdited?.Invoke(Input.Text);
3931
}
4032
}
4133
}

Assets/Scripts/OpenTS2/UI/UITextEditElement.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OpenTS2.UI.Skia;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -20,10 +21,21 @@ public override void ParseProperties(UIProperties properties)
2021
public override UIComponent Instantiate(Transform parent)
2122
{
2223
var component = base.Instantiate(parent) as UITextEditComponent;
24+
var inputField = component.gameObject.AddComponent<SkiaInputField>();
25+
inputField.Label = component.TextComponent;
26+
27+
var caret = new GameObject("Caret");
28+
caret.transform.parent = component.transform;
29+
var rawImage = caret.AddComponent<RawImage>();
30+
rawImage.color = ForeColor;
31+
rawImage.raycastTarget = false;
32+
33+
inputField.Caret = caret.GetComponent<RectTransform>();
34+
inputField.Caret.anchorMin = new Vector2(0f, 1f);
35+
inputField.Caret.anchorMax = new Vector2(0f, 1f);
36+
inputField.Caret.pivot = new Vector2(0.5f, 1f);
37+
inputField.OnTextEdited += component.FireTextEdited;
2338
/*
24-
var inputField = component.gameObject.AddComponent<InputField>();
25-
component.TextComponent.supportRichText = false;
26-
inputField.textComponent = component.TextComponent;
2739
if (SingleLine)
2840
inputField.lineType = InputField.LineType.MultiLineSubmit;
2941
else

0 commit comments

Comments
 (0)