Skip to content

Commit a9abf62

Browse files
committed
v5.0.0
* Introduced FloatRange, IntRange, and DoubleRange structs * Introduced a range attributes for the above range-type structs * Removed MinMaxAttribute * Renamed some extension methods
1 parent 1c2d903 commit a9abf62

32 files changed

+1482
-122
lines changed

Editor/Attributes/MinMaxSliderDrawer.cs

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

Editor/VariableRanges.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using UnityEditor;
2+
using UnityEditor.UIElements;
3+
using UnityEngine;
4+
using UnityEngine.UIElements;
5+
6+
namespace StephanHooft.VariableRanges.EditorScripts
7+
{
8+
/// <summary>
9+
/// A custom property drawer for <see cref="DoubleRange"/>.
10+
/// </summary>
11+
[CustomPropertyDrawer(typeof(DoubleRange))]
12+
public class DoubleRangeDrawer : PropertyDrawer
13+
{
14+
#region Fields
15+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16+
#endregion
17+
#region PropertyDrawer Implementation
18+
19+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
20+
=> base.GetPropertyHeight(property, label);
21+
22+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
23+
{
24+
EditorGUI.BeginProperty(position, label, property);
25+
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Keyboard), label);
26+
var indent = EditorGUI.indentLevel;
27+
var labelWidth = EditorGUIUtility.labelWidth;
28+
EditorGUI.indentLevel = 0;
29+
EditorGUIUtility.labelWidth = 7;
30+
var rects = CreateRects(position, 7, 5);
31+
EditorGUI.BeginChangeCheck();
32+
var lowerProperty = property.FindPropertyRelative("lower");
33+
var upperProperty = property.FindPropertyRelative("upper");
34+
var lower = EditorGUI.FloatField(rects[0], lowerProperty.floatValue);
35+
EditorGUI.LabelField(rects[1], "-");
36+
var upper = EditorGUI.FloatField(rects[2], upperProperty.floatValue);
37+
if (lower > upper)
38+
upper = lower;
39+
if (EditorGUI.EndChangeCheck())
40+
{
41+
lowerProperty.floatValue = lower;
42+
upperProperty.floatValue = upper;
43+
}
44+
EditorGUIUtility.labelWidth = labelWidth;
45+
EditorGUI.indentLevel = indent;
46+
EditorGUI.EndProperty();
47+
}
48+
49+
public override VisualElement CreatePropertyGUI(SerializedProperty property)
50+
{
51+
var container = new VisualElement();
52+
var lowerField = new PropertyField(property.FindPropertyRelative("lower"));
53+
var upperField = new PropertyField(property.FindPropertyRelative("upper"));
54+
container.Add(lowerField);
55+
container.Add(upperField);
56+
return
57+
container;
58+
}
59+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60+
#endregion
61+
#region Methods
62+
63+
private Rect[] CreateRects(Rect controlRect, float labelWidth, float padding)
64+
{
65+
Rect[] rects = new Rect[3];
66+
var fieldWidth = (controlRect.width - ((padding * 2) + labelWidth)) / 2;
67+
rects[0] = new Rect(
68+
controlRect.x,
69+
controlRect.y,
70+
fieldWidth,
71+
controlRect.height
72+
);
73+
rects[1] = new Rect(
74+
controlRect.x + fieldWidth + padding,
75+
controlRect.y,
76+
labelWidth,
77+
controlRect.height
78+
);
79+
rects[2] = new Rect(
80+
controlRect.x + fieldWidth + (padding * 2) + labelWidth,
81+
controlRect.y,
82+
fieldWidth,
83+
controlRect.height
84+
);
85+
return
86+
rects;
87+
}
88+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89+
#endregion
90+
}
91+
}

