@@ -12,16 +12,32 @@ namespace OpenTS2.UI.Skia
12
12
{
13
13
public class SkiaInputField : Selectable , IUpdateSelectedHandler
14
14
{
15
+ public string Text
16
+ {
17
+ get
18
+ {
19
+ return Label . Text ;
20
+ }
21
+
22
+ set
23
+ {
24
+ Label . Text = value ;
25
+ }
26
+ }
15
27
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 ;
20
30
21
31
private int _selectedCharacter = - 1 ;
22
32
private float _caretTimer = 0f ;
23
33
private float CaretTime => WinUtils . GetCaretBlinkTimer ( ) ;
24
34
private Event _processingEvent = new Event ( ) ;
35
+ public Action OnTextEdited ;
36
+
37
+ private void FireTextEdited ( )
38
+ {
39
+ OnTextEdited ? . Invoke ( ) ;
40
+ }
25
41
26
42
private void Update ( )
27
43
{
@@ -34,8 +50,8 @@ private void Update()
34
50
35
51
private void DeselectedUpdate ( )
36
52
{
37
- if ( _caret . gameObject . activeSelf )
38
- _caret . gameObject . SetActive ( false ) ;
53
+ if ( Caret . gameObject . activeSelf )
54
+ Caret . gameObject . SetActive ( false ) ;
39
55
}
40
56
41
57
private void OnGUI ( )
@@ -106,24 +122,25 @@ private void DoCaretAnimation()
106
122
107
123
if ( _caretTimer > caretTime )
108
124
{
109
- if ( _caret . gameObject . activeSelf )
110
- _caret . gameObject . SetActive ( false ) ;
125
+ if ( Caret . gameObject . activeSelf )
126
+ Caret . gameObject . SetActive ( false ) ;
111
127
}
112
128
else
113
129
{
114
- if ( ! _caret . gameObject . activeSelf )
115
- _caret . gameObject . SetActive ( true ) ;
130
+ if ( ! Caret . gameObject . activeSelf )
131
+ Caret . gameObject . SetActive ( true ) ;
116
132
}
117
133
}
118
134
119
135
private void TypeString ( string str )
120
136
{
121
137
_caretTimer = 0f ;
122
- var strBuilder = new StringBuilder ( _label . Text ) ;
138
+ var strBuilder = new StringBuilder ( Label . Text ) ;
123
139
var pointInsertion = _selectedCharacter + 1 ;
124
140
strBuilder . Insert ( pointInsertion , str ) ;
125
- _label . Text = strBuilder . ToString ( ) ;
141
+ Label . Text = strBuilder . ToString ( ) ;
126
142
_selectedCharacter += str . Length ;
143
+ FireTextEdited ( ) ;
127
144
}
128
145
129
146
private void PasteClipboard ( )
@@ -136,10 +153,11 @@ private void Backspace()
136
153
{
137
154
if ( _selectedCharacter >= 0 )
138
155
{
139
- var strBuilder = new StringBuilder ( _label . Text ) ;
156
+ var strBuilder = new StringBuilder ( Label . Text ) ;
140
157
strBuilder . Remove ( _selectedCharacter , 1 ) ;
141
- _label . Text = strBuilder . ToString ( ) ;
158
+ Label . Text = strBuilder . ToString ( ) ;
142
159
MoveCaretLeft ( ) ;
160
+ FireTextEdited ( ) ;
143
161
}
144
162
}
145
163
@@ -157,26 +175,32 @@ private void MoveCaretRight()
157
175
{
158
176
_caretTimer = 0f ;
159
177
_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 ;
162
180
}
163
181
164
182
private void UpdateCaretPosition ( )
165
183
{
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 ) ) ;
167
185
if ( _selectedCharacter < 0 )
168
186
{
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 ) ;
171
189
return ;
172
190
}
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 ) ;
175
193
}
176
194
177
195
private void ValidateSelection ( )
178
196
{
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 ;
180
204
}
181
205
182
206
public void OnUpdateSelected ( BaseEventData eventData )
0 commit comments