-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreGUI.cs
More file actions
837 lines (695 loc) · 25.7 KB
/
CoreGUI.cs
File metadata and controls
837 lines (695 loc) · 25.7 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
using System;
using System.Collections.Generic;
using UnityEngine;
public static partial class CoreGUI
{
#region Prefix and Indentation
public static float prefixLabelWidth = 100f;
public static Side prefixLabelSide = Side.Left;
static Stack<GUIContent> prefixLabels = new Stack<GUIContent>();
static void PrefixLabelStart(GUIContent label)
{
if (label != null)
{
bool wasVertical = LayoutUtility.isVertical;
LayoutGroup g = LayoutUtility.BeginLayoutGroup<LayoutGroup>(GUIStyle.none, layoutOptions);
g.isVertical = prefixLabelSide >= Side.Top;
if (prefixLabelSide == Side.Left)
{
PrefixLabel(Space(wasVertical ? 0 : prefixLabelWidth), label);
}
else if (prefixLabelSide == Side.Top)
{
PrefixLabel(Space(wasVertical ? 0 : prefixLabelWidth), label);
}
else if (ev.type == EventType.Repaint)
{
// in case it's copied from C, we need to copy it out from cache.
label = new GUIContent(label);
}
if ((indentLevel > 0) && (prefixLabelSide == Side.Bottom || prefixLabelSide == Side.Top))
{
LayoutUtility.BeginLayoutGroup<LayoutGroup>(GUIStyle.none, layoutOptions).isVertical = false;
Space(indentLevel * 16);
}
}
prefixLabels.Push(label);
}
static void PrefixLabelEnd()
{
var label = prefixLabels.Pop();
if (label != null)
{
if ((indentLevel > 0) && (prefixLabelSide == Side.Bottom || prefixLabelSide == Side.Top))
{
LayoutUtility.EndLayoutGroup();
}
if (prefixLabelSide == Side.Right)
{
PrefixLabel(Space(0), label);
}
else if (prefixLabelSide == Side.Bottom)
{
PrefixLabel(Space(0), label);
}
LayoutUtility.EndLayoutGroup();
}
}
/// <summary>
/// Reserve position and do PrefixLabel from there
/// </summary>
public static Rect PrefixLabel(GUIContent content, GUIStyle style, GUIContent label, int id = 0)
{
if (id == 0)
id = GUIUtility.GetControlID(FocusType.Passive) + 1; // Educated guess
var r = Reserve(content ?? GUIContent.none, style = style ?? GUIStyle.none);
// Layout.GetRect accounts style margin, so we need to take it out.
r = style.margin.Add(r);
r = PrefixLabel(r, label, id);
r = style.margin.Remove(r);
return r;
}
public static Rect PrefixLabel(Rect totalPosition, GUIContent label, int id = 0)
{
if (label != null && prefixLabelWidth > 0)
{
if (ev.type == EventType.Repaint || ev.type == EventType.MouseUp)
{
Rect r = CalculatePrefixedRect(totalPosition, label);
if (ev.type == EventType.Repaint)
{
Utility.DrawStyle(r, label, Styles.Prefix, id, false);
}
else if (r.Contains(ev.mousePosition) && id != 0)
{
GUIUtility.keyboardControl = id;
}
}
}
if (indentLevel > 0 || label != null)
{
bool shouldEat = LayoutUtility.isVertical != prefixLabelSide >= Side.Top;
float offset = indentLevel * 16;
float width = label != null ? prefixLabelWidth : offset;
if (!shouldEat && width > 0)
Space(width);
switch (prefixLabelSide)
{
case Side.Left:
if (shouldEat) totalPosition.xMin += width;
else totalPosition.x += width;
break;
case Side.Right:
if (shouldEat) totalPosition.xMax -= width;
break;
case Side.Top:
if (shouldEat) totalPosition.yMin += width;
else totalPosition.y += width;
totalPosition.xMin += offset;
break;
case Side.Bottom:
if (shouldEat) totalPosition.yMax -= width;
totalPosition.xMin += offset;
break;
}
}
return totalPosition;
}
private static Rect CalculatePrefixedRect(Rect totalPosition, GUIContent label)
{
bool shouldEat = LayoutUtility.isVertical != prefixLabelSide >= Side.Top;
var r = totalPosition;
var indent = indentLevel * 16;
switch (prefixLabelSide)
{
case Side.Left:
r.x += indent;
if (!shouldEat)
{
r.height = LayoutUtility.GetContainerRect(true).height;
}
r.width = prefixLabelWidth - indent;
break;
case Side.Right:
if (!shouldEat)
{
r.height = LayoutUtility.GetContainerRect(true).height;
r.width = prefixLabelWidth;
}
r.x += r.width - prefixLabelWidth;
r.width = prefixLabelWidth - indent;
break;
case Side.Top:
if (!shouldEat)
{
r.width = LayoutUtility.GetContainerRect().width;
r.height = prefixLabelWidth;
}
r.xMin += indent;
break;
case Side.Bottom:
r.y += r.height;
if (!shouldEat)
{
r.width = LayoutUtility.GetContainerRect().width;
r.height = prefixLabelWidth;
}
r.xMin += indent;
break;
}
return r;
}
public static Rect PrefixFoldout(Rect totalPosition, GUIContent label, ref bool expanded, int id = -1)
{
var r2 = CalculatePrefixedRect(totalPosition, label);
var r = PrefixLabel(totalPosition, GUIContent.none, id);
expanded = GUI.Toggle(r2, expanded, label, Styles.Foldout);
return r;
}
public static Rect PrefixSlider(Rect totalPosition, GUIContent label, int id, out float delta)
{
if (id == 0) id = GUIUtility.GetControlID(FocusType.Passive);
delta = 0;
var r2 = CalculatePrefixedRect(totalPosition, label);
var r = PrefixLabel(totalPosition, label, id + 1);
if (ev.type == EventType.MouseDown && r2.Contains(ev.mousePosition))
GUIUtility.hotControl = id;
else if (ev.type == EventType.MouseDrag && GUIUtility.hotControl == id)
delta = ev.delta.x;
else if (ev.type == EventType.MouseUp && GUIUtility.hotControl == id)
GUIUtility.hotControl = 0;
return r;
}
public static int indentLevel = 0;
public static LayoutOption[] layoutOptions = new LayoutOption[] { };
public static Rect Indent(Rect r, bool ignorePolicy = false)
{
r.xMin += indentLevel * 16;
return r;
}
public static Rect Reserve(GUIContent content = null, GUIStyle style = null)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
return LayoutUtility.GetRect(content ?? GUIContent.none, style ?? GUI.skin.label, layoutOptions);
}
public static Rect Reserve(float aspect)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
return LayoutUtility.GetAspectRect(aspect);
}
public static Rect Reserve(Vector2 size)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
return LayoutUtility.GetRect(size.x, size.y, layoutOptions);
}
public static Rect Reserve(Rect minMax)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
return LayoutUtility.GetRect(minMax.xMin, minMax.xMax, minMax.yMin, minMax.yMax, layoutOptions);
}
/// <summary>
/// Reserve fixed space
/// </summary>
public static Rect Space(float pixels)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
Rect r, cont = LayoutUtility.GetContainerRect();
if (LayoutUtility.isVertical)
{
var arr = ArrayPool<LayoutOption>.Get(Layout.Height(pixels));
r = LayoutUtility.GetRect(0, pixels, LayoutUtility.spaceStyle, arr);
ArrayPool<LayoutOption>.Release(arr);
r.x = cont.x;
r.width = cont.width;
}
else
{
var arr = ArrayPool<LayoutOption>.Get(Layout.Width(pixels));
r = LayoutUtility.GetRect(pixels, 0, LayoutUtility.spaceStyle, arr);
ArrayPool<LayoutOption>.Release(arr);
r.y = cont.y;
r.height = cont.height;
}
return r;
}
/// <summary>
/// Reserve flexible/rest of empty space
/// </summary>
public static Rect FlexibleSpace()
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
Rect r, cont = LayoutUtility.GetContainerRect();
if (LayoutUtility.isVertical)
{
var arr = ArrayPool<LayoutOption>.Get(Layout.ExpandHeight(true));
r = LayoutUtility.GetRect(0, 0, GUIStyle.none, arr);
ArrayPool<LayoutOption>.Release(arr);
r.x = cont.x;
r.width = cont.width;
}
else
{
var arr = ArrayPool<LayoutOption>.Get(Layout.ExpandWidth(true));
r = LayoutUtility.GetRect(0, 0, GUIStyle.none, arr);
ArrayPool<LayoutOption>.Release(arr);
r.y = cont.y;
r.height = cont.height;
}
return r;
}
#endregion
#region C
static GUIContent _cachedTextGUI = new GUIContent();
static GUIContent _cachedTextTooltipGUI = new GUIContent();
static GUIContent _cachedTextureGUI = new GUIContent();
static GUIContent _cachedTextTooltipTextureGUI = new GUIContent();
public static GUIContent C(string text)
{
_cachedTextGUI.text = text;
return _cachedTextGUI;
}
public static GUIContent C(string text, string tooltip)
{
_cachedTextTooltipGUI.text = text;
_cachedTextTooltipGUI.tooltip = tooltip;
return _cachedTextTooltipGUI;
}
public static GUIContent C(Texture image)
{
_cachedTextureGUI.image = image;
return _cachedTextureGUI;
}
public static GUIContent C(Texture image, string text, string tooltip = "")
{
_cachedTextTooltipTextureGUI.image = image;
_cachedTextTooltipTextureGUI.text = text;
_cachedTextTooltipTextureGUI.tooltip = tooltip;
return _cachedTextGUI;
}
#endregion
#region Layouts
public static Rect BeginHorizontal(GUIContent label = null)
{
return BeginHorizontal(label, Utility.emptyLayoutOption);
}
public static Rect BeginHorizontal(GUIContent label, params LayoutOption[] options)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
PrefixLabelStart(label);
BeginIndent(0);
{
LayoutGroup g = LayoutUtility.BeginLayoutGroup<LayoutGroup>(GUIStyle.none, options);
g.isVertical = false;
return g.rect;
}
}
public static void EndHorizontal()
{
if (Utility.currentCustomEvent != null) return;
LayoutUtility.EndLayoutGroup();
EndIndent();
PrefixLabelEnd();
}
public static Rect BeginVertical(GUIContent label = null)
{
return BeginVertical(label, Utility.emptyLayoutOption);
}
public static Rect BeginVertical(GUIContent label = null, params LayoutOption[] options)
{
if (Utility.currentCustomEvent != null) return LayoutUtility.kDummyRect;
PrefixLabelStart(label);
BeginIndent(0);
{
LayoutGroup g = LayoutUtility.BeginLayoutGroup<LayoutGroup>(GUIStyle.none, options);
g.isVertical = true;
return g.rect;
}
}
public static void EndVertical()
{
if (Utility.currentCustomEvent != null) return;
LayoutUtility.EndLayoutGroup();
EndIndent();
PrefixLabelEnd();
}
public static void BeginScrollView(ref Vector2 scroll, bool alwaysShowHorizontal = false, bool alwaysShowVertical = false)
{
scroll = BeginScrollView(scroll, alwaysShowHorizontal, alwaysShowVertical, Utility.emptyLayoutOption);
}
public static void BeginScrollView(ref Vector2 scroll, bool alwaysShowHorizontal = false, bool alwaysShowVertical = false, params LayoutOption[] options)
{
scroll = BeginScrollView(scroll, alwaysShowHorizontal, alwaysShowVertical, options);
}
public static Vector2 BeginScrollView(Vector2 scroll, bool alwaysShowHorizontal = false, bool alwaysShowVertical = false)
{
return BeginScrollView(scroll, alwaysShowHorizontal, alwaysShowVertical, Utility.emptyLayoutOption);
}
public static Vector2 BeginScrollView(Vector2 scroll, bool alwaysShowHorizontal, bool alwaysShowVertical, params LayoutOption[] options)
{
if (Utility.currentCustomEvent != null) return scroll;
ScrollGroup g = (ScrollGroup)LayoutUtility.BeginLayoutGroup<ScrollGroup>(GUIStyle.none, options);
switch (ev.type)
{
case EventType.Layout:
g.resetCoords = true;
g.isVertical = true;
g.stretchWidth = 1;
g.stretchHeight = 1;
g.verticalScrollbar = Styles.VerticalScrollbar;
g.horizontalScrollbar = Styles.HorizontalScrollbar;
g.needsVerticalScrollbar = alwaysShowVertical;
g.needsHorizontalScrollbar = alwaysShowHorizontal;
g.ApplyOptions(layoutOptions);
break;
default:
break;
}
return GUI.BeginScrollView(g.rect, scroll, new Rect(0, 0, g.clientWidth, g.clientHeight),
alwaysShowHorizontal, alwaysShowVertical, Styles.HorizontalScrollbar, Styles.VerticalScrollbar);
}
public static Vector2 DragScrollView(Vector2 scroll)
{
if (ev.type == EventType.MouseDrag && LayoutUtility.GetContainerRect().Contains(ev.mousePosition - scroll))
{
// Drag the scroll
#if UNITY_EDITOR || !(UNITY_ANDROID || UNITY_IOS)
scroll -= ev.delta;
#else
// Don't know why in actual mobile this Y got inverted. Bug?
scroll -= new Vector2(ev.delta.x, -ev.delta.y);
#endif
ev.Use();
}
return scroll;
}
public static void EndScrollView()
{
if (Utility.currentCustomEvent != null) return;
LayoutUtility.EndLayoutGroup();
GUI.EndScrollView();
}
public static void BeginArea(Rect screenRect)
{
if (Utility.currentCustomEvent != null) return;
LayoutGroup g = LayoutUtility.BeginLayoutArea<LayoutGroup>(GUIStyle.none);
if (ev.type == EventType.Layout)
{
g.resetCoords = true;
g.minWidth = g.maxWidth = screenRect.width;
g.minHeight = g.maxHeight = screenRect.height;
g.rect = Rect.MinMaxRect(screenRect.xMin, screenRect.yMin, g.rect.xMax, g.rect.yMax);
}
GUI.BeginGroup(g.rect, GUIContent.none, GUIStyle.none);
}
public static void EndArea()
{
if (Utility.currentCustomEvent != null) return;
LayoutUtility.current.layoutGroups.Pop();
LayoutUtility.current.topLevel = LayoutUtility.current.layoutGroups.Peek();
GUI.EndGroup();
}
#endregion
#region GUI Begin and End
internal static Dictionary<int, Queue<Event>> _pendingEvents = new Dictionary<int, Queue<Event>>();
internal static Event _currentCustomEvent = null;
internal static Dictionary<int, Event> _currentCustomEvents = new Dictionary<int, Event>();
static bool _isEditorWindow = false;
static int _currentGUIID = 0;
static float _currentScale = 1f;
static GUISkin _oldSkin = null;
public static void BeginGUI(UnityEngine.Object obj)
{
BeginGUI(obj, Utility.screenRect);
}
public static void BeginGUI(UnityEngine.Object obj, Rect position, GUISkin skin = null, float scale = 1)
{
BeginGUI(obj.GetInstanceID(), position, skin, scale,
#if UNITY_EDITOR
obj is UnityEditor.EditorWindow || obj is UnityEditor.Editor
#else
false
#endif
);
}
public static void BeginGUI(int id, Rect position, GUISkin skin = null, float scale = 1, bool isEditorWindow = false)
{
if (id == 0) throw new ArgumentException("GUIID should never be zero");
if (_currentCustomEvents.ContainsKey(id))
{
if (ev.type != EventType.Layout)
{
// If you're looking why the GUI flashing during custom event
// (such as after selecting menu popups or closing dialogs)
// This is the why. The runtime GUI has NO WAY to actually
// sending custom event. We can only 'eats' it. Including repaint event.
// We'll looking for better option if found.
GUIUtility.ExitGUI();
}
else
{
_currentCustomEvents.Remove(id);
}
}
if (ev.type != EventType.Repaint)
{
Queue<Event> pends;
if (_pendingEvents.TryGetValue(id, out pends) && pends.Count > 0)
{
// Lets hijack this event
Event.current = _currentCustomEvent = _currentCustomEvents[id] = pends.Dequeue();
}
}
if (skin)
{
_oldSkin = GUI.skin;
GUI.skin = skin;
}
_isEditorWindow = isEditorWindow;
LayoutUtility.Begin(_currentGUIID = id);
if ((_currentScale = scale) != 1f)
{
// Activate scaling
GUI.matrix = Matrix4x4.Scale(new Vector3(scale, scale, 1));
var pos = new Rect(position.position / scale, position.size / scale);
BeginArea(pos);
}
else
BeginArea(position);
}
public static void EndGUI()
{
EndArea();
if (ev.type != EventType.Used && PopupBase.shownPopup.ContainsKey(Utility.currentGUIID))
{
PopupBase.shownPopup[Utility.currentGUIID].OnGUI();
}
if (Utility.delayCall != null)
{
Utility.delayCall();
Utility.delayCall = null;
}
if (indentLevels.Count > 0)
{
Debug.LogError("WARNING: You're pushing more indent stacks than popping it");
while (indentLevels.Count > 0)
{
EndIndent();
}
}
if (LayoutUtility.current.layoutGroups.Count > 1)
{
if (ev.type != EventType.Used)
Debug.LogError("WARNING: You're pushing more layout stacks than popping it");
while (LayoutUtility.current.layoutGroups.Count > 1)
{
LayoutUtility.current.layoutGroups.Pop();
}
}
if (ev.type == EventType.Layout)
LayoutUtility.Layout();
if (_currentCustomEvent != null)
Event.current = _currentCustomEvent = null; // Reset to master event
if (_oldSkin)
{
GUI.skin = _oldSkin;
_oldSkin = null;
}
GUI.matrix = Matrix4x4.identity;
}
public static void DrawTooltips(float maxWidth = 300f)
{
if (ev.type != EventType.Repaint) return;
var text = GUI.tooltip;
if (!string.IsNullOrEmpty(text))
{
Vector2 offset = new Vector2(4, 0);
var style = Styles.Tooltip;
var gui = C(text);
var size = style.CalcSize(gui);
if (size.x > maxWidth)
size = new Vector2(maxWidth, style.CalcHeight(gui, maxWidth));
var pos = ev.mousePosition + offset;
var limit = Utility.scaledScreenRect.size - size;
if (pos.x > limit.x)
pos.x -= size.x + offset.x * 2;
if (pos.y > limit.y)
pos.y -= size.y + offset.y * 2;
size.x += 1;
style.Draw(new Rect(pos, size), gui, 0);
}
}
#endregion
#region Behaviour Blocks
static Stack<bool> changeChecks = new Stack<bool>();
public static void BeginChangeCheck()
{
changeChecks.Push(GUI.changed);
GUI.changed = false;
}
public static bool EndChangeCheck()
{
bool changed = GUI.changed;
GUI.changed |= changeChecks.Pop();
return changed;
}
static Stack<LayoutOption[]> layoutStacks = new Stack<LayoutOption[]>();
public static void BeginLayoutOption(params LayoutOption[] options)
{
layoutStacks.Push(layoutOptions);
layoutOptions = options;
}
public static void EndLayoutOption()
{
layoutOptions = layoutStacks.Pop();
}
static Dictionary<int, float> _fadeTimes = new Dictionary<int, float>();
public static bool BeginFadeGroup(bool val)
{
var id = GUIUtility.GetControlID(FocusType.Passive);
var fade = _fadeTimes.ContainsKey(id) ? _fadeTimes[id] : _fadeTimes[id] = (val ? 1 : 0);
if (!val && fade <= 0)
{
BeginFadeGroup(0);
return false;
}
else if (val && fade >= 1)
{
BeginFadeGroup(1);
return true;
}
else
{
if (ev.type == EventType.Layout)
{
// Standard duration for animated foldout is 0.4 seconds
fade += (val ? 1 : -1) * Time.unscaledDeltaTime * 2.5f;
fade = Mathf.Clamp(fade, 0, 1);
_fadeTimes[id] = fade;
}
if (val)
{
var f = 1 - fade;
BeginFadeGroup(1 - f * f * f * f);
}
else
BeginFadeGroup(fade * fade * fade * fade);
return fade > 0;
}
}
public static bool BeginFadeGroup(float value)
{
if (Utility.currentCustomEvent != null) return value > 0;
// Fade groups interfere with layout even when the fade group is collapsed because
// the margins of the elements before and after are added together instead of overlapping.
// This creates unwanted extra space between controls if there's an inactive fade group in between.
// We avoid this by simply not having the fade group if it's collapsed.
if (value == 0)
{
return false;
}
// Active fade groups interfere with styles that have overflow, because the overflow part gets clipped.
// We avoid this by simply not having the fade group if it's fully expanded.
if (value == 1)
{
return true;
}
LayoutFadeGroup g = (LayoutFadeGroup)LayoutUtility.BeginLayoutGroup<LayoutFadeGroup>(GUIStyle.none, null);
g.isVertical = true;
g.resetCoords = true;
g.fadeValue = value;
g.wasGUIEnabled = GUI.enabled;
g.guiColor = GUI.color;
if (value != 0.0f && value != 1.0f && ev.type == EventType.MouseDown)
{
ev.Use();
}
// We don't want the fade group gui clip to be used for calculating the label width of controls in this fade group, so we lock the context width.
//EditorGUIUtility.LockContextWidth();
GUI.BeginGroup(g.rect);
return value != 0;
}
public static void EndFadeGroup()
{
if (Utility.currentCustomEvent != null) return;
// If we're inside a fade group, end it here.
// See BeginFadeGroup for details on why it's not always present.
LayoutFadeGroup g = LayoutUtility.topLevel as LayoutFadeGroup;
if (g != null)
{
GUI.EndGroup();
//EditorGUIUtility.UnlockContextWidth();
GUI.enabled = g.wasGUIEnabled;
GUI.color = g.guiColor;
LayoutUtility.EndLayoutGroup();
}
}
static Stack<bool> enabledStacks = new Stack<bool>();
public static void BeginDisabledGroup(bool disabled, bool overriding = false)
{
enabledStacks.Push(GUI.enabled);
if (overriding)
GUI.enabled = !disabled;
else
GUI.enabled &= !disabled;
}
public static void EndDisabledGroup()
{
GUI.enabled = enabledStacks.Pop();
}
static Stack<int> indentLevels = new Stack<int>();
public static void BeginIndent(int indent = -1)
{
indentLevels.Push(indentLevel);
if (indent >= 0)
indentLevel = indent;
else
indentLevel++;
}
public static void EndIndent()
{
var val = indentLevels.Pop();
indentLevel = val;
}
static Stack<ValueTuple<float, Side>> labelStacks = new Stack<ValueTuple<float, Side>>();
public static void BeginLabelOption(Side side)
{
BeginLabelOption(-1, side);
}
public static void BeginLabelOption(float width = -1, Side side = Side.Inherit)
{
labelStacks.Push(new ValueTuple<float, Side>(prefixLabelWidth, prefixLabelSide));
if (side != Side.Inherit)
prefixLabelSide = side;
if (width >= 0)
prefixLabelWidth = width;
if (side == Side.Bottom || side == Side.Top)
prefixLabelWidth = GUI.skin.label.CalcHeight(C(" "), 60);
}
public static void EndLabelOption()
{
var o = labelStacks.Pop();
prefixLabelWidth = o.item1;
prefixLabelSide = o.item2;
}
#endregion
}