Skip to content

Commit

Permalink
Minor refactor changes; possibility to use EditorButtonAttribute and …
Browse files Browse the repository at this point in the history
…DynamicHelpAttribute in nested types; implement AnimationCurveSettingsAttribute & Drawer
  • Loading branch information
arimger committed Nov 28, 2024
1 parent 99baede commit 9dabb40
Show file tree
Hide file tree
Showing 46 changed files with 361 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;

using UnityEditor;

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

public static bool TryGetValue(string source, SerializedProperty causer, out object value, out bool hasMixedValues, Func<object, object, bool> nextValuesComparer)
{
var targetObjects = causer.serializedObject.targetObjects;
var parentObjects = new object[targetObjects.Length];
for (var i = 0; i < targetObjects.Length; i++)
{
var targetObject = targetObjects[i];
parentObjects[i] = causer.GetDeclaringObject(targetObject);
}

//NOTE: consider using NonAlloc implementation
var parentObjects = causer.GetDeclaringObjects();
return TryGetValue(source, parentObjects, out value, out hasMixedValues, nextValuesComparer);
}
}
Expand Down
13 changes: 13 additions & 0 deletions Assets/Editor Toolbox/Editor/Drawers/ISerializedPropertyContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Reflection;
using UnityEditor;

namespace Toolbox.Editor.Drawers
{
public interface ISerializedPropertyContext
{
SerializedProperty Property { get; }
FieldInfo FieldInfo { get; }
Type Type { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Drawers
{
[CustomPropertyDrawer(typeof(AnimationCurveSettingsAttribute))]
public class AnimationCurveSettingsAttributeDrawer : PropertyDrawerBase
{
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
{
var attribute = Attribute;
var curveRanges = new Rect(
attribute.Min.x,
attribute.Min.y,
attribute.Max.x - attribute.Min.x,
attribute.Max.y - attribute.Min.y);

var color = attribute.Color;

EditorGUI.BeginProperty(position, label, property);
EditorGUI.CurveField(position, property, color, curveRanges, label);
EditorGUI.EndProperty();
}

public override bool IsPropertyValid(SerializedProperty property)
{
return base.IsPropertyValid(property) && property.propertyType == SerializedPropertyType.AnimationCurve;
}

private AnimationCurveSettingsAttribute Attribute => attribute as AnimationCurveSettingsAttribute;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,13 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
}
}


public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.ObjectReference;
}


private AssetPreviewAttribute Attribute => attribute as AssetPreviewAttribute;


private static class Style
{
internal static readonly float offset = 6.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ protected virtual void OnGUISafe(Rect position, SerializedProperty property, GUI
EditorGUI.PropertyField(position, property, label);
}


/// <summary>
/// Native call to return the expected height.
/// </summary>
public override sealed float GetPropertyHeight(SerializedProperty property, GUIContent label)
public sealed override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return IsPropertyValid(property)
? GetPropertyHeightSafe(property, label)
Expand All @@ -40,22 +39,20 @@ public override sealed float GetPropertyHeight(SerializedProperty property, GUIC
/// <summary>
/// Native call to draw the provided property.
/// </summary>
public override sealed void OnGUI(Rect position, SerializedProperty property, GUIContent label)
public sealed override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (IsPropertyValid(property))
{
OnGUISafe(position, property, label);
return;
}

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


/// <summary>
/// Checks if provided property can be properly handled by this drawer.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DynamicHelpAttributeDrawer : ToolboxDecoratorDrawer<DynamicHelpAttr
protected override void OnGuiBeginSafe(DynamicHelpAttribute attribute)
{
var sourceHandle = attribute.SourceHandle;
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
var targetObjects = GetDeclaringObjects();
if (ValueExtractionHelper.TryGetValue(sourceHandle, targetObjects, out var value, out var hasMixedValues))
{
var messageText = hasMixedValues ? "-" : value?.ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections;
using System.Reflection;

using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;
Expand All @@ -9,7 +8,7 @@ namespace Toolbox.Editor.Drawers
{
public class EditorButtonAttributeDrawer : ToolboxDecoratorDrawer<EditorButtonAttribute>
{
private MethodInfo GetMethod(EditorButtonAttribute attribute, Object[] targetObjects, string methodName)
private MethodInfo GetMethod(EditorButtonAttribute attribute, object[] targetObjects, string methodName)
{
var methodInfo = ReflectionUtility.GetObjectMethod(methodName, targetObjects);
if (methodInfo == null)
Expand Down Expand Up @@ -50,7 +49,7 @@ private bool IsClickable(ButtonActivityType activityType)
return true;
}

private bool IsClickable(EditorButtonAttribute attribute, Object[] targetObjects)
private bool IsClickable(EditorButtonAttribute attribute, object[] targetObjects)
{
if (!IsClickable(attribute.ActivityType))
{
Expand Down Expand Up @@ -93,7 +92,7 @@ private bool IsClickable(EditorButtonAttribute attribute, Object[] targetObjects
return true;
}

private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects)
private void CallMethods(EditorButtonAttribute attribute, object[] targetObjects)
{
var methodInfo = GetMethod(attribute, targetObjects, attribute.MethodName);
if (methodInfo == null)
Expand All @@ -120,17 +119,16 @@ private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects
}
}


protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
{
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
if (targetObjects == null || targetObjects.Length == 0)
var declaringObjects = GetDeclaringObjects();
if (declaringObjects == null || declaringObjects.Length == 0)
{
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope
return;
}

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

if (GUILayout.Button(content, Style.buttonStyle))
{
CallMethods(attribute, targetObjects);
CallMethods(attribute, declaringObjects);
}
}
}


private static class Style
{
internal static readonly GUIStyle buttonStyle = new GUIStyle(GUI.skin.button)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/// </summary>
public abstract class ToolboxAttributeDrawer : ToolboxDrawer
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ protected virtual PropertyCondition OnGuiValidateSafe(SerializedProperty propert
return PropertyCondition.Valid;
}


public sealed override PropertyCondition OnGuiValidate(SerializedProperty property)
{
return OnGuiValidate(property, PropertyUtility.GetAttribute<T>(property));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;

using UnityEngine;

namespace Toolbox.Editor.Drawers
Expand All @@ -16,7 +15,6 @@ protected virtual void OnGuiCloseSafe(T attribute)
protected virtual void OnGuiEndSafe(T attribute)
{ }


public sealed override void OnGuiBegin(ToolboxAttribute attribute)
{
OnGuiBegin(attribute as T);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
using UnityEngine;
using System;
using UnityEngine;

namespace Toolbox.Editor.Drawers
{
public abstract class ToolboxDecoratorDrawerBase : ToolboxAttributeDrawer
{
public abstract void OnGuiBegin(ToolboxAttribute attribute);
protected object[] GetDeclaringObjects()
{
if (PropertyContext == null)
{
return Array.Empty<object>();
}

var property = PropertyContext.Property;
return property.GetDeclaringObjects();
}

internal virtual void OnGuiBegin(ToolboxAttribute attribute, ISerializedPropertyContext propertyContext)
{
PropertyContext = propertyContext;
OnGuiBegin(attribute);
PropertyContext = null;
}

internal virtual void OnGuiClose(ToolboxAttribute attribute, ISerializedPropertyContext propertyContext)
{
PropertyContext = propertyContext;
OnGuiClose(attribute);
PropertyContext = null;
}

public abstract void OnGuiBegin(ToolboxAttribute attribute);
public abstract void OnGuiClose(ToolboxAttribute attribute);

/// <summary>
/// Context associated with <see cref="UnityEditor.SerializedProperty"/> that is currently handled.
/// </summary>
protected ISerializedPropertyContext PropertyContext { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ protected virtual void OnGuiSafe(SerializedProperty property, GUIContent label,
ToolboxEditorGui.DrawDefaultProperty(property, label);
}


public override bool IsPropertyValid(SerializedProperty property)
{
return true;
}

public override sealed void OnGui(SerializedProperty property, GUIContent label)
public sealed override void OnGui(SerializedProperty property, GUIContent label)
{
OnGui(property, label, PropertyUtility.GetAttribute<T>(property));
}

public override sealed void OnGui(SerializedProperty property, GUIContent label, ToolboxAttribute attribute)
public sealed override void OnGui(SerializedProperty property, GUIContent label, ToolboxAttribute attribute)
{
OnGui(property, label, attribute as T);
}
Expand All @@ -39,10 +38,9 @@ public void OnGui(SerializedProperty property, GUIContent label, T attribute)
return;
}

var warningContent = new GUIContent(string.Format("{0} has invalid property drawer", property.displayName));
//create additional warning log to the Console window
ToolboxEditorLog.WrongAttributeUsageWarning(attribute, property);
//create additional warning label based on the property name
var warningContent = new GUIContent(string.Format("{0} has invalid property drawer", property.displayName));
ToolboxEditorGui.DrawEmptyProperty(property, warningContent);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;

using UnityEditor;
using UnityEngine;

Expand All @@ -8,7 +7,6 @@ namespace Toolbox.Editor.Drawers
public abstract class ToolboxTargetTypeDrawer : ToolboxDrawer
{
public abstract void OnGui(SerializedProperty property, GUIContent label);

public abstract Type GetTargetType();
public abstract bool UseForChildren();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor
namespace Toolbox.Editor.Drawers
{
using Toolbox.Editor.Drawers;

//TODO:
//1. dedicated class to initialize and hold drawer-related data
//2. dedicated class used for settings initialization
//3. separate logic for resettings active drawers
//4. validations drawers

internal static class ToolboxDrawerModule
internal static class ToolboxDrawersManager
{
[InitializeOnLoadMethod]
internal static void InitializeModule()
Expand Down Expand Up @@ -199,7 +197,6 @@ private static void PrepareTargetTypeDrawers(IToolboxInspectorSettings settings)
}
}


/// <summary>
/// Clears all currently cached <see cref="ToolboxPropertyHandler"/>s.
/// </summary>
Expand Down Expand Up @@ -231,7 +228,7 @@ internal static void UpdateDrawers()
/// </summary>
internal static void UpdateDrawers(IToolboxInspectorSettings settings)
{
ToolboxDrawerModule.settings = settings;
ToolboxDrawersManager.settings = settings;

if (settings == null)
{
Expand All @@ -254,7 +251,6 @@ internal static void UpdateDrawers(IToolboxInspectorSettings settings)
validationEnabled = false;
}


/// <summary>
/// Determines if property has any associated drawer (built-in or custom one).
/// This method does not take into account <see cref="ToolboxDrawer"/>s.
Expand Down
Loading

0 comments on commit 9dabb40

Please sign in to comment.