[Bf-blender-cvs] [007a0e43a01] master: Cleanup: Reduce variable scope in node drawing code

Hans Goudey noreply at git.blender.org
Mon Nov 30 19:57:10 CET 2020


Commit: 007a0e43a01af97a2e393e2f98d9a86469ec4a78
Author: Hans Goudey
Date:   Mon Nov 30 13:56:46 2020 -0500
Branches: master
https://developer.blender.org/rB007a0e43a01af97a2e393e2f98d9a86469ec4a78

Cleanup: Reduce variable scope in node drawing code

Also use LISTBASE_FOREACH in a few places and generally clean up
the code for the two sidebar panels "Sockets" and "Interface".

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

M	source/blender/editors/space_node/node_buttons.c
M	source/blender/editors/space_node/node_draw.c

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

diff --git a/source/blender/editors/space_node/node_buttons.c b/source/blender/editors/space_node/node_buttons.c
index 6475e753409..0aba45ceafc 100644
--- a/source/blender/editors/space_node/node_buttons.c
+++ b/source/blender/editors/space_node/node_buttons.c
@@ -68,23 +68,20 @@ static bool node_sockets_poll(const bContext *C, PanelType *UNUSED(pt))
 
 static void node_sockets_panel(const bContext *C, Panel *panel)
 {
-  SpaceNode *snode = CTX_wm_space_node(C);
-  bNodeTree *ntree = (snode) ? snode->edittree : NULL;
-  bNode *node = (ntree) ? nodeGetActive(ntree) : NULL;
-  bNodeSocket *sock;
-  uiLayout *layout = panel->layout, *split;
-  char name[UI_MAX_NAME_STR];
-
-  if (ELEM(NULL, ntree, node)) {
+  SpaceNode *snode = CTX_wm_space_node(C); /* NULL checked in poll function. */
+  bNodeTree *ntree = snode->edittree;      /* NULL checked in poll function. */
+  bNode *node = nodeGetActive(ntree);
+  if (node == NULL) {
     return;
   }
 
-  for (sock = node->inputs.first; sock; sock = sock->next) {
-    BLI_snprintf(name, sizeof(name), "%s:", sock->name);
+  LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) {
+    char name[UI_MAX_NAME_STR];
+    BLI_snprintf(name, sizeof(name), "%s:", socket->name);
 
-    split = uiLayoutSplit(layout, 0.35f, false);
+    uiLayout *split = uiLayoutSplit(panel->layout, 0.35f, false);
     uiItemL(split, name, ICON_NONE);
-    uiTemplateNodeLink(split, (bContext *)C, ntree, node, sock);
+    uiTemplateNodeLink(split, (bContext *)C, ntree, node, socket);
   }
 }
 
@@ -96,19 +93,20 @@ static bool node_tree_interface_poll(const bContext *C, PanelType *UNUSED(pt))
           (snode->edittree->inputs.first || snode->edittree->outputs.first));
 }
 
