-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIInput.cs
More file actions
1424 lines (1209 loc) · 31.9 KB
/
UIInput.cs
File metadata and controls
1424 lines (1209 loc) · 31.9 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 || UNITY_BLACKBERRY)
#define MOBILE
#endif
using UnityEngine;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Input field makes it possible to enter custom information within the UI.
/// </summary>
[AddComponentMenu("NGUI/UI/Input Field")]
public class UIInput : MonoBehaviour
{
public enum InputType
{
Standard,
AutoCorrect,
Password,
}
public enum Validation
{
None,
Integer,
Float,
Alphanumeric,
Username,
Name,
}
public enum KeyboardType
{
Default = 0,
ASCIICapable = 1,
NumbersAndPunctuation = 2,
URL = 3,
NumberPad = 4,
PhonePad = 5,
NamePhonePad = 6,
EmailAddress = 7,
}
public enum OnReturnKey
{
Default,
Submit,
NewLine,
}
public delegate char OnValidate (string text, int charIndex, char addedChar);
/// <summary>
/// Currently active input field. Only valid during callbacks.
/// </summary>
static public UIInput current;
/// <summary>
/// Currently selected input field, if any.
/// </summary>
static public UIInput selection;
/// <summary>
/// Text label used to display the input's value.
/// </summary>
public UILabel label;
/// <summary>
/// Type of data expected by the input field.
/// </summary>
public InputType inputType = InputType.Standard;
/// <summary>
/// What to do when the Return key is pressed on the keyboard.
/// </summary>
public OnReturnKey onReturnKey = OnReturnKey.Default;
/// <summary>
/// Keyboard type applies to mobile keyboards that get shown.
/// </summary>
public KeyboardType keyboardType = KeyboardType.Default;
/// <summary>
/// Whether the input will be hidden on mobile platforms.
/// </summary>
public bool hideInput = false;
/// <summary>
/// What kind of validation to use with the input field's data.
/// </summary>
public Validation validation = Validation.None;
/// <summary>
/// Maximum number of characters allowed before input no longer works.
/// </summary>
public int characterLimit = 0;
/// <summary>
/// Field in player prefs used to automatically save the value.
/// </summary>
public string savedAs;
/// <summary>
/// Object to select when Tab key gets pressed.
/// </summary>
public GameObject selectOnTab;
/// <summary>
/// Color of the label when the input field has focus.
/// </summary>
public Color activeTextColor = Color.white;
/// <summary>
/// Color used by the caret symbol.
/// </summary>
public Color caretColor = new Color(1f, 1f, 1f, 0.8f);
/// <summary>
/// Color used by the selection rectangle.
/// </summary>
public Color selectionColor = new Color(1f, 223f / 255f, 141f / 255f, 0.5f);
/// <summary>
/// Event delegates triggered when the input field submits its data.
/// </summary>
public List<EventDelegate> onSubmit = new List<EventDelegate>();
/// <summary>
/// Event delegates triggered when the input field's text changes for any reason.
/// </summary>
public List<EventDelegate> onChange = new List<EventDelegate>();
/// <summary>
/// Custom validation callback.
/// </summary>
public OnValidate onValidate;
/// <summary>
/// Input field's value.
/// </summary>
[SerializeField][HideInInspector] protected string mValue;
[System.NonSerialized] protected string mDefaultText = "";
[System.NonSerialized] protected Color mDefaultColor = Color.white;
[System.NonSerialized] protected float mPosition = 0f;
[System.NonSerialized] protected bool mDoInit = true;
[System.NonSerialized] protected UIWidget.Pivot mPivot = UIWidget.Pivot.TopLeft;
[System.NonSerialized] protected bool mLoadSavedValue = true;
static protected int mDrawStart = 0;
static protected string mLastIME = "";
#if MOBILE
// Unity fails to compile if the touch screen keyboard is used on a non-mobile device
static protected TouchScreenKeyboard mKeyboard;
#endif
[System.NonSerialized] protected int mSelectionStart = 0;
[System.NonSerialized] protected int mSelectionEnd = 0;
[System.NonSerialized] protected UITexture mHighlight = null;
[System.NonSerialized] protected UITexture mCaret = null;
[System.NonSerialized] protected Texture2D mBlankTex = null;
[System.NonSerialized] protected float mNextBlink = 0f;
[System.NonSerialized] protected float mLastAlpha = 0f;
[System.NonSerialized] protected string mCached = "";
[System.NonSerialized] protected int mSelectMe = -1;
/// <summary>
/// Default text used by the input's label.
/// </summary>
public string defaultText
{
get
{
return mDefaultText;
}
set
{
if (mDoInit) Init();
mDefaultText = value;
UpdateLabel();
}
}
/// <summary>
/// Should the input be hidden?
/// </summary>
public bool inputShouldBeHidden
{
get
{
return hideInput && label != null && !label.multiLine;
}
}
[System.Obsolete("Use UIInput.value instead")]
public string text { get { return this.value; } set { this.value = value; } }
/// <summary>
/// Input field's current text value.
/// </summary>
public string value
{
get
{
#if UNITY_EDITOR
if (!Application.isPlaying) return "";
#endif
if (mDoInit) Init();
return mValue;
}
set
{
#if UNITY_EDITOR
if (!Application.isPlaying) return;
#endif
if (mDoInit) Init();
mDrawStart = 0;
// BB10's implementation has a bug in Unity
#if UNITY_4_0 || UNITY_4_2 || UNITY_4_3
if (Application.platform == RuntimePlatform.BB10Player)
#else
if (Application.platform == RuntimePlatform.BlackBerryPlayer)
#endif
value = value.Replace("\\b", "\b");
// Validate all input
value = Validate(value);
#if MOBILE
if (isSelected && mKeyboard != null && mCached != value)
{
mKeyboard.text = value;
mCached = value;
}
#endif
if (mValue != value)
{
mValue = value;
mLoadSavedValue = false;
if (isSelected)
{
if (string.IsNullOrEmpty(value))
{
mSelectionStart = 0;
mSelectionEnd = 0;
}
else
{
mSelectionStart = value.Length;
mSelectionEnd = mSelectionStart;
}
}
else SaveToPlayerPrefs(value);
UpdateLabel();
ExecuteOnChange();
}
}
}
[System.Obsolete("Use UIInput.isSelected instead")]
public bool selected { get { return isSelected; } set { isSelected = value; } }
/// <summary>
/// Whether the input is currently selected.
/// </summary>
public bool isSelected
{
get
{
return selection == this;
}
set
{
if (!value) { if (isSelected) UICamera.selectedObject = null; }
else UICamera.selectedObject = gameObject;
}
}
/// <summary>
/// Current position of the cursor.
/// </summary>
public int cursorPosition
{
get
{
#if MOBILE
if (mKeyboard != null && !inputShouldBeHidden) return value.Length;
#endif
return isSelected ? mSelectionEnd : value.Length;
}
set
{
if (isSelected)
{
#if MOBILE
if (mKeyboard != null && !inputShouldBeHidden) return;
#endif
mSelectionEnd = value;
UpdateLabel();
}
}
}
/// <summary>
/// Index of the character where selection begins.
/// </summary>
public int selectionStart
{
get
{
#if MOBILE
if (mKeyboard != null && !inputShouldBeHidden) return 0;
#endif
return isSelected ? mSelectionStart : value.Length;
}
set
{
if (isSelected)
{
#if MOBILE
if (mKeyboard != null && !inputShouldBeHidden) return;
#endif
mSelectionStart = value;
UpdateLabel();
}
}
}
/// <summary>
/// Index of the character where selection ends.
/// </summary>
public int selectionEnd
{
get
{
#if MOBILE
if (mKeyboard != null && !inputShouldBeHidden) return value.Length;
#endif
return isSelected ? mSelectionEnd : value.Length;
}
set
{
if (isSelected)
{
#if MOBILE
if (mKeyboard != null && !inputShouldBeHidden) return;
#endif
mSelectionEnd = value;
UpdateLabel();
}
}
}
/// <summary>
/// Caret, in case it's needed.
/// </summary>
public UITexture caret { get { return mCaret; } }
/// <summary>
/// Validate the specified text, returning the validated version.
/// </summary>
public string Validate (string val)
{
if (string.IsNullOrEmpty(val)) return "";
StringBuilder sb = new StringBuilder(val.Length);
for (int i = 0; i < val.Length; ++i)
{
char c = val[i];
if (onValidate != null) c = onValidate(sb.ToString(), sb.Length, c);
else if (validation != Validation.None) c = Validate(sb.ToString(), sb.Length, c);
if (c != 0) sb.Append(c);
}
if (characterLimit > 0 && sb.Length > characterLimit)
return sb.ToString(0, characterLimit);
return sb.ToString();
}
/// <summary>
/// Automatically set the value by loading it from player prefs if possible.
/// </summary>
void Start ()
{
if (mLoadSavedValue && !string.IsNullOrEmpty(savedAs)) LoadValue();
else value = mValue.Replace("\\n", "\n");
}
/// <summary>
/// Labels used for input shouldn't support rich text.
/// </summary>
protected void Init ()
{
if (mDoInit && label != null)
{
mDoInit = false;
mDefaultText = label.text;
mDefaultColor = label.color;
label.supportEncoding = false;
if (label.alignment == NGUIText.Alignment.Justified)
{
label.alignment = NGUIText.Alignment.Left;
Debug.LogWarning("Input fields using labels with justified alignment are not supported at this time", this);
}
mPivot = label.pivot;
mPosition = label.cachedTransform.localPosition.x;
UpdateLabel();
}
}
/// <summary>
/// Save the specified value to player prefs.
/// </summary>
protected void SaveToPlayerPrefs (string val)
{
if (!string.IsNullOrEmpty(savedAs))
{
if (string.IsNullOrEmpty(val)) PlayerPrefs.DeleteKey(savedAs);
else PlayerPrefs.SetString(savedAs, val);
}
}
/// <summary>
/// Selection event, sent by the EventSystem.
/// </summary>
protected virtual void OnSelect (bool isSelected)
{
if (isSelected) OnSelectEvent();
else OnDeselectEvent();
}
/// <summary>
/// Notification of the input field gaining selection.
/// </summary>
protected void OnSelectEvent ()
{
selection = this;
if (mDoInit) Init();
// Unity has issues bringing up the keyboard properly if it's in "hideInput" mode and you happen
// to select one input in the same Update as de-selecting another.
if (label != null && NGUITools.GetActive(this)) mSelectMe = Time.frameCount;
}
/// <summary>
/// Notification of the input field losing selection.
/// </summary>
protected void OnDeselectEvent ()
{
if (mDoInit) Init();
if (label != null && NGUITools.GetActive(this))
{
mValue = value;
#if MOBILE
if (mKeyboard != null)
{
mKeyboard.active = false;
mKeyboard = null;
}
#endif
if (string.IsNullOrEmpty(mValue))
{
label.text = mDefaultText;
label.color = mDefaultColor;
}
else label.text = mValue;
Input.imeCompositionMode = IMECompositionMode.Auto;
RestoreLabelPivot();
}
selection = null;
UpdateLabel();
}
/// <summary>
/// Update the text based on input.
/// </summary>
void Update ()
{
#if UNITY_EDITOR
if (!Application.isPlaying) return;
#endif
if (isSelected)
{
if (mDoInit) Init();
// Unity has issues bringing up the keyboard properly if it's in "hideInput" mode and you happen
// to select one input in the same Update as de-selecting another.
if (mSelectMe != -1 && mSelectMe != Time.frameCount)
{
mSelectMe = -1;
label.color = activeTextColor;
#if MOBILE
if (Application.platform == RuntimePlatform.IPhonePlayer
|| Application.platform == RuntimePlatform.Android
|| Application.platform == RuntimePlatform.WP8Player
#if UNITY_4_0 || UNITY_4_2 || UNITY_4_3
|| Application.platform == RuntimePlatform.BB10Player
#else
|| Application.platform == RuntimePlatform.BlackBerryPlayer
#endif
)
{
string val;
TouchScreenKeyboardType kt;
if (inputShouldBeHidden)
{
TouchScreenKeyboard.hideInput = true;
kt = (TouchScreenKeyboardType)((int)keyboardType);
val = "|";
}
else if (inputType == InputType.Password)
{
TouchScreenKeyboard.hideInput = false;
kt = TouchScreenKeyboardType.Default;
val = mValue;
}
else
{
TouchScreenKeyboard.hideInput = false;
kt = (TouchScreenKeyboardType)((int)keyboardType);
val = mValue;
}
mKeyboard = (inputType == InputType.Password) ?
TouchScreenKeyboard.Open(val, kt, false, false, true) :
TouchScreenKeyboard.Open(val, kt, inputType == InputType.AutoCorrect, label.multiLine && !hideInput, false, false, defaultText);
}
else
#endif
{
Vector2 pos = (UICamera.current != null && UICamera.current.cachedCamera != null) ?
UICamera.current.cachedCamera.WorldToScreenPoint(label.worldCorners[0]) :
label.worldCorners[0];
pos.y = Screen.height - pos.y;
Input.imeCompositionMode = IMECompositionMode.On;
Input.compositionCursorPos = pos;
mSelectionStart = 0;
mSelectionEnd = string.IsNullOrEmpty(mValue) ? 0 : mValue.Length;
mDrawStart = 0;
}
UpdateLabel();
}
#if MOBILE
if (mKeyboard != null)
{
string text = mKeyboard.text;
if (inputShouldBeHidden)
{
if (text != "|")
{
if (!string.IsNullOrEmpty(text))
{
Insert(text.Substring(1));
}
else DoBackspace();
mKeyboard.text = "|";
}
}
else if (mCached != text)
{
mCached = text;
value = text;
}
if (mKeyboard.done || !mKeyboard.active)
{
if (!mKeyboard.wasCanceled) Submit();
mKeyboard = null;
isSelected = false;
mCached = "";
}
}
else
#endif
{
if (selectOnTab != null && Input.GetKeyDown(KeyCode.Tab))
{
UICamera.selectedObject = selectOnTab;
return;
}
string ime = Input.compositionString;
// There seems to be an inconsistency between IME on Windows, and IME on OSX.
// On Windows, Input.inputString is always empty while IME is active. On the OSX it is not.
if (string.IsNullOrEmpty(ime) && !string.IsNullOrEmpty(Input.inputString))
{
// Process input ignoring non-printable characters as they are not consistent.
// Windows has them, OSX may not. They get handled inside OnGUI() instead.
string s = Input.inputString;
for (int i = 0; i < s.Length; ++i)
{
char ch = s[i];
if (ch < ' ') continue;
// OSX inserts these characters for arrow keys
if (ch == '\uF700') continue;
if (ch == '\uF701') continue;
if (ch == '\uF702') continue;
if (ch == '\uF703') continue;
Insert(ch.ToString());
}
}
// Append IME composition
if (mLastIME != ime)
{
mSelectionEnd = string.IsNullOrEmpty(ime) ? mSelectionStart : mValue.Length + ime.Length;
mLastIME = ime;
UpdateLabel();
ExecuteOnChange();
}
}
// Blink the caret
if (mCaret != null && mNextBlink < RealTime.time)
{
mNextBlink = RealTime.time + 0.5f;
mCaret.enabled = !mCaret.enabled;
}
// If the label's final alpha changes, we need to update the drawn geometry,
// or the highlight widgets (which have their geometry set manually) won't update.
if (isSelected && mLastAlpha != label.finalAlpha)
UpdateLabel();
}
}
/// <summary>
/// Unfortunately Unity 4.3 and earlier doesn't offer a way to properly process events outside of OnGUI.
/// </summary>
void OnGUI ()
{
if (isSelected && Event.current.rawType == EventType.KeyDown)
ProcessEvent(Event.current);
}
/// <summary>
/// Perform a backspace operation.
/// </summary>
protected void DoBackspace ()
{
if (!string.IsNullOrEmpty(mValue))
{
if (mSelectionStart == mSelectionEnd)
{
if (mSelectionStart < 1) return;
--mSelectionEnd;
}
Insert("");
}
}
/// <summary>
/// Handle the specified event.
/// </summary>
protected virtual bool ProcessEvent (Event ev)
{
if (label == null) return false;
RuntimePlatform rp = Application.platform;
bool isMac = (
rp == RuntimePlatform.OSXEditor ||
rp == RuntimePlatform.OSXPlayer ||
rp == RuntimePlatform.OSXWebPlayer);
bool ctrl = isMac ?
((ev.modifiers & EventModifiers.Command) != 0) :
((ev.modifiers & EventModifiers.Control) != 0);
bool shift = ((ev.modifiers & EventModifiers.Shift) != 0);
switch (ev.keyCode)
{
case KeyCode.Backspace:
{
ev.Use();
DoBackspace();
return true;
}
case KeyCode.Delete:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
if (mSelectionStart == mSelectionEnd)
{
if (mSelectionStart >= mValue.Length) return true;
++mSelectionEnd;
}
Insert("");
}
return true;
}
case KeyCode.LeftArrow:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
mSelectionEnd = Mathf.Max(mSelectionEnd - 1, 0);
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.RightArrow:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
mSelectionEnd = Mathf.Min(mSelectionEnd + 1, mValue.Length);
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.PageUp:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
mSelectionEnd = 0;
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.PageDown:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
mSelectionEnd = mValue.Length;
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.Home:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
if (label.multiLine)
{
mSelectionEnd = label.GetCharacterIndex(mSelectionEnd, KeyCode.Home);
}
else mSelectionEnd = 0;
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.End:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
if (label.multiLine)
{
mSelectionEnd = label.GetCharacterIndex(mSelectionEnd, KeyCode.End);
}
else mSelectionEnd = mValue.Length;
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.UpArrow:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
mSelectionEnd = label.GetCharacterIndex(mSelectionEnd, KeyCode.UpArrow);
if (mSelectionEnd != 0) mSelectionEnd += mDrawStart;
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
case KeyCode.DownArrow:
{
ev.Use();
if (!string.IsNullOrEmpty(mValue))
{
mSelectionEnd = label.GetCharacterIndex(mSelectionEnd, KeyCode.DownArrow);
if (mSelectionEnd != label.processedText.Length) mSelectionEnd += mDrawStart;
else mSelectionEnd = mValue.Length;
if (!shift) mSelectionStart = mSelectionEnd;
UpdateLabel();
}
return true;
}
// Select all
case KeyCode.A:
{
if (ctrl)
{
ev.Use();
mSelectionStart = 0;
mSelectionEnd = mValue.Length;
UpdateLabel();
}
return true;
}
// Copy
case KeyCode.C:
{
if (ctrl)
{
ev.Use();
NGUITools.clipboard = GetSelection();
}
return true;
}
// Paste
case KeyCode.V:
{
if (ctrl)
{
ev.Use();
Insert(NGUITools.clipboard);
}
return true;
}
// Cut
case KeyCode.X:
{
if (ctrl)
{
ev.Use();
NGUITools.clipboard = GetSelection();
Insert("");
}
return true;
}
// Submit
case KeyCode.Return:
case KeyCode.KeypadEnter:
{
ev.Use();
bool newLine = (onReturnKey == OnReturnKey.NewLine) ||
(onReturnKey == OnReturnKey.Default &&
label.multiLine && !ctrl &&
label.overflowMethod != UILabel.Overflow.ClampContent &&
validation == Validation.None);
if (newLine)
{
Insert("\n");
}
else
{
UICamera.currentScheme = UICamera.ControlScheme.Controller;
UICamera.currentKey = ev.keyCode;
Submit();
UICamera.currentKey = KeyCode.None;
}
return true;
}
}
return false;
}
/// <summary>
/// Insert the specified text string into the current input value, respecting selection and validation.
/// </summary>
protected virtual void Insert (string text)
{
string left = GetLeftText();
string right = GetRightText();
int rl = right.Length;
StringBuilder sb = new StringBuilder(left.Length + right.Length + text.Length);
sb.Append(left);
// Append the new text
for (int i = 0, imax = text.Length; i < imax; ++i)
{
// Can't go past the character limit
if (characterLimit > 0 && sb.Length + rl >= characterLimit) break;
// If we have an input validator, validate the input first
char c = text[i];
if (onValidate != null) c = onValidate(sb.ToString(), sb.Length, c);
else if (validation != Validation.None) c = Validate(sb.ToString(), sb.Length, c);
// Append the character if it hasn't been invalidated
if (c != 0) sb.Append(c);
}
// Advance the selection
mSelectionStart = sb.Length;
mSelectionEnd = mSelectionStart;
// Append the text that follows it, ensuring that it's also validated after the inserted value
for (int i = 0, imax = right.Length; i < imax; ++i)
{
char c = right[i];
if (onValidate != null) c = onValidate(sb.ToString(), sb.Length, c);
else if (validation != Validation.None) c = Validate(sb.ToString(), sb.Length, c);
if (c != 0) sb.Append(c);
}
mValue = sb.ToString();
UpdateLabel();
ExecuteOnChange();
}
/// <summary>
/// Get the text to the left of the selection.
/// </summary>