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

Added NodeGraphEditor.GetTypeColor for custom node-graph specific type colors

This commit is contained in:
Thor Kramer Brigsted 2017-11-28 10:49:24 +01:00
parent 9e68617cb8
commit aabd2e4145
5 changed files with 30 additions and 21 deletions

View File

@ -9,12 +9,10 @@ namespace XNodeEditor {
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>
[CustomNodeEditor(typeof(Node))]
public class NodeEditor : XNodeInternal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute> {
public class NodeEditor : XNodeInternal.NodeEditorBase<NodeEditor, NodeEditor.CustomNodeEditorAttribute, Node> {
/// <summary> Fires every whenever a node was modified through the editor </summary>
public static Action<Node> onUpdateNode;
public Node target;
public SerializedObject serializedObject;
public static Dictionary<NodePort, Vector2> portPositions;
/// <summary> Draws the node GUI.</summary>

View File

@ -3,16 +3,28 @@ using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using XNode;
using XNodeEditor;
namespace XNodeInternal {
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
public class NodeEditorBase<T, A> where A : Attribute, NodeEditorBase<T, A>.INodeEditorAttrib where T : class {
public class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T,A,K> where K : ScriptableObject {
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
private static Dictionary<Type, T> editors;
public K target;
public SerializedObject serializedObject;
public static T GetEditor(Type type) {
public static T GetEditor(K target) {
if (target == null) return null;
Type type = target.GetType();
T editor = GetEditor(type);
editor.target = target;
editor.serializedObject = new SerializedObject(target);
return editor;
}
private static T GetEditor(Type type) {
if (type == null) return null;
if (editors == null) CacheCustomEditors();
if (editors.ContainsKey(type)) return editors[type];

View File

@ -13,9 +13,7 @@ namespace XNodeEditor {
Event e = Event.current;
Matrix4x4 m = GUI.matrix;
if (graph == null) return;
currentGraphEditor = NodeGraphEditor.GetEditor(graph.GetType());
currentGraphEditor.target = graph;
currentGraphEditor.serializedObject = new SerializedObject(graph);
currentGraphEditor = NodeGraphEditor.GetEditor(graph);
Controls();
@ -162,7 +160,8 @@ namespace XNodeEditor {
if (!input.IsConnectedTo(output)) input.Connect(output);
if (!_portConnectionPoints.ContainsKey(input)) continue;
Vector2 to = _portConnectionPoints[input].center;
DrawConnection(from, to, NodeEditorPreferences.GetTypeColor(output.ValueType));
Color connectionColor = currentGraphEditor.GetTypeColor(output.ValueType);
DrawConnection(from, to, connectionColor);
}
}
}
@ -198,11 +197,8 @@ namespace XNodeEditor {
while (graph.nodes[n] == null) graph.nodes.RemoveAt(n);
if (n >= graph.nodes.Count) return;
Node node = graph.nodes[n];
Type nodeType = node.GetType();
NodeEditor nodeEditor = NodeEditor.GetEditor(nodeType);
nodeEditor.target = node;
nodeEditor.serializedObject = new SerializedObject(node);
NodeEditor nodeEditor = NodeEditor.GetEditor(node);
NodeEditor.portPositions = new Dictionary<NodePort, Vector2>();
//Get node position

View File

@ -93,7 +93,8 @@ namespace XNodeEditor {
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);
Color col = NodeGraphEditor.GetEditor(port.node.graph).GetTypeColor(port.ValueType);
DrawPortHandle(rect, backgroundColor, col);
// Register the handle position
Vector2 portPos = rect.center;
@ -119,7 +120,8 @@ namespace XNodeEditor {
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);
Color col = NodeGraphEditor.GetEditor(port.node.graph).GetTypeColor(port.ValueType);
DrawPortHandle(rect, backgroundColor, col);
// Register the handle position
Vector2 portPos = rect.center;
@ -127,11 +129,11 @@ namespace XNodeEditor {
else NodeEditor.portPositions.Add(port, portPos);
}
private static void DrawPortHandle(Rect rect, Type type, Color backgroundColor) {
private static void DrawPortHandle(Rect rect, Color backgroundColor, Color typeColor) {
Color col = GUI.color;
GUI.color = backgroundColor;
GUI.DrawTexture(rect, NodeEditorResources.dotOuter);
GUI.color = NodeEditorPreferences.GetTypeColor(type);
GUI.color = typeColor;
GUI.DrawTexture(rect, NodeEditorResources.dot);
GUI.color = col;
}

View File

@ -8,13 +8,10 @@ using XNode;
namespace XNodeEditor {
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
[CustomNodeGraphEditor(typeof(NodeGraph))]
public class NodeGraphEditor : XNodeInternal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute> {
public class NodeGraphEditor : XNodeInternal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, NodeGraph> {
/// <summary> Custom node editors defined with [CustomNodeGraphEditor] </summary>
[NonSerialized] private static Dictionary<Type, NodeGraphEditor> editors;
public NodeGraph target;
public SerializedObject serializedObject;
public virtual Texture2D GetGridTexture() {
return NodeEditorPreferences.gridTexture;
}
@ -33,6 +30,10 @@ namespace XNodeEditor {
return ObjectNames.NicifyVariableName(type.ToString().Replace('.', '/'));
}
public virtual Color GetTypeColor(Type type) {
return NodeEditorPreferences.GetTypeColor(type);
}
[AttributeUsage(AttributeTargets.Class)]
public class CustomNodeGraphEditorAttribute : Attribute,
INodeEditorAttrib {