-
Notifications
You must be signed in to change notification settings - Fork 6
/
LabelWidthScope.cs
83 lines (69 loc) · 2.44 KB
/
LabelWidthScope.cs
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
using System;
using UnityEditor;
using UnityEngine;
namespace Coimbra.Editor
{
/// <summary>
/// Scope for managing the <see cref="EditorGUIUtility.labelWidth"/>.
/// </summary>
public sealed class LabelWidthScope : GUI.Scope
{
public enum MagnitudeMode
{
/// <summary>
/// Will use "+=" and "-=" operators to modify the <see cref="EditorGUIUtility.labelWidth"/>. <see cref="LabelWidthScope.SavedMagnitude"/> will be the value to be used with these operators.
/// </summary>
Increment = 0,
/// <summary>
/// Will use "=" operator to modify the <see cref="EditorGUIUtility.labelWidth"/>. <see cref="LabelWidthScope.SavedMagnitude"/> will be the <see cref="EditorGUIUtility.labelWidth"/> before the scope.
/// </summary>
Absolute = 1,
}
/// <summary>
/// The value before entering this scope.
/// </summary>
public readonly float SavedMagnitude;
/// <summary>
/// The <see cref="MagnitudeMode"/> of this scope.
/// </summary>
public readonly MagnitudeMode CurrentMagnitudeMode;
public LabelWidthScope(float magnitude, MagnitudeMode magnitudeMode)
{
switch (CurrentMagnitudeMode = magnitudeMode)
{
case MagnitudeMode.Increment:
{
SavedMagnitude = magnitude;
EditorGUIUtility.labelWidth += magnitude;
break;
}
case MagnitudeMode.Absolute:
{
SavedMagnitude = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = magnitude;
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
protected override void CloseScope()
{
switch (CurrentMagnitudeMode)
{
case MagnitudeMode.Increment:
{
EditorGUIUtility.labelWidth -= SavedMagnitude;
break;
}
case MagnitudeMode.Absolute:
{
EditorGUIUtility.labelWidth = SavedMagnitude;
break;
}
default:
throw new ArgumentOutOfRangeException();
}
}
}
}