Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Editor/Drawers/RawReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace TNRD.Drawers
{
internal class RawReferenceDrawer : ReferenceDrawer, IReferenceDrawer
{
private readonly GUIContent label;
private readonly FieldInfo fieldInfo;
private GUIContent label;
private FieldInfo fieldInfo;

private static object previousReferenceValue;
private static string previousPropertyPath;
private object previousReferenceValue;
private string previousPropertyPath;

private object RawReferenceValue
{
Expand All @@ -37,10 +37,9 @@ private object RawReferenceValue
}
}

/// <inheritdoc />
public RawReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType, FieldInfo fieldInfo)
: base(property, genericType)
public void Initialize(SerializedProperty property, Type genericType, GUIContent label, FieldInfo fieldInfo)
{
Initialize(property, genericType);
this.label = label;
this.fieldInfo = fieldInfo;
}
Expand Down
16 changes: 10 additions & 6 deletions Editor/Drawers/ReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,30 @@ private enum DragAndDropMode

private DragAndDropMode dragAndDropMode;

protected readonly SerializedProperty Property;
protected readonly Type GenericType;
protected readonly CustomObjectDrawer CustomObjectDrawer;

protected SerializedProperty Property { get; private set; }
protected Type GenericType { get; private set; }

protected SerializedProperty ReferenceModeProperty => Property.FindPropertyRelative("mode");
protected SerializedProperty RawReferenceProperty => Property.FindPropertyRelative("rawReference");
protected SerializedProperty UnityReferenceProperty => Property.FindPropertyRelative("unityReference");

protected ReferenceDrawer(SerializedProperty property, Type genericType)
protected ReferenceDrawer()
{
Property = property;
GenericType = genericType;

CustomObjectDrawer = new CustomObjectDrawer();
CustomObjectDrawer.ButtonClicked += OnButtonClicked;
CustomObjectDrawer.Clicked += OnClicked;
CustomObjectDrawer.DeletePressed += OnDeletePressed;
CustomObjectDrawer.PropertiesClicked += OnPropertiesClicked;
}

protected void Initialize(SerializedProperty property, Type genericType)
{
Property = property;
GenericType = genericType;
}

private void OnButtonClicked(Rect position)
{
AdvancedDropdownState state = new AdvancedDropdownState();
Expand Down
6 changes: 3 additions & 3 deletions Editor/Drawers/UnityReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace TNRD.Drawers
{
internal class UnityReferenceDrawer : ReferenceDrawer, IReferenceDrawer
{
private readonly GUIContent label;
private GUIContent label;

public UnityReferenceDrawer(SerializedProperty property, GUIContent label, Type genericType)
: base(property, genericType)
public void Initialize(SerializedProperty property, Type genericType, GUIContent label)
{
Initialize(property, genericType);
this.label = label;
}

Expand Down
28 changes: 10 additions & 18 deletions Editor/SerializableInterfacePropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ namespace TNRD
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer
{
private readonly RawReferenceDrawer rawReferenceDrawer = new RawReferenceDrawer();
private readonly UnityReferenceDrawer unityReferenceDrawer = new UnityReferenceDrawer();

private SerializedProperty serializedProperty;
private Type genericType;

private IReferenceDrawer activeDrawer;

/// <inheritdoc />
public override bool CanCacheInspectorGUI(SerializedProperty property) => false;

Expand All @@ -23,7 +24,6 @@ private void Initialize(SerializedProperty property)
if (serializedProperty == property)
return;

activeDrawer = null;
serializedProperty = property;
genericType = GetGenericArgument();
Assert.IsNotNull(genericType, "Unable to find generic argument, are you doing some shady inheritance?");
Expand All @@ -33,16 +33,14 @@ private void Initialize(SerializedProperty property)
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
Initialize(property);
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
return activeDrawer.GetHeight();
return GetReferenceDrawer(property, label).GetHeight();
}

/// <inheritdoc />
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Initialize(property);
activeDrawer = GetReferenceDrawer(activeDrawer, property, label);
activeDrawer.OnGUI(position);
GetReferenceDrawer(property, label).OnGUI(position);
}

private Type GetGenericArgument()
Expand Down Expand Up @@ -81,25 +79,19 @@ private Type GetGenericArgument()
return null;
}

private IReferenceDrawer GetReferenceDrawer(
IReferenceDrawer original,
SerializedProperty property,
GUIContent label
)
private IReferenceDrawer GetReferenceDrawer(SerializedProperty property, GUIContent label)
{
SerializedProperty modeProperty = serializedProperty.FindPropertyRelative("mode");
ReferenceMode referenceMode = (ReferenceMode)modeProperty.enumValueIndex;

switch (referenceMode)
{
case ReferenceMode.Raw:
return original is RawReferenceDrawer
? original
: new RawReferenceDrawer(property, label, genericType, fieldInfo);
rawReferenceDrawer.Initialize(property, genericType, label, fieldInfo);
return rawReferenceDrawer;
case ReferenceMode.Unity:
return original is UnityReferenceDrawer
? original
: new UnityReferenceDrawer(property, label, genericType);
unityReferenceDrawer.Initialize(property, genericType, label);
return unityReferenceDrawer;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down