[Bf-blender-cvs] [338c1060d5d] master: Cleanup: Remove unnecessary node type callbacks for drawing

Hans Goudey noreply at git.blender.org
Sun Dec 5 22:45:56 CET 2021


Commit: 338c1060d5d7b6a8bd3ec5632f543738de4c103c
Author: Hans Goudey
Date:   Sun Dec 5 16:45:41 2021 -0500
Branches: master
https://developer.blender.org/rB338c1060d5d7b6a8bd3ec5632f543738de4c103c

Cleanup: Remove unnecessary node type callbacks for drawing

Currently there are a few callbacks on `bNodeType` that do the same
thing for every node type except reroutes and frame nodes. Having a
callback for basic things complicates code and makes it harder to
understand, and reroutes and frames are special cases in larger way.

Arguably frame nodes shouldn't even be drawn like regular nodes,
given that it adds a case of O(N^2) looping through all nodes.
"Unrolling" the callbacks makes it easier to see what's happening,
and therefore easier to optimize.

Differential Revision: https://developer.blender.org/D13463

===================================================================

M	source/blender/blenkernel/BKE_node.h
M	source/blender/editors/space_node/drawnode.cc
M	source/blender/editors/space_node/node_draw.cc
M	source/blender/editors/space_node/node_edit.cc
M	source/blender/editors/space_node/node_intern.hh
M	source/blender/editors/space_node/node_select.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 85699e4c28d..0584a8c811f 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -25,7 +25,6 @@
 
 #include "BLI_compiler_compat.h"
 #include "BLI_ghash.h"
-#include "BLI_utildefines.h"
 
 #include "DNA_listBase.h"
 
@@ -223,16 +222,6 @@ typedef int (*NodeGPUExecFunction)(struct GPUMaterial *mat,
                                    struct GPUNodeStack *in,
                                    struct GPUNodeStack *out);
 
-typedef enum NodeResizeDirection {
-  NODE_RESIZE_NONE = 0,
-  NODE_RESIZE_TOP = (1 << 0),
-  NODE_RESIZE_BOTTOM = (1 << 1),
-  NODE_RESIZE_RIGHT = (1 << 2),
-  NODE_RESIZE_LEFT = (1 << 3),
-} NodeResizeDirection;
-
-ENUM_OPERATORS(NodeResizeDirection, NODE_RESIZE_LEFT);
-
 /**
  * \brief Defines a node type.
  *
@@ -265,10 +254,6 @@ typedef struct bNodeType {
                         struct bNodeTree *ntree,
                         struct bNode *node,
                         bNodeInstanceKey key);
-  /* Updates the node geometry attributes according to internal state before actual drawing */
-  void (*draw_nodetype_prepare)(const struct bContext *C,
-                                struct bNodeTree *ntree,
-                                struct bNode *node);
 
   /* Draw the option buttons on the node */
   void (*draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr);
@@ -284,12 +269,6 @@ typedef struct bNodeType {
    * \note Used as a fallback when #bNode.label isn't set.
    */
   void (*labelfunc)(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen);
-  /** Optional custom resize handle polling. */
-  NodeResizeDirection (*resize_area_func)(const struct bNode *node, int x, int y);
-  /** Optional selection area polling. */
-  int (*select_area_func)(struct bNode *node, int x, int y);
-  /** Optional tweak area polling (for grabbing). */
-  int (*tweak_area_func)(struct bNode *node, int x, int y);
 
   /** Called when the node is updated in the editor. */
   void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node);
diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc
index cdce876f57a..b031067ab14 100644
--- a/source/blender/editors/space_node/drawnode.cc
+++ b/source/blender/editors/space_node/drawnode.cc
@@ -250,11 +250,39 @@ static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *pt
   uiItemR(layout, ptr, "use_clamp", DEFAULT_FLAGS, nullptr, ICON_NONE);
 }
 
