From c015b6ec4bcb51b3928d28f3b8749069415079c2 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Sun, 17 Jun 2018 14:16:13 +0200 Subject: [PATCH] Added attribute NodeWidth. Changes the width used when rendering the node in the editor. --- Scripts/Editor/NodeEditor.cs | 4 +++- Scripts/Editor/NodeEditorReflection.cs | 15 +++++++++++++++ Scripts/Node.cs | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 8a64663..d76a624 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -58,7 +58,9 @@ namespace XNodeEditor { } public virtual int GetWidth() { - return 208; + Type type = target.GetType(); + if (NodeEditorWindow.nodeWidth.ContainsKey(type)) return NodeEditorWindow.nodeWidth[type]; + else return 208; } public virtual Color GetTint() { diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 6cdbc42..7eaec2b 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -13,6 +13,10 @@ namespace XNodeEditor { public static Dictionary nodeTint { get { return _nodeTint != null ? _nodeTint : _nodeTint = GetNodeTint(); } } [NonSerialized] private static Dictionary _nodeTint; + /// Custom node widths defined with [NodeWidth(width)] + public static Dictionary nodeWidth { get { return _nodeWidth != null ? _nodeWidth : _nodeWidth = GetNodeWidth(); } } + + [NonSerialized] private static Dictionary _nodeWidth; /// All available node types public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } } @@ -34,6 +38,17 @@ namespace XNodeEditor { return tints; } + public static Dictionary GetNodeWidth() { + Dictionary widths = new Dictionary(); + for (int i = 0; i < nodeTypes.Length; i++) { + var attribs = nodeTypes[i].GetCustomAttributes(typeof(XNode.Node.NodeWidth), true); + if (attribs == null || attribs.Length == 0) continue; + XNode.Node.NodeWidth attrib = attribs[0] as XNode.Node.NodeWidth; + widths.Add(nodeTypes[i], attrib.width); + } + return widths; + } + /// Get all classes deriving from baseType via reflection public static Type[] GetDerivedTypes(Type baseType) { List types = new List(); diff --git a/Scripts/Node.cs b/Scripts/Node.cs index 111ee18..7c52d9b 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -272,6 +272,16 @@ namespace XNode { } } + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class NodeWidth : Attribute { + public int width; + /// Specify a width for this node type + /// Width + public NodeWidth(int width) { + this.width = width; + } + } + [Serializable] private class NodePortDictionary : Dictionary, ISerializationCallbackReceiver { [SerializeField] private List keys = new List(); [SerializeField] private List values = new List();