1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-21 01:36:03 +08:00

Merge branch 'development'

This commit is contained in:
Thor Brigsted 2017-11-14 01:07:59 +01:00
commit 5267ac1a30
4 changed files with 50 additions and 6 deletions

View File

@ -159,12 +159,15 @@ namespace XNodeEditor {
hoveredPort = null; hoveredPort = null;
} }
//Save guiColor so we can revert it
Color guiColor = GUI.color;
for (int n = 0; n < graph.nodes.Count; n++) { for (int n = 0; n < graph.nodes.Count; n++) {
while (graph.nodes[n] == null) graph.nodes.RemoveAt(n); while (graph.nodes[n] == null) graph.nodes.RemoveAt(n);
if (n >= graph.nodes.Count) return; if (n >= graph.nodes.Count) return;
Node node = graph.nodes[n]; Node node = graph.nodes[n];
Type nodeType = node.GetType();
NodeEditor nodeEditor = GetNodeEditor(node.GetType()); NodeEditor nodeEditor = GetNodeEditor(nodeType);
nodeEditor.target = node; nodeEditor.target = node;
nodeEditor.serializedObject = new SerializedObject(node); nodeEditor.serializedObject = new SerializedObject(node);
NodeEditor.portPositions = new Dictionary<NodePort, Vector2>(); NodeEditor.portPositions = new Dictionary<NodePort, Vector2>();
@ -175,7 +178,9 @@ namespace XNodeEditor {
GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000))); GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000)));
GUIStyle style = NodeEditorResources.styles.nodeBody; GUIStyle style = NodeEditorResources.styles.nodeBody;
if (nodeTint.ContainsKey(nodeType)) GUI.color = nodeTint[nodeType];
GUILayout.BeginVertical(new GUIStyle(style)); GUILayout.BeginVertical(new GUIStyle(style));
GUI.color = guiColor;
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
//Draw node contents //Draw node contents

View File

@ -42,7 +42,9 @@ namespace XNodeEditor {
rect.size = new Vector2(16, 16); rect.size = new Vector2(16, 16);
DrawPortHandle(rect, port.ValueType); Color backgroundColor = new Color32(90, 97, 105, 255);
if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()];
DrawPortHandle(rect, port.ValueType, backgroundColor);
// Register the handle position // Register the handle position
Vector2 portPos = rect.center; Vector2 portPos = rect.center;
@ -66,7 +68,9 @@ namespace XNodeEditor {
else if (port.direction == NodePort.IO.Output) rect.position = rect.position + new Vector2(rect.width, 0); else if (port.direction == NodePort.IO.Output) rect.position = rect.position + new Vector2(rect.width, 0);
rect.size = new Vector2(16, 16); rect.size = new Vector2(16, 16);
DrawPortHandle(rect, port.ValueType); Color backgroundColor = new Color32(90, 97, 105, 255);
if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()];
DrawPortHandle(rect, port.ValueType, backgroundColor);
// Register the handle position // Register the handle position
Vector2 portPos = rect.center; Vector2 portPos = rect.center;
@ -74,9 +78,9 @@ namespace XNodeEditor {
else NodeEditor.portPositions.Add(port, portPos); else NodeEditor.portPositions.Add(port, portPos);
} }
private static void DrawPortHandle(Rect rect, Type type) { private static void DrawPortHandle(Rect rect, Type type, Color backgroundColor) {
Color col = GUI.color; Color col = GUI.color;
GUI.color = new Color32(90, 97, 105, 255); GUI.color = backgroundColor;
GUI.DrawTexture(rect, NodeEditorResources.dotOuter); GUI.DrawTexture(rect, NodeEditorResources.dotOuter);
GUI.color = NodeEditorPreferences.GetTypeColor(type); GUI.color = NodeEditorPreferences.GetTypeColor(type);
GUI.DrawTexture(rect, NodeEditorResources.dot); GUI.DrawTexture(rect, NodeEditorResources.dot);

View File

@ -2,12 +2,18 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEngine;
using XNode; using XNode;
namespace XNodeEditor { namespace XNodeEditor {
/// <summary> Contains reflection-related info </summary> /// <summary> Contains reflection-related info </summary>
public partial class NodeEditorWindow { public partial class NodeEditorWindow {
/// <summary> Custom node editors defined with [CustomNodeInspector] </summary>
[NonSerialized] private static Dictionary<Type, NodeEditor> customNodeEditor; [NonSerialized] private static Dictionary<Type, NodeEditor> customNodeEditor;
/// <summary> Custom node tint colors defined with [NodeColor(r, g, b)] </summary>
public static Dictionary<Type, Color> nodeTint { get { return _nodeTint != null ? _nodeTint : _nodeTint = GetNodeTint(); } }
[NonSerialized] private static Dictionary<Type, Color> _nodeTint;
/// <summary> All available node types </summary>
public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } } public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } }
[NonSerialized] private static Type[] _nodeTypes = null; [NonSerialized] private static Type[] _nodeTypes = null;
@ -36,6 +42,17 @@ namespace XNodeEditor {
} }
} }
public static Dictionary<Type, Color> GetNodeTint() {
Dictionary<Type, Color> tints = new Dictionary<Type, Color>();
for (int i = 0; i < nodeTypes.Length; i++) {
var attribs = nodeTypes[i].GetCustomAttributes(typeof(Node.NodeTint), true);
if (attribs == null || attribs.Length == 0) continue;
Node.NodeTint attrib = attribs[0] as Node.NodeTint;
tints.Add(nodeTypes[i], attrib.color);
}
return tints;
}
public static Type[] GetDerivedTypes(Type baseType) { public static Type[] GetDerivedTypes(Type baseType) {
//Get all classes deriving from baseType via reflection //Get all classes deriving from baseType via reflection
Assembly assembly = Assembly.GetAssembly(baseType); Assembly assembly = Assembly.GetAssembly(baseType);

View File

@ -174,6 +174,24 @@ namespace XNode {
} }
} }
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class NodeTint : Attribute {
public Color color;
/// <summary> Specify a color for this node type </summary>
/// <param name="r"> Red [0..1] </param>
/// <param name="g"> Green [0..1] </param>
/// <param name="b"> Blue [0..1] </param>
public NodeTint(float r, float g, float b) {
color = new Color(r, g, b);
}
/// <summary> Specify a color for this node type </summary>
/// <param name="hex"> HEX color value </param>
public NodeTint(string hex) {
ColorUtility.TryParseHtmlString(hex, out color);
}
}
[Serializable] private class NodePortDictionary : Dictionary<string, NodePort>, ISerializationCallbackReceiver { [Serializable] private class NodePortDictionary : Dictionary<string, NodePort>, ISerializationCallbackReceiver {
[SerializeField] private List<string> keys = new List<string>(); [SerializeField] private List<string> keys = new List<string>();
[SerializeField] private List<NodePort> values = new List<NodePort>(); [SerializeField] private List<NodePort> values = new List<NodePort>();