From 4d4d29771033bc538757e30166969ed2cbbc73ce Mon Sep 17 00:00:00 2001 From: illua1 Date: Fri, 3 Mar 2023 23:01:31 +0300 Subject: [PATCH 1/3] init commit --- source/blender/blenkernel/BKE_node.h | 7 +-- source/blender/blenkernel/intern/node.cc | 17 ++----- .../editors/space_node/node_relationships.cc | 44 ++++++++----------- source/blender/makesrna/intern/rna_nodetree.c | 31 +++++++------ 4 files changed, 38 insertions(+), 61 deletions(-) diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index ec211c9a6b4..bc465638955 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -757,7 +757,6 @@ void nodeInternalRelink(struct bNodeTree *ntree, struct bNode *node); void nodeToView(const struct bNode *node, float x, float y, float *rx, float *ry); void nodeFromView(const struct bNode *node, float x, float y, float *rx, float *ry); -bool nodeAttachNodeCheck(const struct bNode *node, const struct bNode *parent); void nodeAttachNode(struct bNodeTree *ntree, struct bNode *node, struct bNode *parent); void nodeDetachNode(struct bNodeTree *ntree, struct bNode *node); @@ -790,11 +789,7 @@ void nodeFindNode(struct bNodeTree *ntree, */ struct bNode *nodeFindRootParent(bNode *node); -/** - * \returns true if \a child has \a parent as a parent/grandparent/... etc. - * \note Recursive - */ -bool nodeIsChildOf(const bNode *parent, const bNode *child); +bool nodeIsParentAndChild(const bNode *parent, const bNode *child); /** * Iterate over a chain of nodes, starting with \a node_start, executing diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index eccc790d68a..f3667f8cb74 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -2059,13 +2059,13 @@ bNode *nodeFindRootParent(bNode *node) return node->type == NODE_FRAME ? node : nullptr; } -bool nodeIsChildOf(const bNode *parent, const bNode *child) +bool nodeIsParentAndChild(const bNode *parent, const bNode *child) { if (parent == child) { return true; } if (child->parent) { - return nodeIsChildOf(parent, child->parent); + return nodeIsParentAndChild(parent, child->parent); } return false; } @@ -2573,21 +2573,10 @@ void nodeFromView(const bNode *node, const float x, const float y, float *rx, fl } } -bool nodeAttachNodeCheck(const bNode *node, const bNode *parent) -{ - for (const bNode *parent_iter = node; parent_iter; parent_iter = parent_iter->parent) { - if (parent_iter == parent) { - return true; - } - } - - return false; -} - void nodeAttachNode(bNodeTree *ntree, bNode *node, bNode *parent) { BLI_assert(parent->type == NODE_FRAME); - BLI_assert(nodeAttachNodeCheck(parent, node) == false); + BLI_assert(nodeIsParentAndChild(parent, node) == false); float locx, locy; nodeToView(node, 0.0f, 0.0f, &locx, &locy); diff --git a/source/blender/editors/space_node/node_relationships.cc b/source/blender/editors/space_node/node_relationships.cc index 97ab6b7786c..0d13338ff3b 100644 --- a/source/blender/editors/space_node/node_relationships.cc +++ b/source/blender/editors/space_node/node_relationships.cc @@ -1890,30 +1890,24 @@ static int node_attach_invoke(bContext *C, wmOperator * /*op*/, const wmEvent *e continue; } - if (node->parent == nullptr) { - /* Disallow moving a parent into its child. */ - if (nodeAttachNodeCheck(frame, node) == false) { - /* Attach all unparented nodes. */ - nodeAttachNode(&ntree, node, frame); - } + /* Disallow moving a parent into its child. */ + if (node->is_frame() && nodeIsParentAndChild(node, frame)) { + continue; } - else { - /* Attach nodes which share parent with the frame. */ - bNode *parent; - for (parent = frame->parent; parent; parent = parent->parent) { - if (parent == node->parent) { - break; - } - } - if (parent) { - /* Disallow moving a parent into its child. */ - if (nodeAttachNodeCheck(frame, node) == false) { - nodeDetachNode(&ntree, node); - nodeAttachNode(&ntree, node, frame); - } - } + if (node->parent == nullptr) { + nodeAttachNode(&ntree, node, frame); + continue; } + + /* Attach nodes which share parent with the frame. */ + const bool share_parent = nodeIsParentAndChild(node->parent, frame); + if (!share_parent) { + continue; + } + + nodeDetachNode(&ntree, node); + nodeAttachNode(&ntree, node, frame); } node_sort(ntree); @@ -2305,7 +2299,7 @@ static void node_parent_offset_apply(NodeInsertOfsData *data, bNode *parent, con /* Flag all children as offset to prevent them from being offset * separately (they've already moved with the parent). */ for (bNode *node : data->ntree->all_nodes()) { - if (nodeIsChildOf(parent, node)) { + if (nodeIsParentAndChild(parent, node)) { /* NODE_TEST is used to flag nodes that shouldn't be offset (again) */ node->flag |= NODE_TEST; } @@ -2345,7 +2339,7 @@ static void node_link_insert_offset_frame_chains(bNodeTree *ntree, const bool reversed) { for (bNode *node : ntree->all_nodes()) { - if (nodeIsChildOf(parent, node)) { + if (nodeIsParentAndChild(parent, node)) { nodeChainIter(ntree, node, node_link_insert_offset_frame_chain_cb, data, reversed); } } @@ -2372,7 +2366,7 @@ static bool node_link_insert_offset_chain_cb(bNode *fromnode, node_offset_apply(*ofs_node, data->offset_x); } - if (nodeIsChildOf(data->insert_parent, ofs_node) == false) { + if (!nodeIsParentAndChild(data->insert_parent, ofs_node)) { data->insert_parent = nullptr; } } @@ -2472,7 +2466,7 @@ static void node_link_insert_offset_ntree(NodeInsertOfsData *iofsd, if (needs_alignment) { bNode *offs_node = right_alignment ? next : prev; if (!offs_node->parent || offs_node->parent == insert.parent || - nodeIsChildOf(offs_node->parent, &insert)) { + nodeIsParentAndChild(offs_node->parent, &insert)) { node_offset_apply(*offs_node, addval); } else if (!insert.parent && offs_node->parent) { diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index 12d4827bd9b..dbd9807be78 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -2384,24 +2384,24 @@ static void rna_Node_parent_set(PointerRNA *ptr, bNode *parent = value.data; bNodeTree *ntree = (bNodeTree *)ptr->owner_id; - if (parent) { - /* XXX only Frame node allowed for now, - * in the future should have a poll function or so to test possible attachment. - */ - if (parent->type != NODE_FRAME) { - return; - } + if (!parent) { + nodeDetachNode(ntree, node); + return; + } - /* make sure parent is not attached to the node */ - if (nodeAttachNodeCheck(parent, node)) { - return; - } + /* XXX only Frame node allowed for now, + * in the future should have a poll function or so to test possible attachment. + */ + if (parent->type != NODE_FRAME) { + return; + } + + if (nodeIsParentAndChild(node, parent)) { + return; } nodeDetachNode(ntree, node); - if (parent) { - nodeAttachNode(ntree, node, parent); - } + nodeAttachNode(ntree, node, parent); } static void rna_Node_internal_links_begin(CollectionPropertyIterator *iter, PointerRNA *ptr) @@ -2425,8 +2425,7 @@ static bool rna_Node_parent_poll(PointerRNA *ptr, PointerRNA value) return false; } - /* make sure parent is not attached to the node */ - if (nodeAttachNodeCheck(parent, node)) { + if (nodeIsParentAndChild(node, parent)) { return false; } -- 2.30.2 From 2bc503d51760d41928107ab95d8f122e8a832511 Mon Sep 17 00:00:00 2001 From: illua1 Date: Fri, 3 Mar 2023 23:05:34 +0300 Subject: [PATCH 2/3] delete: == false... --- source/blender/blenkernel/intern/node.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index f3667f8cb74..391d2d9546b 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -2576,7 +2576,7 @@ void nodeFromView(const bNode *node, const float x, const float y, float *rx, fl void nodeAttachNode(bNodeTree *ntree, bNode *node, bNode *parent) { BLI_assert(parent->type == NODE_FRAME); - BLI_assert(nodeIsParentAndChild(parent, node) == false); + BLI_assert(!nodeIsParentAndChild(parent, node)); float locx, locy; nodeToView(node, 0.0f, 0.0f, &locx, &locy); -- 2.30.2 From 64c69e68b5c626a6ac53616d7e53c11f4864c090 Mon Sep 17 00:00:00 2001 From: illua1 Date: Fri, 3 Mar 2023 23:22:24 +0300 Subject: [PATCH 3/3] avoid another one extra call --- source/blender/makesrna/intern/rna_nodetree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index dbd9807be78..bf1a9963f87 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -2425,7 +2425,7 @@ static bool rna_Node_parent_poll(PointerRNA *ptr, PointerRNA value) return false; } - if (nodeIsParentAndChild(node, parent)) { + if (node->type == NODE_FRAME && nodeIsParentAndChild(node, parent)) { return false; } -- 2.30.2