forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextInputFieldBase.cs
More file actions
391 lines (313 loc) · 14.2 KB
/
Copy pathTextInputFieldBase.cs
File metadata and controls
391 lines (313 loc) · 14.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using UnityEngine.Experimental.UIElements.StyleSheets;
namespace UnityEngine.Experimental.UIElements
{
public abstract class TextInputFieldBase : BaseTextElement
{
const string SelectionColorProperty = "selection-color";
const string CursorColorProperty = "cursor-color";
StyleValue<Color> m_SelectionColor;
StyleValue<Color> m_CursorColor;
public void SelectAll()
{
if (editorEngine != null)
{
editorEngine.SelectAll();
}
}
public void UpdateText(string value)
{
if (text != value)
{
// Setting the VisualElement text here cause a repaint since it dirty the layout flag.
using (InputEvent evt = InputEvent.GetPooled(text, value))
{
evt.target = this;
text = value;
UIElementsUtility.eventDispatcher.DispatchEvent(evt, panel);
}
}
}
// Password field (indirectly lossy behaviour when activated via multiline)
public virtual bool isPasswordField { get; set; }
public Color selectionColor
{
get { return m_SelectionColor.GetSpecifiedValueOrDefault(Color.clear); }
}
public Color cursorColor
{
get { return m_CursorColor.GetSpecifiedValueOrDefault(Color.clear); }
}
public int maxLength { get; set; }
internal const int kMaxLengthNone = -1;
public bool doubleClickSelectsWord { get; set; }
public bool tripleClickSelectsLine { get; set; }
bool touchScreenTextField
{
get { return TouchScreenKeyboard.isSupported; }
}
internal bool hasFocus
{
get { return elementPanel != null && elementPanel.focusController.focusedElement == this; }
}
/* internal for VisualTree tests */
internal TextEditorEventHandler editorEventHandler { get; private set; }
/* internal for VisualTree tests */
internal TextEditorEngine editorEngine { get; private set; }
public char maskChar { get; set; }
public override string text
{
set
{
base.text = value;
editorEngine.text = value;
}
}
public TextInputFieldBase(int maxLength, char maskChar)
{
this.maxLength = maxLength;
this.maskChar = maskChar;
editorEngine = new TextEditorEngine(this);
if (touchScreenTextField)
{
editorEventHandler = new TouchScreenTextEditorEventHandler(editorEngine, this);
}
else
{
// TODO: Default values should come from GUI.skin.settings
doubleClickSelectsWord = true;
tripleClickSelectsLine = true;
editorEventHandler = new KeyboardTextEditorEventHandler(editorEngine, this);
}
// Make the editor style unique across all textfields
editorEngine.style = new GUIStyle(editorEngine.style);
// TextField are focusable by default.
focusIndex = 0;
}
ContextualMenu.MenuAction.StatusFlags CutCopyActionStatus(EventBase e)
{
return (editorEngine.hasSelection && !isPasswordField) ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Disabled;
}
ContextualMenu.MenuAction.StatusFlags PasteActionStatus(EventBase e)
{
return (editorEngine.CanPaste() ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Disabled);
}
void Cut(EventBase e)
{
editorEngine.Cut();
editorEngine.text = CullString(editorEngine.text);
UpdateText(editorEngine.text);
}
void Copy(EventBase e)
{
editorEngine.Copy();
}
void Paste(EventBase e)
{
editorEngine.Paste();
editorEngine.text = CullString(editorEngine.text);
UpdateText(editorEngine.text);
}
protected override void OnStyleResolved(ICustomStyle style)
{
base.OnStyleResolved(style);
effectiveStyle.ApplyCustomProperty(SelectionColorProperty, ref m_SelectionColor); // TODO: Switch over to default style properties
effectiveStyle.ApplyCustomProperty(CursorColorProperty, ref m_CursorColor);
effectiveStyle.WriteToGUIStyle(editorEngine.style);
}
internal virtual void SyncTextEngine()
{
editorEngine.text = CullString(text);
editorEngine.SaveBackup();
editorEngine.position = layout;
editorEngine.DetectFocusChange();
}
internal string CullString(string s)
{
if (maxLength >= 0 && s != null && s.Length > maxLength)
return s.Substring(0, maxLength);
return s;
}
internal override void DoRepaint(IStylePainter painter)
{
// When this is used, we can get rid of the content.text trick and use mask char directly in the text to print
if (touchScreenTextField)
{
TouchScreenTextEditorEventHandler touchScreenEditor = editorEventHandler as TouchScreenTextEditorEventHandler;
if (touchScreenEditor != null && editorEngine.keyboardOnScreen != null)
{
UpdateText(CullString(editorEngine.keyboardOnScreen.text));
if (editorEngine.keyboardOnScreen.status != TouchScreenKeyboard.Status.Visible)
{
editorEngine.keyboardOnScreen = null;
GUI.changed = true;
}
}
// if we use system keyboard we will have normal text returned (hiding symbols is done inside os)
// so before drawing make sure we hide them ourselves
string drawText = text;
if (touchScreenEditor != null && !string.IsNullOrEmpty(touchScreenEditor.secureText))
drawText = "".PadRight(touchScreenEditor.secureText.Length, maskChar);
base.DoRepaint(painter);
text = drawText;
}
else
{
if (!hasFocus)
base.DoRepaint(painter);
else
DrawWithTextSelectionAndCursor(painter, text);
}
}
internal void DrawWithTextSelectionAndCursor(IStylePainter painter, string newText)
{
KeyboardTextEditorEventHandler keyboardTextEditor = editorEventHandler as KeyboardTextEditorEventHandler;
if (keyboardTextEditor == null)
return;
keyboardTextEditor.PreDrawCursor(newText);
int cursorIndex = editorEngine.cursorIndex;
int selectIndex = editorEngine.selectIndex;
Rect localPosition = editorEngine.localPosition;
Vector2 scrollOffset = editorEngine.scrollOffset;
IStyle style = this.style;
var textParams = painter.GetDefaultTextParameters(this);
textParams.text = " ";
textParams.wordWrapWidth = 0.0f;
textParams.wordWrap = false;
float lineHeight = painter.ComputeTextHeight(textParams);
float wordWrapWidth = style.wordWrap ? contentRect.width : 0.0f;
Input.compositionCursorPos = editorEngine.graphicalCursorPos - scrollOffset +
new Vector2(localPosition.x, localPosition.y + lineHeight);
Color drawCursorColor = m_CursorColor.GetSpecifiedValueOrDefault(Color.grey);
int selectionEndIndex = string.IsNullOrEmpty(Input.compositionString)
? selectIndex
: cursorIndex + Input.compositionString.Length;
// Draw the background as in VisualElement
painter.DrawBackground(this);
CursorPositionStylePainterParameters cursorParams;
// Draw highlighted section, if any
if (cursorIndex != selectionEndIndex)
{
var painterParams = painter.GetDefaultRectParameters(this);
painterParams.color = selectionColor;
painterParams.border.SetWidth(0.0f);
painterParams.border.SetRadius(0.0f);
int min = cursorIndex < selectionEndIndex ? cursorIndex : selectionEndIndex;
int max = cursorIndex > selectionEndIndex ? cursorIndex : selectionEndIndex;
cursorParams = painter.GetDefaultCursorPositionParameters(this);
cursorParams.text = editorEngine.text;
cursorParams.wordWrapWidth = wordWrapWidth;
cursorParams.cursorIndex = min;
Vector2 minPos = painter.GetCursorPosition(cursorParams);
cursorParams.cursorIndex = max;
Vector2 maxPos = painter.GetCursorPosition(cursorParams);
minPos -= scrollOffset;
maxPos -= scrollOffset;
if (Mathf.Approximately(minPos.y, maxPos.y))
{
painterParams.rect = new Rect(minPos.x, minPos.y, maxPos.x - minPos.x, lineHeight);
painter.DrawRect(painterParams);
}
else
{
// Draw first line
painterParams.rect = new Rect(minPos.x, minPos.y, wordWrapWidth - minPos.x, lineHeight);
painter.DrawRect(painterParams);
var inbetweenHeight = (maxPos.y - minPos.y) - lineHeight;
if (inbetweenHeight > 0f)
{
// Draw all lines in-between
painterParams.rect = new Rect(0f, minPos.y + lineHeight, wordWrapWidth, inbetweenHeight);
painter.DrawRect(painterParams);
}
// Draw last line
painterParams.rect = new Rect(0f, maxPos.y, maxPos.x, lineHeight);
painter.DrawRect(painterParams);
}
}
// Draw the border as in VisualElement
painter.DrawBorder(this);
// Draw the text with the scroll offset
if (!string.IsNullOrEmpty(editorEngine.text) && contentRect.width > 0.0f && contentRect.height > 0.0f)
{
textParams = painter.GetDefaultTextParameters(this);
textParams.rect = new Rect(contentRect.x - scrollOffset.x, contentRect.y - scrollOffset.y, contentRect.width, contentRect.height);
textParams.text = editorEngine.text;
painter.DrawText(textParams);
}
// Draw the cursor
if (cursorIndex == selectionEndIndex && (Font)style.font != null)
{
cursorParams = painter.GetDefaultCursorPositionParameters(this);
cursorParams.text = editorEngine.text;
cursorParams.wordWrapWidth = wordWrapWidth;
cursorParams.cursorIndex = cursorIndex;
Vector2 cursorPosition = painter.GetCursorPosition(cursorParams);
cursorPosition -= scrollOffset;
var painterParams = new RectStylePainterParameters
{
rect = new Rect(cursorPosition.x, cursorPosition.y, 1f, lineHeight),
color = drawCursorColor
};
painter.DrawRect(painterParams);
}
// Draw alternate cursor, if any
if (editorEngine.altCursorPosition != -1)
{
cursorParams = painter.GetDefaultCursorPositionParameters(this);
cursorParams.text = editorEngine.text.Substring(0, editorEngine.altCursorPosition);
cursorParams.wordWrapWidth = wordWrapWidth;
cursorParams.cursorIndex = editorEngine.altCursorPosition;
Vector2 altCursorPosition = painter.GetCursorPosition(cursorParams);
altCursorPosition -= scrollOffset;
var painterParams = new RectStylePainterParameters
{
rect = new Rect(altCursorPosition.x, altCursorPosition.y, 1f, lineHeight),
color = drawCursorColor
};
painter.DrawRect(painterParams);
}
keyboardTextEditor.PostDrawCursor();
}
internal virtual bool AcceptCharacter(char c)
{
return true;
}
protected virtual void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
if (evt.target is TextInputFieldBase)
{
evt.menu.AppendAction("Cut", Cut, CutCopyActionStatus);
evt.menu.AppendAction("Copy", Copy, CutCopyActionStatus);
evt.menu.AppendAction("Paste", Paste, PasteActionStatus);
}
}
protected internal override void ExecuteDefaultActionAtTarget(EventBase evt)
{
base.ExecuteDefaultActionAtTarget(evt);
if (elementPanel != null && elementPanel.contextualMenuManager != null)
{
elementPanel.contextualMenuManager.DisplayMenuIfEventMatches(evt, this);
}
if (evt.GetEventTypeId() == ContextualMenuPopulateEvent.TypeId())
{
ContextualMenuPopulateEvent e = evt as ContextualMenuPopulateEvent;
int count = e.menu.MenuItems().Count;
BuildContextualMenu(e);
if (count > 0 && e.menu.MenuItems().Count > count)
{
e.menu.InsertSeparator(count);
}
}
editorEventHandler.ExecuteDefaultActionAtTarget(evt);
}
protected internal override void ExecuteDefaultAction(EventBase evt)
{
base.ExecuteDefaultAction(evt);
editorEventHandler.ExecuteDefaultAction(evt);
}
}
}