mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-21 09:46:03 +08:00
Rewrite that caches the ordered property attributes so the gui layout drawer can draw them in the correct order.
Rewrote how the property attributes are drawn, more compact less messy code.
This commit is contained in:
parent
984fe730c0
commit
72ff273b5c
@ -41,14 +41,7 @@ namespace XNodeEditor {
|
|||||||
else {
|
else {
|
||||||
Rect rect = new Rect();
|
Rect rect = new Rect();
|
||||||
|
|
||||||
float spacePadding = 0;
|
List<PropertyAttribute> propertyAttributes = NodeEditorUtilities.GetCachedPropertyAttribs(port.node.GetType(), property.name);
|
||||||
SpaceAttribute spaceAttribute;
|
|
||||||
if (NodeEditorUtilities.GetCachedAttrib(port.node.GetType(), property.name, out spaceAttribute)) spacePadding = spaceAttribute.height;
|
|
||||||
|
|
||||||
//GUI Values are from https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ScriptAttributeGUI/Implementations/DecoratorDrawers.cs
|
|
||||||
float headerPadding = 0;
|
|
||||||
HeaderAttribute headerAttribute;
|
|
||||||
if (NodeEditorUtilities.GetCachedAttrib(port.node.GetType(), property.name, out headerAttribute)) headerPadding = EditorGUIUtility.singleLineHeight * 1.5f;
|
|
||||||
|
|
||||||
// If property is an input, display a regular property field and put a port handle on the left side
|
// If property is an input, display a regular property field and put a port handle on the left side
|
||||||
if (port.direction == XNode.NodePort.IO.Input) {
|
if (port.direction == XNode.NodePort.IO.Input) {
|
||||||
@ -61,21 +54,24 @@ namespace XNodeEditor {
|
|||||||
showBacking = inputAttribute.backingValue;
|
showBacking = inputAttribute.backingValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Call GUILayout.Space if Space attribute is set and we are NOT drawing a PropertyField
|
bool usePropertyAttributes = dynamicPortList ||
|
||||||
bool useLayoutSpace = dynamicPortList ||
|
|
||||||
showBacking == XNode.Node.ShowBackingValue.Never ||
|
showBacking == XNode.Node.ShowBackingValue.Never ||
|
||||||
(showBacking == XNode.Node.ShowBackingValue.Unconnected && port.IsConnected);
|
(showBacking == XNode.Node.ShowBackingValue.Unconnected && port.IsConnected);
|
||||||
if (spacePadding > 0 && useLayoutSpace) {
|
|
||||||
GUILayout.Space(spacePadding);
|
float spacePadding = 0;
|
||||||
spacePadding = 0;
|
foreach (var attr in propertyAttributes) {
|
||||||
}
|
if (attr is SpaceAttribute) {
|
||||||
if (headerPadding > 0 && useLayoutSpace)
|
if (usePropertyAttributes) GUILayout.Space((attr as SpaceAttribute).height);
|
||||||
{
|
else spacePadding += (attr as SpaceAttribute).height;
|
||||||
Rect position = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight * 1.5f);
|
} else if (attr is HeaderAttribute) {
|
||||||
position.yMin += (EditorGUIUtility.singleLineHeight * 0.5f) + EditorGUIUtility.standardVerticalSpacing;
|
if (usePropertyAttributes) {
|
||||||
position = EditorGUI.IndentedRect(position);
|
//GUI Values are from https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ScriptAttributeGUI/Implementations/DecoratorDrawers.cs
|
||||||
GUI.Label(position, headerAttribute.header, EditorStyles.boldLabel);
|
Rect position = GUILayoutUtility.GetRect(0, (EditorGUIUtility.singleLineHeight * 1.5f) - EditorGUIUtility.standardVerticalSpacing); //Layout adds standardVerticalSpacing after rect so we subtract it.
|
||||||
headerPadding = 0;
|
position.yMin += EditorGUIUtility.singleLineHeight * 0.5f;
|
||||||
|
position = EditorGUI.IndentedRect(position);
|
||||||
|
GUI.Label(position, (attr as HeaderAttribute).header, EditorStyles.boldLabel);
|
||||||
|
} else spacePadding += EditorGUIUtility.singleLineHeight * 1.5f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dynamicPortList) {
|
if (dynamicPortList) {
|
||||||
@ -102,7 +98,7 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rect = GUILayoutUtility.GetLastRect();
|
rect = GUILayoutUtility.GetLastRect();
|
||||||
rect.position = rect.position - new Vector2(16, -(spacePadding + headerPadding));
|
rect.position = rect.position - new Vector2(16, -spacePadding);
|
||||||
// If property is an output, display a text label and put a port handle on the right side
|
// If property is an output, display a text label and put a port handle on the right side
|
||||||
} else if (port.direction == XNode.NodePort.IO.Output) {
|
} else if (port.direction == XNode.NodePort.IO.Output) {
|
||||||
// Get data from [Output] attribute
|
// Get data from [Output] attribute
|
||||||
@ -114,21 +110,24 @@ namespace XNodeEditor {
|
|||||||
showBacking = outputAttribute.backingValue;
|
showBacking = outputAttribute.backingValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Call GUILayout.Space if Space attribute is set and we are NOT drawing a PropertyField
|
bool usePropertyAttributes = dynamicPortList ||
|
||||||
bool useLayoutSpace = dynamicPortList ||
|
|
||||||
showBacking == XNode.Node.ShowBackingValue.Never ||
|
showBacking == XNode.Node.ShowBackingValue.Never ||
|
||||||
(showBacking == XNode.Node.ShowBackingValue.Unconnected && port.IsConnected);
|
(showBacking == XNode.Node.ShowBackingValue.Unconnected && port.IsConnected);
|
||||||
if (spacePadding > 0 && useLayoutSpace) {
|
|
||||||
GUILayout.Space(spacePadding);
|
float spacePadding = 0;
|
||||||
spacePadding = 0;
|
foreach (var attr in propertyAttributes) {
|
||||||
}
|
if (attr is SpaceAttribute) {
|
||||||
if (headerPadding > 0 && useLayoutSpace)
|
if (usePropertyAttributes) GUILayout.Space((attr as SpaceAttribute).height);
|
||||||
{
|
else spacePadding += (attr as SpaceAttribute).height;
|
||||||
Rect position = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight * 1.5f);
|
} else if (attr is HeaderAttribute) {
|
||||||
position.yMin += (EditorGUIUtility.singleLineHeight * 0.5f) + EditorGUIUtility.standardVerticalSpacing;
|
if (usePropertyAttributes) {
|
||||||
position = EditorGUI.IndentedRect(position);
|
//GUI Values are from https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ScriptAttributeGUI/Implementations/DecoratorDrawers.cs
|
||||||
GUI.Label(position, headerAttribute.header, EditorStyles.boldLabel);
|
Rect position = GUILayoutUtility.GetRect(0, (EditorGUIUtility.singleLineHeight * 1.5f) - EditorGUIUtility.standardVerticalSpacing); //Layout adds standardVerticalSpacing after rect so we subtract it.
|
||||||
headerPadding = 0;
|
position.yMin += EditorGUIUtility.singleLineHeight * 0.5f;
|
||||||
|
position = EditorGUI.IndentedRect(position);
|
||||||
|
GUI.Label(position, (attr as HeaderAttribute).header, EditorStyles.boldLabel);
|
||||||
|
} else spacePadding += EditorGUIUtility.singleLineHeight * 1.5f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dynamicPortList) {
|
if (dynamicPortList) {
|
||||||
@ -155,7 +154,7 @@ namespace XNodeEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rect = GUILayoutUtility.GetLastRect();
|
rect = GUILayoutUtility.GetLastRect();
|
||||||
rect.position = rect.position + new Vector2(rect.width, spacePadding + headerPadding);
|
rect.position = rect.position + new Vector2(rect.width, spacePadding);
|
||||||
}
|
}
|
||||||
|
|
||||||
rect.size = new Vector2(16, 16);
|
rect.size = new Vector2(16, 16);
|
||||||
|
|||||||
@ -18,6 +18,9 @@ namespace XNodeEditor {
|
|||||||
/// Saves Attribute from Type+Field for faster lookup. Resets on recompiles.
|
/// Saves Attribute from Type+Field for faster lookup. Resets on recompiles.
|
||||||
private static Dictionary<Type, Dictionary<string, Dictionary<Type, Attribute>>> typeAttributes = new Dictionary<Type, Dictionary<string, Dictionary<Type, Attribute>>>();
|
private static Dictionary<Type, Dictionary<string, Dictionary<Type, Attribute>>> typeAttributes = new Dictionary<Type, Dictionary<string, Dictionary<Type, Attribute>>>();
|
||||||
|
|
||||||
|
/// Saves ordered PropertyAttribute from Type+Field for faster lookup. Resets on recompiles.
|
||||||
|
private static Dictionary<Type, Dictionary<string, List<PropertyAttribute>>> typeOrderedPropertyAttributes = new Dictionary<Type, Dictionary<string, List<PropertyAttribute>>>();
|
||||||
|
|
||||||
public static bool GetAttrib<T>(Type classType, out T attribOut) where T : Attribute {
|
public static bool GetAttrib<T>(Type classType, out T attribOut) where T : Attribute {
|
||||||
object[] attribs = classType.GetCustomAttributes(typeof(T), false);
|
object[] attribs = classType.GetCustomAttributes(typeof(T), false);
|
||||||
return GetAttrib(attribs, out attribOut);
|
return GetAttrib(attribs, out attribOut);
|
||||||
@ -84,6 +87,24 @@ namespace XNodeEditor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<PropertyAttribute> GetCachedPropertyAttribs(Type classType, string fieldName) {
|
||||||
|
Dictionary<string, List<PropertyAttribute>> typeFields;
|
||||||
|
if (!typeOrderedPropertyAttributes.TryGetValue(classType, out typeFields)) {
|
||||||
|
typeFields = new Dictionary<string, List<PropertyAttribute>>();
|
||||||
|
typeOrderedPropertyAttributes.Add(classType, typeFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<PropertyAttribute> typeAttributes;
|
||||||
|
if (!typeFields.TryGetValue(fieldName, out typeAttributes)) {
|
||||||
|
FieldInfo field = classType.GetFieldInfo(fieldName);
|
||||||
|
object[] attribs = field.GetCustomAttributes(typeof(PropertyAttribute), true);
|
||||||
|
typeAttributes = attribs.Cast<PropertyAttribute>().Reverse().ToList(); //Unity draws them in reverse
|
||||||
|
typeFields.Add(fieldName, typeAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return typeAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
public static bool IsMac() {
|
public static bool IsMac() {
|
||||||
#if UNITY_2017_1_OR_NEWER
|
#if UNITY_2017_1_OR_NEWER
|
||||||
return SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX;
|
return SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user