Skip to content

Commit 9dabb40

Browse files
committed
Minor refactor changes; possibility to use EditorButtonAttribute and DynamicHelpAttribute in nested types; implement AnimationCurveSettingsAttribute & Drawer
1 parent 99baede commit 9dabb40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+361
-135
lines changed

Assets/Editor Toolbox/Editor/Drawers/Helpers/Extraction/ValueExtractionHelper.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
43
using UnityEditor;
54

65
namespace Toolbox.Editor.Drawers
@@ -68,14 +67,8 @@ public static bool TryGetValue(string source, SerializedProperty causer, out obj
6867

6968
public static bool TryGetValue(string source, SerializedProperty causer, out object value, out bool hasMixedValues, Func<object, object, bool> nextValuesComparer)
7069
{
71-
var targetObjects = causer.serializedObject.targetObjects;
72-
var parentObjects = new object[targetObjects.Length];
73-
for (var i = 0; i < targetObjects.Length; i++)
74-
{
75-
var targetObject = targetObjects[i];
76-
parentObjects[i] = causer.GetDeclaringObject(targetObject);
77-
}
78-
70+
//NOTE: consider using NonAlloc implementation
71+
var parentObjects = causer.GetDeclaringObjects();
7972
return TryGetValue(source, parentObjects, out value, out hasMixedValues, nextValuesComparer);
8073
}
8174
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Reflection;
3+
using UnityEditor;
4+
5+
namespace Toolbox.Editor.Drawers
6+
{
7+
public interface ISerializedPropertyContext
8+
{
9+
SerializedProperty Property { get; }
10+
FieldInfo FieldInfo { get; }
11+
Type Type { get; }
12+
}
13+
}

Assets/Editor Toolbox/Editor/ToolboxDrawerModule.cs.meta renamed to Assets/Editor Toolbox/Editor/Drawers/ISerializedPropertyContext.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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Drawers
5+
{
6+
[CustomPropertyDrawer(typeof(AnimationCurveSettingsAttribute))]
7+
public class AnimationCurveSettingsAttributeDrawer : PropertyDrawerBase
8+
{
9+
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
10+
{
11+
var attribute = Attribute;
12+
var curveRanges = new Rect(
13+
attribute.Min.x,
14+
attribute.Min.y,
15+
attribute.Max.x - attribute.Min.x,
16+
attribute.Max.y - attribute.Min.y);
17+
18+
var color = attribute.Color;
19+
20+
EditorGUI.BeginProperty(position, label, property);
21+
EditorGUI.CurveField(position, property, color, curveRanges, label);
22+
EditorGUI.EndProperty();
23+
}
24+
25+
public override bool IsPropertyValid(SerializedProperty property)
26+
{
27+
return base.IsPropertyValid(property) && property.propertyType == SerializedPropertyType.AnimationCurve;
28+
}
29+
30+
private AnimationCurveSettingsAttribute Attribute => attribute as AnimationCurveSettingsAttribute;
31+
}
32+
}

Assets/Editor Toolbox/Editor/Drawers/Regular/AnimationCurveSettingsAttributeDrawer.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/Drawers/Regular/AssetPreviewAttributeDrawer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,13 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
9999
}
100100
}
101101

102-
103102
public override bool IsPropertyValid(SerializedProperty property)
104103
{
105104
return property.propertyType == SerializedPropertyType.ObjectReference;
106105
}
107106

108-
109107
private AssetPreviewAttribute Attribute => attribute as AssetPreviewAttribute;
110108

