1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Added ShowBackingValue options for Input and Output

Added NodeEditorUtilities.GetAttrib<T>(Type classType, string fieldName, out T attribute)
This commit is contained in:
Thor Brigsted 2017-11-27 00:59:16 +01:00
parent 8c688b9f8b
commit fe057a7be2
3 changed files with 54 additions and 6 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using XNode; using XNode;
@ -36,15 +37,54 @@ namespace XNodeEditor {
// 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 == NodePort.IO.Input) { if (port.direction == NodePort.IO.Input) {
// Display a label if port is connected // Get data from [Input] attribute
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName)); Node.ShowBackingValue showBacking = Node.ShowBackingValue.Unconnected;
// Display an editable property field if port is not connected Node.InputAttribute inputAttribute;
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30)); if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out inputAttribute)) showBacking = inputAttribute.backingValue;
switch (showBacking) {
case Node.ShowBackingValue.Unconnected:
// Display a label if port is connected
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
// Display an editable property field if port is not connected
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
case Node.ShowBackingValue.Never:
// Display a label
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName));
break;
case Node.ShowBackingValue.Always:
// Display an editable property field
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
}
rect = GUILayoutUtility.GetLastRect(); rect = GUILayoutUtility.GetLastRect();
rect.position = rect.position - new Vector2(16, 0); rect.position = rect.position - new Vector2(16, 0);
// 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 == NodePort.IO.Output) { } else if (port.direction == NodePort.IO.Output) {
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.styles.outputPort, GUILayout.MinWidth(30)); // Get data from [Output] attribute
Node.ShowBackingValue showBacking = Node.ShowBackingValue.Unconnected;
Node.OutputAttribute outputAttribute;
if (NodeEditorUtilities.GetAttrib(port.node.GetType(), property.name, out outputAttribute)) showBacking = outputAttribute.backingValue;
switch (showBacking) {
case Node.ShowBackingValue.Unconnected:
// Display a label if port is connected
if (port.IsConnected) EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.styles.outputPort, GUILayout.MinWidth(30));
// Display an editable property field if port is not connected
else EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
case Node.ShowBackingValue.Never:
// Display a label
EditorGUILayout.LabelField(label != null ? label : new GUIContent(property.displayName), NodeEditorResources.styles.outputPort, GUILayout.MinWidth(30));
break;
case Node.ShowBackingValue.Always:
// Display an editable property field
EditorGUILayout.PropertyField(property, label, includeChildren, GUILayout.MinWidth(30));
break;
}
rect = GUILayoutUtility.GetLastRect(); rect = GUILayoutUtility.GetLastRect();
rect.position = rect.position + new Vector2(rect.width, 0); rect.position = rect.position + new Vector2(rect.width, 0);
} }

View File

@ -23,6 +23,11 @@ namespace XNodeEditor {
return false; return false;
} }
public static bool GetAttrib<T>(Type classType, string fieldName, out T attribOut) where T : Attribute {
object[] attribs = classType.GetField(fieldName).GetCustomAttributes(typeof(T), false);
return GetAttrib(attribs, out attribOut);
}
public static bool HasAttrib<T>(object[] attribs) where T : Attribute { public static bool HasAttrib<T>(object[] attribs) where T : Attribute {
for (int i = 0; i < attribs.Length; i++) { for (int i = 0; i < attribs.Length; i++) {
if (attribs[i].GetType() == typeof(T)) { if (attribs[i].GetType() == typeof(T)) {

View File

@ -160,8 +160,11 @@ namespace XNode {
/// <summary> Mark a serializable field as an output port. You can access this through <see cref="GetOutput(string)"/> </summary> /// <summary> Mark a serializable field as an output port. You can access this through <see cref="GetOutput(string)"/> </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public class OutputAttribute : Attribute { public class OutputAttribute : Attribute {
public ShowBackingValue backingValue;
/// <summary> Mark a serializable field as an output port. You can access this through <see cref="GetOutput(string)"/> </summary> /// <summary> Mark a serializable field as an output port. You can access this through <see cref="GetOutput(string)"/> </summary>
public OutputAttribute() { } /// <param name="backingValue">Should we display the backing value for this port as an editor field? </param>
public OutputAttribute(ShowBackingValue backingValue = ShowBackingValue.Never) { this.backingValue = backingValue; }
} }
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]