Skip to content

Commit

Permalink
Refactored uses of GenericPropertyDrawer
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielEverland committed May 15, 2019
1 parent 3538bad commit 174d153
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 66 deletions.
Expand Up @@ -87,7 +87,7 @@ private void DrawGenericPropertyField(Rect valueRect)
{
if (ValueType != null)
{
GenericPropertyDrawer.DrawPropertyDrawer(valueRect, ValueType, constantValue, GUIContent.none);
GenericPropertyDrawer.DrawPropertyDrawer(valueRect, GUIContent.none, ValueType, constantValue, GUIContent.none);
}
else
{
Expand Down
58 changes: 9 additions & 49 deletions Assets/SO Architecture/Editor/Drawers/GenericPropertyDrawer.cs
Expand Up @@ -7,31 +7,8 @@ namespace ScriptableObjectArchitecture.Editor
{
public static class GenericPropertyDrawer
{
private const string VALUE_LABEL = "Value";

private static GUIContent ValueGUIContent
{
get
{
if (_valueGUIContent == null)
{
_valueGUIContent = new GUIContent(VALUE_LABEL);
}

return _valueGUIContent;
}
}

private static GUIContent _valueGUIContent;

/// <summary>
/// Draws a property drawer using the <see cref="EditorGUI"/> methods and the area the drawer is drawn
/// in is determined by the passed <see cref="Rect"/> <paramref name="rect"/>.
/// </summary>
public static void DrawPropertyDrawer(Rect rect, Type type, SerializedProperty property, GUIContent errorLabel, bool drawValueLabel = false)
public static void DrawPropertyDrawer(Rect rect, GUIContent label, Type type, SerializedProperty property, GUIContent errorLabel)
{
GUIContent label = drawValueLabel ? ValueGUIContent : GUIContent.none;

if (SOArchitecture_EditorUtility.HasPropertyDrawer(type) || typeof(Object).IsAssignableFrom(type) || type.IsEnum)
{
//Unity doesn't like it when you have scene objects on assets,
Expand All @@ -47,10 +24,9 @@ public static void DrawPropertyDrawer(Rect rect, Type type, SerializedProperty p
}
else if (type.IsAssignableFrom(typeof(Quaternion)))
{
property.quaternionValue = EditorGUI.Vector4Field(
rect,
label,
property.quaternionValue.ToVector4()).ToQuaternion();
Vector4 displayValue = property.quaternionValue.ToVector4();

property.quaternionValue = EditorGUI.Vector4Field(rect, label, displayValue).ToQuaternion();
}
else if (type.IsAssignableFrom(typeof(Vector4)))
{
Expand All @@ -66,14 +42,9 @@ public static void DrawPropertyDrawer(Rect rect, Type type, SerializedProperty p
EditorGUI.LabelField(rect, errorLabel);
}
}

/// <summary>
/// Draws a property drawer using the <see cref="EditorGUILayout"/> methods.
/// </summary>
public static void DrawPropertyDrawerLayout(Type type, SerializedProperty property, GUIContent errorLabel, bool drawValueLabel = false)

public static void DrawPropertyDrawerLayout(Type type, GUIContent label, SerializedProperty property, GUIContent errorLabel)
{
GUIContent label = drawValueLabel ? ValueGUIContent : GUIContent.none;

if (SOArchitecture_EditorUtility.HasPropertyDrawer(type) || typeof(Object).IsAssignableFrom(type) || type.IsEnum)
{
//Unity doesn't like it when you have scene objects on assets,
Expand All @@ -89,9 +60,9 @@ public static void DrawPropertyDrawerLayout(Type type, SerializedProperty proper
}
else if (type.IsAssignableFrom(typeof(Quaternion)))
{
property.quaternionValue = EditorGUILayout.Vector4Field(
label,
property.quaternionValue.ToVector4()).ToQuaternion();
Vector4 displayValue = property.quaternionValue.ToVector4();

property.quaternionValue = EditorGUILayout.Vector4Field(label, displayValue).ToQuaternion();
}
else if (type.IsAssignableFrom(typeof(Vector4)))
{
Expand All @@ -107,16 +78,5 @@ public static void DrawPropertyDrawerLayout(Type type, SerializedProperty proper
EditorGUILayout.LabelField(errorLabel);
}
}

/// <summary>
/// Returns true if the passed <see cref="Type"/> <paramref name="type"/> should be displayed on
/// single line regardless of whether <see cref="EditorGUI.GetPropertyHeight(SerializedProperty)"/>
/// says otherwise.
/// </summary>
public static bool IsSingleLineGUIType(Type type)
{
return type.IsAssignableFrom(typeof(Vector4)) ||
type.IsAssignableFrom(typeof(Quaternion));
}
}
}
Expand Up @@ -53,7 +53,7 @@ protected virtual void DrawValue()
using (var scope = new EditorGUI.ChangeCheckScope())
{
string content = "Cannot display value. No PropertyDrawer for (" + Target.Type + ") [" + Target.ToString() + "]";
GenericPropertyDrawer.DrawPropertyDrawerLayout(Target.Type, _valueProperty, new GUIContent(content, content), true);
GenericPropertyDrawer.DrawPropertyDrawerLayout(Target.Type, new GUIContent("Value"), _valueProperty, new GUIContent(content, content));

if (scope.changed)
{
Expand Down
17 changes: 2 additions & 15 deletions Assets/SO Architecture/Editor/Inspectors/CollectionEditor.cs
Expand Up @@ -51,7 +51,6 @@ private void OnEnable()
{
drawHeaderCallback = DrawHeader,
drawElementCallback = DrawElement,
elementHeightCallback = GetElementHeight
};
}
public override void OnInspectorGUI()
Expand All @@ -73,26 +72,14 @@ private void DrawHeader(Rect rect)
}
private void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
rect = SOArchitecture_EditorUtility.GetReorderableListElementFieldRect(rect);
SerializedProperty property = CollectionItemsProperty.GetArrayElementAtIndex(index);

EditorGUI.BeginDisabledGroup(DISABLE_ELEMENTS);

GenericPropertyDrawer.DrawPropertyDrawer(rect, Target.Type, property, _noPropertyDrawerWarningGUIContent);
GenericPropertyDrawer.DrawPropertyDrawer(rect, new GUIContent("Element " + index), Target.Type, property, _noPropertyDrawerWarningGUIContent);

EditorGUI.EndDisabledGroup();
}

/// <summary>
/// Return the height of the item in the <see cref="ReorderableList"/> that is being drawn.
/// </summary>
/// <param name="index">The index of the item in the list being drawn.</param>
/// <returns></returns>
private float GetElementHeight(int index)
{
var indexedItemProperty = CollectionItemsProperty.GetArrayElementAtIndex(index);
return GenericPropertyDrawer.IsSingleLineGUIType(Target.Type)
? EditorGUIUtility.singleLineHeight
: EditorGUI.GetPropertyHeight(indexedItemProperty);
}
}
}
11 changes: 11 additions & 0 deletions Assets/SO Architecture/Editor/SOArchitecture_EditorUtility.cs
Expand Up @@ -36,6 +36,17 @@ private static void CreateDebugStyle()

DebugStyle.normal.background = CreateTexture(2, 2, debugColor);
}

/// <summary>
/// Converts the entire rect of a <see cref="UnityEditorInternal.ReorderableList"/> element into a rect used for displaying a field
/// </summary>
public static Rect GetReorderableListElementFieldRect(Rect elementRect)
{
elementRect.height = EditorGUIUtility.singleLineHeight;
elementRect.y++;

return elementRect;
}
public static bool SupportsMultiLine(Type type)
{
return type.GetCustomAttributes(typeof(MultiLine), true).Length > 0;
Expand Down

0 comments on commit 174d153

Please sign in to comment.