Skip to content

Commit 1666653

Browse files
committed
Added API to check if node was created from a duplication operation
1 parent 32ad90b commit 1666653

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ void UnserializeAndPasteCallback(string operationName, string serializedData)
222222

223223
Dictionary<string, BaseNode> copiedNodesMap = new Dictionary<string, BaseNode>();
224224

225+
var unserializedGroups = data.copiedGroups.Select(g => JsonSerializer.Deserialize<Group>(g)).ToList();
226+
225227
foreach (var serializedNode in data.copiedNodes)
226228
{
227229
var node = JsonSerializer.DeserializeNode(serializedNode);
@@ -232,6 +234,8 @@ void UnserializeAndPasteCallback(string operationName, string serializedData)
232234
string sourceGUID = node.GUID;
233235
graph.nodesPerGUID.TryGetValue(sourceGUID, out var sourceNode);
234236
//Call OnNodeCreated on the new fresh copied node
237+
node.createdFromDuplication = true;
238+
node.createWithinGroup = unserializedGroups.Any(g => g.innerNodeGUIDs.Contains(sourceGUID));
235239
node.OnNodeCreated();
236240
//And move a bit the new node
237241
node.position.position += new Vector2(20, 20);
@@ -247,10 +251,8 @@ void UnserializeAndPasteCallback(string operationName, string serializedData)
247251
AddToSelection(nodeViewsPerNode[node]);
248252
}
249253

250-
foreach (var serializedGroup in data.copiedGroups)
254+
foreach (var group in unserializedGroups)
251255
{
252-
var group = JsonSerializer.Deserialize<Group>(serializedGroup);
253-
254256
//Same than for node
255257
group.OnCreated();
256258

@@ -588,7 +590,13 @@ void MouseDownCallback(MouseDownEvent e)
588590
}
589591

590592
bool DoesSelectionContainsInspectorNodes()
591-
=> selection.Any(s => s is BaseNodeView v && v.nodeTarget.needsInspector);
593+
{
594+
var selectedNodes = selection.Where(s => s is BaseNodeView).ToList();
595+
var selectedNodesNotInInspector = selectedNodes.Except(nodeInspector.selectedNodes).ToList();
596+
var nodeInInspectorWithoutSelectedNodes = nodeInspector.selectedNodes.Except(selectedNodes).ToList();
597+
598+
return selectedNodesNotInInspector.Any() || nodeInInspectorWithoutSelectedNodes.Any();
599+
}
592600

593601
void DragPerformedCallback(DragPerformEvent e)
594602
{

Assets/com.alelievr.NodeGraphProcessor/Runtime/Elements/BaseNode.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ public abstract class BaseNode
119119
/// </summary>
120120
public virtual bool isRenamable => false;
121121

122+
/// <summary>
123+
/// Is the node created from a duplicate operation (either ctrl-D or copy/paste).
124+
/// </summary>
125+
public bool createdFromDuplication {get; internal set; } = false;
126+
127+
/// <summary>
128+
/// True only when the node was created from a duplicate operation and is inside a group that was also duplicated at the same time.
129+
/// </summary>
130+
public bool createWithinGroup {get; internal set; } = false;
131+
122132
[NonSerialized]
123133
internal Dictionary< string, NodeFieldInformation > nodeFields = new Dictionary< string, NodeFieldInformation >();
124134

Assets/com.alelievr.NodeGraphProcessor/Runtime/Graph/BaseGraph.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ protected virtual void OnEnable()
174174

175175
void InitializeGraphElements()
176176
{
177+
// Sanitize the element lists (it's possible that nodes are null if their full class name have changed)
178+
// If you rename / change the assembly of a node or parameter, please use the MovedFrom() attribute to avoid breaking the graph.
179+
nodes.RemoveAll(n => n == null);
180+
exposedParameters.RemoveAll(e => e == null);
181+
177182
foreach (var node in nodes.ToList())
178183
{
179184
nodesPerGUID[node.GUID] = node;

0 commit comments

Comments
 (0)