From 78ae587649bb7b3350586e7f2ed68ea67b75d1f0 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 6 Dec 2021 17:12:46 -0500 Subject: Cleanup: Use C++ types for multi input socket sorting The algorithm used is still quite inefficient, but at least the code is easier to read and a little bit simpler now. --- source/blender/editors/space_node/drawnode.cc | 20 ++++---- source/blender/editors/space_node/node_edit.cc | 17 +++---- source/blender/editors/space_node/node_intern.hh | 8 ++- .../editors/space_node/node_relationships.cc | 57 ++++++---------------- 4 files changed, 36 insertions(+), 66 deletions(-) diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc index 8bce1248e8e..b3c88dafe91 100644 --- a/source/blender/editors/space_node/drawnode.cc +++ b/source/blender/editors/space_node/drawnode.cc @@ -3529,11 +3529,11 @@ bool node_link_bezier_handles(const View2D *v2d, vec[0][0] = link.fromsock->locx; vec[0][1] = link.fromsock->locy; if (link.fromsock->flag & SOCK_MULTI_INPUT) { - node_link_calculate_multi_input_position(link.fromsock->locx, - link.fromsock->locy, - link.fromsock->total_inputs - 1, - link.fromsock->total_inputs, - vec[0]); + const float2 position = node_link_calculate_multi_input_position( + {link.fromsock->locx, link.fromsock->locy}, + link.fromsock->total_inputs - 1, + link.fromsock->total_inputs); + copy_v2_v2(vec[0], position); } fromreroute = (link.fromnode && link.fromnode->type == NODE_REROUTE); } @@ -3548,11 +3548,11 @@ bool node_link_bezier_handles(const View2D *v2d, vec[3][0] = link.tosock->locx; vec[3][1] = link.tosock->locy; if (!(link.tonode->flag & NODE_HIDDEN) && link.tosock->flag & SOCK_MULTI_INPUT) { - node_link_calculate_multi_input_position(link.tosock->locx, - link.tosock->locy, - link.multi_input_socket_index, - link.tosock->total_inputs, - vec[3]); + const float2 position = node_link_calculate_multi_input_position( + {link.tosock->locx, link.tosock->locy}, + link.multi_input_socket_index, + link.tosock->total_inputs); + copy_v2_v2(vec[3], position); } toreroute = (link.tonode && link.tonode->type == NODE_REROUTE); } diff --git a/source/blender/editors/space_node/node_edit.cc b/source/blender/editors/space_node/node_edit.cc index 5a598a1bd04..3bce32e7af4 100644 --- a/source/blender/editors/space_node/node_edit.cc +++ b/source/blender/editors/space_node/node_edit.cc @@ -111,15 +111,14 @@ float node_socket_calculate_height(const bNodeSocket &socket) return sock_height; } -void node_link_calculate_multi_input_position(const float socket_x, - const float socket_y, - const int index, - const int total_inputs, - float r[2]) -{ - float offset = (total_inputs * NODE_MULTI_INPUT_LINK_GAP - NODE_MULTI_INPUT_LINK_GAP) * 0.5; - r[0] = socket_x - NODE_SOCKSIZE * 0.5f; - r[1] = socket_y - offset + (index * NODE_MULTI_INPUT_LINK_GAP); +float2 node_link_calculate_multi_input_position(const float2 &socket_position, + const int index, + const int total_inputs) +{ + const float offset = (total_inputs * NODE_MULTI_INPUT_LINK_GAP - NODE_MULTI_INPUT_LINK_GAP) * + 0.5f; + return {socket_position.x - NODE_SOCKSIZE * 0.5f, + socket_position.y - offset + index * NODE_MULTI_INPUT_LINK_GAP}; } static void compo_tag_output_nodes(bNodeTree *nodetree, int recalc_flags) diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh index 71cdd9145b9..5a2603bbe9d 100644 --- a/source/blender/editors/space_node/node_intern.hh +++ b/source/blender/editors/space_node/node_intern.hh @@ -93,11 +93,9 @@ ENUM_OPERATORS(NodeResizeDirection, NODE_RESIZE_LEFT); blender::float2 space_node_group_offset(const SpaceNode &snode); float node_socket_calculate_height(const bNodeSocket &socket); -void node_link_calculate_multi_input_position(const float socket_x, - const float socket_y, - const int index, - const int total_inputs, - float r[2]); +blender::float2 node_link_calculate_multi_input_position(const blender::float2 &socket_position, + int index, + int total_inputs); int node_get_colorid(bNode &node); int node_get_resize_cursor(NodeResizeDirection directions); diff --git a/source/blender/editors/space_node/node_relationships.cc b/source/blender/editors/space_node/node_relationships.cc index a4873b356a3..7e845940afd 100644 --- a/source/blender/editors/space_node/node_relationships.cc +++ b/source/blender/editors/space_node/node_relationships.cc @@ -440,17 +440,6 @@ struct LinkAndPosition { float2 multi_socket_position; }; -static int compare_link_by_y_position(const void *a, const void *b) -{ - const LinkAndPosition *link_and_position_a = *(const LinkAndPosition **)a; - const LinkAndPosition *link_and_position_b = *(const LinkAndPosition **)b; - - BLI_assert(link_and_position_a->link->tosock == link_and_position_b->link->tosock); - const float link_a_y = link_and_position_a->multi_socket_position[1]; - const float link_b_y = link_and_position_b->multi_socket_position[1]; - return link_a_y > link_b_y ? 1 : -1; -} - void sort_multi_input_socket_links(SpaceNode &snode, bNode &node, bNodeLink *drag_link, @@ -460,50 +449,34 @@ void sort_multi_input_socket_links(SpaceNode &snode, if (!(socket->flag & SOCK_MULTI_INPUT)) { continue; } - /* The total is calculated in #node_update_nodetree, which runs before this draw step. */ - int total_inputs = socket->total_inputs + 1; - struct LinkAndPosition **input_links = (LinkAndPosition **)MEM_malloc_arrayN( - total_inputs, sizeof(LinkAndPosition *), __func__); + Vector links; - int index = 0; LISTBASE_FOREACH (bNodeLink *, link, &snode.edittree->links) { if (link->tosock == socket) { - struct LinkAndPosition *link_and_position = (LinkAndPosition *)MEM_callocN( - sizeof(struct LinkAndPosition), __func__); - link_and_position->link = link; - node_link_calculate_multi_input_position(link->tosock->locx, - link->tosock->locy, - link->multi_input_socket_index, - link->tosock->total_inputs, - link_and_position->multi_socket_position); - input_links[index] = link_and_position; - index++; + links.append( + {link, + node_link_calculate_multi_input_position({link->tosock->locx, link->tosock->locy}, + link->multi_input_socket_index, + link->tosock->total_inputs)}); } } if (drag_link) { - LinkAndPosition *link_and_position = (LinkAndPosition *)MEM_callocN(sizeof(LinkAndPosition), - __func__); - link_and_position->link = drag_link; + LinkAndPosition link_and_position{}; + link_and_position.link = drag_link; if (cursor) { - link_and_position->multi_socket_position = *cursor; + link_and_position.multi_socket_position = *cursor; } - input_links[index] = link_and_position; - index++; + links.append(link_and_position); } - qsort(input_links, index, sizeof(bNodeLink *), compare_link_by_y_position); + std::sort(links.begin(), links.end(), [](const LinkAndPosition a, const LinkAndPosition b) { + return a.multi_socket_position.y < b.multi_socket_position.y; + }); - for (int i = 0; i < index; i++) { - input_links[i]->link->multi_input_socket_index = i; - } - - for (int i = 0; i < index; i++) { - if (input_links[i]) { - MEM_freeN(input_links[i]); - } + for (const int i : links.index_range()) { + links[i].link->multi_input_socket_index = i; } - MEM_freeN(input_links); } } -- cgit v1.2.3