-static bool node_tree_find_active_socket(bNodeTree *ntree, bNodeSocket **r_sock, int *r_in_out)
+static bool node_tree_find_active_socket(bNodeTree *ntree,
+                                         bNodeSocket **r_sock,
+                                         eNodeSocketInOut *r_in_out)
 {
-  bNodeSocket *sock;
-  for (sock = ntree->inputs.first; sock; sock = sock->next) {
-    if (sock->flag & SELECT) {
-      *r_sock = sock;
+  LISTBASE_FOREACH (bNodeSocket *, socket, &ntree->inputs) {
+    if (socket->flag & SELECT) {
+      *r_sock = socket;
       *r_in_out = SOCK_IN;
       return true;
     }
   }
-  for (sock = ntree->outputs.first; sock; sock = sock->next) {
-    if (sock->flag & SELECT) {
-      *r_sock = sock;
+  LISTBASE_FOREACH (bNodeSocket *, socket, &ntree->outputs) {
+    if (socket->flag & SELECT) {
+      *r_sock = socket;
       *r_in_out = SOCK_OUT;
       return true;
     }
@@ -121,28 +119,24 @@ static bool node_tree_find_active_socket(bNodeTree *ntree, bNodeSocket **r_sock,
 
 static void node_tree_interface_panel(const bContext *C, Panel *panel)
 {
-  SpaceNode *snode = CTX_wm_space_node(C);
-  bNodeTree *ntree = (snode) ? snode->edittree : NULL;
-  bNodeSocket *sock;
-  int in_out;
-  uiLayout *layout = panel->layout, *row, *split, *col;
-  PointerRNA ptr, sockptr, opptr;
-  wmOperatorType *ot;
-
-  if (!ntree) {
-    return;
-  }
+  SpaceNode *snode = CTX_wm_space_node(C); /* NULL checked in poll function. */
+  bNodeTree *ntree = snode->edittree;      /* NULL checked in poll function. */
+  uiLayout *layout = panel->layout;
 
+  PointerRNA ptr;
   RNA_id_pointer_create((ID *)ntree, &ptr);
 
-  node_tree_find_active_socket(ntree, &sock, &in_out);
-  RNA_pointer_create((ID *)ntree, &RNA_NodeSocketInterface, sock, &sockptr);
+  bNodeSocket *socket;
+  eNodeSocketInOut in_out;
+  node_tree_find_active_socket(ntree, &socket, &in_out);
+  PointerRNA sockptr;
+  RNA_pointer_create((ID *)ntree, &RNA_NodeSocketInterface, socket, &sockptr);
 
-  row = uiLayoutRow(layout, false);
+  uiLayout *row = uiLayoutRow(layout, false);
 
-  split = uiLayoutRow(row, true);
-  col = uiLayoutColumn(split, true);
-  ot = WM_operatortype_find("NODE_OT_tree_socket_add", false);
+  uiLayout *split = uiLayoutRow(row, true);
+  uiLayout *col = uiLayoutColumn(split, true);
+  wmOperatorType *ot = WM_operatortype_find("NODE_OT_tree_socket_add", false);
   uiItemL(col, IFACE_("Inputs:"), ICON_NONE);
   uiTemplateList(col,
                  (bContext *)C,
@@ -159,6 +153,7 @@ static void node_tree_interface_panel(const bContext *C, Panel *panel)
                  0,
                  false,
                  false);
+  PointerRNA opptr;
   uiItemFullO_ptr(col, ot, "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
   RNA_enum_set(&opptr, "in_out", SOCK_IN);
 
@@ -189,14 +184,14 @@ static void node_tree_interface_panel(const bContext *C, Panel *panel)
   uiItemFullO_ptr(col, ot, "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
   RNA_enum_set(&opptr, "direction", 2);
 
-  if (sock) {
+  if (socket) {
     row = uiLayoutRow(layout, true);
     uiItemR(row, &sockptr, "name", 0, NULL, ICON_NONE);
     uiItemO(row, "", ICON_X, "NODE_OT_tree_socket_remove");
 
-    if (sock->typeinfo->interface_draw) {
+    if (socket->typeinfo->interface_draw) {
       uiItemS(layout);
-      sock->typeinfo->interface_draw((bContext *)C, layout, &sockptr);
+      socket->typeinfo->interface_draw((bContext *)C, layout, &sockptr);
     }
   }
 }
diff --git a/source/blender/editors/space_node/node_draw.c b/source/blender/editors/space_node/node_draw.c
index 0e68ebe7c03..a567ae41a54 100644
--- a/source/blender/editors/space_node/node_draw.c
+++ b/source/blender/editors/space_node/node_draw.c
@@ -300,7 +300,6 @@ static void do_node_internal_buttons(bContext *C, void *UNUSED(node_v), int even
 
 static void node_uiblocks_init(const bContext *C, bNodeTree *ntree)
 {
-
   /* add node uiBlocks in drawing order - prevents events going to overlapping nodes */
 
   LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
@@ -995,8 +994,7 @@ void node_draw_sockets(View2D *v2d,
 
   /* socket inputs */
   short selected_input_len = 0;
-  bNodeSocket *sock;
-  for (sock = node->inputs.first; sock; sock = sock->next) {
+  LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
     if (nodeSocketIsHidden(sock)) {
       continue;
     }
@@ -1021,7 +1019,7 @@ void node_draw_sockets(View2D *v2d,
   /* socket outputs */
   short selected_output_len = 0;
   if (draw_outputs) {
-    for (sock = node->outputs.first; sock; sock = sock->next) {
+    LISTBASE_FOREACH (bNodeSocket *, sock, &node->outputs) {
       if (nodeSocketIsHidden(sock)) {
         continue;
       }
@@ -1058,7 +1056,7 @@ void node_draw_sockets(View2D *v2d,
 
     if (selected_input_len) {
       /* socket inputs */
-      for (sock = node->inputs.first; sock; sock = sock->next) {
+      LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
         if (nodeSocketIsHidden(sock)) {
           continue;
         }
@@ -1083,7 +1081,7 @@ void node_draw_sockets(View2D *v2d,
 
     if (selected_output_len) {
       /* socket outputs */
-      for (sock = node->outputs.first; sock; sock = sock->next) {
+      LISTBASE_FOREACH (bNodeSocket *, sock, &node->outputs) {
         if (nodeSocketIsHidden(sock)) {
           continue;
         }
@@ -1162,23 +1160,22 @@ static void node_draw_basis(const bContext *C,
 
   /* preview */
   if (node->typeinfo->flag & NODE_PREVIEW) {
-    uiBut *but;
     iconofs -= iconbutw;
     UI_block_emboss_set(node->block, UI_EMBOSS_NONE);
-    but = uiDefIconBut(node->block,
-                       UI_BTYPE_BUT_TOGGLE,
-                       B_REDR,
-                       ICON_MATERIAL,
-                       iconofs,
-                       rct->ymax - NODE_DY,
-                       iconbutw,
-                       UI_UNIT_Y,
-                       NULL,
-                       0,
-                       0,
-                       0,
-                       0,
-                       "");
+    uiBut *but = uiDefIconBut(node->block,
+                              UI_BTYPE_BUT_TOGGLE,
+                              B_REDR,
+                              ICON_MATERIAL,
+                              iconofs,
+                              rct->ymax - NODE_DY,
+                              iconbutw,
+                              UI_UNIT_Y,
+                              NULL,
+                              0,
+                              0,
+                              0,
+                              0,
+                              "");
     UI_but_func_set(but, node_toggle_button_cb, node, (void *)"NODE_OT_preview_toggle");
     /* XXX this does not work when node is activated and the operator called right afterwards,
      * since active ID is not updated yet (needs to process the notifier).
@@ -1190,23 +1187,22 @@ static void node_draw_basis(const bContext *C,
   }
   /* group edit */
   if (node->type == NODE_GROUP) {
-    uiBut *but;
     iconofs -= iconbutw;
     UI_block_emboss_set(node->block, UI_EMBOSS_NONE);
-    but = uiDefIconBut(node->block,
-                       UI_BTYPE_BUT_TOGGLE,
-                       B_REDR,
-                       ICON_NODETREE,
-                       iconofs,
-                       rct->ymax - NODE_DY,
-                       iconbutw,
-                       UI_UNIT_Y,
-                       NULL,
-                       0,
-                       0,
-                       0,
-                       0,
-                       "");
+    uiBut *but = uiDefIconBut(node->block,
+                              UI_BTYPE_BUT_TOGGLE,
+                              B_REDR,
+                              ICON_NODETREE,
+                              iconofs,
+                              rct->ymax - NODE_DY,
+                              iconbutw,
+                              UI_UNIT_Y,
+                              NULL,
+                              0,
+                              0,
+                              0,
+                              0,
+                              "");
     UI_but_func_set(but, node_toggle_button_cb, node, (void *)"NODE_OT_group_edit");
     UI_block_emboss_set(node->block, UI_EMBOSS);
   }
@@ -1240,24 +1236,23 @@ static void node_draw_basis(const bContext *C,
 
   /* open/close entirely? */
   {
-    uiBut *but;
     int but_size = U.widget_unit * 0.8f;
     /* XXX button uses a custom triangle draw below, so make it invisi

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list