Skip to content

Commit

Permalink
feat: added passing serialized property to CustomObjectDrawer and Dro…
Browse files Browse the repository at this point in the history
…pdown
  • Loading branch information
Thundernerd committed Jul 24, 2022
1 parent aba0c30 commit 5913ab7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 46 deletions.
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
32 changes: 16 additions & 16 deletions Editor/Drawers/ReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,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 @@ -90,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 @@ -200,12 +200,12 @@ 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)
{
Expand Down
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);
}
}
}
14 changes: 9 additions & 5 deletions Editor/Utilities/SerializableInterfaceAdvancedDropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using TNRD.Builders;
using TNRD.Items;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEngine.Assertions;
Expand All @@ -16,16 +17,18 @@ internal sealed class SerializableInterfaceAdvancedDropdown : AdvancedDropdown
private readonly MethodInfo sortChildrenMethod;
private readonly bool canSort;
private readonly Scene? relevantScene;
private readonly SerializedProperty property;

public delegate void ItemSelectedDelegate(ReferenceMode mode, object reference);
public delegate void ItemSelectedDelegate(SerializedProperty property, ReferenceMode mode, object reference);

public event ItemSelectedDelegate ItemSelectedEvent; // Suffixed with Event because of the override

/// <inheritdoc />
public SerializableInterfaceAdvancedDropdown(
AdvancedDropdownState state,
Type interfaceType,
Scene? relevantScene
Scene? relevantScene,
SerializedProperty property
)
: base(state)
{
Expand All @@ -38,6 +41,7 @@ internal sealed class SerializableInterfaceAdvancedDropdown : AdvancedDropdown
minimumSize = new Vector2(0, 300);
this.interfaceType = interfaceType;
this.relevantScene = relevantScene;
this.property = property;
}

/// <inheritdoc />
Expand All @@ -52,7 +56,7 @@ protected override AdvancedDropdownItem BuildRoot()
{
dropdownItem.AddChild(new NoneDropdownItem());
}

if (canSort)
{
sortChildrenMethod.Invoke(item,
Expand All @@ -72,7 +76,7 @@ private int Sort(AdvancedDropdownItem a, AdvancedDropdownItem b)
return -1;
if (b is NoneDropdownItem)
return 1;

int childrenA = a.children.Count();
int childrenB = b.children.Count();

Expand All @@ -90,7 +94,7 @@ protected override void ItemSelected(AdvancedDropdownItem item)
{
if (item is IDropdownItem dropdownItem)
{
ItemSelectedEvent?.Invoke(dropdownItem.Mode, dropdownItem.GetValue());
ItemSelectedEvent?.Invoke(property, dropdownItem.Mode, dropdownItem.GetValue());
}
}
}
Expand Down

0 comments on commit 5913ab7

Please sign in to comment.