Skip to content

Commit

Permalink
Merge pull request #4 from Sibz/v1.1.0
Browse files Browse the repository at this point in the history
V1.1.0
  • Loading branch information
Sibz authored Nov 21, 2019
2 parents 713ac56 + 0812fc8 commit 18243fd
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 59 deletions.
28 changes: 17 additions & 11 deletions Editor/ListDrawer.Methods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEditor;
using System;
using UnityEditor;
using UnityEngine;

namespace Sibz.EditorList
Expand All @@ -15,6 +16,8 @@ public abstract partial class ListDrawer<T> : PropertyDrawer
/// </summary>
/// <param name="property">The main SerializedProperty the list belongs to</param>
/// <param name="listProperty">The list SerializedProperty that has the associated array attached.</param>
protected virtual void ClearList() => ListProperty.ClearArray();
[Obsolete("Use ClearList() instead")]
protected virtual void ClearList(SerializedProperty listProperty) => listProperty.ClearArray();

/// <summary>
Expand All @@ -24,11 +27,12 @@ public abstract partial class ListDrawer<T> : PropertyDrawer
/// <param name="listProperty"></param>
/// <param name="listItemProperty"></param>
/// <param name="index"></param>
protected virtual void DeleteItem(SerializedProperty listProperty, SerializedProperty listItemProperty, int index)
protected virtual void DeleteItem(SerializedProperty listItemProperty, int index)
{
listProperty.DeleteArrayElementAtIndex(index);
ListProperty.DeleteArrayElementAtIndex(index);
}

