forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.cs
More file actions
625 lines (496 loc) · 23.4 KB
/
Copy pathTextEditor.cs
File metadata and controls
625 lines (496 loc) · 23.4 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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// 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.Bindings;
using UnityEngine.Scripting;
using System.Collections.Generic;
using UnityEngine.TextCore.Text;
namespace UnityEngine
{
public class TextEditor
{
private readonly GUIContent m_Content = new GUIContent();
private TextSelectingUtilities m_TextSelecting;
//Used in automated tests
internal TextEditingUtilities m_TextEditing;
//Used in automated tests
internal IMGUITextHandle m_TextHandle;
public TouchScreenKeyboard keyboardOnScreen = null;
public int controlID = 0;
public GUIStyle style;
[Obsolete("'multiline' has been deprecated. Changes to this member will not be observed. Use 'isMultiline' instead.", true)]
public bool multiline;
public bool isMultiline
{ get => m_TextEditing.multiline; set => m_TextEditing.multiline = value; }
[Obsolete("'hasHorizontalCursorPos' has been deprecated. Changes to this member will not be observed. Use 'hasHorizontalCursor' instead.", true)]
public bool hasHorizontalCursorPos = false;
public bool hasHorizontalCursor
{
get => m_TextSelecting.hasHorizontalCursorPos;
set => m_TextSelecting.hasHorizontalCursorPos = value;
}
public bool isPasswordField = false;
// The text field can have a scroll offset in order to display its contents
public Vector2 scrollOffset;
[Obsolete("'revealCursor' has been deprecated. Changes to this member will not be observed. Use 'showCursor' instead.", true)]
public bool revealCursor;
public bool showCursor
{
get => m_TextSelecting.revealCursor;
set => m_TextSelecting.revealCursor = value;
}
private bool focus;
internal bool m_HasFocus { get { return focus; } set { focus = value; } }
[Obsolete("Please use 'text' instead of 'content'", true)]
public GUIContent content
{
get => throw new NotImplementedException("Please use 'text' instead of 'content'");
set => throw new NotImplementedException("Please use 'text' instead of 'content'");
}
public string text
{
get => m_TextEditing.text;
set
{
var newValue = value ?? "";
if (m_TextEditing.text == newValue)
return;
m_TextEditing.SetTextWithoutNotify(newValue);
m_Content.SetTextWithoutNotify(newValue);
textWithWhitespace = newValue;
// TODO: Change this call to only do the parsing of the text, to update the characterCount properly.
UpdateTextHandle();
}
}
string m_TextWithWhitespace;
internal string textWithWhitespace
{
get => string.IsNullOrEmpty(m_TextWithWhitespace) ? GUIContent.k_ZeroWidthSpace : m_TextWithWhitespace;
set =>
//The NoWidthSpace unicode is added at the end of the string to make sure LineFeeds update the layout of the text.
m_TextWithWhitespace = value + GUIContent.k_ZeroWidthSpace;
}
public Rect position { get; set; }
internal virtual Rect localPosition
{
get => style.padding.Remove(position);
}
public int cursorIndex
{
get => m_TextSelecting.cursorIndex;
set => m_TextSelecting.cursorIndex = value;
}
internal int stringCursorIndex
{
get => m_TextEditing.stringCursorIndex;
set => m_TextEditing.stringCursorIndex = value;
}
public int selectIndex
{
get => m_TextSelecting.selectIndex;
set => m_TextSelecting.selectIndex = value;
}
internal int stringSelectIndex
{
get => m_TextEditing.stringSelectIndex;
set => m_TextEditing.stringSelectIndex = value;
}
// are we up/downing?
public Vector2 graphicalCursorPos;
public Vector2 graphicalSelectCursorPos;
public DblClickSnapping doubleClickSnapping
{
get => m_TextSelecting.dblClickSnap;
set => m_TextSelecting.dblClickSnap = value;
}
public int altCursorPosition
{
get => m_TextSelecting.iAltCursorPos;
set => m_TextSelecting.iAltCursorPos = value;
}
public enum DblClickSnapping : byte { WORDS, PARAGRAPHS }
[RequiredByNativeCode]
public TextEditor()
{
var style = GUIStyle.none;
m_TextHandle = IMGUITextHandle.GetTextHandle(style, position, textWithWhitespace, Color.white);
m_TextHandle.AddTextInfoToCache();
m_TextSelecting = new TextSelectingUtilities(m_TextHandle);
m_TextEditing = new TextEditingUtilities(m_TextSelecting, m_TextHandle, m_Content.text);
m_Content.OnTextChanged += OnContentTextChangedHandle;
m_TextEditing.OnTextChanged += OnTextChangedHandle;
this.style = style;
m_TextSelecting.OnCursorIndexChange += OnCursorIndexChange;
m_TextSelecting.OnSelectIndexChange += OnSelectIndexChange;
}
private void OnTextChangedHandle()
{
m_Content.SetTextWithoutNotify(text);
textWithWhitespace = text;
}
private void OnContentTextChangedHandle()
{
text = m_Content.text;
textWithWhitespace = text;
}
public void OnFocus()
{
m_HasFocus = true;
m_TextSelecting.OnFocus();
}
public void OnLostFocus()
{
m_HasFocus = false;
}
public bool HasClickedOnLink(Vector2 mousePosition, out string linkData)
{
linkData = "";
var intersectingLink = m_TextHandle.FindIntersectingLink(mousePosition - new Vector2(position.x, position.y));
if (intersectingLink < 0)
return false;
var link = m_TextHandle.textInfo.linkInfo[intersectingLink];
if (link.linkId != null && link.linkIdLength > 0)
{
linkData = new string(link.linkId);
return true;
}
return false;
}
public bool HasClickedOnHREF(Vector2 mousePosition, out string href)
{
href = "";
var intersectingLink = m_TextHandle.FindIntersectingLink(mousePosition - new Vector2(position.x, position.y));
if (intersectingLink < 0)
return false;
var link = m_TextHandle.textInfo.linkInfo[intersectingLink];
if (link.hashCode == (int)MarkupTag.HREF)
{
if (link.linkId != null && link.linkIdLength > 0)
{
href = new string(link.linkId);
if (!href.StartsWith("href"))
return false;
// Removes href="..."
href = href.Substring(6, href.Length - 7);
if (Uri.IsWellFormedUriString(href, UriKind.Absolute))
{
return true;
}
}
}
return false;
}
// Handle a key event.
// Looks up the platform-dependent key-action table & performs the event
// return true if the event was recognized.
public bool HandleKeyEvent(Event e)
{
return m_TextEditing.HandleKeyEvent(e) || m_TextSelecting.HandleKeyEvent(e);
}
// Deletes previous text on the line
public bool DeleteLineBack()
{
var result = m_TextEditing.DeleteLineBack();
UpdateTextHandle();
return result;
}
// Deletes the previous word
public bool DeleteWordBack()
{
var result = m_TextEditing.DeleteWordBack();
UpdateTextHandle();
return result;
}
// Deletes the following word
public bool DeleteWordForward()
{
var result = m_TextEditing.DeleteWordForward();
UpdateTextHandle();
return result;
}
// perform a right-delete
public bool Delete()
{
var result = m_TextEditing.Delete();
UpdateTextHandle();
return result;
}
public bool CanPaste() => m_TextEditing.CanPaste();
// Perform a left-delete
public bool Backspace()
{
var result = m_TextEditing.Backspace();
UpdateTextHandle();
return result;
}
/// Select all the text
public void SelectAll() => m_TextSelecting.SelectAll();
/// Select none of the text
public void SelectNone() => m_TextSelecting.SelectNone();
/// Does this text field has a selection
public bool hasSelection => m_TextSelecting.hasSelection;
/// Returns the selected text
public string SelectedText => m_TextSelecting.selectedText;
/// Delete the current selection. If there is no selection, this function does not do anything...
public bool DeleteSelection()
{
var result = m_TextEditing.DeleteSelection();
UpdateTextHandle();
return result;
}
/// Replace the selection with /replace/. If there is no selection, /replace/ is inserted at the current cursor point.
public void ReplaceSelection(string replace)
{
m_TextEditing.ReplaceSelection(replace);
UpdateTextHandle();
}
/// Replaced the selection with /c/
public void Insert(char c)
{
m_TextEditing.Insert(c);
UpdateTextHandle();
}
/// Move selection to alt cursor /position/
public void MoveSelectionToAltCursor() => m_TextEditing.MoveSelectionToAltCursor();
/// Move the cursor one character to the right and deselect.
public void MoveRight() => m_TextSelecting.MoveRight();
/// Move the cursor one character to the left and deselect.
public void MoveLeft() => m_TextSelecting.MoveLeft();
/// Move the cursor up and deselects.
public void MoveUp() => m_TextSelecting.MoveUp();
/// Move the cursor down and deselects.
public void MoveDown() => m_TextSelecting.MoveDown();
/// Moves the cursor to the start of the current line.
public void MoveLineStart() => m_TextSelecting.MoveLineStart();
/// Moves the selection to the end of the current line
public void MoveLineEnd() => m_TextSelecting.MoveLineEnd();
/// Move to the start of the current graphical line. This takes word-wrapping into consideration.
public void MoveGraphicalLineStart() => m_TextSelecting.MoveGraphicalLineStart();
/// Move to the end of the current graphical line. This takes word-wrapping into consideration.
public void MoveGraphicalLineEnd() => m_TextSelecting.MoveGraphicalLineEnd();
/// Moves the cursor to the beginning of the text
public void MoveTextStart() => m_TextSelecting.MoveTextStart();
/// Moves the cursor to the end of the text
public void MoveTextEnd() => m_TextSelecting.MoveTextEnd();
/// Move to the next paragraph
public void MoveParagraphForward() => m_TextSelecting.MoveParagraphForward();
/// Move to the previous paragraph
public void MoveParagraphBackward() => m_TextSelecting.MoveParagraphBackward();
// Move the cursor to a graphical position. Used for moving the cursor on MouseDown events.
public void MoveCursorToPosition(Vector2 cursorPosition)
{
MoveCursorToPosition_Internal(cursorPosition, Event.current.shift);
}
// Move the cursor to a graphical position. Used for moving the cursor on MouseDown events.
protected internal void MoveCursorToPosition_Internal(Vector2 cursorPosition, bool shift) => m_TextSelecting.MoveCursorToPosition_Internal(GetLocalCursorPosition(cursorPosition), shift);
public void MoveAltCursorToPosition(Vector2 cursorPosition) => m_TextSelecting.MoveAltCursorToPosition(GetLocalCursorPosition(cursorPosition));
public bool IsOverSelection(Vector2 cursorPosition)=> m_TextSelecting.IsOverSelection(GetLocalCursorPosition(cursorPosition));
// Do a drag selection. Used to expand the selection in MouseDrag events.
public void SelectToPosition(Vector2 cursorPosition) => m_TextSelecting.SelectToPosition(GetLocalCursorPosition(cursorPosition));
private Vector2 GetLocalCursorPosition(Vector2 cursorPosition)
{
return cursorPosition - style.Internal_GetTextRectOffset(position, m_Content, new Vector2(m_TextHandle.preferredSize.x, m_TextHandle.preferredSize.y > 0 ? m_TextHandle.preferredSize.y : style.lineHeight)) + scrollOffset;
}
/// Expand the selection to the left
public void SelectLeft() => m_TextSelecting.SelectLeft();
public void SelectRight() => m_TextSelecting.SelectRight();
public void SelectUp() => m_TextSelecting.SelectUp();
public void SelectDown() => m_TextSelecting.SelectDown();
/// Select to the end of the text
public void SelectTextEnd() => m_TextSelecting.SelectTextEnd();
/// Select to the start of the text
public void SelectTextStart() => m_TextSelecting.SelectTextStart();
/// sets whether the text selection is done by dbl click or not
public void MouseDragSelectsWholeWords(bool on) => m_TextSelecting.MouseDragSelectsWholeWords(on);
public void DblClickSnap(DblClickSnapping snapping) => m_TextSelecting.DblClickSnap(snapping);
/// Move to the end of the word.
/// If the cursor is over some space characters, these are skipped
/// Then, the cursor moves to the end of the following word.
/// This corresponds to Alt-RightArrow on a Mac
public void MoveWordRight() => m_TextSelecting.MoveWordRight();
public void MoveToStartOfNextWord() => m_TextSelecting.MoveToStartOfNextWord();
public void MoveToEndOfPreviousWord() => m_TextSelecting.MoveToEndOfPreviousWord();
public void SelectToStartOfNextWord() => m_TextSelecting.SelectToStartOfNextWord();
public void SelectToEndOfPreviousWord() => m_TextSelecting.SelectToEndOfPreviousWord();
/// Move to start of next word.
/// This corresponds to Ctrl-RightArrow on Windows
/// If the cursor is over a whitespace, it's moved forwards ''till the first non-whitespace character
/// If the cursor is over an alphanumeric character, it''s moved forward 'till it encounters space or a punctuation mark.
/// If the stopping character is a space, this is skipped as well.
/// If the cursor is over an punctuation mark, it's moved forward ''till it a letter or a space of a punctuation mark. If the stopping character is a space, this is skipped as well
public int FindStartOfNextWord(int p) => m_TextSelecting.FindStartOfNextWord(p);
public void MoveWordLeft() => m_TextSelecting.MoveWordLeft();
public void SelectWordRight() => m_TextSelecting.SelectWordRight();
public void SelectWordLeft() => m_TextSelecting.SelectWordLeft();
/// Expand the selection to the start of the line
/// Used on a mac for CMD-SHIFT-LEFT
public void ExpandSelectGraphicalLineStart() => m_TextSelecting.ExpandSelectGraphicalLineStart();
/// Expand the selection to the end of the line
/// Used on a mac for CMD-SHIFT-RIGHT
public void ExpandSelectGraphicalLineEnd() => m_TextSelecting.ExpandSelectGraphicalLineEnd();
/// Move the selection point to the start of the line
/// Used on a Windows for SHIFT-Home
public void SelectGraphicalLineStart() => m_TextSelecting.SelectGraphicalLineStart();
/// Expand the selection to the end of the line
/// Used on a mac for SHIFT-End
public void SelectGraphicalLineEnd() => m_TextSelecting.SelectGraphicalLineEnd();
public void SelectParagraphForward() => m_TextSelecting.SelectParagraphForward();
public void SelectParagraphBackward() => m_TextSelecting.SelectParagraphBackward();
/// Select the word under the cursor
public void SelectCurrentWord() => m_TextSelecting.SelectCurrentWord();
// Select the entire paragraph the cursor is on (separated by \n)
public void SelectCurrentParagraph() => m_TextSelecting.SelectCurrentParagraph();
public void UpdateScrollOffsetIfNeeded(Event evt)
{
if (evt.type != EventType.Repaint && evt.type != EventType.Layout)
{
UpdateScrollOffset();
}
}
internal void UpdateTextHandle()
{
m_TextHandle = IMGUITextHandle.GetTextHandle(style, style.padding.Remove(position), textWithWhitespace, Color.white);
m_TextHandle.AddTextInfoToCache();
m_TextEditing.textHandle = m_TextHandle;
m_TextSelecting.textHandle = m_TextHandle;
}
Vector2 lastCursorPos = Vector2.zero;
Vector2 previousContentSize = Vector2.zero;
[VisibleToOtherModules]
internal void UpdateScrollOffset()
{
const int kCursorWidth = 1;
const float epsilon = 0.05f;
var newXOffset = scrollOffset.x;
var newYOffset = scrollOffset.y;
graphicalCursorPos = style.GetCursorPixelPosition(new Rect(0, 0, position.width, position.height), m_Content, m_TextSelecting.cursorIndexNoValidation);
// The rectangle inside which the text is displayed.
Rect viewRect = style.padding.Remove(position);
// Position of the cursor in the viewRect coordinate system.
var localGraphicalCursorPos = graphicalCursorPos;
localGraphicalCursorPos.x -= style.padding.left;
localGraphicalCursorPos.y -= style.padding.top;
// The size of the text, without any padding.
Vector2 contentSize = previousContentSize = style.GetPreferredSize(m_Content.textWithWhitespace, position);
// If there is plenty of room, simply show entire string
if (contentSize.x < viewRect.width)
{
newXOffset = 0;
}
else if (showCursor)
{
//go right
if (localGraphicalCursorPos.x > scrollOffset.x + viewRect.width - kCursorWidth)
newXOffset = localGraphicalCursorPos.x - viewRect.width + kCursorWidth;
//go left
else if (localGraphicalCursorPos.x < scrollOffset.x)
newXOffset = Mathf.Max(localGraphicalCursorPos.x, 0);
//go left - applies when deleting from the string
else if (previousContentSize.x != contentSize.x && localGraphicalCursorPos.x < viewRect.x + Math.Abs(contentSize.x + kCursorWidth - viewRect.width))
newXOffset = Mathf.Max(viewRect.width - localGraphicalCursorPos.x, 0);
}
// ... and height/y as well
// If there is plenty of room, simply show entire string
if (Mathf.Round(contentSize.y) <= Mathf.Round(viewRect.height) || viewRect.height == 0)
newYOffset = 0;
else if (showCursor && Math.Abs(lastCursorPos.y - localGraphicalCursorPos.y) > epsilon)
{
//go down
if (localGraphicalCursorPos.y + style.lineHeight > scrollOffset.y + viewRect.height)
newYOffset = localGraphicalCursorPos.y - viewRect.height + style.lineHeight;
//go up
else if (localGraphicalCursorPos.y < style.lineHeight + scrollOffset.y)
newYOffset = localGraphicalCursorPos.y - style.lineHeight;
}
if (scrollOffset.x != newXOffset || scrollOffset.y != newYOffset)
scrollOffset = new Vector2(newXOffset, newYOffset < 0 ? 0 : newYOffset);
lastCursorPos = localGraphicalCursorPos;
}
// TODO: get the height from the font
public void DrawCursor(string newText)
{
string realText = text;
int cursorPos = cursorIndex;
if (GUIUtility.compositionString.Length > 0)
{
m_Content.text = newText.Substring(0, cursorIndex) + GUIUtility.compositionString + newText.Substring(selectIndex);
cursorPos += GUIUtility.compositionString.Length;
}
else
m_Content.text = newText;
graphicalCursorPos = style.GetCursorPixelPosition(position, m_Content, cursorPos);
Vector2 originalContentOffset = style.contentOffset;
style.contentOffset -= scrollOffset;
style.Internal_clipOffset = scrollOffset;
GUIUtility.compositionCursorPos = GUIClip.UnclipToWindow(graphicalCursorPos + new Vector2(position.x, position.y + style.lineHeight) - scrollOffset);
if (GUIUtility.compositionString.Length > 0)
style.DrawWithTextSelection(position, m_Content, controlID, cursorIndex, cursorIndex + GUIUtility.compositionString.Length, true);
else
style.DrawWithTextSelection(position, m_Content, controlID, cursorIndex, selectIndex);
if (m_TextSelecting.iAltCursorPos != -1)
style.DrawCursor(position, m_Content, controlID, m_TextSelecting.iAltCursorPos);
// reset
style.contentOffset = originalContentOffset;
style.Internal_clipOffset = Vector2.zero;
m_Content.text = realText;
}
string oldText;
int oldPos, oldSelectPos;
public void SaveBackup()
{
oldText = text;
oldPos = cursorIndex;
oldSelectPos = selectIndex;
}
public void Undo()
{
m_Content.text = oldText;
cursorIndex = oldPos;
selectIndex = oldSelectPos;
}
public bool Cut()
{
if (isPasswordField)
return false;
bool result = m_TextEditing.Cut();
UpdateTextHandle();
return result;
}
public void Copy()
{
if (isPasswordField)
return;
m_TextSelecting.Copy();
}
internal Rect[] GetHyperlinksRect()
{
return style.GetHyperlinkRects(m_TextHandle, localPosition);
}
public bool Paste()
{
bool result = m_TextEditing.Paste();
UpdateTextHandle();
return result;
}
public void DetectFocusChange()
{
OnDetectFocusChange();
}
internal virtual void OnDetectFocusChange()
{
if (m_HasFocus == true && controlID != GUIUtility.keyboardControl)
OnLostFocus();
if (m_HasFocus == false && controlID == GUIUtility.keyboardControl)
OnFocus();
}
internal virtual void OnCursorIndexChange()
{
UpdateScrollOffset();
}
internal virtual void OnSelectIndexChange()
{
UpdateScrollOffset();
}
}
} // namespace