Editor/Attributes/MinMaxSliderDrawer.cs.meta renamed to Editor/VariableRanges/DoubleRangeDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using UnityEditor;
2+
using UnityEditor.UIElements;
3+
using UnityEngine;
4+
using UnityEngine.UIElements;
5+
6+
namespace StephanHooft.VariableRanges.EditorScripts
7+
{
8+
/// <summary>
9+
/// A custom property drawer for <see cref="DoubleRangeMinMaxAttribute"/>.
10+
/// </summary>
11+
[CustomPropertyDrawer(typeof(DoubleRangeMinMaxAttribute))]
12+
public class DoubleRangeMinMaxAttributeDrawer : PropertyDrawer
13+
{
14+
#region Fields
15+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16+
#endregion
17+
#region PropertyDrawer Implementation
18+
19+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
20+
=> base.GetPropertyHeight(property, label);
21+
22+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
23+
{
24+
EditorGUI.BeginProperty(position, label, property);
25+
var attribute = (DoubleRangeMinMaxAttribute)base.attribute;
26+
var propertyType = property.type;
27+
if (propertyType == typeof(DoubleRange).Name)
28+
label.tooltip = string.Format("Range between {0} and {1}.",
29+
attribute.min.ToString("F2"), attribute.max.ToString("F2"));
30+
position = EditorGUI.PrefixLabel(position, label);
31+
var priorIndentLevel = EditorGUI.indentLevel;
32+
EditorGUI.indentLevel = 0;
33+
var splittedRect = CreateMinMaxSliderRects(position, 50, 5);
34+
if (propertyType == typeof(DoubleRange).Name)
35+
{
36+
EditorGUI.BeginChangeCheck();
37+
var lowerProperty = property.FindPropertyRelative("lower");
38+
var upperProperty = property.FindPropertyRelative("upper");
39+
var lower = (float)EditorGUI.DoubleField(splittedRect[0], double.Parse(lowerProperty.doubleValue.ToString("F2")));
40+
var upper = (float)EditorGUI.DoubleField(splittedRect[2], double.Parse(upperProperty.doubleValue.ToString("F2")));
41+
EditorGUI.MinMaxSlider(splittedRect[1], ref lower, ref upper, (float)attribute.min, (float)attribute.max);
42+
if (lower < attribute.min)
43+
lower = (float)attribute.min;
44+
if (upper > attribute.max)
45+
upper = (float)attribute.max;
46+
if (EditorGUI.EndChangeCheck())
47+
{
48+
lowerProperty.doubleValue = lower;
49+
upperProperty.doubleValue = upper;
50+
}
51+
}
52+
else
53+
throw
54+
new System.Exception(
55+
string.Format("{0} can only be assigned to {1}.",
56+
typeof(DoubleRangeMinMaxAttribute).Name, typeof(DoubleRange).Name));
57+
EditorGUI.indentLevel = priorIndentLevel;
58+
EditorGUI.EndProperty();
59+
}
60+
61+
public override VisualElement CreatePropertyGUI(SerializedProperty property)
62+
{
63+
var container = new VisualElement();
64+
var lowerField = new PropertyField(property.FindPropertyRelative("lower"));
65+
var upperField = new PropertyField(property.FindPropertyRelative("upper"));
66+
container.Add(lowerField);
67+
container.Add(upperField);
68+
return
69+
container;
70+
}
71+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72+
#endregion
73+
#region Methods
74+
75+
private Rect[] CreateMinMaxSliderRects(Rect controlRect, int doubleFieldWidth, int padding)
76+
{
77+
Rect[] rects = new Rect[3];
78+
rects[0] = new Rect(
79+
controlRect.x,
80+
controlRect.y,
81+
doubleFieldWidth,
82+
controlRect.height
83+
);
84+
rects[1] = new Rect(
85+
controlRect.x + doubleFieldWidth + padding,
86+
controlRect.y,
87+
controlRect.width - 2 * (doubleFieldWidth + padding),
88+
controlRect.height
89+
);
90+
rects[2] = new Rect(
91+
controlRect.x + (controlRect.width - doubleFieldWidth),
92+
controlRect.y,
93+
doubleFieldWidth,
94+
controlRect.height
95+
);
96+
return
97+
rects;
98+
}
99+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
100+
#endregion
101+
}
102+
}

Runtime/Attributes/MinMaxAttribute.cs.meta renamed to Editor/VariableRanges/DoubleRangeMinMaxAttributeDrawer.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)