[Obsolete("Use DeleteItem(SerializedProperty listItemProperty, int index) instead")]
protected virtual void DeleteItem(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => DeleteItem(listItemProperty, index);
/// <summary>
/// Moves an item within the list property. Is only called if toIndex is valid (I.e. not outside the list).
/// <para>Can be overridden if not altering property directly or other logic is required.</para>
Expand All @@ -37,22 +41,24 @@ protected virtual void DeleteItem(SerializedProperty listProperty, SerializedPro
/// <param name="listItemProperty"></param>
/// <param name="fromIndex"></param>
/// <param name="toIndex"></param>
protected virtual void MoveTo(SerializedProperty listProperty, SerializedProperty listItemProperty, int fromIndex, int toIndex)
protected virtual void MoveTo(SerializedProperty listItemProperty, int fromIndex, int toIndex)
{
listProperty.MoveArrayElement(fromIndex, toIndex);
ListProperty.MoveArrayElement(fromIndex, toIndex);
}
[Obsolete("Use MoveTo(SerializedProperty listItemProperty, int index) instead")]
protected virtual void MoveTo(SerializedProperty listProperty, SerializedProperty listItemProperty, int fromIndex, int toIndex) => MoveTo(listItemProperty, fromIndex, toIndex);

/// <summary>
/// Checks item index and calls MoveTo
/// </summary>
/// <param name="listProperty"></param>
/// <param name="listItemProperty"></param>
/// <param name="fromIndex"></param>
private void MoveUp(SerializedProperty listProperty, SerializedProperty listItemProperty, int fromIndex)
protected void MoveUp(SerializedProperty listItemProperty, int fromIndex)
{
if (fromIndex > 0)
{
MoveTo(listProperty, listItemProperty, fromIndex, fromIndex - 1);
MoveTo(listItemProperty, fromIndex, fromIndex - 1);
}
}

Expand All @@ -62,11 +68,11 @@ private void MoveUp(SerializedProperty listProperty, SerializedProperty listItem
/// <param name="listProperty"></param>
/// <param name="listItemProperty"></param>
/// <param name="fromIndex"></param>
private void MoveDown(SerializedProperty listProperty, SerializedProperty listItemProperty, int fromIndex)
protected void MoveDown(SerializedProperty listItemProperty, int fromIndex)
{
if (fromIndex < listProperty.arraySize)
if (fromIndex < ListProperty.arraySize)
{
MoveTo(listProperty, listItemProperty, fromIndex, fromIndex + 1);
MoveTo(listItemProperty, fromIndex, fromIndex + 1);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Editor/ListDrawer.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public abstract partial class ListDrawer<T> : PropertyDrawer

protected SerializedProperty Property { get; private set; }

protected SerializedProperty ListProperty { get; private set; }

/// <summary>
/// Indicates if list is ordered
/// If ordered full set of delete, moveup and movedown buttons
Expand Down
73 changes: 44 additions & 29 deletions Editor/ListDrawer.Sections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ namespace Sibz.EditorList
/// <typeparam name="T">Type of item in the list</typeparam>
public abstract partial class ListDrawer<T> : PropertyDrawer
{

protected virtual void PreGUI() { }
protected virtual void PostGUI() { }

protected virtual void ContentSection(GUIContent label)
{

var listProperty = Property.FindPropertyRelative(nameof(EditorList<T>.List));
if (listProperty != null)
{
HeaderSection(listProperty, label);
HeaderSection(label);

ListAreaSection(listProperty);
ListAreaSection();

FooterSection(listProperty);
FooterSection();
}
else
{
Expand All @@ -32,108 +36,119 @@ protected virtual void ContentSection(GUIContent label)
///
/// </summary>
/// <param name="listProperty"></param>
protected virtual void ListAreaSection(SerializedProperty listProperty)
protected virtual void ListAreaSection()
{
for (int i = 0; i < listProperty.arraySize; i++)
for (int i = 0; i < ListProperty.arraySize; i++)
{
var listItemProperty = listProperty.GetArrayElementAtIndex(i);
ListItemAreaSection(listProperty, listItemProperty, i);
ListItemAreaSection(ListProperty.GetArrayElementAtIndex(i), i);
}
}
[Obsolete("Use ListAreaSection instead")]
[Obsolete("Use ListAreaSection() instead")]
protected virtual void ListAreaDrawer(SerializedProperty listProperty) => ListAreaSection(listProperty);
[Obsolete("Use ListAreaSection() instead")]
protected virtual void ListAreaSection(SerializedProperty listProperty) => ListAreaSection();

/// <summary>
/// Header section. Can be overriden to change what content appears above the list.
/// </summary>
/// <param name="listProperty">The list SerializedProperty that has the associated array attached.</param>
/// <param name="label">Label provided to the main PropertyField</param>
protected virtual void HeaderSection(SerializedProperty listProperty, GUIContent label) { }
[Obsolete("Use HeaderSection instead")]
protected virtual void Header(SerializedProperty listProperty, GUIContent label) => HeaderSection(listProperty, label);
protected virtual void HeaderSection(GUIContent label) { }
[Obsolete("Use HeaderSection() instead")]
protected virtual void HeaderSection(SerializedProperty listProperty, GUIContent label) => HeaderSection(label);
[Obsolete("Use HeaderSection() instead")]
protected virtual void Header(SerializedProperty listProperty, GUIContent label) => HeaderSection(label);

/// <summary>
///
/// </summary>
/// <param name="listProperty"></param>
/// <param name="listItemProperty"></param>
/// <param name="index"></param>
protected virtual void ListItemAreaSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index)
protected virtual void ListItemAreaSection(SerializedProperty listItemProperty, int index)
{
EditorGUILayout.BeginHorizontal();
{
ListItemSection(listProperty, listItemProperty, index);
ListItemSection(listItemProperty, index);

ListItemButtonsSection(listProperty, listItemProperty, index);
ListItemButtonsSection(listItemProperty, index);

}
EditorGUILayout.EndHorizontal();
}
[Obsolete("Use ListItemAreaSection instead")]
protected virtual void ListItemAreaDrawer(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemAreaSection(listProperty, listItemProperty, index);
[Obsolete("Use ListItemAreaSection() instead")]
protected virtual void ListItemAreaDrawer(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemAreaSection(listItemProperty, index);
[Obsolete("Use ListItemAreaSection() instead")]
protected virtual void ListItemAreaSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemAreaSection(listItemProperty, index);

/// <summary>
/// Item section. Defaults to a Property Field. Override if required. Is drawn inside Horizontal Section with buttons.
/// </summary>
/// <param name="listProperty">The list SerializedProperty that has the associated array attached.</param>
/// <param name="listItemProperty">The listItem SerializedProperty</param>
/// <param name="index">Index of item</param>
protected virtual void ListItemSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index)
protected virtual void ListItemSection(SerializedProperty listItemProperty, int index)
{
EditorGUILayout.PropertyField(listItemProperty, GUIContent.none);
}
[Obsolete("Use ListItemSection() instead")]
protected virtual void ListItemDrawer(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemSection(listItemProperty, index);
[Obsolete("Use ListItemSection() instead")]
protected virtual void ListItemSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemSection(listItemProperty, index);

[Obsolete("Use ListItemSection instead")]
protected virtual void ListItemDrawer(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemSection(listProperty, listItemProperty, index);
/// <summary>
/// Item Buttons Section. Defaults to Delete button and if Ordered=true, MoveUp and MoveDown buttons.
/// Is drawn inside Horizontal Section with list item.
/// </summary>
/// <param name="listProperty">The list SerializedProperty that has the associated array attached.</param>
/// <param name="listItemProperty">The listItem SerializedProperty</param>
/// <param name="index">Index of item</param>
protected virtual void ListItemButtonsSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index)
protected virtual void ListItemButtonsSection(SerializedProperty listItemProperty, int index)
{
if (DeleteButton)
{
DeleteItem(listProperty, listItemProperty, index);
DeleteItem(listItemProperty, index);
}
if (Ordered)
{
if (MoveUpButton)
{
MoveUp(listProperty, listItemProperty, index);
MoveUp(listItemProperty, index);
}

if (MoveDownButton)
{
MoveDown(listProperty, listItemProperty, index);
MoveDown(listItemProperty, index);
}
}
}
[Obsolete("Use ListItemButtonsSection instead")]
protected virtual void ListItemButtonsDrawer(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemButtonsSection(listProperty, listItemProperty, index);
[Obsolete("Use ListItemButtonsSection() instead")]
protected virtual void ListItemButtonsDrawer(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemButtonsSection(listItemProperty, index);
[Obsolete("Use ListItemButtonsSection() instead")]
protected virtual void ListItemButtonsSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) => ListItemButtonsSection(listItemProperty, index);

/// <summary>
/// Footer section. Defaults to a Delete All Button.
/// Can be overridden to display other content, or remove the button.
/// </summary>
/// <param name="property">The main SerializedProperty the list belongs to</param>
/// <param name="listProperty">The list SerializedProperty that has the associated array attached.</param>
protected virtual void FooterSection(SerializedProperty listProperty)
protected virtual void FooterSection()
{
EditorGUILayout.BeginHorizontal();
{
int indent = IndentLevelChange + (IndentContent ? 1 : 0);
GUILayout.Label("", GUILayout.MaxWidth(indent * 10));
if (DeleteAllButton)
{
ClearList(listProperty);
ClearList();
}
}
EditorGUILayout.EndHorizontal();
}
[Obsolete("Use FooterSection instead")]
protected virtual void Footer(SerializedProperty listProperty) => FooterSection(listProperty);
[Obsolete("Use FooterSection() instead")]
protected virtual void Footer(SerializedProperty listProperty) => FooterSection();
[Obsolete("Use FooterSection() instead")]
protected virtual void FooterSection(SerializedProperty listProperty) => FooterSection();
}
}
16 changes: 15 additions & 1 deletion Editor/ListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
Property = property;

EditorGUI.BeginProperty(position, label, property);
ListProperty = Property.FindPropertyRelative(nameof(EditorList<T>.List));
if (ListProperty == null)
{
Debug.LogWarning($"{nameof(ListDrawer<T>)}: Unable to get list property. Be sure your property extends EditorList<T> and that the property class & T are Serializable");
return;
}


if (AddColonToLabel)
{
label.text = label.text + ":";
}

EditorGUI.BeginProperty(position, label, property);

PreGUI();

if (UseFoldout && (IsFoldedOut = Foldout(IsFoldedOut, label)) || !UseFoldout)
{
if (!UseFoldout)
Expand All @@ -48,6 +59,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
EditorGUI.indentLevel -= IndentLevelChange;
}

PostGUI();

EditorGUI.EndProperty();
}

Expand Down
Loading

0 comments on commit 18243fd

Please sign in to comment.