Nodes: add cycles shader nodes, these will only be available to render engines
[blender.git] / source / blender / editors / space_node / node_templates.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Blender Foundation 2009.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/interface/interface_node.c
26  *  \ingroup edinterface
27  */
28
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_node_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38
39 #include "BLI_listbase.h"
40 #include "BLI_string.h"
41 #include "BLI_utildefines.h"
42
43 #include "BKE_context.h"
44 #include "BKE_depsgraph.h"
45 #include "BKE_library.h"
46 #include "BKE_main.h"
47 #include "BKE_node.h"
48 #include "BKE_scene.h"
49
50 #include "RNA_access.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57 #include "../interface/interface_intern.h"
58
59 #include "ED_node.h"
60
61 /************************* Node Socket Manipulation **************************/
62
63 static void node_tag_recursive(bNode *node)
64 {
65         bNodeSocket *input;
66
67         if(!node || (node->flag & NODE_TEST))
68                 return; /* in case of cycles */
69         
70         node->flag |= NODE_TEST;
71
72         for(input=node->inputs.first; input; input=input->next)
73                 if(input->link)
74                         node_tag_recursive(input->link->fromnode);
75 }
76
77 static void node_clear_recursive(bNode *node)
78 {
79         bNodeSocket *input;
80
81         if(!node || !(node->flag & NODE_TEST))
82                 return; /* in case of cycles */
83         
84         node->flag &= ~NODE_TEST;
85
86         for(input=node->inputs.first; input; input=input->next)
87                 if(input->link)
88                         node_clear_recursive(input->link->fromnode);
89 }
90
91 static void node_remove_linked(bNodeTree *ntree, bNode *rem_node)
92 {
93         bNode *node, *next;
94         bNodeSocket *sock;
95
96         if(!rem_node)
97                 return;
98
99         /* tag linked nodes to be removed */
100         for(node=ntree->nodes.first; node; node=node->next)
101                 node->flag &= ~NODE_TEST;
102         
103         node_tag_recursive(rem_node);
104
105         /* clear tags on nodes that are still used by other nodes */
106         for(node=ntree->nodes.first; node; node=node->next)
107                 if(!(node->flag & NODE_TEST))
108                         for(sock=node->inputs.first; sock; sock=sock->next)
109                                 if(sock->link && sock->link->fromnode != rem_node)
110                                         node_clear_recursive(sock->link->fromnode);
111
112         /* remove nodes */
113         for(node=ntree->nodes.first; node; node=next) {
114                 next = node->next;
115
116                 if(node->flag & NODE_TEST) {
117                         if(node->id)
118                                 node->id->us--;
119                         nodeFreeNode(ntree, node);
120                 }
121         }
122 }
123
124 /* disconnect socket from the node it is connected to */
125 static void node_socket_disconnect(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to)
126 {
127         if(!sock_to->link)
128                 return;
129
130         nodeRemLink(ntree, sock_to->link);
131
132         nodeUpdate(ntree, node_to);
133         ntreeUpdateTree(ntree);
134
135         ED_node_generic_update(bmain, ntree, node_to);
136 }
137
138 /* remove all nodes connected to this socket, if they aren't connected to other nodes */
139 static void node_socket_remove(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to)
140 {
141         if(!sock_to->link)
142                 return;
143
144         node_remove_linked(ntree, sock_to->link->fromnode);
145
146         nodeUpdate(ntree, node_to);
147         ntreeUpdateTree(ntree);
148
149         ED_node_generic_update(bmain, ntree, node_to);
150 }
151
152 /* add new node connected to this socket, or replace an existing one */
153 static void node_socket_add_replace(Main *bmain, bNodeTree *ntree, bNode *node_to, bNodeSocket *sock_to, bNodeTemplate *ntemp, int sock_num)
154 {
155         bNode *node_from;
156         bNodeSocket *sock_from;
157         bNode *node_prev = NULL;
158
159         /* unlink existing node */
160         if(sock_to->link) {
161                 node_prev = sock_to->link->fromnode;
162                 nodeRemLink(ntree, sock_to->link);
163         }
164
165         /* find existing node that we can use */
166         for(node_from=ntree->nodes.first; node_from; node_from=node_from->next)
167                 if(node_from->type == ntemp->type)
168                         break;
169
170         if(node_from)
171                 if(!(node_from->inputs.first == NULL && !(node_from->typeinfo->flag & NODE_OPTIONS)))
172                         node_from = NULL;
173
174         if(node_prev && node_prev->type == ntemp->type &&
175                 (ntemp->type != NODE_GROUP || node_prev->id == &ntemp->ngroup->id)) {
176                 /* keep the previous node if it's the same type */
177                 node_from = node_prev;
178         }
179         else if(!node_from) {
180                 node_from= nodeAddNode(ntree, ntemp);
181                 node_from->locx = node_to->locx - (node_from->typeinfo->width + 50);
182                 node_from->locy = node_to->locy;
183
184                 if(node_from->id)
185                         id_us_plus(node_from->id);
186         }
187
188         nodeSetActive(ntree, node_from);
189
190         /* add link */
191         sock_from = BLI_findlink(&node_from->outputs, sock_num);
192         nodeAddLink(ntree, node_from, sock_from, node_to, sock_to);
193
194         /* copy input sockets from previous node */
195         if(node_prev && node_from != node_prev) {
196                 bNodeSocket *sock_prev, *sock_from;
197
198                 for(sock_prev=node_prev->inputs.first; sock_prev; sock_prev=sock_prev->next) {
199                         for(sock_from=node_from->inputs.first; sock_from; sock_from=sock_from->next) {
200                                 if(strcmp(sock_prev->name, sock_from->name) == 0 && sock_prev->type == sock_from->type) {
201                                         bNodeLink *link = sock_prev->link;
202
203                                         if(link && link->fromnode) {
204                                                 nodeAddLink(ntree, link->fromnode, link->fromsock, node_from, sock_from);
205                                                 nodeRemLink(ntree, link);
206                                         }
207
208                                         if(sock_prev->default_value) {
209                                                 if(sock_from->default_value)
210                                                         MEM_freeN(sock_from->default_value);
211
212                                                 sock_from->default_value = MEM_dupallocN(sock_prev->default_value);
213                                         }
214                                 }
215                         }
216                 }
217
218                 /* remove node */
219                 node_remove_linked(ntree, node_prev);
220         }
221
222         nodeUpdate(ntree, node_from);
223         nodeUpdate(ntree, node_to);
224         ntreeUpdateTree(ntree);
225
226         ED_node_generic_update(bmain, ntree, node_to);
227 }
228
229 /****************************** Node Link Menu *******************************/
230
231 #define UI_NODE_LINK_ADD                0
232 #define UI_NODE_LINK_DISCONNECT -1
233 #define UI_NODE_LINK_REMOVE             -2
234
235 typedef struct NodeLinkArg {
236         Main *bmain;
237         Scene *scene;
238         bNodeTree *ntree;
239         bNode *node;
240         bNodeSocket *sock;
241
242         bNodeTree *ngroup;
243         int type;
244         int output;
245
246         uiLayout *layout;
247 } NodeLinkArg;
248
249 static void ui_node_link(bContext *UNUSED(C), void *arg_p, void *event_p)
250 {
251         NodeLinkArg *arg = (NodeLinkArg*)arg_p;
252         Main *bmain = arg->bmain;
253         bNode *node_to = arg->node;
254         bNodeSocket *sock_to = arg->sock;
255         bNodeTree *ntree = arg->ntree;
256         int event = GET_INT_FROM_POINTER(event_p);
257         bNodeTemplate ntemp;
258
259         ntemp.type = arg->type;
260         ntemp.ngroup = arg->ngroup;
261
262         if(event == UI_NODE_LINK_DISCONNECT)
263                 node_socket_disconnect(bmain, ntree, node_to, sock_to);
264         else if(event == UI_NODE_LINK_REMOVE)
265                 node_socket_remove(bmain, ntree, node_to, sock_to);
266         else
267                 node_socket_add_replace(bmain, ntree, node_to, sock_to, &ntemp, arg->output);
268 }
269
270 static void ui_node_sock_name(bNodeSocket *sock, char name[UI_MAX_NAME_STR])
271 {
272         if(sock->link && sock->link->fromnode) {
273                 bNode *node = sock->link->fromnode;
274                 char node_name[UI_MAX_NAME_STR];
275
276                 if(node->type == NODE_GROUP)
277                         BLI_strncpy(node_name, node->id->name+2, UI_MAX_NAME_STR);
278                 else
279                         BLI_strncpy(node_name, node->typeinfo->name, UI_MAX_NAME_STR);
280
281                 if(node->inputs.first == NULL &&
282                    node->outputs.first != node->outputs.last &&
283                    !(node->typeinfo->flag & NODE_OPTIONS))
284                         BLI_snprintf(name, UI_MAX_NAME_STR, "%s | %s", node_name, sock->link->fromsock->name);
285                 else
286                         BLI_strncpy(name, node_name, UI_MAX_NAME_STR);
287         }
288         else if(sock->type == SOCK_SHADER)
289                 BLI_strncpy(name, "None", UI_MAX_NAME_STR);
290         else
291                 BLI_strncpy(name, "Default", UI_MAX_NAME_STR);
292 }
293
294 static int ui_compatible_sockets(int typeA, int typeB)
295 {
296         return (typeA == typeB);
297 }
298
299 static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
300 {
301         Main *bmain = arg->bmain;
302         bNodeTree *ntree = arg->ntree;
303         bNodeSocket *sock = arg->sock;
304         uiLayout *layout = arg->layout;
305         uiLayout *column = NULL;
306         uiBlock *block = uiLayoutGetBlock(layout);
307         uiBut *but;
308         bNodeType *ntype;
309         bNodeTree *ngroup;
310         NodeLinkArg *argN;
311         int first = 1;
312         int compatibility= 0;
313
314         if(ntree->type == NTREE_SHADER) {
315                 if(scene_use_new_shading_nodes(arg->scene))
316                         compatibility= NODE_NEW_SHADING;
317                 else
318                         compatibility= NODE_OLD_SHADING;
319         }
320
321         if(nclass == NODE_CLASS_GROUP) {
322                 for(ngroup=bmain->nodetree.first; ngroup; ngroup=ngroup->id.next) {
323                         bNodeSocket *gsock;
324                         char name[UI_MAX_NAME_STR];
325                         int i, j, num = 0;
326
327                         if(ngroup->type != ntree->type)
328                                 continue;
329
330                         for(gsock=ngroup->inputs.first; gsock; gsock=gsock->next)
331                                 if(ui_compatible_sockets(gsock->type, sock->type))
332                                         num++;
333
334                         for(i=0, j=0, gsock=ngroup->outputs.first; gsock; gsock=gsock->next, i++) {
335                                 if(!ui_compatible_sockets(gsock->type, sock->type))
336                                         continue;
337
338                                 if(first) {
339                                         column= uiLayoutColumn(layout, 0);
340                                         uiBlockSetCurLayout(block, column);
341
342                                         uiItemL(column, cname, ICON_NODE);
343                                         but= block->buttons.last;
344                                         but->flag= UI_TEXT_LEFT;
345
346                                         first = 0;
347                                 }
348
349                                 if(num > 1) {
350                                         if(j == 0) {
351                                                 uiItemL(column, ngroup->id.name+2, ICON_NODE);
352                                                 but= block->buttons.last;
353                                                 but->flag= UI_TEXT_LEFT;
354                                         }
355
356                                         BLI_snprintf(name, UI_MAX_NAME_STR, "  %s", gsock->name);
357                                         j++;
358                                 }
359                                 else
360                                         BLI_strncpy(name, ngroup->id.name+2, UI_MAX_NAME_STR);
361
362                                 but = uiDefBut(block, BUT, 0, ngroup->id.name+2, 0, 0, UI_UNIT_X*4, UI_UNIT_Y,
363                                         NULL, 0.0, 0.0, 0.0, 0.0, "Add node to input");
364
365                                 argN = MEM_dupallocN(arg);
366                                 argN->ngroup = ngroup;
367                                 argN->output = i;
368                                 uiButSetNFunc(but, ui_node_link, argN, NULL);
369                         }
370                 }
371         }
372         else {
373                 bNodeTreeType *ttype= ntreeGetType(ntree->type);
374
375                 for(ntype=ttype->node_types.first; ntype; ntype=ntype->next) {
376                         bNodeSocketTemplate *stemp;
377                         char name[UI_MAX_NAME_STR];
378                         int i, j, num = 0;
379
380                         if(compatibility && !(ntype->compatibility & compatibility))
381                                 continue;
382
383                         if(ntype->nclass != nclass)
384                                 continue;
385
386                         for(i=0, stemp=ntype->outputs; stemp && stemp->type != -1; stemp++, i++)
387                                 if(ui_compatible_sockets(stemp->type, sock->type))
388                                         num++;
389
390                         for(i=0, j=0, stemp=ntype->outputs; stemp && stemp->type != -1; stemp++, i++) {
391                                 if(!ui_compatible_sockets(stemp->type, sock->type))
392                                         continue;
393
394                                 if(first) {
395                                         column= uiLayoutColumn(layout, 0);
396                                         uiBlockSetCurLayout(block, column);
397
398                                         uiItemL(column, cname, ICON_NODE);
399                                         but= block->buttons.last;
400                                         but->flag= UI_TEXT_LEFT;
401
402                                         first = 0;
403                                 }
404
405                                 if(num > 1) {
406                                         if(j == 0) {
407                                                 uiItemL(column, ntype->name, ICON_NODE);
408                                                 but= block->buttons.last;
409                                                 but->flag= UI_TEXT_LEFT;
410                                         }
411
412                                         BLI_snprintf(name, UI_MAX_NAME_STR, "  %s", stemp->name);
413                                         j++;
414                                 }
415                                 else
416                                         BLI_strncpy(name, ntype->name, UI_MAX_NAME_STR);
417
418                                 but = uiDefBut(block, BUT, 0, name, 0, 0, UI_UNIT_X*4, UI_UNIT_Y,
419                                         NULL, 0.0, 0.0, 0.0, 0.0, "Add node to input");
420
421                                 argN = MEM_dupallocN(arg);
422                                 argN->type = ntype->type;
423                                 argN->output = i;
424                                 uiButSetNFunc(but, ui_node_link, argN, NULL);
425                         }
426                 }
427         }
428 }
429
430 static void node_menu_column_foreach_cb(void *calldata, int nclass, const char *name)
431 {
432         NodeLinkArg *arg = (NodeLinkArg*)calldata;
433
434         if(!ELEM(nclass, NODE_CLASS_GROUP, NODE_CLASS_LAYOUT))
435                 ui_node_menu_column(arg, nclass, name);
436 }
437
438 static void ui_template_node_link_menu(bContext *C, uiLayout *layout, void *but_p)
439 {
440         Main *bmain= CTX_data_main(C);
441         Scene *scene= CTX_data_scene(C);
442         uiBlock *block = uiLayoutGetBlock(layout);
443         uiBut *but = (uiBut*)but_p;
444         uiLayout *split, *column;
445         NodeLinkArg *arg = (NodeLinkArg*)but->func_argN;
446         bNodeSocket *sock = arg->sock;
447         bNodeTreeType *ntreetype= ntreeGetType(arg->ntree->type);
448
449         uiBlockSetCurLayout(block, layout);
450         split= uiLayoutSplit(layout, 0, 0);
451
452         arg->bmain= bmain;
453         arg->scene= scene;
454         arg->layout= split;
455         
456         if(ntreetype && ntreetype->foreach_nodeclass)
457                 ntreetype->foreach_nodeclass(scene, arg, node_menu_column_foreach_cb);
458
459         column= uiLayoutColumn(split, 0);
460         uiBlockSetCurLayout(block, column);
461
462         if(sock->link) {
463                 uiItemL(column, "Link", ICON_NONE);
464                 but= block->buttons.last;
465                 but->flag= UI_TEXT_LEFT;
466
467                 but = uiDefBut(block, BUT, 0, "Remove", 0, 0, UI_UNIT_X*4, UI_UNIT_Y,
468                         NULL, 0.0, 0.0, 0.0, 0.0, "Remove nodes connected to the input");
469                 uiButSetNFunc(but, ui_node_link, MEM_dupallocN(arg), SET_INT_IN_POINTER(UI_NODE_LINK_REMOVE));
470
471                 but = uiDefBut(block, BUT, 0, "Disconnect", 0, 0, UI_UNIT_X*4, UI_UNIT_Y,
472                         NULL, 0.0, 0.0, 0.0, 0.0, "Disconnect nodes connected to the input");
473                 uiButSetNFunc(but, ui_node_link, MEM_dupallocN(arg), SET_INT_IN_POINTER(UI_NODE_LINK_DISCONNECT));
474         }
475
476         ui_node_menu_column(arg, NODE_CLASS_GROUP, IFACE_("Group"));
477 }
478
479 void uiTemplateNodeLink(uiLayout *layout, bNodeTree *ntree, bNode *node, bNodeSocket *sock)
480 {
481         uiBlock *block = uiLayoutGetBlock(layout);
482         NodeLinkArg *arg;
483         uiBut *but;
484
485         arg = MEM_callocN(sizeof(NodeLinkArg), "NodeLinkArg");
486         arg->ntree = ntree;
487         arg->node = node;
488         arg->sock = sock;
489         arg->type = 0;
490         arg->output = 0;
491
492         uiBlockSetCurLayout(block, layout);
493
494         if(sock->link || sock->type == SOCK_SHADER || (sock->flag & SOCK_HIDE_VALUE)) {
495                 char name[UI_MAX_NAME_STR];
496                 ui_node_sock_name(sock, name);
497                 but= uiDefMenuBut(block, ui_template_node_link_menu, NULL, name, 0, 0, UI_UNIT_X*4, UI_UNIT_Y, "");
498         }
499         else
500                 but= uiDefIconMenuBut(block, ui_template_node_link_menu, NULL, ICON_NONE, 0, 0, UI_UNIT_X, UI_UNIT_Y, "");
501
502         but->type= MENU;
503         but->flag |= UI_TEXT_LEFT|UI_BUT_NODE_LINK;
504         but->poin= (char*)but;
505         but->func_argN = arg;
506 }
507
508 /**************************** Node Tree Layout *******************************/
509
510 static void ui_node_draw_input(uiLayout *layout, bContext *C,
511         bNodeTree *ntree, bNode *node, bNodeSocket *input, int depth);
512
513 static void ui_node_draw_node(uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, int depth)
514 {
515         bNodeSocket *input;
516         uiLayout *col, *split;
517         PointerRNA nodeptr;
518
519         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
520
521         if(node->typeinfo->uifunc) {
522                 if(node->type != NODE_GROUP) {
523                         split = uiLayoutSplit(layout, 0.35f, 0);
524                         col = uiLayoutColumn(split, 0);
525                         col = uiLayoutColumn(split, 0);
526
527                         node->typeinfo->uifunc(col, C, &nodeptr);
528                 }
529         }
530
531         for(input=node->inputs.first; input; input=input->next)
532                 ui_node_draw_input(layout, C, ntree, node, input, depth+1);
533 }
534
535 static void ui_node_draw_input(uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input, int depth)
536 {
537         PointerRNA inputptr;
538         uiBlock *block = uiLayoutGetBlock(layout);
539         uiBut *bt;
540         uiLayout *split, *row, *col;
541         bNode *lnode;
542         char label[UI_MAX_NAME_STR];
543         int indent = (depth > 1)? 2*(depth - 1): 0;
544
545         if(input->flag & SOCK_UNAVAIL)
546                 return;
547
548         /* to avoid eternal loops on cyclic dependencies */
549         node->flag |= NODE_TEST;
550         lnode = (input->link)? input->link->fromnode: NULL;
551
552         /* socket RNA pointer */
553         RNA_pointer_create(&ntree->id, &RNA_NodeSocket, input, &inputptr);
554
555         /* indented label */
556         memset(label, ' ', indent);
557         label[indent] = '\0';
558         BLI_snprintf(label, UI_MAX_NAME_STR, "%s%s:", label, input->name);
559
560         /* split in label and value */
561         split = uiLayoutSplit(layout, 0.35f, 0);
562
563         row = uiLayoutRow(split, 1);
564
565         if(depth > 0) {
566                 uiBlockSetEmboss(block, UI_EMBOSSN);
567
568                 if(lnode && (lnode->inputs.first || (lnode->typeinfo->uifunc && lnode->type != NODE_GROUP))) {
569                         int icon = (input->flag & SOCK_COLLAPSED)? ICON_DISCLOSURE_TRI_RIGHT: ICON_DISCLOSURE_TRI_DOWN;
570                         uiItemR(row, &inputptr, "show_expanded", UI_ITEM_R_ICON_ONLY, "", icon);
571                 }
572                 else
573                         uiItemL(row, "", ICON_BLANK1);
574
575                 bt = block->buttons.last;
576                 bt->x2 = UI_UNIT_X/2;
577
578                 uiBlockSetEmboss(block, UI_EMBOSS);
579         }
580
581         uiItemL(row, label, ICON_NONE);
582         bt= block->buttons.last;
583         bt->flag= UI_TEXT_LEFT;
584
585         if(lnode) {
586                 /* input linked to a node */
587                 uiTemplateNodeLink(split, ntree, node, input);
588
589                 if(!(input->flag & SOCK_COLLAPSED)) {
590                         if(depth == 0)
591                                 uiItemS(layout);
592
593                         ui_node_draw_node(layout, C, ntree, lnode, depth);
594                 }
595         }
596         else {
597                 /* input not linked, show value */
598                 if(input->type != SOCK_SHADER && !(input->flag & SOCK_HIDE_VALUE)) {
599                         if(input->type == SOCK_VECTOR) {
600                                 row = uiLayoutRow(split, 0);
601                                 col = uiLayoutColumn(row, 0);
602
603                                 uiItemR(col, &inputptr, "default_value", 0, "", 0);
604                         }
605                         else {
606                                 row = uiLayoutRow(split, 1);
607                                 uiItemR(row, &inputptr, "default_value", 0, "", 0);
608                         }
609                 }
610                 else
611                         row = uiLayoutRow(split, 0);
612
613                 uiTemplateNodeLink(row, ntree, node, input);
614         }
615
616         /* clear */
617         node->flag &= ~NODE_TEST;
618 }
619
620 void uiTemplateNodeView(uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input)
621 {
622         bNode *tnode;
623
624         if(!ntree)
625                 return;
626
627         /* clear for cycle check */
628         for(tnode=ntree->nodes.first; tnode; tnode=tnode->next)
629                 tnode->flag &= ~NODE_TEST;
630
631         if(input)
632                 ui_node_draw_input(layout, C, ntree, node, input, 0);
633         else
634                 ui_node_draw_node(layout, C, ntree, node, 0);
635 }
636