mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-22 08:08:51 -05:00
Move AtomListAttribute to core
This commit is contained in:
parent
24831f5927
commit
9fd64a12f3
@ -7,127 +7,6 @@ namespace UnityAtoms.BaseAtoms.Editor
|
||||
/// <summary>
|
||||
/// A custom property drawer for AtomBaseVariableList.
|
||||
/// </summary>
|
||||
[CustomPropertyDrawer(typeof(AtomListAttribute))]
|
||||
[CustomPropertyDrawer(typeof(AtomBaseVariableList))]
|
||||
public class AtomListDrawer : PropertyDrawer
|
||||
{
|
||||
static int INDEX_LABEL_WIDTH = 16;
|
||||
static int BUTTON_WIDTH = 24;
|
||||
static GUIContent PLUS_ICON = IconContent("Toolbar Plus", "Add entry");
|
||||
static GUIContent MINUS_ICON = IconContent("Toolbar Minus", "Remove entry");
|
||||
static float DRAWER_MARGIN = 6f;
|
||||
static float LINE_BOTTOM_MARGIN = 4f;
|
||||
static float GUTTER = 6f;
|
||||
static string SERIALIZED_LIST_PROPNAME = "_serializedList";
|
||||
static string LIST_LABEL_NAME = "List";
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
AtomListAttribute atomListAttr = attribute as AtomListAttribute;
|
||||
|
||||
var propertyHeight = EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN + DRAWER_MARGIN * 2f;
|
||||
var listProperty = property.FindPropertyRelative(atomListAttr != null && !string.IsNullOrWhiteSpace(atomListAttr.ChildPropName) ? atomListAttr.ChildPropName : SERIALIZED_LIST_PROPNAME);
|
||||
|
||||
var length = listProperty.arraySize;
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
var itemProp = listProperty.GetArrayElementAtIndex(i);
|
||||
var itemPropHeight = EditorGUI.GetPropertyHeight(itemProp);
|
||||
propertyHeight += itemPropHeight + LINE_BOTTOM_MARGIN;
|
||||
}
|
||||
|
||||
return propertyHeight;
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
AtomListAttribute atomListAttr = attribute as AtomListAttribute;
|
||||
|
||||
label = EditorGUI.BeginProperty(position, label, property);
|
||||
var proColor = new Color(83f / 255f, 83f / 255f, 83f / 255f);
|
||||
var basicColor = new Color(174f / 255f, 174f / 255f, 174f / 255f);
|
||||
EditorGUI.DrawRect(position, EditorGUIUtility.isProSkin ? proColor : basicColor);
|
||||
|
||||
var indent = EditorGUI.indentLevel;
|
||||
EditorGUI.indentLevel = 0;
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
var listArrayProperty = property.FindPropertyRelative(atomListAttr != null && !string.IsNullOrWhiteSpace(atomListAttr.ChildPropName) ? atomListAttr.ChildPropName : SERIALIZED_LIST_PROPNAME);
|
||||
|
||||
var restRect = new Rect();
|
||||
var initialPosition = new Rect(position);
|
||||
initialPosition.y = initialPosition.y + DRAWER_MARGIN;
|
||||
initialPosition.x = initialPosition.x + DRAWER_MARGIN;
|
||||
initialPosition.width = initialPosition.width - DRAWER_MARGIN * 2f;
|
||||
|
||||
var labelPosition = IMGUIUtils.SnipRectH(initialPosition, initialPosition.width - BUTTON_WIDTH, out restRect);
|
||||
labelPosition.height = EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
|
||||
EditorGUI.PrefixLabel(initialPosition, new GUIContent(atomListAttr != null ? (atomListAttr.Label ?? label.text) : LIST_LABEL_NAME));
|
||||
|
||||
var addButtonPosition = IMGUIUtils.SnipRectH(restRect, restRect.width, out restRect);
|
||||
addButtonPosition.height = EditorGUIUtility.singleLineHeight;
|
||||
var insertIndex = -1;
|
||||
if (GUI.Button(addButtonPosition, PLUS_ICON))
|
||||
{
|
||||
insertIndex = listArrayProperty.arraySize;
|
||||
}
|
||||
|
||||
var linePosition = new Rect(initialPosition);
|
||||
linePosition.height = EditorGUIUtility.singleLineHeight;
|
||||
linePosition.y += EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
|
||||
|
||||
var indexToDelete = -1;
|
||||
var length = listArrayProperty.arraySize;
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
var itemProp = listArrayProperty.GetArrayElementAtIndex(i);
|
||||
|
||||
var indexLabelPos = IMGUIUtils.SnipRectH(linePosition, INDEX_LABEL_WIDTH, out restRect, GUTTER);
|
||||
EditorGUI.PrefixLabel(indexLabelPos, new GUIContent(i.ToString()));
|
||||
|
||||
var itemPos = IMGUIUtils.SnipRectH(restRect, linePosition.width - BUTTON_WIDTH - INDEX_LABEL_WIDTH - GUTTER * 2, out restRect, GUTTER);
|
||||
EditorGUI.PropertyField(itemPos, itemProp, GUIContent.none, atomListAttr != null ? atomListAttr.IncludeChildrenForItems : false);
|
||||
|
||||
var removeButtonPosition = new Rect(restRect);
|
||||
removeButtonPosition.height = EditorGUIUtility.singleLineHeight;
|
||||
|
||||
if (GUI.Button(removeButtonPosition, MINUS_ICON))
|
||||
{
|
||||
indexToDelete = i;
|
||||
}
|
||||
|
||||
linePosition.y += EditorGUI.GetPropertyHeight(itemProp) + LINE_BOTTOM_MARGIN;
|
||||
}
|
||||
|
||||
if (insertIndex != -1)
|
||||
{
|
||||
if (listArrayProperty != null)
|
||||
{
|
||||
listArrayProperty.InsertArrayElementAtIndex(insertIndex);
|
||||
var newProp = listArrayProperty.GetArrayElementAtIndex(insertIndex);
|
||||
newProp.isExpanded = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (indexToDelete != -1)
|
||||
{
|
||||
if (listArrayProperty != null)
|
||||
{
|
||||
listArrayProperty.RemoveArrayElement(indexToDelete);
|
||||
}
|
||||
}
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
property.serializedObject.ApplyModifiedProperties();
|
||||
|
||||
EditorGUI.indentLevel = indent;
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
|
||||
static GUIContent IconContent(string name, string tooltip)
|
||||
{
|
||||
var builtinIcon = EditorGUIUtility.IconContent(name);
|
||||
return new GUIContent(builtinIcon.image, tooltip);
|
||||
}
|
||||
}
|
||||
public class AtomListDrawer : AtomListAttributeDrawer { }
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed96ad5d334a847438fd3f164b941c3f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
131
Packages/Core/Editor/Drawers/AtomListAttributeDrawer.cs
Normal file
131
Packages/Core/Editor/Drawers/AtomListAttributeDrawer.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityAtoms.Editor
|
||||
{
|
||||
/// <summary>
|
||||
/// A custom property drawer for properties using the `AtomList` attribute.
|
||||
/// </summary>
|
||||
[CustomPropertyDrawer(typeof(AtomListAttribute))]
|
||||
public class AtomListAttributeDrawer : PropertyDrawer
|
||||
{
|
||||
static int INDEX_LABEL_WIDTH = 16;
|
||||
static int BUTTON_WIDTH = 24;
|
||||
static GUIContent PLUS_ICON = IconContent("Toolbar Plus", "Add entry");
|
||||
static GUIContent MINUS_ICON = IconContent("Toolbar Minus", "Remove entry");
|
||||
static float DRAWER_MARGIN = 6f;
|
||||
static float LINE_BOTTOM_MARGIN = 4f;
|
||||
static float GUTTER = 6f;
|
||||
static string SERIALIZED_LIST_PROPNAME = "_serializedList";
|
||||
static string LIST_LABEL_NAME = "List";
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
||||
{
|
||||
AtomListAttribute atomListAttr = attribute as AtomListAttribute;
|
||||
|
||||
var propertyHeight = EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN + DRAWER_MARGIN * 2f;
|
||||
var listProperty = property.FindPropertyRelative(atomListAttr != null && !string.IsNullOrWhiteSpace(atomListAttr.ChildPropName) ? atomListAttr.ChildPropName : SERIALIZED_LIST_PROPNAME);
|
||||
|
||||
var length = listProperty.arraySize;
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
var itemProp = listProperty.GetArrayElementAtIndex(i);
|
||||
var itemPropHeight = EditorGUI.GetPropertyHeight(itemProp);
|
||||
propertyHeight += itemPropHeight + LINE_BOTTOM_MARGIN;
|
||||
}
|
||||
|
||||
return propertyHeight;
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
AtomListAttribute atomListAttr = attribute as AtomListAttribute;
|
||||
|
||||
label = EditorGUI.BeginProperty(position, label, property);
|
||||
var proColor = new Color(83f / 255f, 83f / 255f, 83f / 255f);
|
||||
var basicColor = new Color(174f / 255f, 174f / 255f, 174f / 255f);
|
||||
EditorGUI.DrawRect(position, EditorGUIUtility.isProSkin ? proColor : basicColor);
|
||||
|
||||
var indent = EditorGUI.indentLevel;
|
||||
EditorGUI.indentLevel = 0;
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
var listArrayProperty = property.FindPropertyRelative(atomListAttr != null && !string.IsNullOrWhiteSpace(atomListAttr.ChildPropName) ? atomListAttr.ChildPropName : SERIALIZED_LIST_PROPNAME);
|
||||
|
||||
var restRect = new Rect();
|
||||
var initialPosition = new Rect(position);
|
||||
initialPosition.y = initialPosition.y + DRAWER_MARGIN;
|
||||
initialPosition.x = initialPosition.x + DRAWER_MARGIN;
|
||||
initialPosition.width = initialPosition.width - DRAWER_MARGIN * 2f;
|
||||
|
||||
var labelPosition = IMGUIUtils.SnipRectH(initialPosition, initialPosition.width - BUTTON_WIDTH, out restRect);
|
||||
labelPosition.height = EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
|
||||
EditorGUI.PrefixLabel(initialPosition, new GUIContent(atomListAttr != null ? (atomListAttr.Label ?? label.text) : LIST_LABEL_NAME));
|
||||
|
||||
var addButtonPosition = IMGUIUtils.SnipRectH(restRect, restRect.width, out restRect);
|
||||
addButtonPosition.height = EditorGUIUtility.singleLineHeight;
|
||||
var insertIndex = -1;
|
||||
if (GUI.Button(addButtonPosition, PLUS_ICON))
|
||||
{
|
||||
insertIndex = listArrayProperty.arraySize;
|
||||
}
|
||||
|
||||
var linePosition = new Rect(initialPosition);
|
||||
linePosition.height = EditorGUIUtility.singleLineHeight;
|
||||
linePosition.y += EditorGUIUtility.singleLineHeight + LINE_BOTTOM_MARGIN;
|
||||
|
||||
var indexToDelete = -1;
|
||||
var length = listArrayProperty.arraySize;
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
var itemProp = listArrayProperty.GetArrayElementAtIndex(i);
|
||||
|
||||
var indexLabelPos = IMGUIUtils.SnipRectH(linePosition, INDEX_LABEL_WIDTH, out restRect, GUTTER);
|
||||
EditorGUI.PrefixLabel(indexLabelPos, new GUIContent(i.ToString()));
|
||||
|
||||
var itemPos = IMGUIUtils.SnipRectH(restRect, linePosition.width - BUTTON_WIDTH - INDEX_LABEL_WIDTH - GUTTER * 2, out restRect, GUTTER);
|
||||
EditorGUI.PropertyField(itemPos, itemProp, GUIContent.none, atomListAttr != null ? atomListAttr.IncludeChildrenForItems : false);
|
||||
|
||||
var removeButtonPosition = new Rect(restRect);
|
||||
removeButtonPosition.height = EditorGUIUtility.singleLineHeight;
|
||||
|
||||
if (GUI.Button(removeButtonPosition, MINUS_ICON))
|
||||
{
|
||||
indexToDelete = i;
|
||||
}
|
||||
|
||||
linePosition.y += EditorGUI.GetPropertyHeight(itemProp) + LINE_BOTTOM_MARGIN;
|
||||
}
|
||||
|
||||
if (insertIndex != -1)
|
||||
{
|
||||
if (listArrayProperty != null)
|
||||
{
|
||||
listArrayProperty.InsertArrayElementAtIndex(insertIndex);
|
||||
var newProp = listArrayProperty.GetArrayElementAtIndex(insertIndex);
|
||||
newProp.isExpanded = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (indexToDelete != -1)
|
||||
{
|
||||
if (listArrayProperty != null)
|
||||
{
|
||||
listArrayProperty.RemoveArrayElement(indexToDelete);
|
||||
}
|
||||
}
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
property.serializedObject.ApplyModifiedProperties();
|
||||
|
||||
EditorGUI.indentLevel = indent;
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
|
||||
static GUIContent IconContent(string name, string tooltip)
|
||||
{
|
||||
var builtinIcon = EditorGUIUtility.IconContent(name);
|
||||
return new GUIContent(builtinIcon.image, tooltip);
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Core/Editor/Drawers/AtomListAttributeDrawer.cs.meta
Normal file
11
Packages/Core/Editor/Drawers/AtomListAttributeDrawer.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72134fc0222df496fb1d6bb39bd94002
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93604d20e8e8f4a7da3af79e4303a48f
|
||||
guid: 59cc564b0be81473aa4950528359c8f8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
@ -1,7 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityAtoms.BaseAtoms
|
||||
namespace UnityAtoms
|
||||
{
|
||||
/// <summary>
|
||||
/// Needed in order to create a property drawer for a List / Array. See this for more info: https://answers.unity.com/questions/605875/custompropertydrawer-for-array-types-in-43.html
|
@ -1,6 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityAtoms.BaseAtoms
|
||||
namespace UnityAtoms
|
||||
{
|
||||
public class AtomListAttribute : PropertyAttribute
|
||||
{
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using UnityAtoms.BaseAtoms;
|
||||
|
||||
namespace UnityAtoms.FSM
|
||||
{
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using UnityAtoms.BaseAtoms;
|
||||
|
||||
namespace UnityAtoms.FSM
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user