111-
112109
private static class Style
113110
{
114111
internal static readonly float offset = 6.0f;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ protected virtual void OnGUISafe(Rect position, SerializedProperty property, GUI
2626
EditorGUI.PropertyField(position, property, label);
2727
}
2828

29-
3029
/// <summary>
3130
/// Native call to return the expected height.
3231
/// </summary>
33-
public override sealed float GetPropertyHeight(SerializedProperty property, GUIContent label)
32+
public sealed override float GetPropertyHeight(SerializedProperty property, GUIContent label)
3433
{
3534
return IsPropertyValid(property)
3635
? GetPropertyHeightSafe(property, label)
@@ -40,22 +39,20 @@ public override sealed float GetPropertyHeight(SerializedProperty property, GUIC
4039
/// <summary>
4140
/// Native call to draw the provided property.
4241
/// </summary>
43-
public override sealed void OnGUI(Rect position, SerializedProperty property, GUIContent label)
42+
public sealed override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
4443
{
4544
if (IsPropertyValid(property))
4645
{
4746
OnGUISafe(position, property, label);
4847
return;
4948
}
5049

51-
var warningContent = new GUIContent(property.displayName + " has invalid property drawer");
52-
//create additional warning log to the console window
5350
ToolboxEditorLog.WrongAttributeUsageWarning(attribute, property);
5451
//create additional warning label based on the property name
52+
var warningContent = new GUIContent(property.displayName + " has invalid property drawer");
5553
ToolboxEditorGui.DrawEmptyProperty(position, property, warningContent);
5654
}
5755

58-
5956
/// <summary>
6057
/// Checks if provided property can be properly handled by this drawer.
6158
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DynamicHelpAttributeDrawer : ToolboxDecoratorDrawer<DynamicHelpAttr
88
protected override void OnGuiBeginSafe(DynamicHelpAttribute attribute)
99
{
1010
var sourceHandle = attribute.SourceHandle;
11-
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
11+
var targetObjects = GetDeclaringObjects();
1212
if (ValueExtractionHelper.TryGetValue(sourceHandle, targetObjects, out var value, out var hasMixedValues))
1313
{
1414
var messageText = hasMixedValues ? "-" : value?.ToString();

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections;
22
using System.Reflection;
3-
43
using Unity.EditorCoroutines.Editor;
54
using UnityEditor;
65
using UnityEngine;
@@ -9,7 +8,7 @@ namespace Toolbox.Editor.Drawers
98
{
109
public class EditorButtonAttributeDrawer : ToolboxDecoratorDrawer<EditorButtonAttribute>
1110
{
12-
private MethodInfo GetMethod(EditorButtonAttribute attribute, Object[] targetObjects, string methodName)
11+
private MethodInfo GetMethod(EditorButtonAttribute attribute, object[] targetObjects, string methodName)
1312
{
1413
var methodInfo = ReflectionUtility.GetObjectMethod(methodName, targetObjects);
1514
if (methodInfo == null)
@@ -50,7 +49,7 @@ private bool IsClickable(ButtonActivityType activityType)
5049
return true;
5150
}
5251

53-
private bool IsClickable(EditorButtonAttribute attribute, Object[] targetObjects)
52+
private bool IsClickable(EditorButtonAttribute attribute, object[] targetObjects)
5453
{
5554
if (!IsClickable(attribute.ActivityType))
5655
{
@@ -93,7 +92,7 @@ private bool IsClickable(EditorButtonAttribute attribute, Object[] targetObjects
9392
return true;
9493
}
9594

96-
private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects)
95+
private void CallMethods(EditorButtonAttribute attribute, object[] targetObjects)
9796
{
9897
var methodInfo = GetMethod(attribute, targetObjects, attribute.MethodName);
9998
if (methodInfo == null)
@@ -120,17 +119,16 @@ private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects
120119
}
121120
}
122121

123-
124122
protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
125123
{
126-
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
127-
if (targetObjects == null || targetObjects.Length == 0)
124+
var declaringObjects = GetDeclaringObjects();
125+
if (declaringObjects == null || declaringObjects.Length == 0)
128126
{
129127
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope
130128
return;
131129
}
132130

133-
var disable = !IsClickable(attribute, targetObjects);
131+
var disable = !IsClickable(attribute, declaringObjects);
134132
using (new EditorGUI.DisabledScope(disable))
135133
{
136134
var label = string.IsNullOrEmpty(attribute.ExtraLabel)
@@ -141,12 +139,11 @@ protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
141139

142140
if (GUILayout.Button(content, Style.buttonStyle))
143141
{
144-
CallMethods(attribute, targetObjects);
142+
CallMethods(attribute, declaringObjects);
145143
}
146144
}
147145
}
148146

149-
150147
private static class Style
151148
{
152149
internal static readonly GUIStyle buttonStyle = new GUIStyle(GUI.skin.button)

Assets/Editor Toolbox/Editor/Drawers/Toolbox/ToolboxAttributeDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
/// </summary>
66
public abstract class ToolboxAttributeDrawer : ToolboxDrawer
77
{ }
8-
}
8+
}

0 commit comments

Comments
 (0)