forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.cs
More file actions
1903 lines (1627 loc) · 82.1 KB
/
GUI.cs
File metadata and controls
1903 lines (1627 loc) · 82.1 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
// 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.Scripting;
namespace UnityEngine
{
public partial class GUI
{
private const float s_ScrollStepSize = 10f;
private static int s_ScrollControlId;
private static int s_HotTextField = -1;
private static readonly int s_BoxHash = "Box".GetHashCode();
private static readonly int s_ButonHash = "Button".GetHashCode();
private static readonly int s_RepeatButtonHash = "repeatButton".GetHashCode();
private static readonly int s_ToggleHash = "Toggle".GetHashCode();
private static readonly int s_ButtonGridHash = "ButtonGrid".GetHashCode();
private static readonly int s_SliderHash = "Slider".GetHashCode();
private static readonly int s_BeginGroupHash = "BeginGroup".GetHashCode();
private static readonly int s_ScrollviewHash = "scrollView".GetHashCode();
static GUI()
{
nextScrollStepTime = DateTime.Now; // whatever but null
}
internal static int scrollTroughSide { get; set; }
internal static DateTime nextScrollStepTime { get; set; }
private static GUISkin s_Skin;
// The global skin to use.
public static GUISkin skin
{
set
{
GUIUtility.CheckOnGUI();
DoSetSkin(value);
}
get
{
GUIUtility.CheckOnGUI();
return s_Skin;
}
}
internal static void DoSetSkin(GUISkin newSkin)
{
if (!newSkin)
newSkin = GUIUtility.GetDefaultSkin();
s_Skin = newSkin;
newSkin.MakeCurrent();
}
internal static void CleanupRoots()
{
// Remove references from roots, so GC can collect them.
// Required for Windows Store Apps when cleaning up everything on application quit.
// Otherwise managed roots are being freed after Unity managers are destroyed, in this case when finalizers are invoked for GUI* objects
// they're accessing non existent managers.
s_Skin = null;
// GUILayoutUtility.CleanupRoots indirectly invokes GUILayoutUtility ctor, which indirectly sets GUIStyle s_None, that's
// why we need to invoke GUILayoutUtility.CleanupRoots before GUIStyle.CleanupRoots
// UnityEngine.DLL!UnityEngine.GUIStyle.Init() Unknown
// UnityEngine.DLL!UnityEngine.GUIStyle.GUIStyle() Line 172 C#
//> UnityEngine.DLL!UnityEngine.GUIStyle.none.get() Line 590 C#
// UnityEngine.DLL!UnityEngine.GUILayoutGroup.GUILayoutGroup() Line 592 C#
// UnityEngine.DLL!UnityEngine.GUILayoutUtility.LayoutCache.LayoutCache() Line 19 C#
// UnityEngine.DLL!UnityEngine.GUILayoutUtility.GUILayoutUtility() Line 43 C#
// [Native to Managed Transition]
// UnityEngine.DLL!UnityEngine.GUILayoutUtility.CleanupRoots() Line 50 C#
GUIUtility.CleanupRoots();
GUILayoutUtility.CleanupRoots();
GUISkin.CleanupRoots();
GUIStyle.CleanupRoots();
}
// MATRIX
// The GUI transform matrix.
public static Matrix4x4 matrix
{
get { return GUIClip.GetMatrix(); }
set { GUIClip.SetMatrix(value); }
}
// The tooltip of the control the mouse is currently over, or which has keyboard focus. (RO).
public static string tooltip
{
get
{
string str = Internal_GetTooltip();
if (str != null)
return str;
return "";
}
set { Internal_SetTooltip(value); }
}
protected static string mouseTooltip => Internal_GetMouseTooltip();
protected static Rect tooltipRect
{
get { return s_ToolTipRect; }
set { s_ToolTipRect = value; }
}
internal static Rect s_ToolTipRect;
public static void Label(Rect position, string text)
{
Label(position, GUIContent.Temp(text), s_Skin.label);
}
public static void Label(Rect position, Texture image)
{
Label(position, GUIContent.Temp(image), s_Skin.label);
}
public static void Label(Rect position, GUIContent content)
{
Label(position, content, s_Skin.label);
}
public static void Label(Rect position, string text, GUIStyle style)
{
Label(position, GUIContent.Temp(text), style);
}
public static void Label(Rect position, Texture image, GUIStyle style)
{
Label(position, GUIContent.Temp(image), style);
}
// Make a text or texture label on screen.
public static void Label(Rect position, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
DoLabel(position, content, style);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image)
{
DrawTexture(position, image, ScaleMode.StretchToFill);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode)
{
DrawTexture(position, image, scaleMode, true);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode , bool alphaBlend)
{
DrawTexture(position, image, scaleMode, alphaBlend, 0);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect)
{
DrawTexture(position, image, scaleMode, alphaBlend, imageAspect, GUI.color, 0, 0);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect, Color color, float borderWidth, float borderRadius)
{
var borderWidths = Vector4.one * borderWidth;
DrawTexture(position, image, scaleMode, alphaBlend, imageAspect, color, borderWidths, borderRadius);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect, Color color, Vector4 borderWidths, float borderRadius)
{
var borderRadiuses = Vector4.one * borderRadius;
DrawTexture(position, image, scaleMode, alphaBlend, imageAspect, color, borderWidths, borderRadiuses);
}
// Draw a texture within a rectangle.
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend
, float imageAspect, Color color, Vector4 borderWidths, Vector4 borderRadiuses)
{
DrawTexture(position, image, scaleMode, alphaBlend, imageAspect, color, color, color, color
, borderWidths, borderRadiuses);
}
// Draw a texture within a rectangle.
internal static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect, Color leftColor, Color topColor, Color rightColor, Color bottomColor, Vector4 borderWidths, Vector4 borderRadiuses)
{
GUIUtility.CheckOnGUI();
if (Event.current.type == EventType.Repaint)
{
if (image == null)
{
Debug.LogWarning("null texture passed to GUI.DrawTexture");
return;
}
if (imageAspect == 0)
imageAspect = (float)image.width / image.height;
Material mat = null;
if (borderWidths != Vector4.zero)
{
if ((leftColor != topColor) || (leftColor != rightColor) || (leftColor != bottomColor))
{
mat = roundedRectWithColorPerBorderMaterial;
}
else
{
mat = roundedRectMaterial;
}
}
else if (borderRadiuses != Vector4.zero)
{
mat = roundedRectMaterial;
}
else
{
mat = alphaBlend ? blendMaterial : blitMaterial;
}
Internal_DrawTextureArguments arguments = new Internal_DrawTextureArguments
{
leftBorder = 0,
rightBorder = 0,
topBorder = 0,
bottomBorder = 0,
color = leftColor,
leftBorderColor = leftColor,
topBorderColor = topColor,
rightBorderColor = rightColor,
bottomBorderColor = bottomColor,
borderWidths = borderWidths,
cornerRadiuses = borderRadiuses,
texture = image,
mat = mat
};
CalculateScaledTextureRects(position, scaleMode, imageAspect, ref arguments.screenRect, ref arguments.sourceRect);
Graphics.Internal_DrawTexture(ref arguments);
}
}
// Calculate screenrect and sourcerect for different scalemodes
internal static bool CalculateScaledTextureRects(Rect position, ScaleMode scaleMode, float imageAspect, ref Rect outScreenRect, ref Rect outSourceRect)
{
float destAspect = position.width / position.height;
bool ret = false;
switch (scaleMode)
{
case ScaleMode.StretchToFill:
outScreenRect = position;
outSourceRect = new Rect(0, 0, 1, 1);
ret = true;
break;
case ScaleMode.ScaleAndCrop:
if (destAspect > imageAspect)
{
float stretch = imageAspect / destAspect;
outScreenRect = position;
outSourceRect = new Rect(0, (1 - stretch) * .5f, 1, stretch);
ret = true;
}
else
{
float stretch = destAspect / imageAspect;
outScreenRect = position;
outSourceRect = new Rect(.5f - stretch * .5f, 0, stretch, 1);
ret = true;
}
break;
case ScaleMode.ScaleToFit:
if (destAspect > imageAspect)
{
float stretch = imageAspect / destAspect;
outScreenRect = new Rect(position.xMin + position.width * (1.0f - stretch) * .5f, position.yMin, stretch * position.width, position.height);
outSourceRect = new Rect(0, 0, 1, 1);
ret = true;
}
else
{
float stretch = destAspect / imageAspect;
outScreenRect = new Rect(position.xMin, position.yMin + position.height * (1.0f - stretch) * .5f, position.width, stretch * position.height);
outSourceRect = new Rect(0, 0, 1, 1);
ret = true;
}
break;
}
return ret;
}
// Draw a texture within a rectangle with the given texture coordinates. Use this function for clipping or tiling the image within the given rectangle.
public static void DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords)
{
DrawTextureWithTexCoords(position, image, texCoords, true);
}
// Draw a texture within a rectangle with the given texture coordinates. Use this function for clipping or tiling the image within the given rectangle.
public static void DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords, bool alphaBlend)
{
GUIUtility.CheckOnGUI();
if (Event.current.type == EventType.Repaint)
{
Material mat = alphaBlend ? blendMaterial : blitMaterial;
Internal_DrawTextureArguments arguments = new Internal_DrawTextureArguments();
arguments.texture = image;
arguments.mat = mat;
arguments.leftBorder = 0; arguments.rightBorder = 0; arguments.topBorder = 0; arguments.bottomBorder = 0;
arguments.color = GUI.color;
arguments.leftBorderColor = GUI.color;
arguments.topBorderColor = GUI.color;
arguments.rightBorderColor = GUI.color;
arguments.bottomBorderColor = GUI.color;
arguments.screenRect = position;
arguments.sourceRect = texCoords;
Graphics.Internal_DrawTexture(ref arguments);
}
}
public static void Box(Rect position, string text)
{
Box(position, GUIContent.Temp(text), s_Skin.box);
}
public static void Box(Rect position, Texture image)
{
Box(position, GUIContent.Temp(image), s_Skin.box);
}
public static void Box(Rect position, GUIContent content)
{
Box(position, content, s_Skin.box);
}
public static void Box(Rect position, string text, GUIStyle style)
{
Box(position, GUIContent.Temp(text), style);
}
public static void Box(Rect position, Texture image, GUIStyle style)
{
Box(position, GUIContent.Temp(image), style);
}
// Make a graphical box.
public static void Box(Rect position, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
int id = GUIUtility.GetControlID(s_BoxHash, FocusType.Passive);
if (Event.current.type == EventType.Repaint)
{
style.Draw(position, content, id);
}
}
public static bool Button(Rect position, string text)
{
return Button(position, GUIContent.Temp(text), s_Skin.button);
}
public static bool Button(Rect position, Texture image)
{
return Button(position, GUIContent.Temp(image), s_Skin.button);
}
public static bool Button(Rect position, GUIContent content)
{
return Button(position, content, s_Skin.button);
}
public static bool Button(Rect position, string text, GUIStyle style)
{
return Button(position, GUIContent.Temp(text), style);
}
public static bool Button(Rect position, Texture image, GUIStyle style)
{
return Button(position, GUIContent.Temp(image), style);
}
// Make a single press button. The user clicks them and something happens immediately.
public static bool Button(Rect position, GUIContent content, GUIStyle style)
{
int id = GUIUtility.GetControlID(s_ButonHash, FocusType.Passive, position);
return Button(position, id, content, style);
}
internal static bool Button(Rect position, int id, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
return DoButton(position, id, content, style);
}
public static bool RepeatButton(Rect position, string text)
{
return DoRepeatButton(position, GUIContent.Temp(text), s_Skin.button, FocusType.Passive);
}
public static bool RepeatButton(Rect position, Texture image)
{
return DoRepeatButton(position, GUIContent.Temp(image), s_Skin.button, FocusType.Passive);
}
public static bool RepeatButton(Rect position, GUIContent content)
{
return DoRepeatButton(position, content, s_Skin.button, FocusType.Passive);
}
public static bool RepeatButton(Rect position, string text, GUIStyle style)
{
return DoRepeatButton(position, GUIContent.Temp(text), style, FocusType.Passive);
}
public static bool RepeatButton(Rect position, Texture image, GUIStyle style)
{
return DoRepeatButton(position, GUIContent.Temp(image), style, FocusType.Passive);
}
// Make a button that is active as long as the user holds it down.
public static bool RepeatButton(Rect position, GUIContent content, GUIStyle style)
{
return DoRepeatButton(position, content, style, FocusType.Passive);
}
private static bool DoRepeatButton(Rect position, GUIContent content, GUIStyle style, FocusType focusType)
{
GUIUtility.CheckOnGUI();
int id = GUIUtility.GetControlID(s_RepeatButtonHash, focusType, position);
switch (Event.current.GetTypeForControl(id))
{
case EventType.MouseDown:
// If the mouse is inside the button, we say that we're the hot control
if (position.Contains(Event.current.mousePosition))
{
GUIUtility.hotControl = id;
Event.current.Use();
}
return false;
case EventType.MouseUp:
if (GUIUtility.hotControl == id)
{
GUIUtility.hotControl = 0;
// If we got the mousedown, the mouseup is ours as well
// (no matter if the click was in the button or not)
Event.current.Use();
// But we only return true if the button was actually clicked
return position.Contains(Event.current.mousePosition);
}
return false;
case EventType.Repaint:
style.Draw(position, content, id, false, position.Contains(Event.current.mousePosition));
return id == GUIUtility.hotControl && position.Contains(Event.current.mousePosition);
}
return false;
}
public static string TextField(Rect position, string text)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, false, -1, GUI.skin.textField);
return t.text;
}
public static string TextField(Rect position, string text, int maxLength)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, false, maxLength, GUI.skin.textField);
return t.text;
}
public static string TextField(Rect position, string text, GUIStyle style)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, false, -1, style);
return t.text;
}
// Make a single-line text field where the user can edit a string.
public static string TextField(Rect position, string text, int maxLength, GUIStyle style)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, false, maxLength, style);
return t.text;
}
public static string PasswordField(Rect position, string password, char maskChar)
{
return PasswordField(position, password, maskChar, -1, GUI.skin.textField);
}
public static string PasswordField(Rect position, string password, char maskChar, int maxLength)
{
return PasswordField(position, password, maskChar, maxLength, GUI.skin.textField);
}
public static string PasswordField(Rect position, string password, char maskChar, GUIStyle style)
{
return PasswordField(position, password, maskChar, -1, style);
}
// Make a text field where the user can enter a password.
public static string PasswordField(Rect position, string password, char maskChar, int maxLength, GUIStyle style)
{
GUIUtility.CheckOnGUI();
string strPassword = PasswordFieldGetStrToShow(password, maskChar);
GUIContent t = GUIContent.Temp(strPassword);
bool oldGUIChanged = GUI.changed;
GUI.changed = false;
if (TouchScreenKeyboard.isSupported)
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard), t, false, maxLength, style, password, maskChar);
else
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, false, maxLength, style);
strPassword = GUI.changed ? t.text : password;
GUI.changed |= oldGUIChanged;
return strPassword;
}
internal static string PasswordFieldGetStrToShow(string password, char maskChar)
{
return (Event.current.type == EventType.Repaint || Event.current.type == EventType.MouseDown) ?
"".PadRight(password.Length, maskChar) : password;
}
public static string TextArea(Rect position, string text)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, true, -1, GUI.skin.textArea);
return t.text;
}
public static string TextArea(Rect position, string text, int maxLength)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, true, maxLength, GUI.skin.textArea);
return t.text;
}
public static string TextArea(Rect position, string text, GUIStyle style)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, true, -1, style);
return t.text;
}
// Make a Multi-line text area where the user can edit a string.
public static string TextArea(Rect position, string text, int maxLength, GUIStyle style)
{
GUIContent t = GUIContent.Temp(text);
DoTextField(position, GUIUtility.GetControlID(FocusType.Keyboard, position), t, true, maxLength, style);
return t.text;
}
internal static void DoTextField(Rect position, int id, GUIContent content, bool multiline, int maxLength, GUIStyle style)
{
DoTextField(position, id, content, multiline, maxLength, style, null);
}
internal static void DoTextField(Rect position, int id, GUIContent content, bool multiline, int maxLength, GUIStyle style, string secureText)
{
DoTextField(position, id, content, multiline, maxLength, style, secureText, '\0');
}
internal static void DoTextField(Rect position, int id, GUIContent content, bool multiline, int maxLength, GUIStyle style, string secureText, char maskChar)
{
GUIUtility.CheckOnGUI();
//Pre-cull input string to maxLength.
if (maxLength >= 0 && content.text.Length > maxLength)
content.text = content.text.Substring(0, maxLength);
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), id);
editor.text = content.text;
editor.SaveBackup();
editor.position = position;
editor.style = style;
editor.multiline = multiline;
editor.controlID = id;
editor.DetectFocusChange();
if (TouchScreenKeyboard.isSupported)
{
HandleTextFieldEventForTouchscreen(position, id, content, multiline, maxLength, style, secureText, maskChar, editor);
}
else // Not supported means we have a physical keyboard attached
{
HandleTextFieldEventForDesktop(position, id, content, multiline, maxLength, style, editor);
}
// Scroll offset might need to be updated
editor.UpdateScrollOffsetIfNeeded(Event.current);
}
private static void HandleTextFieldEventForTouchscreen(Rect position, int id, GUIContent content, bool multiline, int maxLength,
GUIStyle style, string secureText, char maskChar, TextEditor editor)
{
var evt = Event.current;
switch (evt.type)
{
case EventType.MouseDown:
if (position.Contains(evt.mousePosition))
{
GUIUtility.hotControl = id;
// Disable keyboard for previously active text field, if any
if (s_HotTextField != -1 && s_HotTextField != id)
{
TextEditor currentEditor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), s_HotTextField);
currentEditor.keyboardOnScreen = null;
}
s_HotTextField = id;
// in player setting keyboard control calls OnFocus every time, don't want that. In editor it does not do that for some reason
if (GUIUtility.keyboardControl != id)
GUIUtility.keyboardControl = id;
editor.keyboardOnScreen = TouchScreenKeyboard.Open(
secureText ?? content.text,
TouchScreenKeyboardType.Default,
true, // autocorrection
multiline,
(secureText != null));
evt.Use();
}
break;
case EventType.Repaint:
if (editor.keyboardOnScreen != null)
{
content.text = editor.keyboardOnScreen.text;
if (maxLength >= 0 && content.text.Length > maxLength)
content.text = content.text.Substring(0, maxLength);
if (editor.keyboardOnScreen.status != TouchScreenKeyboard.Status.Visible)
{
editor.keyboardOnScreen = null;
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 clearText = content.text;
if (secureText != null)
content.text = PasswordFieldGetStrToShow(clearText, maskChar);
style.Draw(position, content, id, false);
content.text = clearText;
break;
}
}
private static void HandleTextFieldEventForDesktop(Rect position, int id, GUIContent content, bool multiline, int maxLength, GUIStyle style, TextEditor editor)
{
var evt = Event.current;
bool change = false;
switch (evt.type)
{
case EventType.MouseDown:
if (position.Contains(evt.mousePosition))
{
GUIUtility.hotControl = id;
GUIUtility.keyboardControl = id;
editor.m_HasFocus = true;
editor.MoveCursorToPosition(Event.current.mousePosition);
if (Event.current.clickCount == 2 && GUI.skin.settings.doubleClickSelectsWord)
{
editor.SelectCurrentWord();
editor.DblClickSnap(TextEditor.DblClickSnapping.WORDS);
editor.MouseDragSelectsWholeWords(true);
}
if (Event.current.clickCount == 3 && GUI.skin.settings.tripleClickSelectsLine)
{
editor.SelectCurrentParagraph();
editor.MouseDragSelectsWholeWords(true);
editor.DblClickSnap(TextEditor.DblClickSnapping.PARAGRAPHS);
}
evt.Use();
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == id)
{
if (evt.shift)
editor.MoveCursorToPosition(Event.current.mousePosition);
else
editor.SelectToPosition(Event.current.mousePosition);
evt.Use();
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id)
{
editor.MouseDragSelectsWholeWords(false);
GUIUtility.hotControl = 0;
evt.Use();
}
break;
case EventType.KeyDown:
if (GUIUtility.keyboardControl != id)
return;
if (editor.HandleKeyEvent(evt))
{
evt.Use();
change = true;
content.text = editor.text;
break;
}
// Ignore tab & shift-tab in textfields
if (evt.keyCode == KeyCode.Tab || evt.character == '\t')
return;
char c = evt.character;
if (c == '\n' && !multiline && !evt.alt)
return;
// Simplest test: only allow the character if the display font supports it.
Font font = style.font;
if (!font)
font = GUI.skin.font;
if (font.HasCharacter(c) || c == '\n')
{
editor.Insert(c);
change = true;
break;
}
// On windows, keypresses also send events with keycode but no character. Eat them up here.
if (c == 0)
{
// if we have a composition string, make sure we clear the previous selection.
if (GUIUtility.compositionString.Length > 0)
{
editor.ReplaceSelection("");
change = true;
}
evt.Use();
}
// else {
// REALLY USEFUL:
// Debug.Log ("unhandled " +evt);
// evt.Use ();
// }
break;
case EventType.Repaint:
// If we have keyboard focus, draw the cursor
// TODO: check if this OpenGL view has keyboard focus
if (GUIUtility.keyboardControl != id)
{
style.Draw(position, content, id, false);
}
else
{
editor.DrawCursor(content.text);
}
break;
}
if (GUIUtility.keyboardControl == id)
GUIUtility.textFieldInput = true;
if (change)
{
changed = true;
content.text = editor.text;
if (maxLength >= 0 && content.text.Length > maxLength)
content.text = content.text.Substring(0, maxLength);
evt.Use();
}
}
public static bool Toggle(Rect position, bool value, string text)
{
return Toggle(position, value, GUIContent.Temp(text), s_Skin.toggle);
}
public static bool Toggle(Rect position, bool value, Texture image)
{
return Toggle(position, value, GUIContent.Temp(image), s_Skin.toggle);
}
public static bool Toggle(Rect position, bool value, GUIContent content)
{
return Toggle(position, value, content, s_Skin.toggle);
}
public static bool Toggle(Rect position, bool value, string text, GUIStyle style)
{
return Toggle(position, value, GUIContent.Temp(text), style);
}
public static bool Toggle(Rect position, bool value, Texture image, GUIStyle style)
{
return Toggle(position, value, GUIContent.Temp(image), style);
}
// Make an on/off toggle button.
public static bool Toggle(Rect position, bool value, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
return DoToggle(position, GUIUtility.GetControlID(s_ToggleHash, FocusType.Passive, position), value, content, style);
}
public static bool Toggle(Rect position, int id, bool value, GUIContent content, GUIStyle style)
{
GUIUtility.CheckOnGUI();
return DoToggle(position, id, value, content, style);
}
public enum ToolbarButtonSize
{
Fixed,
FitToContents
};
public static int Toolbar(Rect position, int selected, string[] texts)
{
return Toolbar(position, selected, GUIContent.Temp(texts), s_Skin.button);
}
public static int Toolbar(Rect position, int selected, Texture[] images)
{
return Toolbar(position, selected, GUIContent.Temp(images), s_Skin.button);
}
public static int Toolbar(Rect position, int selected, GUIContent[] contents)
{
return Toolbar(position, selected, contents, s_Skin.button);
}
public static int Toolbar(Rect position, int selected, string[] texts, GUIStyle style)
{
return Toolbar(position, selected, GUIContent.Temp(texts), style);
}
public static int Toolbar(Rect position, int selected, Texture[] images, GUIStyle style)
{
return Toolbar(position, selected, GUIContent.Temp(images), style);
}
public static int Toolbar(Rect position, int selected, GUIContent[] contents, GUIStyle style)
{
return Toolbar(position, selected, contents, null, style, ToolbarButtonSize.Fixed);
}
// Make a toolbar
public static int Toolbar(Rect position, int selected, GUIContent[] contents, GUIStyle style, ToolbarButtonSize buttonSize)
{
return Toolbar(position, selected, contents, null, style, buttonSize);
}
internal static int Toolbar(Rect position, int selected, GUIContent[] contents, string[] controlNames, GUIStyle style, ToolbarButtonSize buttonSize, bool[] contentsEnabled = null)
{
GUIUtility.CheckOnGUI();
// Get the styles here
GUIStyle firstStyle, midStyle, lastStyle;
FindStyles(ref style, out firstStyle, out midStyle, out lastStyle, "left", "mid", "right");
return DoButtonGrid(position, selected, contents, controlNames, contents.Length, style, firstStyle, midStyle, lastStyle, buttonSize, contentsEnabled);
}
public static int SelectionGrid(Rect position, int selected, string[] texts, int xCount)
{
return SelectionGrid(position, selected, GUIContent.Temp(texts), xCount, null);
}
public static int SelectionGrid(Rect position, int selected, Texture[] images, int xCount)
{
return SelectionGrid(position, selected, GUIContent.Temp(images), xCount, null);
}
public static int SelectionGrid(Rect position, int selected, GUIContent[] content, int xCount)
{
return SelectionGrid(position, selected, content, xCount, null);
}
public static int SelectionGrid(Rect position, int selected, string[] texts, int xCount, GUIStyle style)
{
return SelectionGrid(position, selected, GUIContent.Temp(texts), xCount, style);
}
public static int SelectionGrid(Rect position, int selected, Texture[] images, int xCount, GUIStyle style)
{
return SelectionGrid(position, selected, GUIContent.Temp(images), xCount, style);
}
// Make a grid of buttons.
public static int SelectionGrid(Rect position, int selected, GUIContent[] contents, int xCount, GUIStyle style)
{
if (style == null) style = s_Skin.button;
return DoButtonGrid(position, selected, contents, null, xCount, style, style, style, style, ToolbarButtonSize.Fixed);
}
// Find many GUIStyles from style.name permutations (Helper function for toolbars).
internal static void FindStyles(ref GUIStyle style, out GUIStyle firstStyle, out GUIStyle midStyle, out GUIStyle lastStyle, string first, string mid, string last)
{
if (style == null)
style = GUI.skin.button;
string baseName = style.name;
midStyle = GUI.skin.FindStyle(baseName + mid) ?? style;
firstStyle = GUI.skin.FindStyle(baseName + first) ?? midStyle;
lastStyle = GUI.skin.FindStyle(baseName + last) ?? midStyle;
}
internal static int CalcTotalHorizSpacing(int xCount, GUIStyle style, GUIStyle firstStyle, GUIStyle midStyle, GUIStyle lastStyle)
{
if (xCount < 2)
return 0;
if (xCount == 2)
return Mathf.Max(firstStyle.margin.right, lastStyle.margin.left);
int internalSpace = Mathf.Max(midStyle.margin.left, midStyle.margin.right);
return Mathf.Max(firstStyle.margin.right, midStyle.margin.left) + Mathf.Max(midStyle.margin.right, lastStyle.margin.left) + internalSpace * (xCount - 3);
}
private static bool DoControl(Rect position, int id, bool on, bool hover, GUIContent content, GUIStyle style)
{
var evt = Event.current;
switch (evt.type)
{
case EventType.Repaint:
style.Draw(position, content, id, on, hover);
break;
case EventType.MouseDown:
if (position.Contains(evt.mousePosition))
{
GrabMouseControl(id);
evt.Use();
}
break;
case EventType.KeyDown:
bool anyModifiers = (evt.alt || evt.shift || evt.command || evt.control);
if ((evt.keyCode == KeyCode.Space || evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.KeypadEnter) && !anyModifiers && GUIUtility.keyboardControl == id)
{
evt.Use();
changed = true;
return !on;
}
break;
case EventType.MouseUp:
if (HasMouseControl(id))
{
ReleaseMouseControl();
evt.Use();
if (position.Contains(evt.mousePosition))
{
changed = true;
return !on;
}
}
break;
case EventType.MouseDrag:
if (HasMouseControl(id))
evt.Use();
break;
}
return on;
}
private static void DoLabel(Rect position, GUIContent content, GUIStyle style)
{
var evt = Event.current;
if (evt.type != EventType.Repaint)
return;
style.Draw(position, content, false, false, false, false);
// Is inside label AND inside guiclip visible rect (prevents tooltips on labels that are clipped)
if (!String.IsNullOrEmpty(content.tooltip) && position.Contains(evt.mousePosition) && GUIClip.visibleRect.Contains(evt.mousePosition))
GUIStyle.SetMouseTooltip(content.tooltip, position);
}