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
34 changes: 17 additions & 17 deletions Editor/Drawers/CustomObjectDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace TNRD.Drawers
{
public partial class CustomObjectDrawer
{
public delegate void ButtonClickedDelegate(Rect position);
public delegate void ButtonClickedDelegate(Rect position, SerializedProperty property);

public delegate void ClickedDelegate();
public delegate void ClickedDelegate(SerializedProperty property);

public delegate void DeletePressedDelegate();
public delegate void DeletePressedDelegate(SerializedProperty property);

public delegate void PropertiesClickedDelegate();
public delegate void PropertiesClickedDelegate(SerializedProperty property);

private bool isSelected;

Expand All @@ -21,18 +21,18 @@ public partial class CustomObjectDrawer
public event ClickedDelegate Clicked;
public event DeletePressedDelegate DeletePressed;
public event PropertiesClickedDelegate PropertiesClicked;

public void OnGUI(Rect position, GUIContent label, GUIContent content)
public void OnGUI(Rect position, GUIContent label, GUIContent content, SerializedProperty property)
{
Rect positionWithoutThumb = new Rect(position);
positionWithoutThumb.xMax -= 20;

position = DrawPrefixLabel(position, label);
DrawObjectField(position, content);
DrawButton(position);
DrawButton(position, property);

HandleMouseDown(position, positionWithoutThumb);
HandleKeyDown();
HandleMouseDown(position, positionWithoutThumb, property);
HandleKeyDown(property);
}

private Rect DrawPrefixLabel(Rect position, GUIContent label)
Expand Down Expand Up @@ -66,7 +66,7 @@ private void ForceRepaintEditors()
}
}

private void DrawButton(Rect position)
private void DrawButton(Rect position, SerializedProperty property)
{
Rect buttonRect = new Rect(position);
buttonRect.yMin += 1;
Expand All @@ -76,11 +76,11 @@ private void DrawButton(Rect position)

if (GUI.Button(buttonRect, string.Empty, "objectFieldButton"))
{
ButtonClicked?.Invoke(position);
ButtonClicked?.Invoke(position, property);
}
}

private void HandleMouseDown(Rect position, Rect positionWithoutThumb)
private void HandleMouseDown(Rect position, Rect positionWithoutThumb, SerializedProperty property)
{
if (Event.type != EventType.MouseDown)
return;
Expand All @@ -89,26 +89,26 @@ private void HandleMouseDown(Rect position, Rect positionWithoutThumb)
{
isSelected = positionWithoutThumb.Contains(Event.mousePosition);
ForceRepaintEditors();
Clicked?.Invoke();
Clicked?.Invoke(property);
}
else if (Event.button == 1 && positionWithoutThumb.Contains(Event.mousePosition))
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Clear"), false, () => { DeletePressed?.Invoke(); });
menu.AddItem(new GUIContent("Properties..."), false, () => { PropertiesClicked?.Invoke(); });
menu.AddItem(new GUIContent("Clear"), false, () => { DeletePressed?.Invoke(property); });
menu.AddItem(new GUIContent("Properties..."), false, () => { PropertiesClicked?.Invoke(property); });
menu.DropDown(position);
Event.Use();
}
}

