Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit abc384e

Browse files
committedApr 27, 2022
feat: added pinging of object
1 parent da31dbb commit abc384e

File tree

3 files changed

+113
-10
lines changed

3 files changed

+113
-10
lines changed
 
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace TNRD
5+
{
6+
internal sealed partial class SerializableInterfacePropertyDrawer
7+
{
8+
private static class Styles
9+
{
10+
private static GUIStyle regularLabelStyle;
11+
private static GUIStyle selectedLabelStyle;
12+
13+
public static GUIStyle RegularLabelStyle => regularLabelStyle ??= new GUIStyle(EditorStyles.label);
14+
15+
public static GUIStyle SelectedLabelStyle
16+
{
17+
get
18+
{
19+
if (selectedLabelStyle == null)
20+
{
21+
selectedLabelStyle = new GUIStyle(EditorStyles.label)
22+
{
23+
normal =
24+
{
25+
textColor = EditorGUIUtility.isProSkin
26+
? new Color32(128, 179, 253, 255)
27+
: new Color32(18, 73, 142, 255)
28+
},
29+
hover =
30+
{
31+
textColor = EditorGUIUtility.isProSkin
32+
? new Color32(128, 179, 253, 255)
33+
: new Color32(18, 73, 142, 255)
34+
}
35+
};
36+
}
37+
38+
return selectedLabelStyle;
39+
}
40+
}
41+
}
42+
}
43+
}

‎Editor/SerializableInterfacePropertyDrawer.Styles.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Editor/SerializableInterfacePropertyDrawer.cs

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
namespace TNRD
1212
{
1313
[CustomPropertyDrawer(typeof(SerializableInterface<>), true)]
14-
internal sealed class SerializableInterfacePropertyDrawer : PropertyDrawer
14+
internal sealed partial class SerializableInterfacePropertyDrawer : PropertyDrawer
1515
{
1616
private SerializedProperty serializedProperty;
17-
private bool isSelected;
17+
private bool labelSelected;
18+
private bool objectPickerSelected;
19+
20+
private bool IsSelected => labelSelected || objectPickerSelected;
1821

1922
private SerializedProperty RawReferenceProperty => serializedProperty.FindPropertyRelative("rawReference");
2023
private SerializedProperty UnityReferenceProperty => serializedProperty.FindPropertyRelative("unityReference");
@@ -81,7 +84,6 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
8184
HandleDeleteButton();
8285
}
8386

84-
8587
private Type GetGenericArgument()
8688
{
8789
Type type = fieldInfo.FieldType;
@@ -118,7 +120,7 @@ Type genericArgument
118120
height = EditorGUIUtility.singleLineHeight
119121
};
120122

121-
objectFieldRect = EditorGUI.PrefixLabel(objectFieldRect, label);
123+
objectFieldRect = DrawPrefixLabel(objectFieldRect, label);
122124
DrawRawReference(objectFieldRect);
123125
DrawButton(objectFieldRect, property, genericArgument);
124126

@@ -150,7 +152,7 @@ private void DrawUnityReferenceMode(
150152
Type genericArgument
151153
)
152154
{
153-
position = EditorGUI.PrefixLabel(position, label);
155+
position = DrawPrefixLabel(position, label);
154156
DrawUnityReference(position);
155157
DrawButton(position, property, genericArgument);
156158
}
@@ -163,8 +165,30 @@ private void DrawUnityReference(Rect position)
163165
DrawObjectField(position, EditorGUIUtility.ObjectContent(unityReference, referenceType), unityReference);
164166
}
165167

168+
private Rect DrawPrefixLabel(Rect position, GUIContent label)
169+
{
170+
GUIStyle labelStyle = IsSelected ? Styles.SelectedLabelStyle : Styles.RegularLabelStyle;
171+
Rect result = EditorGUI.PrefixLabel(position, label, labelStyle);
172+
173+
if (Event.current.type == EventType.MouseDown)
174+
{
175+
Rect delta = new Rect(position)
176+
{
177+
width = position.width - result.width
178+
};
179+
180+
labelSelected = delta.Contains(Event.current.mousePosition);
181+
RepaintActiveInspector();
182+
}
183+
184+
return result;
185+
}
186+
166187
private void DrawObjectField(Rect position, GUIContent objectFieldContent, Object objectToShow)
167188
{
189+
Rect positionWithoutThumb = new Rect(position);
190+
positionWithoutThumb.xMax -= 20;
191+
168192
Event evt = Event.current;
169193
if (evt.type == EventType.Repaint)
170194
{
@@ -173,14 +197,29 @@ private void DrawObjectField(Rect position, GUIContent objectFieldContent, Objec
173197
position.Contains(evt.mousePosition),
174198
false,
175199
false,
176-
isSelected);
200+
IsSelected);
177201
}
178-
else if ((evt.type == EventType.MouseDown || evt.type == EventType.MouseUp) && evt.button == 0)
202+
203+
HandleObjectFieldMouseDown(position, objectToShow, evt, positionWithoutThumb);
204+
}
205+
206+
private void HandleObjectFieldMouseDown(
207+
Rect position,
208+
Object objectToShow,
209+
Event evt,
210+
Rect positionWithoutThumb
211+
)
212+
{
213+
if (evt.type != EventType.MouseDown)
214+
return;
215+
216+
if (evt.button == 0)
179217
{
180-
isSelected = position.Contains(evt.mousePosition);
218+
objectPickerSelected = positionWithoutThumb.Contains(evt.mousePosition);
219+
PingObject();
181220
RepaintActiveInspector();
182221
}
183-
else if (evt.type == EventType.MouseDown && evt.button == 1 && position.Contains(evt.mousePosition))
222+
else if (evt.button == 1 && positionWithoutThumb.Contains(evt.mousePosition))
184223
{
185224
if (objectToShow != null)
186225
{
@@ -195,6 +234,24 @@ private void DrawObjectField(Rect position, GUIContent objectFieldContent, Objec
195234
}
196235
}
197236

237+
private void PingObject()
238+
{
239+
if (!IsSelected)
240+
return;
241+
242+
switch (ReferenceMode)
243+
{
244+
case ReferenceMode.Raw:
245+
// No support for pinging raw objects for now (I guess this would ping the MonoScript?)
246+
break;
247+
case ReferenceMode.Unity:
248+
EditorGUIUtility.PingObject(UnityReferenceProperty.objectReferenceValue);
249+
break;
250+
default:
251+
throw new ArgumentOutOfRangeException();
252+
}
253+
}
254+
198255
private void RepaintActiveInspector()
199256
{
200257
// Forcing a repaint of the inspector for this object, not the prettiest but it works
@@ -263,7 +320,7 @@ private void DropdownOnItemSelectedEvent(SerializedProperty property, ReferenceM
263320

264321
private void HandleDeleteButton()
265322
{
266-
if (!isSelected)
323+
if (!IsSelected)
267324
return;
268325

269326
Event evt = Event.current;

0 commit comments

Comments
 (0)
Please sign in to comment.