Skip to content

Commit 889a8ff

Browse files
committed
Add DynamicMinMaxSliderAttribute&Drawer
1 parent 95a3563 commit 889a8ff

7 files changed

Lines changed: 115 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
1919
var yValue = property.vector2Value.y;
2020

2121
label = EditorGUI.BeginProperty(position, label, property);
22+
EditorGUI.BeginChangeCheck();
2223
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
2324
if (EditorGUI.EndChangeCheck())
2425
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Toolbox.Editor.Drawers
7+
{
8+
public class DynamicMinMaxSliderAttributeDrawer : ToolboxSelfPropertyDrawer<DynamicMinMaxSliderAttribute>
9+
{
10+
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, DynamicMinMaxSliderAttribute attribute)
11+
{
12+
var minValueSource = attribute.MinValueSource;
13+
var maxValueSource = attribute.MaxValueSource;
14+
if (!ValueExtractionHelper.TryGetValue(minValueSource, property, out var minValueCandidate, out _) ||
15+
!ValueExtractionHelper.TryGetValue(maxValueSource, property, out var maxValueCandidate, out _))
16+
{
17+
ToolboxEditorLog.MemberNotFoundWarning(attribute, property,
18+
string.Format("{0} or {1}", minValueSource, maxValueSource));
19+
base.OnGuiSafe(property, label, attribute);
20+
return;
21+
}
22+
23+
var maxValue = 0.0f;
24+
var minValue = 0.0f;
25+
try
26+
{
27+
minValue = Convert.ToSingle(minValueCandidate);
28+
maxValue = Convert.ToSingle(maxValueCandidate);
29+
}
30+
catch (Exception e) when (e is InvalidCastException || e is FormatException)
31+
{
32+
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
33+
string.Format("Invalid source types, cannot convert them to {0}", typeof(float)));
34+
}
35+
36+
var xValue = property.vector2Value.x;
37+
var yValue = property.vector2Value.y;
38+
ToolboxEditorGui.BeginProperty(property, ref label, out var position);
39+
EditorGUI.BeginChangeCheck();
40+
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
41+
if (EditorGUI.EndChangeCheck())
42+
{
43+
property.vector2Value = new Vector2(xValue, yValue);
44+
}
45+
46+
ToolboxEditorGui.CloseProperty();
47+
}
48+
49+
50+
public override bool IsPropertyValid(SerializedProperty property)
51+
{
52+
return property.propertyType == SerializedPropertyType.Vector2;
53+
}
54+
}
55+
}

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelfDrawers/DynamicMinMaxSliderAttributeDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,5 +588,23 @@ public static void DrawEmptyProperty(Rect position, SerializedProperty property,
588588
#endif
589589
EditorGUI.LabelField(position, label);
590590
}
591+
592+
/// <summary>
593+
/// Layout-based equivalent of the <see cref="EditorGUI.BeginProperty(Rect, GUIContent, SerializedProperty)"/> method.
594+
/// </summary>
595+
public static void BeginProperty(SerializedProperty property, ref GUIContent label, out Rect position)
596+
{
597+
var rowHeight = EditorGUIUtility.singleLineHeight;
598+
position = EditorGUILayout.GetControlRect(true, rowHeight);
599+
label = EditorGUI.BeginProperty(position, label, property);
600+
}
601+
602+
/// <summary>
603+
/// Layout-based equivalent of the <see cref="EditorGUI.EndProperty"/> method.
604+
/// </summary>
605+
public static void CloseProperty()
606+
{
607+
EditorGUI.EndProperty();
608+
}
591609
}
592610
}

Assets/Editor Toolbox/EditorSettings.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ MonoBehaviour:
7878
- classReference: Toolbox.Editor.Drawers.ShowIfAttributeDrawer, Toolbox-Editor
7979
- classReference: Toolbox.Editor.Drawers.ShowWarningIfAttributeDrawer, Toolbox-Editor
8080
selfPropertyDrawerHandlers:
81+
- classReference: Toolbox.Editor.Drawers.DynamicMinMaxSliderAttributeDrawer, Toolbox-Editor
8182
- classReference: Toolbox.Editor.Drawers.IgnoreParentAttributeDrawer, Toolbox-Editor
8283
- classReference: Toolbox.Editor.Drawers.InLineEditorAttributeDrawer, Toolbox-Editor
8384
- classReference: Toolbox.Editor.Drawers.RegexValueAttributeDrawer, Toolbox-Editor
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace UnityEngine
4+
{
5+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
6+
public class DynamicMinMaxSliderAttribute : ToolboxSelfPropertyAttribute
7+
{
8+
public DynamicMinMaxSliderAttribute(string minValueSource, string maxValueSource)
9+
{
10+
MinValueSource = minValueSource;
11+
MaxValueSource = maxValueSource;
12+
}
13+
14+
public string MinValueSource { get; private set; }
15+
16+
public string MaxValueSource { get; private set; }
17+
}
18+
}

Assets/Editor Toolbox/Scripts/Attributes/ToolboxAttributes/PropertySelfAttributes/DynamicMinMaxSliderAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)