private void HandleKeyDown()
private void HandleKeyDown(SerializedProperty property)
{
if (!isSelected)
return;

if (Event.type == EventType.KeyDown && Event.keyCode == KeyCode.Delete)
{
DeletePressed?.Invoke();
DeletePressed?.Invoke(property);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Editor/Drawers/RawReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void OnGUI(Rect position)
? EditorGUIUtility.ObjectContent((MonoScript)null, typeof(MonoScript))
: new GUIContent(rawReferenceValue.GetType().Name, IconUtility.ScriptIcon);

CustomObjectDrawer.OnGUI(objectFieldRect, label, content);
CustomObjectDrawer.OnGUI(objectFieldRect, label, content, Property);

HandleDragAndDrop(objectFieldRect);

Expand Down Expand Up @@ -79,13 +79,13 @@ private void DrawLine(Rect position)
EditorGUI.DrawRect(line, Styles.LineColor);
}

protected override void PingObject()
protected override void PingObject(SerializedProperty property)
{
// No support for pinging raw objects for now (I guess this would ping the MonoScript?)
}

/// <inheritdoc />
protected override void OnPropertiesClicked()
protected override void OnPropertiesClicked(SerializedProperty property)
{
if (RawReferenceValue == null)
return;
Expand Down
160 changes: 94 additions & 66 deletions Editor/Drawers/ReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,69 +27,28 @@ private enum DragAndDropMode
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 SerializedProperty ReferenceModeProperty => Property.ReferenceModeProperty();
protected SerializedProperty RawReferenceProperty => Property.RawReferenceProperty();
protected SerializedProperty UnityReferenceProperty => Property.UnityReferenceProperty();

protected FieldInfo FieldInfo { get; private set; }

protected ReferenceMode ModeValue
{
get => (ReferenceMode)ReferenceModeProperty.enumValueIndex;
set => ReferenceModeProperty.enumValueIndex = (int)value;
get => GetModeValue(Property);
set => SetModeValue(Property, value);
}

protected object RawReferenceValue
{
get
{
#if UNITY_2021_1_OR_NEWER
return RawReferenceProperty.managedReferenceValue;
#else
ISerializableInterface instance =
(ISerializableInterface)FieldInfo.GetValue(Property.serializedObject.targetObject);
return instance.GetRawReference();
#endif
}
set
{
#if UNITY_2021_1_OR_NEWER
RawReferenceProperty.managedReferenceValue = value;
#else
FieldInfo.SetValue(Property.serializedObject.targetObject, value);
#endif
}
get => GetRawReferenceValue(Property);
set => SetRawReferenceValue(Property, value);
}

protected object PropertyValue
{
get
{
return ModeValue switch
{
ReferenceMode.Raw => RawReferenceValue,
ReferenceMode.Unity => UnityReferenceProperty.objectReferenceValue,
_ => throw new ArgumentOutOfRangeException()
};
}
set
{
switch (ModeValue)
{
case ReferenceMode.Raw:
RawReferenceValue = value;
UnityReferenceProperty.objectReferenceValue = null;
break;
case ReferenceMode.Unity:
UnityReferenceProperty.objectReferenceValue = GetUnityObject((Object)value);
RawReferenceValue = null;
break;
default:
throw new ArgumentOutOfRangeException();
}

Property.serializedObject.ApplyModifiedProperties();
}
get => GetPropertyValue(Property);
set => SetPropertyValue(Property, value);
}

protected ReferenceDrawer()
Expand All @@ -108,18 +67,18 @@ protected void Initialize(SerializedProperty property, Type genericType, FieldIn
FieldInfo = fieldInfo;
}

private void OnButtonClicked(Rect position)
private void OnButtonClicked(Rect position, SerializedProperty property)
{
AdvancedDropdownState state = new AdvancedDropdownState();
SerializableInterfaceAdvancedDropdown dropdown =
new SerializableInterfaceAdvancedDropdown(state, GenericType, GetRelevantScene());
new SerializableInterfaceAdvancedDropdown(state, GenericType, GetRelevantScene(property), property);
dropdown.ItemSelectedEvent += OnItemSelected;
dropdown.Show(position);
}

private Scene? GetRelevantScene()
private static Scene? GetRelevantScene(SerializedProperty property)
{
Object target = Property.serializedObject.targetObject;
Object target = property.serializedObject.targetObject;

if (target is ScriptableObject)
return null;
Expand All @@ -131,30 +90,30 @@ private void OnButtonClicked(Rect position)
return null;
}

private void OnClicked()
private void OnClicked(SerializedProperty property)
{
PingObject();
PingObject(property);
}

private void OnDeletePressed()
private void OnDeletePressed(SerializedProperty property)
{
ModeValue = default;
PropertyValue = null;
SetModeValue(property, default);
SetPropertyValue(property, null);
}

private void OnItemSelected(ReferenceMode mode, object reference)
private void OnItemSelected(SerializedProperty property, ReferenceMode mode, object reference)
{
ModeValue = mode;
PropertyValue = reference;
SetModeValue(property, mode);
SetPropertyValue(property, reference);
}

protected abstract void OnPropertiesClicked();
protected abstract void OnPropertiesClicked(SerializedProperty property);

protected void HandleDragAndDrop(Rect position)
{
if (!position.Contains(Event.current.mousePosition))
return;

if (Event.current.type == EventType.DragPerform)
{
HandleDragUpdated();
Expand Down Expand Up @@ -241,11 +200,80 @@ private void HandleDragPerform()

private Object GetUnityObject(Object objectReference)
{
if(objectReference is GameObject gameObject)
if (objectReference is GameObject gameObject)
return gameObject.GetComponent(GenericType);
return objectReference;
}

protected abstract void PingObject();
protected abstract void PingObject(SerializedProperty property);

protected ReferenceMode GetModeValue(SerializedProperty property)
{
return (ReferenceMode)property.ReferenceModeProperty().enumValueIndex;
}

protected void SetModeValue(SerializedProperty property, ReferenceMode mode)
{
property.ReferenceModeProperty().enumValueIndex = (int)mode;
}

protected object GetRawReferenceValue(SerializedProperty property)
{
#if UNITY_2021_1_OR_NEWER
return property.RawReferenceProperty().managedReferenceValue;
#else
ISerializableInterface instance =
(ISerializableInterface)FieldInfo.GetValue(property.serializedObject.targetObject);
return instance.GetRawReference();
#endif
}

protected void SetRawReferenceValue(SerializedProperty property, object value)
{
#if UNITY_2021_1_OR_NEWER
property.RawReferenceProperty().managedReferenceValue = value;
#else
FieldInfo.SetValue(property.serializedObject.targetObject, value);
#endif
}

protected Object GetUnityReferenceValue(SerializedProperty property)
{
return property.UnityReferenceProperty().objectReferenceValue;
}

protected void SetUnityReferenceValue(SerializedProperty property, object value)
{
property.UnityReferenceProperty().objectReferenceValue = GetUnityObject((Object)value);
}

protected object GetPropertyValue(SerializedProperty property)
{
return GetModeValue(property) switch
{
ReferenceMode.Raw => GetRawReferenceValue(property),
ReferenceMode.Unity => GetUnityReferenceValue(property),
_ => throw new ArgumentOutOfRangeException()
};
}

protected void SetPropertyValue(SerializedProperty property, object value)
{
switch (GetModeValue(property))
{
case ReferenceMode.Unity:
SetUnityReferenceValue(property, value);
SetRawReferenceValue(property, null);
break;
case ReferenceMode.Raw:
SetRawReferenceValue(property, value);
SetUnityReferenceValue(property, null);
break;
default:
throw new ArgumentOutOfRangeException();
}

property.serializedObject.ApplyModifiedProperties();
}
}
}
10 changes: 5 additions & 5 deletions Editor/Drawers/UnityReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ public void OnGUI(Rect position)
Object unityReference = UnityReferenceProperty.objectReferenceValue;
Type referenceType = unityReference == null ? typeof(Object) : unityReference.GetType();
GUIContent objectContent = EditorGUIUtility.ObjectContent(unityReference, referenceType);
CustomObjectDrawer.OnGUI(position, label, objectContent);
CustomObjectDrawer.OnGUI(position, label, objectContent, Property);
HandleDragAndDrop(position);
}

protected override void PingObject()
protected override void PingObject(SerializedProperty property)
{
EditorGUIUtility.PingObject((Object)PropertyValue);
EditorGUIUtility.PingObject((Object)GetPropertyValue(property));
}

protected override void OnPropertiesClicked()
protected override void OnPropertiesClicked(SerializedProperty property)
{
PropertyEditorUtility.Show(UnityReferenceProperty.objectReferenceValue);
PropertyEditorUtility.Show(property.UnityReferenceProperty().objectReferenceValue);
}
}
}
Loading