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", 0, 0, 0);
149 opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
150 RNA_enum_set(&opptr, "in_out", SOCK_IN);
152 col = uiLayoutColumn(split, TRUE);
153 uiItemL(col, IFACE_("Outputs:"), ICON_NONE);
154 uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "outputs", &ptr, "outputs", &ptr, "active_output", 0, 0, 0);
155 opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
156 RNA_enum_set(&opptr, "in_out", SOCK_OUT);
158 col = uiLayoutColumn(row, TRUE);
159 opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_UP, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
160 RNA_enum_set(&opptr, "direction", 1);
161 opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
162 RNA_enum_set(&opptr, "direction", 2);
165 row = uiLayoutRow(layout, TRUE);
166 uiItemR(row, &sockptr, "name", 0, NULL, ICON_NONE);
167 uiItemO(row, "", ICON_X, "NODE_OT_tree_socket_remove");
169 if (sock->typeinfo->interface_draw) {
171 sock->typeinfo->interface_draw((bContext *)C, layout, &sockptr);
176 /* ******************* node buttons registration ************** */
178 void node_buttons_register(ARegionType *art)
182 pt = MEM_callocN(sizeof(PanelType), "spacetype node panel node sockets");
183 strcpy(pt->idname, "NODE_PT_sockets");
184 strcpy(pt->label, N_("Sockets"));
185 strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
186 pt->draw = node_sockets_panel;
187 pt->poll = node_sockets_poll;
188 pt->flag |= PNL_DEFAULT_CLOSED;
189 BLI_addtail(&art->paneltypes, pt);
191 pt = MEM_callocN(sizeof(PanelType), "spacetype node panel tree interface");
192 strcpy(pt->idname, "NODE_PT_node_tree_interface");
193 strcpy(pt->label, N_("Interface"));
194 strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
195 pt->draw = node_tree_interface_panel;
196 pt->poll = node_tree_interface_poll;
197 BLI_addtail(&art->paneltypes, pt);
199 pt = MEM_callocN(sizeof(PanelType), "spacetype node panel gpencil");
200 strcpy(pt->idname, "NODE_PT_gpencil");
201 strcpy(pt->label, N_("Grease Pencil"));
202 strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
203 pt->draw_header = gpencil_panel_standard_header;
204 pt->draw = gpencil_panel_standard;
205 pt->poll = active_nodetree_poll;
206 BLI_addtail(&art->paneltypes, pt);
209 static int node_properties_toggle_exec(bContext *C, wmOperator *UNUSED(op))
211 ScrArea *sa = CTX_wm_area(C);
212 ARegion *ar = node_has_buttons_region(sa);
215 ED_region_toggle_hidden(C, ar);
217 return OPERATOR_FINISHED;
220 /* non-standard poll operator which doesn't care if there are any nodes */
221 static int node_properties_poll(bContext *C)
223 ScrArea *sa = CTX_wm_area(C);
224 return (sa && (sa->spacetype == SPACE_NODE));
227 void NODE_OT_properties(wmOperatorType *ot)
229 ot->name = "Properties";
230 ot->description = "Toggles the properties panel display";
231 ot->idname = "NODE_OT_properties";
233 ot->exec = node_properties_toggle_exec;
234 ot->poll = node_properties_poll;