diff --git a/Editor/ListDrawer.Methods.cs b/Editor/ListDrawer.Methods.cs index 87d5317..9c41e17 100644 --- a/Editor/ListDrawer.Methods.cs +++ b/Editor/ListDrawer.Methods.cs @@ -1,4 +1,5 @@ -using UnityEditor; +using System; +using UnityEditor; using UnityEngine; namespace Sibz.EditorList @@ -15,6 +16,8 @@ public abstract partial class ListDrawer : PropertyDrawer /// /// The main SerializedProperty the list belongs to /// The list SerializedProperty that has the associated array attached. + protected virtual void ClearList() => ListProperty.ClearArray(); + [Obsolete("Use ClearList() instead")] protected virtual void ClearList(SerializedProperty listProperty) => listProperty.ClearArray(); /// @@ -24,11 +27,12 @@ public abstract partial class ListDrawer : PropertyDrawer /// /// /// - 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); /// /// Moves an item within the list property. Is only called if toIndex is valid (I.e. not outside the list). /// Can be overridden if not altering property directly or other logic is required. @@ -37,10 +41,12 @@ protected virtual void DeleteItem(SerializedProperty listProperty, SerializedPro /// /// /// - 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); /// /// Checks item index and calls MoveTo @@ -48,11 +54,11 @@ protected virtual void MoveTo(SerializedProperty listProperty, SerializedPropert /// /// /// - 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); } } @@ -62,11 +68,11 @@ private void MoveUp(SerializedProperty listProperty, SerializedProperty listItem /// /// /// - 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); } } } diff --git a/Editor/ListDrawer.Properties.cs b/Editor/ListDrawer.Properties.cs index 24c3346..a037281 100644 --- a/Editor/ListDrawer.Properties.cs +++ b/Editor/ListDrawer.Properties.cs @@ -17,6 +17,8 @@ public abstract partial class ListDrawer : PropertyDrawer protected SerializedProperty Property { get; private set; } + protected SerializedProperty ListProperty { get; private set; } + /// /// Indicates if list is ordered /// If ordered full set of delete, moveup and movedown buttons diff --git a/Editor/ListDrawer.Sections.cs b/Editor/ListDrawer.Sections.cs index 381f90f..d4de520 100644 --- a/Editor/ListDrawer.Sections.cs +++ b/Editor/ListDrawer.Sections.cs @@ -10,17 +10,21 @@ namespace Sibz.EditorList /// Type of item in the list public abstract partial class ListDrawer : PropertyDrawer { + + protected virtual void PreGUI() { } + protected virtual void PostGUI() { } + protected virtual void ContentSection(GUIContent label) { var listProperty = Property.FindPropertyRelative(nameof(EditorList.List)); if (listProperty != null) { - HeaderSection(listProperty, label); + HeaderSection(label); - ListAreaSection(listProperty); + ListAreaSection(); - FooterSection(listProperty); + FooterSection(); } else { @@ -32,25 +36,28 @@ protected virtual void ContentSection(GUIContent label) /// /// /// - 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(); /// /// Header section. Can be overriden to change what content appears above the list. /// /// The list SerializedProperty that has the associated array attached. /// Label provided to the main PropertyField - 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); /// /// @@ -58,19 +65,21 @@ protected virtual void HeaderSection(SerializedProperty listProperty, GUIContent /// /// /// - 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); /// /// Item section. Defaults to a Property Field. Override if required. Is drawn inside Horizontal Section with buttons. @@ -78,13 +87,15 @@ protected virtual void ListItemAreaSection(SerializedProperty listProperty, Seri /// The list SerializedProperty that has the associated array attached. /// The listItem SerializedProperty /// Index of item - 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); /// /// Item Buttons Section. Defaults to Delete button and if Ordered=true, MoveUp and MoveDown buttons. /// Is drawn inside Horizontal Section with list item. @@ -92,27 +103,29 @@ protected virtual void ListItemSection(SerializedProperty listProperty, Serializ /// The list SerializedProperty that has the associated array attached. /// The listItem SerializedProperty /// Index of item - 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); /// /// Footer section. Defaults to a Delete All Button. @@ -120,7 +133,7 @@ protected virtual void ListItemButtonsSection(SerializedProperty listProperty, S /// /// The main SerializedProperty the list belongs to /// The list SerializedProperty that has the associated array attached. - protected virtual void FooterSection(SerializedProperty listProperty) + protected virtual void FooterSection() { EditorGUILayout.BeginHorizontal(); { @@ -128,12 +141,14 @@ protected virtual void FooterSection(SerializedProperty listProperty) 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(); } } diff --git a/Editor/ListDrawer.cs b/Editor/ListDrawer.cs index 221ff5a..e31a8e8 100644 --- a/Editor/ListDrawer.cs +++ b/Editor/ListDrawer.cs @@ -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.List)); + if (ListProperty == null) + { + Debug.LogWarning($"{nameof(ListDrawer)}: Unable to get list property. Be sure your property extends EditorList 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) @@ -48,6 +59,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten } EditorGUI.indentLevel -= IndentLevelChange; } + + PostGUI(); + EditorGUI.EndProperty(); } diff --git a/Editor/ObjectListDrawer.cs b/Editor/ObjectListDrawer.cs index dba0ae1..c0458d7 100644 --- a/Editor/ObjectListDrawer.cs +++ b/Editor/ObjectListDrawer.cs @@ -34,12 +34,12 @@ public abstract class ObjectListDrawer : ListDrawer where T : UnityEngine. /// The list SerializedProperty that has the associated array attached. /// The listItem SerializedProperty /// Index of item - protected virtual void ClearItem(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) + protected virtual void ClearItem(SerializedProperty listItemProperty, int index) { // Check value is not null, otherwise we will unset it if (listItemProperty.objectReferenceValue != null) { - listProperty.DeleteArrayElementAtIndex(index); + ListProperty.DeleteArrayElementAtIndex(index); } } @@ -49,17 +49,17 @@ protected virtual void ClearItem(SerializedProperty listProperty, SerializedProp /// The list SerializedProperty that has the associated array attached. /// The listItem SerializedProperty /// Index of item - protected override void DeleteItem(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) + protected override void DeleteItem(SerializedProperty listItemProperty, int index) { - int count = listProperty.arraySize; + int count = ListProperty.arraySize; - base.DeleteItem(listProperty, listItemProperty, index); + base.DeleteItem(listItemProperty, index); // If deleting a object field, the first time will only clear the GO // We need a second attempt to delete it - if (listProperty.arraySize == count) + if (ListProperty.arraySize == count) { - base.DeleteItem(listProperty, listItemProperty, index); + base.DeleteItem(listItemProperty, index); } } @@ -69,14 +69,14 @@ protected override void DeleteItem(SerializedProperty listProperty, SerializedPr /// The main SerializedProperty the list belongs to /// The list SerializedProperty that has the associated array attached. /// Label provided to the main PropertyField - protected override void HeaderSection(SerializedProperty listProperty, GUIContent label) + protected override void HeaderSection(GUIContent label) { T go = null; if ((go = AddObjectField(go)) != null) { - int newItemIndex = listProperty.arraySize; - listProperty.InsertArrayElementAtIndex(newItemIndex); - listProperty.GetArrayElementAtIndex(newItemIndex).objectReferenceValue = go; + int newItemIndex = ListProperty.arraySize; + ListProperty.InsertArrayElementAtIndex(newItemIndex); + ListProperty.GetArrayElementAtIndex(newItemIndex).objectReferenceValue = go; } } @@ -86,16 +86,16 @@ protected override void HeaderSection(SerializedProperty listProperty, GUIConten /// The list SerializedProperty that has the associated array attached. /// The listItem SerializedProperty /// Index of item - protected override void ListItemButtonsSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) + protected override void ListItemButtonsSection(SerializedProperty listItemProperty, int index) { if (AllowNullObjects) { if (ClearButton) { - ClearItem(listProperty, listItemProperty, index); + ClearItem(listItemProperty, index); } } - base.ListItemButtonsSection(listProperty, listItemProperty, index); + base.ListItemButtonsSection(listItemProperty, index); } /// @@ -104,15 +104,15 @@ protected override void ListItemButtonsSection(SerializedProperty listProperty, /// The list SerializedProperty that has the associated array attached. /// The listItem SerializedProperty /// Index of item - protected override void ListItemSection(SerializedProperty listProperty, SerializedProperty listItemProperty, int index) + protected override void ListItemSection(SerializedProperty listItemProperty, int index) { if (!AllowNullObjects && listItemProperty.objectReferenceValue == null) { - DeleteItem(listProperty, listItemProperty, index); + DeleteItem(listItemProperty, index); } else { - base.ListItemSection(listProperty, listItemProperty, index); + base.ListItemSection(listItemProperty, index); } } } diff --git a/package.json b/package.json index 92b3046..87bb3a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.sibz.editorlist", - "version": "1.0.3", + "version": "1.1.0", "displayName": "Sibz: Editor Friendly Lists", "description": "This packages enables lists that can be nicely displayed in the editor and is fully extensible. https://github.com/Sibz/Com.Sibz.EditorList", "unity": "2019.1",