From 40d815dff6515834c1bce40c0a34cc80b39655d5 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Fri, 2 Sep 2022 15:25:34 -0500 Subject: Cleanup: Further split of node link Bezier calculation function Now dragged handles are handled separately, and the function returns a statically sized array by value. The functions are also renamed to be more consistent with curve naming elsewhere in Blender. --- source/blender/editors/space_node/drawnode.cc | 94 ++++++++-------------- source/blender/editors/space_node/node_add.cc | 2 +- source/blender/editors/space_node/node_intern.hh | 10 +-- .../editors/space_node/node_relationships.cc | 7 +- 4 files changed, 41 insertions(+), 72 deletions(-) diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc index 541c82be2f3..cec33f4b3e2 100644 --- a/source/blender/editors/space_node/drawnode.cc +++ b/source/blender/editors/space_node/drawnode.cc @@ -1585,53 +1585,19 @@ void draw_nodespace_back_pix(const bContext &C, GPU_matrix_pop(); } -bool node_link_bezier_handles(const SpaceNode *snode, - const bNodeLink &link, - std::array &points) +static float2 socket_link_connection_location(const bNodeSocket &socket, const bNodeLink &link) { - float2 cursor = {0.0f, 0.0f}; - - /* this function can be called with snode null (via cut_links_intersect) */ - /* XXX map snode->runtime->cursor back to view space */ - if (snode) { - cursor = snode->runtime->cursor * UI_DPI_FAC; - } - - /* in v0 and v3 we put begin/end points */ - if (link.fromsock) { - points[0].x = link.fromsock->locx; - points[0].y = link.fromsock->locy; - if (link.fromsock->flag & SOCK_MULTI_INPUT) { - points[0] = node_link_calculate_multi_input_position( - {link.fromsock->locx, link.fromsock->locy}, - link.fromsock->total_inputs - 1, - link.fromsock->total_inputs); - } - } - else { - if (snode == nullptr) { - return false; - } - points[0] = cursor; - } - if (link.tosock) { - points[3].x = link.tosock->locx; - points[3].y = link.tosock->locy; - if (!(link.tonode->flag & NODE_HIDDEN) && link.tosock->flag & SOCK_MULTI_INPUT) { - points[3] = node_link_calculate_multi_input_position({link.tosock->locx, link.tosock->locy}, - link.multi_input_socket_index, - link.tosock->total_inputs); - } - } - else { - if (snode == nullptr) { - return false; - } - points[3] = cursor; + const float2 socket_location(socket.locx, socket.locy); + if (socket.flag & SOCK_MULTI_INPUT && socket.in_out == SOCK_IN) { + return node_link_calculate_multi_input_position( + socket_location, link.multi_input_socket_index, socket.total_inputs); } + return socket_location; +} +static void calculate_inner_link_bezier_points(std::array &points) +{ const int curving = UI_GetThemeValueType(TH_NODE_CURVING, SPACE_NODE); - if (curving == 0) { /* Straight line: align all points. */ points[1] = math::interpolate(points[0], points[3], 1.0f / 3.0f); @@ -1646,8 +1612,15 @@ bool node_link_bezier_handles(const SpaceNode *snode, points[2].x = points[3].x - dist; points[2].y = points[3].y; } +} - return true; +std::array node_link_bezier_points(const bNodeLink &link) +{ + std::array points; + points[0] = socket_link_connection_location(*link.fromsock, link); + points[3] = socket_link_connection_location(*link.tosock, link); + calculate_inner_link_bezier_points(points); + return points; } static bool node_link_draw_is_visible(const View2D &v2d, const std::array &points) @@ -1661,15 +1634,11 @@ static bool node_link_draw_is_visible(const View2D &v2d, const std::array points; - if (!node_link_bezier_handles(snode, link, points)) { - return false; - } + const std::array points = node_link_bezier_points(link); /* always do all three, to prevent data hanging around */ BKE_curve_forward_diff_bezier(points[0].x, @@ -2182,10 +2151,7 @@ void node_draw_link_bezier(const bContext &C, const int th_col3, const bool selected) { - std::array points; - if (!node_link_bezier_handles(&snode, link, points)) { - return; - } + const std::array points = node_link_bezier_points(link); if (!node_link_draw_is_visible(v2d, points)) { return; } @@ -2241,6 +2207,17 @@ void node_draw_link(const bContext &C, node_draw_link_bezier(C, v2d, snode, link, th_col1, th_col2, th_col3, selected); } +static std::array node_link_bezier_points_dragged(const SpaceNode &snode, + const bNodeLink &link) +{ + const float2 cursor = snode.runtime->cursor * UI_DPI_FAC; + std::array points; + points[0] = link.fromsock ? socket_link_connection_location(*link.fromsock, link) : cursor; + points[3] = link.tosock ? socket_link_connection_location(*link.tosock, link) : cursor; + calculate_inner_link_bezier_points(points); + return points; +} + void node_draw_link_dragged(const bContext &C, const View2D &v2d, const SpaceNode &snode, @@ -2250,10 +2227,7 @@ void node_draw_link_dragged(const bContext &C, return; } - std::array points; - if (!node_link_bezier_handles(&snode, link, points)) { - return; - } + const std::array points = node_link_bezier_points_dragged(snode, link); const NodeLinkDrawConfig draw_config = nodelink_get_draw_config( C, v2d, snode, link, TH_ACTIVE, TH_ACTIVE, TH_WIRE, true); diff --git a/source/blender/editors/space_node/node_add.cc b/source/blender/editors/space_node/node_add.cc index fcbb45a48f4..37db3296ec9 100644 --- a/source/blender/editors/space_node/node_add.cc +++ b/source/blender/editors/space_node/node_add.cc @@ -108,7 +108,7 @@ static bool add_reroute_intersect_check(const bNodeLink &link, { float coord_array[NODE_LINK_RESOL + 1][2]; - if (node_link_bezier_points(nullptr, link, coord_array, NODE_LINK_RESOL)) { + if (node_link_bezier_points_evaluated(link, coord_array, NODE_LINK_RESOL)) { for (int i = 0; i < tot - 1; i++) { for (int b = 0; b < NODE_LINK_RESOL; b++) { if (isect_seg_seg_v2_point( diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh index 52e6521370e..5b376618912 100644 --- a/source/blender/editors/space_node/node_intern.hh +++ b/source/blender/editors/space_node/node_intern.hh @@ -229,16 +229,12 @@ void node_draw_link_bezier(const bContext &C, int th_col2, int th_col3, bool selected); -bool node_link_bezier_points(const SpaceNode *snode, - const bNodeLink &link, - float coord_array[][2], - int resol); +bool node_link_bezier_points_evaluated(const bNodeLink &link, float coord_array[][2], int resol); /** * Return quadratic beziers points for a given nodelink. */ -bool node_link_bezier_handles(const SpaceNode *snode, - const bNodeLink &ink, - std::array &points); +std::array node_link_bezier_points(const bNodeLink &link); + void draw_nodespace_back_pix(const bContext &C, ARegion ®ion, SpaceNode &snode, diff --git a/source/blender/editors/space_node/node_relationships.cc b/source/blender/editors/space_node/node_relationships.cc index aac05fb1d25..c1f18e16d70 100644 --- a/source/blender/editors/space_node/node_relationships.cc +++ b/source/blender/editors/space_node/node_relationships.cc @@ -137,8 +137,7 @@ static void pick_input_link_by_link_intersect(const bContext &C, LISTBASE_FOREACH (bNodeLink *, link, &snode->edittree->links) { if (link->tosock == socket) { /* Test if the cursor is near a link. */ - std::array points; - node_link_bezier_handles(snode, *link, points); + const std::array points = node_link_bezier_points(*link); std::array data; BKE_curve_forward_diff_bezier(points[0].x, @@ -1325,7 +1324,7 @@ static bool node_links_intersect(bNodeLink &link, const float mcoords[][2], int { float coord_array[NODE_LINK_RESOL + 1][2]; - if (node_link_bezier_points(nullptr, link, coord_array, NODE_LINK_RESOL)) { + if (node_link_bezier_points_evaluated(link, coord_array, NODE_LINK_RESOL)) { for (int i = 0; i < tot - 1; i++) { for (int b = 0; b < NODE_LINK_RESOL; b++) { if (isect_seg_seg_v2(mcoords[i], mcoords[i + 1], coord_array[b], coord_array[b + 1]) > 0) { @@ -1965,7 +1964,7 @@ void ED_node_link_intersect_test(ScrArea *area, int test) continue; } - if (node_link_bezier_points(nullptr, *link, coord_array, NODE_LINK_RESOL)) { + if (node_link_bezier_points_evaluated(*link, coord_array, NODE_LINK_RESOL)) { float dist = FLT_MAX; /* loop over link coords to find shortest dist to -- cgit v1.2.3