diff options
author | Hans Goudey <h.goudey@me.com> | 2021-12-04 00:25:17 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-12-04 00:25:17 +0300 |
commit | 2d8606b36071dd14290aa8852451535a49d3096d (patch) | |
tree | 66674726756a859fa1a1e66e9b8eac84387ba2b8 /source/blender/editors/space_node/node_view.cc | |
parent | ca0dbf8c26fb637ad06bd170e4e25d200d7ca0f0 (diff) |
Cleanup: Use references in node editor, other improvements
This helps to tell when a pointer is expected to be null, and avoid
overly verbose code when dereferencing. This commit also includes
a few other cleanups in this area:
- Use const in a few places
- Use `float2` instead of `float[2]`
- Remove some unnecessary includes and old code
The change can be continued further in the future.
Diffstat (limited to 'source/blender/editors/space_node/node_view.cc')
-rw-r--r-- | source/blender/editors/space_node/node_view.cc | 80 |
1 files changed, 39 insertions, 41 deletions
diff --git a/source/blender/editors/space_node/node_view.cc b/source/blender/editors/space_node/node_view.cc index 36b84bec7eb..1153b28bd82 100644 --- a/source/blender/editors/space_node/node_view.cc +++ b/source/blender/editors/space_node/node_view.cc @@ -62,25 +62,21 @@ using blender::StringRef; /** \name View All Operator * \{ */ -int space_node_view_flag( - bContext *C, SpaceNode *snode, ARegion *region, const int node_flag, const int smooth_viewtx) +bool space_node_view_flag( + bContext &C, SpaceNode &snode, ARegion ®ion, const int node_flag, const int smooth_viewtx) { - bNode *node; - rctf cur_new; - float oldwidth, oldheight, width, height; - float oldasp, asp; - int tot = 0; - bool has_frame = false; + const float oldwidth = BLI_rctf_size_x(®ion.v2d.cur); + const float oldheight = BLI_rctf_size_y(®ion.v2d.cur); - oldwidth = BLI_rctf_size_x(®ion->v2d.cur); - oldheight = BLI_rctf_size_y(®ion->v2d.cur); - - oldasp = oldwidth / oldheight; + const float old_aspect = oldwidth / oldheight; + rctf cur_new; BLI_rctf_init_minmax(&cur_new); - if (snode->edittree) { - for (node = (bNode *)snode->edittree->nodes.first; node; node = node->next) { + int tot = 0; + bool has_frame = false; + if (snode.edittree) { + LISTBASE_FOREACH (const bNode *, node, &snode.edittree->nodes) { if ((node->flag & node_flag) == node_flag) { BLI_rctf_union(&cur_new, &node->totr); tot++; @@ -92,37 +88,39 @@ int space_node_view_flag( } } - if (tot) { - width = BLI_rctf_size_x(&cur_new); - height = BLI_rctf_size_y(&cur_new); - asp = width / height; + if (tot == 0) { + return false; + } - /* for single non-frame nodes, don't zoom in, just pan view, - * but do allow zooming out, this allows for big nodes to be zoomed out */ - if ((tot == 1) && (has_frame == false) && ((oldwidth * oldheight) > (width * height))) { - /* center, don't zoom */ - BLI_rctf_resize(&cur_new, oldwidth, oldheight); + const float width = BLI_rctf_size_x(&cur_new); + const float height = BLI_rctf_size_y(&cur_new); + const float new_aspect = width / height; + + /* for single non-frame nodes, don't zoom in, just pan view, + * but do allow zooming out, this allows for big nodes to be zoomed out */ + if ((tot == 1) && (has_frame == false) && ((oldwidth * oldheight) > (width * height))) { + /* center, don't zoom */ + BLI_rctf_resize(&cur_new, oldwidth, oldheight); + } + else { + if (old_aspect < new_aspect) { + const float height_new = width / old_aspect; + cur_new.ymin = cur_new.ymin - height_new / 2.0f; + cur_new.ymax = cur_new.ymax + height_new / 2.0f; } else { - if (oldasp < asp) { - const float height_new = width / oldasp; - cur_new.ymin = cur_new.ymin - height_new / 2.0f; - cur_new.ymax = cur_new.ymax + height_new / 2.0f; - } - else { - const float width_new = height * oldasp; - cur_new.xmin = cur_new.xmin - width_new / 2.0f; - cur_new.xmax = cur_new.xmax + width_new / 2.0f; - } - - /* add some padding */ - BLI_rctf_scale(&cur_new, 1.1f); + const float width_new = height * old_aspect; + cur_new.xmin = cur_new.xmin - width_new / 2.0f; + cur_new.xmax = cur_new.xmax + width_new / 2.0f; } - UI_view2d_smooth_view(C, region, &cur_new, smooth_viewtx); + /* add some padding */ + BLI_rctf_scale(&cur_new, 1.1f); } - return (tot != 0); + UI_view2d_smooth_view(&C, ®ion, &cur_new, smooth_viewtx); + + return true; } static int node_view_all_exec(bContext *C, wmOperator *op) @@ -135,7 +133,7 @@ static int node_view_all_exec(bContext *C, wmOperator *op) snode->xof = 0; snode->yof = 0; - if (space_node_view_flag(C, snode, region, 0, smooth_viewtx)) { + if (space_node_view_flag(*C, *snode, *region, 0, smooth_viewtx)) { return OPERATOR_FINISHED; } return OPERATOR_CANCELLED; @@ -168,7 +166,7 @@ static int node_view_selected_exec(bContext *C, wmOperator *op) SpaceNode *snode = CTX_wm_space_node(C); const int smooth_viewtx = WM_operator_smooth_viewtx_get(op); - if (space_node_view_flag(C, snode, region, NODE_SELECT, smooth_viewtx)) { + if (space_node_view_flag(*C, *snode, *region, NODE_SELECT, smooth_viewtx)) { return OPERATOR_FINISHED; } return OPERATOR_CANCELLED; @@ -738,7 +736,7 @@ static int space_node_view_geometry_nodes_legacy(bContext *C, SpaceNode *snode, } const int smooth_viewtx = WM_operator_smooth_viewtx_get(op); - if (space_node_view_flag(C, snode, region, NODE_SELECT, smooth_viewtx)) { + if (space_node_view_flag(*C, *snode, *region, NODE_SELECT, smooth_viewtx)) { return OPERATOR_FINISHED; } return OPERATOR_CANCELLED; |