forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditingUtilities.cs
More file actions
588 lines (509 loc) · 22.2 KB
/
Copy pathTextEditingUtilities.cs
File metadata and controls
588 lines (509 loc) · 22.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// 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 System.Collections.Generic;
using System.Globalization;
using UnityEngine.Bindings;
using UnityEngine.TextCore.Text;
namespace UnityEngine
{
enum TextEditOp
{
MoveLeft, MoveRight, MoveUp, MoveDown, MoveLineStart, MoveLineEnd, MoveTextStart, MoveTextEnd, MovePageUp,
MovePageDown, MoveGraphicalLineStart, MoveGraphicalLineEnd, MoveWordLeft, MoveWordRight, MoveParagraphForward,
MoveParagraphBackward, MoveToStartOfNextWord, MoveToEndOfPreviousWord, Delete, Backspace, DeleteWordBack,
DeleteWordForward, DeleteLineBack, Cut, Paste, ScrollStart, ScrollEnd, ScrollPageUp, ScrollPageDown
}
enum TextSelectOp
{
SelectLeft, SelectRight, SelectUp, SelectDown, SelectTextStart, SelectTextEnd, SelectPageUp, SelectPageDown,
ExpandSelectGraphicalLineStart, ExpandSelectGraphicalLineEnd, SelectGraphicalLineStart, SelectGraphicalLineEnd,
SelectWordLeft, SelectWordRight, SelectToEndOfPreviousWord, SelectToStartOfNextWord, SelectParagraphBackward,
SelectParagraphForward, Copy, SelectAll, SelectNone
}
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal class TextEditingUtilities
{
private TextSelectingUtilities m_TextSelectingUtility;
internal TextHandle textHandle;
private bool hasSelection => m_TextSelectingUtility.hasSelection;
private string SelectedText => m_TextSelectingUtility.selectedText;
private int m_iAltCursorPos => m_TextSelectingUtility.iAltCursorPos;
int m_CursorIndexSavedState = -1;
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal bool isCompositionActive;
bool m_UpdateImeWindowPosition;
internal Action OnTextChanged;
public bool multiline = false;
internal bool revealCursor
{
get => m_TextSelectingUtility.revealCursor;
set => m_TextSelectingUtility.revealCursor = value;
}
//Used by automated tests
internal int stringCursorIndex
{
get => textHandle.GetCorrespondingStringIndex(cursorIndex);
set => cursorIndex = textHandle.GetCorrespondingCodePointIndex(value);
}
private int cursorIndex
{
get => m_TextSelectingUtility.cursorIndex;
set => m_TextSelectingUtility.cursorIndex = value;
}
private int cursorIndexNoValidation {
get => m_TextSelectingUtility.cursorIndexNoValidation;
set => m_TextSelectingUtility.cursorIndexNoValidation = value;
}
private int selectIndexNoValidation {
get => m_TextSelectingUtility.selectIndexNoValidation;
set => m_TextSelectingUtility.selectIndexNoValidation = value;
}
private int stringCursorIndexNoValidation {
get => textHandle.GetCorrespondingStringIndex(m_TextSelectingUtility.cursorIndexNoValidation);
}
//Used by automated tests
internal int stringSelectIndex
{
get => textHandle.GetCorrespondingStringIndex(selectIndex);
set => selectIndex = textHandle.GetCorrespondingCodePointIndex(value);
}
private int selectIndex
{
get => m_TextSelectingUtility.selectIndex;
set => m_TextSelectingUtility.selectIndex = value;
}
string m_Text;
public string text
{
get => m_Text;
set
{
if (value == m_Text)
return;
m_Text = value ?? string.Empty;
OnTextChanged?.Invoke();
}
}
internal void SetTextWithoutNotify(string value)
{
m_Text = value;
}
public TextEditingUtilities(TextSelectingUtilities selectingUtilities, TextHandle textHandle, string text)
{
m_TextSelectingUtility = selectingUtilities;
this.textHandle = textHandle;
m_Text = text;
}
/// <summary>
/// Checks if IME is active and updates the internal states. This should be run each update.
/// </summary>
public bool UpdateImeState()
{
if (GUIUtility.compositionString.Length > 0)
{
if (!isCompositionActive)
{
m_UpdateImeWindowPosition = true;
ReplaceSelection(string.Empty);
}
isCompositionActive = true;
}
else
{
isCompositionActive = false;
}
return isCompositionActive;
}
public bool ShouldUpdateImeWindowPosition()
{
return m_UpdateImeWindowPosition;
}
public void SetImeWindowPosition(Vector2 worldPosition)
{
var cursorPos = textHandle.GetCursorPositionFromStringIndexUsingCharacterHeight(cursorIndex, true);
GUIUtility.compositionCursorPos = worldPosition + cursorPos;
}
public string GeneratePreviewString(bool richText)
{
RestoreCursorState();
var compositionString = GUIUtility.compositionString;
if (isCompositionActive)
{
return richText ? text.Insert(stringCursorIndex, $"<u>{compositionString}</u>") : text.Insert(stringCursorIndex, compositionString);
}
return text;
}
/// <summary>
/// Sets the cursor position to be at the end of the current IME item being previewing in the text.
/// When generateVisualContent is invoked this cursor position will be returned. Any changes to the text will revert the cursor back to the original position.
/// </summary>
/// <param name="cursor"></param>
public void EnableCursorPreviewState()
{
if (m_CursorIndexSavedState != -1)
return;
m_CursorIndexSavedState = m_TextSelectingUtility.cursorIndexNoValidation;
cursorIndexNoValidation = selectIndexNoValidation = m_CursorIndexSavedState + GUIUtility.compositionString.Length;
}
/// <summary>
/// Restores the cursor back to its original position after previewing the IME string cursor position with <see cref="SetCursorPreviewState(int)"/>
/// </summary>
public void RestoreCursorState()
{
if (m_CursorIndexSavedState == -1)
return;
cursorIndex = selectIndex = m_CursorIndexSavedState;
m_CursorIndexSavedState = -1;
}
[VisibleToOtherModules]
internal bool HandleKeyEvent(Event e)
{
RestoreCursorState();
InitKeyActions();
EventModifiers m = e.modifiers;
e.modifiers &= ~EventModifiers.CapsLock;
if (s_KeyEditOps.ContainsKey(e))
{
TextEditOp op = (TextEditOp)s_KeyEditOps[e];
PerformOperation(op);
e.modifiers = m;
return true;
}
e.modifiers = m;
return false;
}
void PerformOperation(TextEditOp operation)
{
revealCursor = true;
switch (operation)
{
// NOTE the TODOs below:
case TextEditOp.MoveLeft: m_TextSelectingUtility.MoveLeft(); break;
case TextEditOp.MoveRight: m_TextSelectingUtility.MoveRight(); break;
case TextEditOp.MoveUp: m_TextSelectingUtility.MoveUp(); break;
case TextEditOp.MoveDown: m_TextSelectingUtility.MoveDown(); break;
case TextEditOp.MoveLineStart: m_TextSelectingUtility.MoveLineStart(); break;
case TextEditOp.MoveLineEnd: m_TextSelectingUtility.MoveLineEnd(); break;
case TextEditOp.MoveWordRight: m_TextSelectingUtility.MoveWordRight(); break;
case TextEditOp.MoveToStartOfNextWord: m_TextSelectingUtility.MoveToStartOfNextWord(); break;
case TextEditOp.MoveToEndOfPreviousWord: m_TextSelectingUtility.MoveToEndOfPreviousWord(); break;
case TextEditOp.MoveWordLeft: m_TextSelectingUtility.MoveWordLeft(); break;
case TextEditOp.MoveTextStart: m_TextSelectingUtility.MoveTextStart(); break;
case TextEditOp.MoveTextEnd: m_TextSelectingUtility.MoveTextEnd(); break;
case TextEditOp.MoveParagraphForward: m_TextSelectingUtility.MoveParagraphForward(); break;
case TextEditOp.MoveParagraphBackward: m_TextSelectingUtility.MoveParagraphBackward(); break;
// case TextEditOp.MovePageUp: return MovePageUp (); break;
// case TextEditOp.MovePageDown: return MovePageDown (); break;
case TextEditOp.MoveGraphicalLineStart: m_TextSelectingUtility.MoveGraphicalLineStart(); break;
case TextEditOp.MoveGraphicalLineEnd: m_TextSelectingUtility.MoveGraphicalLineEnd(); break;
case TextEditOp.Delete: Delete(); break;
case TextEditOp.Backspace: Backspace(); break;
case TextEditOp.Cut: Cut(); break;
case TextEditOp.Paste: Paste(); break;
// case TextEditOp.ScrollStart: return ScrollStart (); break;
// case TextEditOp.ScrollEnd: return ScrollEnd (); break;
// case TextEditOp.ScrollPageUp: return ScrollPageUp (); break;
// case TextEditOp.ScrollPageDown: return ScrollPageDown (); break;
case TextEditOp.DeleteWordBack: DeleteWordBack(); break;
case TextEditOp.DeleteLineBack: DeleteLineBack(); break;
case TextEditOp.DeleteWordForward: DeleteWordForward(); break;
default:
Debug.Log("Unimplemented: " + operation);
break;
}
}
static void MapKey(string key, TextEditOp action)
{
s_KeyEditOps[Event.KeyboardEvent(key)] = action;
}
static Dictionary<Event, TextEditOp> s_KeyEditOps;
/// Set up a platform independent keyboard->Edit action map. This varies depending on whether we are on mac or windows.
void InitKeyActions()
{
if (s_KeyEditOps != null)
return;
s_KeyEditOps = new Dictionary<Event, TextEditOp>();
// key mappings shared by the platforms
MapKey("left", TextEditOp.MoveLeft);
MapKey("right", TextEditOp.MoveRight);
MapKey("up", TextEditOp.MoveUp);
MapKey("down", TextEditOp.MoveDown);
MapKey("delete", TextEditOp.Delete);
MapKey("backspace", TextEditOp.Backspace);
MapKey("#backspace", TextEditOp.Backspace);
// OSX is the special case for input shortcuts
if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX)
{
// Keyboard mappings for mac
// TODO MapKey ("home", TextEditOp.ScrollStart);
// TODO MapKey ("end", TextEditOp.ScrollEnd);
// TODO MapKey ("page up", TextEditOp.ScrollPageUp);
// TODO MapKey ("page down", TextEditOp.ScrollPageDown);
MapKey("^left", TextEditOp.MoveGraphicalLineStart);
MapKey("^right", TextEditOp.MoveGraphicalLineEnd);
// TODO MapKey ("^up", TextEditOp.ScrollPageUp);
// TODO MapKey ("^down", TextEditOp.ScrollPageDown);
MapKey("&left", TextEditOp.MoveWordLeft);
MapKey("&right", TextEditOp.MoveWordRight);
MapKey("&up", TextEditOp.MoveParagraphBackward);
MapKey("&down", TextEditOp.MoveParagraphForward);
MapKey("%left", TextEditOp.MoveGraphicalLineStart);
MapKey("%right", TextEditOp.MoveGraphicalLineEnd);
MapKey("%up", TextEditOp.MoveTextStart);
MapKey("%down", TextEditOp.MoveTextEnd);
MapKey("%x", TextEditOp.Cut);
MapKey("%v", TextEditOp.Paste);
// emacs-like keybindings
MapKey("^d", TextEditOp.Delete);
MapKey("^h", TextEditOp.Backspace);
MapKey("^b", TextEditOp.MoveLeft);
MapKey("^f", TextEditOp.MoveRight);
MapKey("^a", TextEditOp.MoveLineStart);
MapKey("^e", TextEditOp.MoveLineEnd);
MapKey("&delete", TextEditOp.DeleteWordForward);
MapKey("&backspace", TextEditOp.DeleteWordBack);
MapKey("%backspace", TextEditOp.DeleteLineBack);
}
else
{
// Windows/Linux keymappings
MapKey("home", TextEditOp.MoveGraphicalLineStart);
MapKey("end", TextEditOp.MoveGraphicalLineEnd);
// TODO MapKey ("page up", TextEditOp.MovePageUp);
// TODO MapKey ("page down", TextEditOp.MovePageDown);
MapKey("%left", TextEditOp.MoveWordLeft);
MapKey("%right", TextEditOp.MoveWordRight);
MapKey("%up", TextEditOp.MoveParagraphBackward);
MapKey("%down", TextEditOp.MoveParagraphForward);
MapKey("^left", TextEditOp.MoveToEndOfPreviousWord);
MapKey("^right", TextEditOp.MoveToStartOfNextWord);
MapKey("^up", TextEditOp.MoveParagraphBackward);
MapKey("^down", TextEditOp.MoveParagraphForward);
// TODO MapKey ("#page up", TextEditOp.SelectPageUp);
// TODO MapKey ("#page down", TextEditOp.SelectPageDown);
MapKey("^delete", TextEditOp.DeleteWordForward);
MapKey("^backspace", TextEditOp.DeleteWordBack);
MapKey("%backspace", TextEditOp.DeleteLineBack);
MapKey("^x", TextEditOp.Cut);
MapKey("^v", TextEditOp.Paste);
MapKey("#delete", TextEditOp.Cut);
MapKey("#insert", TextEditOp.Paste);
}
}
// Deletes previous text on the line
public bool DeleteLineBack()
{
RestoreCursorState();
if (hasSelection)
{
DeleteSelection();
return true;
}
var currentLineInfo = textHandle.GetLineInfoFromCharacterIndex(cursorIndex);
var startIndex = currentLineInfo.firstCharacterIndex;
var stringStartIndex = textHandle.GetCorrespondingStringIndex(startIndex);
if (startIndex != cursorIndex)
{
text = text.Remove(stringStartIndex, stringCursorIndex - stringStartIndex);
cursorIndex = selectIndex = startIndex;
return true;
}
return false;
}
// Deletes the previous word
public bool DeleteWordBack()
{
RestoreCursorState();
if (hasSelection)
{
DeleteSelection();
return true;
}
int prevWordEnd = m_TextSelectingUtility.FindEndOfPreviousWord(cursorIndex);
if (cursorIndex != prevWordEnd)
{
int prevWordEndString = textHandle.GetCorrespondingStringIndex(prevWordEnd);
text = text.Remove(prevWordEndString, stringCursorIndex - prevWordEndString);
selectIndex = cursorIndex = prevWordEnd;
return true;
}
return false;
}
// Deletes the following word
public bool DeleteWordForward()
{
RestoreCursorState();
if (hasSelection)
{
DeleteSelection();
return true;
}
int nextWordStart = m_TextSelectingUtility.FindStartOfNextWord(cursorIndex);
if (cursorIndex < text.Length)
{
int nextWordStartString = textHandle.GetCorrespondingStringIndex(nextWordStart);
text = text.Remove(stringCursorIndex, nextWordStartString - stringCursorIndex);
return true;
}
return false;
}
// perform a right-delete
public bool Delete()
{
RestoreCursorState();
if (hasSelection)
{
DeleteSelection();
return true;
}
else if (stringCursorIndex < text.Length)
{
var count = textHandle.textInfo.textElementInfo[cursorIndex].stringLength;
text = text.Remove(stringCursorIndex, count);
return true;
}
return false;
}
// Perform a left-delete
public bool Backspace()
{
RestoreCursorState();
if (hasSelection)
{
DeleteSelection();
return true;
}
else if (cursorIndex > 0)
{
var startIndex = m_TextSelectingUtility.PreviousCodePointIndex(cursorIndex);
var count = textHandle.textInfo.textElementInfo[cursorIndex - 1].stringLength;
text = text.Remove(stringCursorIndex - count, count);
cursorIndex = startIndex;
selectIndex = startIndex;
m_TextSelectingUtility.ClearCursorPos();
return true;
}
return false;
}
/// Delete the current selection. If there is no selection, this function does not do anything...
public bool DeleteSelection()
{
if (cursorIndex == selectIndex)
return false;
if (cursorIndex < selectIndex)
{
text = text.Substring(0, stringCursorIndex) + text.Substring(stringSelectIndex, text.Length - stringSelectIndex);
selectIndex = cursorIndex;
}
else
{
text = text.Substring(0, stringSelectIndex) + text.Substring(stringCursorIndex, text.Length - stringCursorIndex);
cursorIndex = selectIndex;
}
m_TextSelectingUtility.ClearCursorPos();
return true;
}
/// Replace the selection with /replace/. If there is no selection, /replace/ is inserted at the current cursor point.
public void ReplaceSelection(string replace)
{
RestoreCursorState();
DeleteSelection();
text = text.Insert(stringCursorIndex, replace);
var newIndex = cursorIndexNoValidation + new StringInfo(replace).LengthInTextElements;
cursorIndexNoValidation = newIndex;
selectIndexNoValidation = newIndex;
m_TextSelectingUtility.ClearCursorPos();
}
private char m_HighSurrogate;
/// Replaced the selection with /c/
public bool Insert(char c)
{
if (char.IsHighSurrogate(c))
{
m_HighSurrogate = c;
return false;
}
else if (char.IsLowSurrogate(c))
{
var lowSurrogate = c;
string combinedString = new string(new char[] { m_HighSurrogate, lowSurrogate });
ReplaceSelection(combinedString.ToString());
return true;
}
ReplaceSelection(c.ToString());
return true;
}
/// Move selection to alt cursor /position/
public void MoveSelectionToAltCursor()
{
RestoreCursorState();
if (m_iAltCursorPos == -1)
return;
int p = m_iAltCursorPos;
string tmp = SelectedText;
text = text.Insert(p, tmp);
if (p < cursorIndex)
{
cursorIndex += tmp.Length;
selectIndex += tmp.Length;
}
DeleteSelection();
selectIndex = cursorIndex = p;
m_TextSelectingUtility.ClearCursorPos();
}
public bool CanPaste()
{
return GUIUtility.systemCopyBuffer.Length != 0;
}
public bool Cut()
{
m_TextSelectingUtility.Copy();
return DeleteSelection();
}
public bool Paste()
{
RestoreCursorState();
string pasteval = GUIUtility.systemCopyBuffer;
if (pasteval != "")
{
if (!multiline)
pasteval = ReplaceNewlinesWithSpaces(pasteval);
ReplaceSelection(pasteval);
return true;
}
return false;
}
static string ReplaceNewlinesWithSpaces(string value)
{
// First get rid of Windows style new lines and then *nix so we don't leave '\r' around.
value = value.Replace("\r\n", " ");
value = value.Replace('\n', ' ');
// This probably won't happen, but just in case...
value = value.Replace('\r', ' ');
return value;
}
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal void OnBlur()
{
revealCursor = false;
m_TextSelectingUtility.SelectNone();
}
// Returns true if the TouchScreenKeyboard should be used. On Android and Chrome OS, we only want to use the
// TouchScreenKeyboard if in-place editing is not allowed (i.e. when we do not have a hardware keyboard available).
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal bool TouchScreenKeyboardShouldBeUsed()
{
RuntimePlatform platform = Application.platform;
switch (platform)
{
case RuntimePlatform.Android:
case RuntimePlatform.WebGLPlayer:
return !TouchScreenKeyboard.isInPlaceEditingAllowed;
default:
return TouchScreenKeyboard.isSupported;
}
}
}
}