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".
This commit is contained in:
Hans Goudey 2020-11-30 13:56:46 -05:00
parent 5a35e56bcb
commit 007a0e43a0
2 changed files with 110 additions and 130 deletions

View File

@ -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);
}
}
}

View File

@ -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 invisible without icon */
UI_block_emboss_set(node->block, UI_EMBOSS_NONE);
but = uiDefBut(node->block,
UI_BTYPE_BUT_TOGGLE,
B_REDR,
"",
rct->xmin + 0.35f * U.widget_unit,
rct->ymax - NODE_DY / 2.2f - but_size / 2,
but_size,
but_size,
NULL,
0,
0,
0,
0,
"");
uiBut *but = uiDefBut(node->block,
UI_BTYPE_BUT_TOGGLE,
B_REDR,
"",
rct->xmin + 0.35f * U.widget_unit,
rct->ymax - NODE_DY / 2.2f - but_size / 2,
but_size,
but_size,
NULL,
0,
0,
0,
0,
"");
UI_but_func_set(but, node_toggle_button_cb, node, (void *)"NODE_OT_hide_toggle");
UI_block_emboss_set(node->block, UI_EMBOSS);
@ -1412,24 +1407,23 @@ static void node_draw_hidden(const bContext *C,
/* open entirely icon */
{
uiBut *but;
int but_size = U.widget_unit * 0.8f;
/* XXX button uses a custom triangle draw below, so make it invisible without icon */
UI_block_emboss_set(node->block, UI_EMBOSS_NONE);
but = uiDefBut(node->block,
UI_BTYPE_BUT_TOGGLE,
B_REDR,
"",
rct->xmin + 0.35f * U.widget_unit,
centy - but_size / 2,
but_size,
but_size,
NULL,
0,
0,
0,
0,
"");
uiBut *but = uiDefBut(node->block,
UI_BTYPE_BUT_TOGGLE,
B_REDR,
"",
rct->xmin + 0.35f * U.widget_unit,
centy - but_size / 2,
but_size,
but_size,
NULL,
0,
0,
0,
0,
"");
UI_but_func_set(but, node_toggle_button_cb, node, (void *)"NODE_OT_hide_toggle");
UI_block_emboss_set(node->block, UI_EMBOSS);
@ -1609,11 +1603,8 @@ void node_draw_nodetree(const bContext *C,
#endif
/* draw background nodes, last nodes in front */
int a;
bNode *node;
for (a = 0, node = ntree->nodes.first; node; node = node->next, a++) {
bNodeInstanceKey key;
int a = 0;
LISTBASE_FOREACH_INDEX (bNode *, node, &ntree->nodes, a) {
#ifdef USE_DRAW_TOT_UPDATE
/* unrelated to background nodes, update the v2d->tot,
* can be anywhere before we draw the scroll bars */
@ -1624,7 +1615,7 @@ void node_draw_nodetree(const bContext *C,
continue;
}
key = BKE_node_instance_key(parent_key, ntree, node);
bNodeInstanceKey key = BKE_node_instance_key(parent_key, ntree, node);
node->nr = a; /* index of node in list, used for exec event code */
node_draw(C, region, snode, ntree, node, key);
}
@ -1641,13 +1632,13 @@ void node_draw_nodetree(const bContext *C,
GPU_blend(GPU_BLEND_NONE);
/* draw foreground nodes, last nodes in front */
for (a = 0, node = ntree->nodes.first; node; node = node->next, a++) {
bNodeInstanceKey key;
a = 0;
LISTBASE_FOREACH_INDEX (bNode *, node, &ntree->nodes, a) {
if (node->flag & NODE_BACKGROUND) {
continue;
}
key = BKE_node_instance_key(parent_key, ntree, node);
bNodeInstanceKey key = BKE_node_instance_key(parent_key, ntree, node);
node->nr = a; /* index of node in list, used for exec event code */
node_draw(C, region, snode, ntree, node, key);
}
@ -1695,7 +1686,6 @@ static void draw_group_overlay(const bContext *C, ARegion *region)
{
View2D *v2d = &region->v2d;
rctf rect = v2d->cur;
uiBlock *block;
float color[4];
/* shade node groups to separate them visually */
@ -1707,7 +1697,7 @@ static void draw_group_overlay(const bContext *C, ARegion *region)
GPU_blend(GPU_BLEND_NONE);
/* set the block bounds to clip mouse events from underlying nodes */
block = UI_block_begin(C, region, "node tree bounds block", UI_EMBOSS);
uiBlock *block = UI_block_begin(C, region, "node tree bounds block", UI_EMBOSS);
UI_block_bounds_set_explicit(block, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
UI_block_flag_enable(block, UI_BLOCK_CLIP_EVENTS);
UI_block_end(C, block);
@ -1752,14 +1742,8 @@ void node_draw_space(const bContext *C, ARegion *region)
/* draw parent node trees */
if (snode->treepath.last) {
static const int max_depth = 2;
bNodeTreePath *path;
int depth, curdepth;
float center[2];
bNodeTree *ntree;
bNodeLinkDrag *nldrag;
LinkData *linkdata;
path = snode->treepath.last;
bNodeTreePath *path = snode->treepath.last;
/* update tree path name (drawn in the bottom left) */
ID *name_id = (path->nodetree && path->nodetree != snode->nodetree) ? &path->nodetree->id :
@ -1770,6 +1754,7 @@ void node_draw_space(const bContext *C, ARegion *region)
}
/* current View2D center, will be set temporarily for parent node trees */
float center[2];
UI_view2d_center_get(v2d, &center[0], &center[1]);
/* store new view center in path and current edittree */
@ -1778,15 +1763,15 @@ void node_draw_space(const bContext *C, ARegion *region)
copy_v2_v2(snode->edittree->view_center, center);
}
depth = 0;
int depth = 0;
while (path->prev && depth < max_depth) {
path = path->prev;
depth++;
}
/* parent node trees in the background */
for (curdepth = depth; curdepth > 0; path = path->next, curdepth--) {
ntree = path->nodetree;
for (int curdepth = depth; curdepth > 0; path = path->next, curdepth--) {
bNodeTree *ntree = path->nodetree;
if (ntree) {
snode_setup_v2d(snode, region, path->view_center);
@ -1797,7 +1782,7 @@ void node_draw_space(const bContext *C, ARegion *region)
}
/* top-level edit tree */
ntree = path->nodetree;
bNodeTree *ntree = path->nodetree;
if (ntree) {
snode_setup_v2d(snode, region, center);
@ -1832,8 +1817,8 @@ void node_draw_space(const bContext *C, ARegion *region)
/* temporary links */
GPU_blend(GPU_BLEND_ALPHA);
GPU_line_smooth(true);
for (nldrag = snode->linkdrag.first; nldrag; nldrag = nldrag->next) {
for (linkdata = nldrag->links.first; linkdata; linkdata = linkdata->next) {
LISTBASE_FOREACH (bNodeLinkDrag *, nldrag, &snode->linkdrag) {
LISTBASE_FOREACH (LinkData *, linkdata, &nldrag->links) {
node_draw_link(v2d, snode, (bNodeLink *)linkdata->data);
}
}