Added PredrawerModifier to help modify either SerializedProperty or GUIContent before drawing

This commit is contained in:
Anders Ejlersen 2021-12-04 11:32:57 +01:00
parent 6321fcc107
commit e0fc2c1e05
7 changed files with 62 additions and 3 deletions

View file

@ -15,8 +15,16 @@ namespace Module.Inspector.Editor
return; return;
label.tooltip = result.tooltip; label.tooltip = result.tooltip;
for (var i = 0; i < result.predrawerModifiers.Count; i++)
{
EditorPropertyUtility.ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer> value = result.predrawerModifiers[i];
value.drawer.Modify(value.attribute, property, label);
}
bool prevEnabled = GUI.enabled; bool prevEnabled = GUI.enabled;
GUI.enabled = accessType == EAccessType.Enabled; GUI.enabled = accessType == EAccessType.Enabled;
// TODO: What to do with [Obsolete]?
if (result.draw != null) if (result.draw != null)
{ {
@ -50,7 +58,7 @@ namespace Module.Inspector.Editor
return accessType != EAccessType.Hidden ? EditorGUI.GetPropertyHeight(property, label) : 0.0f; return accessType != EAccessType.Hidden ? EditorGUI.GetPropertyHeight(property, label) : 0.0f;
} }
private EAccessType GetAccessType(SerializedProperty property, EditorPropertyUtility.Result result) private static EAccessType GetAccessType(SerializedProperty property, EditorPropertyUtility.Result result)
{ {
if (result.accessModifiers.Count == 0) if (result.accessModifiers.Count == 0)
return EAccessType.Enabled; return EAccessType.Enabled;

View file

@ -0,0 +1,10 @@
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
public abstract class PredrawerModifierPropertyDrawer : AbstractPropertyDrawer
{
public abstract void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5a747366f0b24cf89b7ebfd2108468f7
timeCreated: 1638612780

View file

@ -37,7 +37,12 @@ namespace Module.Inspector.Editor.Utilities
for (var k = 0; k < arguments.Count; k++) for (var k = 0; k < arguments.Count; k++)
{ {
if (arguments[k].Value is Type argType) if (!(arguments[k].Value is Type argType))
continue;
if (CACHED_ATT_TO_DRAWER.ContainsKey(argType))
Debug.LogWarningFormat("Already contained {0}, but tried to add with {1} and already had {2}.", argType, type, CACHED_ATT_TO_DRAWER[argType]);
else
CACHED_ATT_TO_DRAWER.Add(argType, type); CACHED_ATT_TO_DRAWER.Add(argType, type);
} }
} }
@ -67,6 +72,7 @@ namespace Module.Inspector.Editor.Utilities
if (typeDrawer != null) if (typeDrawer != null)
drawer = new ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer>(null, typeDrawer); drawer = new ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer>(null, typeDrawer);
var predrawerModifiers = new List<ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>>(2);
var valueModifiers = new List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>>(2); var valueModifiers = new List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>>(2);
var accessModifiers = new List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>>(2); var accessModifiers = new List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>>(2);
object[] attributes = fieldInfo.GetCustomAttributes(false); object[] attributes = fieldInfo.GetCustomAttributes(false);
@ -88,6 +94,13 @@ namespace Module.Inspector.Editor.Utilities
if (prop != null) if (prop != null)
drawer = new ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer>(attDrawer, prop); drawer = new ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer>(attDrawer, prop);
} }
else if (att is PredrawerModifierPropertyAttribute predawerModifier)
{
var prop = InternalCreateInstanceOf<PredrawerModifierPropertyDrawer>(predawerModifier);
if (prop != null)
predrawerModifiers.Add(new ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>(predawerModifier, prop));
}
else if (att is ValueModifierPropertyAttribute valueModifier) else if (att is ValueModifierPropertyAttribute valueModifier)
{ {
var prop = InternalCreateInstanceOf<ValueModifierPropertyDrawer>(valueModifier); var prop = InternalCreateInstanceOf<ValueModifierPropertyDrawer>(valueModifier);
@ -121,7 +134,7 @@ namespace Module.Inspector.Editor.Utilities
tooltip = $"Obsolete: {obsoleteText}"; tooltip = $"Obsolete: {obsoleteText}";
} }
return new Result(drawer, valueModifiers, accessModifiers, tooltip, isObsolete); return new Result(drawer, predrawerModifiers, valueModifiers, accessModifiers, tooltip, isObsolete);
} }
private static T InternalCreateInstanceOf<T>(AbstractPropertyAttribute att) where T : AbstractPropertyDrawer private static T InternalCreateInstanceOf<T>(AbstractPropertyAttribute att) where T : AbstractPropertyDrawer
@ -146,23 +159,37 @@ namespace Module.Inspector.Editor.Utilities
public sealed class Result public sealed class Result
{ {
public readonly ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer> draw; public readonly ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer> draw;
public readonly List<ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>> predrawerModifiers;
public readonly List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>> valueModifiers; public readonly List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>> valueModifiers;
public readonly List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>> accessModifiers; public readonly List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>> accessModifiers;
public readonly string tooltip; public readonly string tooltip;
public readonly bool isObsolete; public readonly bool isObsolete;
public Result(ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer> draw, public Result(ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer> draw,
List<ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>> predrawerModifiers,
List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>> valueModifiers, List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>> valueModifiers,
List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>> accessModifiers, List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>> accessModifiers,
string tooltip, string tooltip,
bool isObsolete) bool isObsolete)
{ {
this.draw = draw; this.draw = draw;
this.predrawerModifiers = predrawerModifiers;
this.valueModifiers = valueModifiers; this.valueModifiers = valueModifiers;
this.accessModifiers = accessModifiers; this.accessModifiers = accessModifiers;
this.tooltip = tooltip; this.tooltip = tooltip;
this.isObsolete = isObsolete; this.isObsolete = isObsolete;
} }
public T GetPredrawerModifier<T>() where T : PredrawerModifierPropertyAttribute
{
for (var i = 0; i < predrawerModifiers.Count; i++)
{
if (predrawerModifiers[i].attribute is T att)
return att;
}
return null;
}
public T GetValueModifier<T>() where T : ValueModifierPropertyAttribute public T GetValueModifier<T>() where T : ValueModifierPropertyAttribute
{ {

View file

@ -2,6 +2,7 @@
{ {
public enum EPropertyType : byte public enum EPropertyType : byte
{ {
PredrawerModifier,
Drawer, Drawer,
ValueModifier, ValueModifier,
AccessModifier AccessModifier

View file

@ -0,0 +1,7 @@
namespace Module.Inspector
{
public abstract class PredrawerModifierPropertyAttribute : AbstractPropertyAttribute
{
public override EPropertyType Type => EPropertyType.PredrawerModifier;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 54936da91a3241debe62f6fe4269f129
timeCreated: 1638612653