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

Add [DisallowMultipleNodes] for limiting how many nodes of a certain type can exist on any graph

This commit is contained in:
Thor Brigsted 2020-04-21 15:31:07 +02:00
parent a550a87b9e
commit 8965d365e6
3 changed files with 30 additions and 1 deletions

View File

@ -439,6 +439,15 @@ namespace XNodeEditor {
for (int i = 0; i < nodes.Length; i++) {
XNode.Node srcNode = nodes[i];
if (srcNode == null) continue;
// Check if user is allowed to add more of given node type
XNode.Node.DisallowMultipleNodesAttribute disallowAttrib;
Type nodeType = srcNode.GetType();
if (NodeEditorUtilities.GetAttrib(nodeType, out disallowAttrib)) {
int typeCount = graph.nodes.Count(x => x.GetType() == nodeType);
if (typeCount >= disallowAttrib.max) continue;
}
XNode.Node newNode = graphEditor.CopyNode(srcNode);
substitutes.Add(srcNode, newNode);
newNode.position = srcNode.position + offset;

View File

@ -62,7 +62,17 @@ namespace XNodeEditor {
string path = GetNodeMenuName(type);
if (string.IsNullOrEmpty(path)) continue;
menu.AddItem(new GUIContent(path), false, () => {
// Check if user is allowed to add more of given node type
XNode.Node.DisallowMultipleNodesAttribute disallowAttrib;
bool disallowed = false;
if (NodeEditorUtilities.GetAttrib(type, out disallowAttrib)) {
int typeCount = target.nodes.Count(x => x.GetType() == type);
if (typeCount >= disallowAttrib.max) disallowed = true;
}
// Add node entry to context menu
if (disallowed) menu.AddItem(new GUIContent(path), false, null);
else menu.AddItem(new GUIContent(path), false, () => {
XNode.Node node = CreateNode(type, pos);
NodeEditorWindow.current.AutoConnect(node);
});

View File

@ -334,6 +334,16 @@ namespace XNode {
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class DisallowMultipleNodesAttribute : Attribute {
public int max;
/// <summary> Prevents Node of the same type (or subtype) to be added more than once (configurable) to a NodeGraph </summary>
/// <param name="max"> How many nodes to allow. Defaults to 1. </param>
public DisallowMultipleNodesAttribute(int max = 1) {
this.max = max;
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class NodeTintAttribute : Attribute {
public Color color;