mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
Add [DisallowMultipleNodes] for limiting how many nodes of a certain type can exist on any graph
This commit is contained in:
parent
a550a87b9e
commit
8965d365e6
@ -439,6 +439,15 @@ namespace XNodeEditor {
|
|||||||
for (int i = 0; i < nodes.Length; i++) {
|
for (int i = 0; i < nodes.Length; i++) {
|
||||||
XNode.Node srcNode = nodes[i];
|
XNode.Node srcNode = nodes[i];
|
||||||
if (srcNode == null) continue;
|
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);
|
XNode.Node newNode = graphEditor.CopyNode(srcNode);
|
||||||
substitutes.Add(srcNode, newNode);
|
substitutes.Add(srcNode, newNode);
|
||||||
newNode.position = srcNode.position + offset;
|
newNode.position = srcNode.position + offset;
|
||||||
|
|||||||
@ -62,7 +62,17 @@ namespace XNodeEditor {
|
|||||||
string path = GetNodeMenuName(type);
|
string path = GetNodeMenuName(type);
|
||||||
if (string.IsNullOrEmpty(path)) continue;
|
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);
|
XNode.Node node = CreateNode(type, pos);
|
||||||
NodeEditorWindow.current.AutoConnect(node);
|
NodeEditorWindow.current.AutoConnect(node);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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)]
|
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||||
public class NodeTintAttribute : Attribute {
|
public class NodeTintAttribute : Attribute {
|
||||||
public Color color;
|
public Color color;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user