Blender  V2.93
node_buttons.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_node_types.h"
27 
28 #include "BLI_blenlib.h"
29 #include "BLI_math.h"
30 
31 #include "BLT_translation.h"
32 
33 #include "BKE_context.h"
34 #include "BKE_global.h"
35 #include "BKE_node.h"
36 #include "BKE_screen.h"
37 
38 #include "WM_api.h"
39 #include "WM_types.h"
40 
41 #include "RNA_access.h"
42 
43 #include "ED_screen.h"
44 
45 #include "UI_resources.h"
46 
47 #include "node_intern.h" /* own include */
48 
49 /* ******************* node space & buttons ************** */
50 
51 #if 0
52 /* poll for active nodetree */
53 static bool active_nodetree_poll(const bContext *C, PanelType *UNUSED(pt))
54 {
55  SpaceNode *snode = CTX_wm_space_node(C);
56 
57  return (snode && snode->nodetree);
58 }
59 #endif
60 
61 static bool node_sockets_poll(const bContext *C, PanelType *UNUSED(pt))
62 {
63  SpaceNode *snode = CTX_wm_space_node(C);
64 
65  return (snode && snode->nodetree && G.debug_value == 777);
66 }
67 
68 static void node_sockets_panel(const bContext *C, Panel *panel)
69 {
70  SpaceNode *snode = CTX_wm_space_node(C); /* NULL checked in poll function. */
71  bNodeTree *ntree = snode->edittree; /* NULL checked in poll function. */
73  if (node == NULL) {
74  return;
75  }
76 
77  LISTBASE_FOREACH (bNodeSocket *, socket, &node->inputs) {
78  char name[UI_MAX_NAME_STR];
79  BLI_snprintf(name, sizeof(name), "%s:", socket->name);
80 
81  uiLayout *split = uiLayoutSplit(panel->layout, 0.35f, false);
82  uiItemL(split, name, ICON_NONE);
84  }
85 }
86 
88 {
89  SpaceNode *snode = CTX_wm_space_node(C);
90 
91  return (snode && snode->edittree &&
92  (snode->edittree->inputs.first || snode->edittree->outputs.first));
93 }
94 
96 {
97  ListBase *sockets = (in_out == SOCK_IN) ? &ntree->inputs : &ntree->outputs;
98  LISTBASE_FOREACH (bNodeSocket *, socket, sockets) {
99  if (socket->flag & SELECT) {
100  return socket;
101  }
102  }
103  return NULL;
104 }
105 
106 static void draw_socket_list(const bContext *C,
107  uiLayout *layout,
108  bNodeTree *ntree,
109  const eNodeSocketInOut in_out)
110 {
111  PointerRNA tree_ptr;
112  RNA_id_pointer_create((ID *)ntree, &tree_ptr);
113 
114  uiLayout *split = uiLayoutRow(layout, false);
115  uiLayout *list_col = uiLayoutColumn(split, true);
116  uiTemplateList(list_col,
117  (bContext *)C,
118  "NODE_UL_interface_sockets",
119  (in_out == SOCK_IN) ? "inputs" : "outputs",
120  &tree_ptr,
121  (in_out == SOCK_IN) ? "inputs" : "outputs",
122  &tree_ptr,
123  (in_out == SOCK_IN) ? "active_input" : "active_output",
124  NULL,
125  0,
126  0,
127  0,
128  0,
129  false,
130  false);
131  PointerRNA opptr;
132  uiLayout *ops_col = uiLayoutColumn(split, false);
133  uiLayout *add_remove_col = uiLayoutColumn(ops_col, true);
134  wmOperatorType *ot = WM_operatortype_find("NODE_OT_tree_socket_add", false);
135  uiItemFullO_ptr(add_remove_col, ot, "", ICON_ADD, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
136  RNA_enum_set(&opptr, "in_out", in_out);
137  ot = WM_operatortype_find("NODE_OT_tree_socket_remove", false);
138  uiItemFullO_ptr(add_remove_col, ot, "", ICON_REMOVE, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
139  RNA_enum_set(&opptr, "in_out", in_out);
140 
141  uiItemS(ops_col);
142 
143  uiLayout *up_down_col = uiLayoutColumn(ops_col, true);
144  ot = WM_operatortype_find("NODE_OT_tree_socket_move", false);
145  uiItemFullO_ptr(up_down_col, ot, "", ICON_TRIA_UP, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
146  RNA_enum_set(&opptr, "direction", 1);
147  RNA_enum_set(&opptr, "in_out", in_out);
148  uiItemFullO_ptr(up_down_col, ot, "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, 0, &opptr);
149  RNA_enum_set(&opptr, "direction", 2);
150  RNA_enum_set(&opptr, "in_out", in_out);
151 
153  if (socket != NULL) {
154  uiLayoutSetPropSep(layout, true);
155  uiLayoutSetPropDecorate(layout, false);
156  PointerRNA socket_ptr;
157  RNA_pointer_create((ID *)ntree, &RNA_NodeSocketInterface, socket, &socket_ptr);
158  uiItemR(layout, &socket_ptr, "name", 0, NULL, ICON_NONE);
159 
160  /* Display descriptions only for Geometry Nodes, since it's only used in the modifier panel. */
161  if (ntree->type == NTREE_GEOMETRY) {
162  uiItemR(layout, &socket_ptr, "description", 0, NULL, ICON_NONE);
163  }
164 
165  if (socket->typeinfo->interface_draw) {
166  socket->typeinfo->interface_draw((bContext *)C, layout, &socket_ptr);
167  }
168  }
169 }
170 
172 {
173  SpaceNode *snode = CTX_wm_space_node(C); /* NULL checked in poll function. */
174  bNodeTree *ntree = snode->edittree; /* NULL checked in poll function. */
175 
177 }
178 
180 {
181  SpaceNode *snode = CTX_wm_space_node(C); /* NULL checked in poll function. */
182  bNodeTree *ntree = snode->edittree; /* NULL checked in poll function. */
183 
185 }
186 
187 /* ******************* node buttons registration ************** */
188 
190 {
191  {
192  PanelType *pt = MEM_callocN(sizeof(PanelType), __func__);
193  strcpy(pt->idname, "NODE_PT_sockets");
194  strcpy(pt->category, N_("Node"));
195  strcpy(pt->label, N_("Sockets"));
197  pt->draw = node_sockets_panel;
198  pt->poll = node_sockets_poll;
200  BLI_addtail(&art->paneltypes, pt);
201  }
202 
203  {
204  PanelType *pt = MEM_callocN(sizeof(PanelType), __func__);
205  strcpy(pt->idname, "NODE_PT_node_tree_interface_inputs");
206  strcpy(pt->category, N_("Group"));
207  strcpy(pt->label, N_("Inputs"));
211  BLI_addtail(&art->paneltypes, pt);
212  }
213  {
214  PanelType *pt = MEM_callocN(sizeof(PanelType), __func__);
215  strcpy(pt->idname, "NODE_PT_node_tree_interface_outputs");
216  strcpy(pt->category, N_("Group"));
217  strcpy(pt->label, N_("Outputs"));
221  BLI_addtail(&art->paneltypes, pt);
222  }
223 }
struct SpaceNode * CTX_wm_space_node(const bContext *C)
Definition: context.c:854
struct bNode * nodeGetActive(struct bNodeTree *ntree)
Definition: node.cc:3561
@ PANEL_TYPE_DEFAULT_CLOSED
Definition: BKE_screen.h:297
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define UNUSED(x)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define N_(msgid)
#define NTREE_GEOMETRY
eNodeSocketInOut
@ SOCK_OUT
@ SOCK_IN
Read Guarded memory(de)allocation.
StructRNA RNA_NodeSocketInterface
#define C
Definition: RandGen.cpp:39
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiItemL(uiLayout *layout, const char *name, int icon)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemS(uiLayout *layout)
void uiItemFullO_ptr(uiLayout *layout, struct wmOperatorType *ot, const char *name, int icon, struct IDProperty *properties, int context, int flag, struct PointerRNA *r_opptr)
uiLayout * uiLayoutRow(uiLayout *layout, bool align)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
uiLayout * uiLayoutSplit(uiLayout *layout, float percentage, bool align)
void uiTemplateNodeLink(uiLayout *layout, struct bContext *C, struct bNodeTree *ntree, struct bNode *node, struct bNodeSocket *input)
#define UI_MAX_NAME_STR
Definition: UI_interface.h:91
void uiTemplateList(uiLayout *layout, struct bContext *C, const char *listtype_name, const char *list_id, struct PointerRNA *dataptr, const char *propname, struct PointerRNA *active_dataptr, const char *active_propname, const char *item_dyntip_propname, int rows, int maxrows, int layout_type, int columns, bool sort_reverse, bool sort_lock)
@ WM_OP_EXEC_DEFAULT
Definition: WM_types.h:204
#define SELECT
OperationNode * node
bNodeTree * ntree
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition: abc_util.cc:115
static bool node_sockets_poll(const bContext *C, PanelType *UNUSED(pt))
Definition: node_buttons.c:61
void node_buttons_register(ARegionType *art)
Definition: node_buttons.c:189
static void node_tree_interface_outputs_panel(const bContext *C, Panel *panel)
Definition: node_buttons.c:179
static void draw_socket_list(const bContext *C, uiLayout *layout, bNodeTree *ntree, const eNodeSocketInOut in_out)
Definition: node_buttons.c:106
static bNodeSocket * node_tree_find_active_socket(bNodeTree *ntree, const eNodeSocketInOut in_out)
Definition: node_buttons.c:95
static void node_tree_interface_inputs_panel(const bContext *C, Panel *panel)
Definition: node_buttons.c:171
static bool node_tree_interface_poll(const bContext *C, PanelType *UNUSED(pt))
Definition: node_buttons.c:87
static void node_sockets_panel(const bContext *C, Panel *panel)
Definition: node_buttons.c:68
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:6413
ListBase paneltypes
Definition: BKE_screen.h:216
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
void(* draw)(const struct bContext *C, struct Panel *panel)
Definition: BKE_screen.h:266
bool(* poll)(const struct bContext *C, struct PanelType *pt)
Definition: BKE_screen.h:260
char idname[BKE_ST_MAXNAME]
Definition: BKE_screen.h:241
char translation_context[BKE_ST_MAXNAME]
Definition: BKE_screen.h:244
char category[BKE_ST_MAXNAME]
Definition: BKE_screen.h:246
char label[BKE_ST_MAXNAME]
Definition: BKE_screen.h:242
struct uiLayout * layout
struct bNodeTree * edittree
struct bNodeTree * nodetree
void(* interface_draw)(struct bContext *C, struct uiLayout *layout, struct PointerRNA *ptr)
Definition: BKE_node.h:156
struct bNodeSocketType * typeinfo
ListBase inputs
ListBase outputs
#define G(x, y, z)
wmOperatorType * ot
Definition: wm_files.c:3156
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)