Skip to content

Commit be2bca4

Browse files
committed
Reimplement LabelWidthAttribute & Drawer as a Toolbox decorator
1 parent 9bdec64 commit be2bca4

File tree

7 files changed

+52
-46
lines changed

7 files changed

+52
-46
lines changed

Assets/Editor Toolbox/Editor/Drawers/Regular/LabelWidthAttributeDrawer.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/LabelAttributeDrawer.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,13 @@ namespace Toolbox.Editor.Drawers
77
{
88
public class LabelAttributeDrawer : ToolboxDecoratorDrawer<LabelAttribute>
99
{
10-
protected override void OnGuiBeginSafe(LabelAttribute attribute)
10+
private static GUIStyle GetLabelStyle(FontStyle style)
1111
{
12-
//prepare proper styles for the scope and text
13-
var scopeStyle = GetScopeStyle(attribute.SkinStyle);
14-
var labelStyle = GetLabelStyle(attribute.FontStyle);
15-
16-
GUILayout.Space(attribute.SpaceBefore);
17-
//create (optionally) the vertical scope group
18-
using (CreateScopeIfNeeded(scopeStyle))
12+
switch (style)
1913
{
20-
labelStyle.alignment = attribute.Alignment;
21-
labelStyle.fontStyle = attribute.FontStyle;
22-
EditorGUILayout.LabelField(GetContent(attribute), labelStyle);
14+
default:
15+
return Style.labelStyle;
2316
}
24-
25-
GUILayout.Space(attribute.SpaceAfter);
26-
}
27-
28-
29-
private static GUIStyle GetLabelStyle(FontStyle style)
30-
{
31-
return Style.labelStyle;
3217
}
3318

3419
private static GUIStyle GetScopeStyle(SkinStyle style)
@@ -41,9 +26,9 @@ private static GUIStyle GetScopeStyle(SkinStyle style)
4126
return Style.boxedScopeStyle;
4227
case SkinStyle.Round:
4328
return Style.roundScopeStyle;
29+
default:
30+
return Style.labelScopeStyle;
4431
}
45-
46-
return Style.labelScopeStyle;
4732
}
4833

4934
private static GUIContent GetContent(LabelAttribute attribute)
@@ -72,6 +57,23 @@ private static IDisposable CreateScopeIfNeeded(GUIStyle style)
7257
return style != null ? new EditorGUILayout.VerticalScope(style) : null;
7358
}
7459

60+
protected override void OnGuiBeginSafe(LabelAttribute attribute)
61+
{
62+
//prepare proper styles for the scope and text
63+
var scopeStyle = GetScopeStyle(attribute.SkinStyle);
64+
var labelStyle = GetLabelStyle(attribute.FontStyle);
65+
66+
GUILayout.Space(attribute.SpaceBefore);
67+
//create (optionally) the vertical scope group
68+
using (CreateScopeIfNeeded(scopeStyle))
69+
{
70+
labelStyle.alignment = attribute.Alignment;
71+
labelStyle.fontStyle = attribute.FontStyle;
72+
EditorGUILayout.LabelField(GetContent(attribute), labelStyle);
73+
}
74+
75+
GUILayout.Space(attribute.SpaceAfter);
76+
}
7577

7678
private static class Style
7779
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Drawers
5+
{
6+
public class LabelWidthAttributeDrawer : ToolboxDecoratorDrawer<LabelWidthAttribute>
7+
{
8+
private float widthToRestore;
9+
10+
protected override void OnGuiBeginSafe(LabelWidthAttribute attribute)
11+
{
12+
base.OnGuiBeginSafe(attribute);
13+
widthToRestore = EditorGUIUtility.labelWidth;
14+
EditorGUIUtility.labelWidth = attribute.Width;
15+
}
16+
17+
protected override void OnGuiCloseSafe(LabelWidthAttribute attribute)
18+
{
19+
EditorGUIUtility.labelWidth = widthToRestore;
20+
base.OnGuiCloseSafe(attribute);
21+
}
22+
}
23+
}

Assets/Editor Toolbox/Runtime/Attributes/Property/Regular/LabelWidthAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace UnityEngine
1010
/// </summary>
1111
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
1212
[Conditional("UNITY_EDITOR")]
13-
public class LabelWidthAttribute : PropertyAttribute
13+
public class LabelWidthAttribute : ToolboxDecoratorAttribute
1414
{
1515
public LabelWidthAttribute(float width = 120)
1616
{

Assets/Examples/Scripts/SampleBehaviour1.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,4 @@ public enum FlagExample
113113
public int bigNumber;
114114
[FormattedNumber("c")]
115115
public float currency;
116-
117-
[Label("Label Width", skinStyle: SkinStyle.Box)]
118-
119-
[LabelWidth(220.0f)]
120-
public int veryVeryVeryVeryVeryLongName;
121116
}

Assets/Examples/Scripts/SampleBehaviour4.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ private static void TestStaticMethod()
121121
[GuiColor(1, 0, 0)]
122122
public int var56;
123123

124+
[Label("Label Width", skinStyle: SkinStyle.Box)]
125+
126+
[LabelWidth(220.0f)]
127+
public int veryVeryVeryVeryVeryLongName;
128+
124129
[Label("Title", skinStyle: SkinStyle.Box)]
125130

126131
[Title("Standard Title")]

0 commit comments

Comments
 (0)