-static NodeResizeDirection node_resize_area_default(const bNode *node, const int x, const int y)
+NodeResizeDirection node_get_resize_direction(const bNode *node, const int x, const int y)
 {
+  if (node->type == NODE_FRAME) {
+    const float size = 10.0f;
+    NodeFrame *data = (NodeFrame *)node->storage;
+
+    /* shrinking frame size is determined by child nodes */
+    if (!(data->flag & NODE_FRAME_RESIZEABLE)) {
+      return NODE_RESIZE_NONE;
+    }
+
+    NodeResizeDirection dir = NODE_RESIZE_NONE;
+
+    const rctf &totr = node->totr;
+    if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax) {
+      dir |= NODE_RESIZE_RIGHT;
+    }
+    if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax) {
+      dir |= NODE_RESIZE_LEFT;
+    }
+    if (x >= totr.xmin && x < totr.xmax && y >= totr.ymax - size && y < totr.ymax) {
+      dir |= NODE_RESIZE_TOP;
+    }
+    if (x >= totr.xmin && x < totr.xmax && y >= totr.ymin && y < totr.ymin + size) {
+      dir |= NODE_RESIZE_BOTTOM;
+    }
+
+    return dir;
+  }
+
   if (node->flag & NODE_HIDDEN) {
-    rctf totr = node->totr;
     /* right part of node */
+    rctf totr = node->totr;
     totr.xmin = node->totr.xmax - 1.0f * U.widget_unit;
     if (BLI_rctf_isect_pt(&totr, x, y)) {
       return NODE_RESIZE_RIGHT;
@@ -284,59 +312,6 @@ static void node_draw_buttons_group(uiLayout *layout, bContext *C, PointerRNA *p
       layout, C, ptr, "node_tree", nullptr, nullptr, nullptr, UI_TEMPLATE_ID_FILTER_ALL, nullptr);
 }
 
-/* XXX Does a bounding box update by iterating over all children.
- * Not ideal to do this in every draw call, but doing as transform callback doesn't work,
- * since the child node totr rects are not updated properly at that point.
- */
-static void node_draw_frame_prepare(const bContext *UNUSED(C), bNodeTree *ntree, bNode *node)
-{
-  const float margin = 1.5f * U.widget_unit;
-  NodeFrame *data = (NodeFrame *)node->storage;
-
-  /* init rect from current frame size */
-  rctf rect;
-  node_to_view(*node, node->offsetx, node->offsety, &rect.xmin, &rect.ymax);
-  node_to_view(
-      *node, node->offsetx + node->width, node->offsety - node->height, &rect.xmax, &rect.ymin);
-
-  /* frame can be resized manually only if shrinking is disabled or no children are attached */
-  data->flag |= NODE_FRAME_RESIZEABLE;
-  /* for shrinking bbox, initialize the rect from first child node */
-  bool bbinit = (data->flag & NODE_FRAME_SHRINK);
-  /* fit bounding box to all children */
-  LISTBASE_FOREACH (bNode *, tnode, &ntree->nodes) {
-    if (tnode->parent != node) {
-      continue;
-    }
-
-    /* add margin to node rect */
-    rctf noderect = tnode->totr;
-    noderect.xmin -= margin;
-    noderect.xmax += margin;
-    noderect.ymin -= margin;
-    noderect.ymax += margin;
-
-    /* first child initializes frame */
-    if (bbinit) {
-      bbinit = false;
-      rect = noderect;
-      data->flag &= ~NODE_FRAME_RESIZEABLE;
-    }
-    else {
-      BLI_rctf_union(&rect, &noderect);
-    }
-  }
-
-  /* now adjust the frame size from view-space bounding box */
-  node_from_view(*node, rect.xmin, rect.ymax, &node->offsetx, &node->offsety);
-  float xmax, ymax;
-  node_from_view(*node, rect.xmax, rect.ymin, &xmax, &ymax);
-  node->width = xmax - node->offsetx;
-  node->height = -ymax + node->offsety;
-
-  node->totr = rect;
-}
-
 static void node_draw_frame_label(bNodeTree &ntree, bNode &node, SpaceNode &snode)
 {
   const float aspect = snode.runtime->aspect;
@@ -478,35 +453,6 @@ static void node_draw_frame(const bContext *C,
   node->block = nullptr;
 }
 
-static NodeResizeDirection node_resize_area_frame(const bNode *node, const int x, const int y)
-{
-  const float size = 10.0f;
-  NodeFrame *data = (NodeFrame *)node->storage;
-  rctf totr = node->totr;
-
-  /* shrinking frame size is determined by child nodes */
-  if (!(data->flag & NODE_FRAME_RESIZEABLE)) {
-    return NODE_RESIZE_NONE;
-  }
-
-  NodeResizeDirection dir = NODE_RESIZE_NONE;
-
-  if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax) {
-    dir |= NODE_RESIZE_RIGHT;
-  }
-  if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax) {
-    dir |= NODE_RESIZE_LEFT;
-  }
-  if (x >= totr.xmin && x < totr.xmax && y >= totr.ymax - size && y < totr.ymax) {
-    dir |= NODE_RESIZE_TOP;
-  }
-  if (x >= totr.xmin && x < totr.xmax && y >= totr.ymin && y < totr.ymin + size) {
-    dir |= NODE_RESIZE_BOTTOM;
-  }
-
-  return dir;
-}
-
 static void node_buts_frame_ex(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
 {
   uiItemR(layout, ptr, "label_size", DEFAULT_FLAGS, IFACE_("Label Size"), ICON_NONE);
@@ -514,33 +460,6 @@ static void node_buts_frame_ex(uiLayout *layout, bContext *UNUSED(C), PointerRNA
   uiItemR(layout, ptr, "text", DEFAULT_FLAGS, nullptr, ICON_NONE);
 }
 
-#define NODE_REROUTE_SIZE 8.0f
-
-static void node_draw_reroute_prepare(const bContext *UNUSED(C),
-                                      bNodeTree *UNUSED(ntree),
-                                      bNode *node)
-{
-  /* get "global" coords */
-  float locx, locy;
-  node_to_view(*node, 0.0f, 0.0f, &locx, &locy);
-
-  /* reroute node has exactly one input and one output, both in the same place */
-  bNodeSocket *nsock = (bNodeSocket *)node->outputs.first;
-  nsock->locx = locx;
-  nsock->locy = locy;
-
-  nsock = (bNodeSocket *)node->inputs.first;
-  nsock->locx = locx;
-  nsock->locy = locy;
-
-  const float size = NODE_REROUTE_SIZE;
-  node->width = size * 2;
-  node->totr.xmin = locx - size;
-  node->totr.xmax = locx + size;
-  node->totr.ymax = locy + size;
-  node->totr.ymin = locy - size;
-}
-
 static void node_draw_reroute(const bContext *C,
                               ARegion *region,
                               SpaceNode *UNUSED(snode),
@@ -588,20 +507,6 @@ static void node_draw_reroute(const bContext *C,
   node->block = nullptr;
 }
 
-/* Special tweak area for reroute node.
- * Since this node is quite small, we use a larger tweak area for grabbing than for selection.
- */
-static int node_tweak_area_reroute(bNode *node, int x, int y)
-{
-  /* square of tweak radius */
-  const float tweak_radius_sq = square_f(24.0f);
-
-  bNodeSocket *sock = (bNodeSocket *)node->inputs.first;
-  float dx = sock->locx - x;
-  float dy = sock->locy - y;
-  return (dx * dx + dy * dy <= tweak_radius_sq);
-}
-
 static void node_common_set_butfunc(bNodeType *ntype)
 {
   switch (ntype->type) {
@@ -610,14 +515,10 @@ static void node_common_set_butfunc(bNodeType *ntype)
       break;
     case NODE_FRAME:
       ntype->draw_nodetype = node_draw_frame;
-      ntype->draw_nodetype_prepare = node_draw_frame_prepare;
       ntype->draw_buttons_ex = node_buts_frame_ex;
-      ntype->resize_area_func = node_resize_area_frame;
       break;
     case NODE_REROUTE:
       ntype->draw_nodetype = node_draw_reroute;
-      ntype->draw_nodetype_prepare = node_draw_reroute_prepare;
-      ntype->tweak_area_func = node_tweak_area_reroute;
       break;
   }
 }
@@ -3373,12 +3274,8 @@ void ED_node_init_butfuncs(void)
 
   /* default ui functions */
   NodeTypeUndefined.draw_nodetype = node_draw_default;
-  NodeTypeUndefined.draw_nodetype_prepare = node_update_default;
-  NodeTypeUndefined.select_area_func = node_select_area_default;
-  NodeTypeUndefined.tweak_area_func = node_tweak_area_default;
   NodeTypeUndefined.draw_buttons = nullptr;
   NodeTypeUndefined.draw_buttons_ex = nullptr;
-  NodeTypeUndefined.resize_area_func = node_resize_area_default;
 
   NodeSocketTypeUndefined.draw = node_socket_undefined_draw;
   NodeSocketTypeUndefined.d

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list