2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_node/node_buttons.c
31 #include "MEM_guardedalloc.h"
33 #include "DNA_node_types.h"
36 #include "BLI_blenlib.h"
38 #include "BLF_translation.h"
40 #include "BKE_context.h"
41 #include "BKE_global.h"
43 #include "BKE_screen.h"
48 #include "RNA_access.h"
50 #include "ED_gpencil.h"
51 #include "ED_screen.h"
53 #include "UI_resources.h"
55 #include "node_intern.h" /* own include */
58 /* ******************* node space & buttons ************** */
60 /* poll for active nodetree */
61 static int active_nodetree_poll(const bContext *C, PanelType *UNUSED(pt))
63 SpaceNode *snode = CTX_wm_space_node(C);
65 return (snode && snode->nodetree);
68 static int node_sockets_poll(const bContext *C, PanelType *UNUSED(pt))
70 SpaceNode *snode = CTX_wm_space_node(C);
72 return (snode && snode->nodetree && G.debug_value == 777);
75 static void node_sockets_panel(const bContext *C, Panel *pa)
77 SpaceNode *snode = CTX_wm_space_node(C);
78 bNodeTree *ntree = (snode) ? snode->edittree : NULL;
79 bNode *node = (ntree) ? nodeGetActive(ntree) : NULL;
81 uiLayout *layout = pa->layout, *split;
82 char name[UI_MAX_NAME_STR];
84 if (ELEM(NULL, ntree, node))
87 for (sock = node->inputs.first; sock; sock = sock->next) {
88 BLI_snprintf(name, sizeof(name), "%s:", sock->name);
90 split = uiLayoutSplit(layout, 0.35f, FALSE);
91 uiItemL(split, name, ICON_NONE);
92 uiTemplateNodeLink(split, ntree, node, sock);
96 static int node_tree_interface_poll(const bContext *C, PanelType *UNUSED(pt))
98 SpaceNode *snode = CTX_wm_space_node(C);
100 return (snode && snode->edittree && (snode->edittree->inputs.first || snode->edittree->outputs.first));
103 static int node_tree_find_active_socket(bNodeTree *ntree, bNodeSocket **r_sock, int *r_in_out)
106 for (sock = ntree->inputs.first; sock; sock = sock->next) {
107 if (sock->flag & SELECT) {
113 for (sock = ntree->outputs.first; sock; sock = sock->next) {
114 if (sock->flag & SELECT) {
116 *r_in_out = SOCK_OUT;
126 static void node_tree_interface_panel(const bContext *C, Panel *pa)
128 SpaceNode *snode = CTX_wm_space_node(C);
129 bNodeTree *ntree = (snode) ? snode->edittree : NULL;
132 uiLayout *layout = pa->layout, *row, *split, *col;
133 PointerRNA ptr, sockptr, opptr;
138 RNA_id_pointer_create((ID *)ntree, &ptr);
140 node_tree_find_active_socket(ntree, &sock, &in_out);
141 RNA_pointer_create((ID *)ntree, &RNA_NodeSocketInterface, sock, &sockptr);
143 row = uiLayoutRow(layout, FALSE);
145 split = uiLayoutRow(row, TRUE);
146 col = uiLayoutColumn(split, TRUE);
147 uiItemL(col, IFACE_("Inputs:"), ICON_NONE);
148 uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "inputs", &ptr, "inputs", &ptr, "active_input",
150 opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
151 RNA_enum_set(&opptr, "in_out", SOCK_IN);
153 col = uiLayoutColumn(split, TRUE);
154 uiItemL(col, IFACE_("Outputs:"), ICON_NONE);
155 uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "outputs", &ptr, "outputs", &ptr, "active_output",
157 opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
158 RNA_enum_set(&opptr, "in_out", SOCK_OUT);
160 col = uiLayoutColumn(row, TRUE);
161 opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_UP, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
162 RNA_enum_set(&opptr, "direction", 1);
163 opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
164 RNA_enum_set(&opptr, "direction", 2);
167 row = uiLayoutRow(layout, TRUE);
168 uiItemR(row, &sockptr, "name", 0, NULL, ICON_NONE);
169 uiItemO(row, "", ICON_X, "NODE_OT_tree_socket_remove");
171 if (sock->typeinfo->interface_draw) {
173 sock->typeinfo->interface_draw((bContext *)C, layout, &sockptr);
178 /* ******************* node buttons registration ************** */
180 void node_buttons_register(ARegionType *art)
184 pt = MEM_callocN(sizeof(PanelType), "spacetype node panel node sockets");
185 strcpy(pt->idname, "NODE_PT_sockets");
186 strcpy(pt->label, N_("Sockets"));
187 strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
188 pt->draw = node_sockets_panel;
189 pt->poll = node_sockets_poll;
190 pt->flag |= PNL_DEFAULT_CLOSED;
191 BLI_addtail(&art->paneltypes, pt);
193 pt = MEM_callocN(sizeof(PanelType), "spacetype node panel tree interface");
194 strcpy(pt->idname, "NODE_PT_node_tree_interface");
195 strcpy(pt->label, N_("Interface"));
196 strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
197 pt->draw = node_tree_interface_panel;
198 pt->poll = node_tree_interface_poll;
199 BLI_addtail(&art->paneltypes, pt);
201 pt = MEM_callocN(sizeof(PanelType), "spacetype node panel gpencil");
202 strcpy(pt->idname, "NODE_PT_gpencil");
203 strcpy(pt->label, N_("Grease Pencil"));
204 strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
205 pt->draw_header = gpencil_panel_standard_header;
206 pt->draw = gpencil_panel_standard;
207 pt->poll = active_nodetree_poll;
208 BLI_addtail(&art->paneltypes, pt);
211 static int node_properties_toggle_exec(bContext *C, wmOperator *UNUSED(op))
213 ScrArea *sa = CTX_wm_area(C);
214 ARegion *ar = node_has_buttons_region(sa);
217 ED_region_toggle_hidden(C, ar);
219 return OPERATOR_FINISHED;
222 /* non-standard poll operator which doesn't care if there are any nodes */
223 static int node_properties_poll(bContext *C)
225 ScrArea *sa = CTX_wm_area(C);
226 return (sa && (sa->spacetype == SPACE_NODE));
229 void NODE_OT_properties(wmOperatorType *ot)
231 ot->name = "Properties";
232 ot->description = "Toggles the properties panel display";
233 ot->idname = "NODE_OT_properties";
235 ot->exec = node_properties_toggle_exec;
236 ot->poll = node_properties_poll;