split node_edit.c into separate files (add, group, relationshops), was almost 5000...
[blender.git] / source / blender / editors / space_node / node_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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. 
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Nathan Letwory
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_node/node_edit.c
29  *  \ingroup spnode
30  */
31
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_ID.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_node_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_particle_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_world_types.h"
49 #include "DNA_action_types.h"
50 #include "DNA_anim_types.h"
51
52 #include "BLI_math.h"
53 #include "BLI_blenlib.h"
54 #include "BLI_utildefines.h"
55
56 #include "BKE_action.h"
57 #include "BKE_animsys.h"
58 #include "BKE_context.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_global.h"
61 #include "BKE_image.h"
62 #include "BKE_library.h"
63 #include "BKE_main.h"
64 #include "BKE_node.h"
65 #include "BKE_material.h"
66 #include "BKE_modifier.h"
67 #include "BKE_paint.h"
68 #include "BKE_scene.h"
69 #include "BKE_screen.h"
70 #include "BKE_texture.h"
71 #include "BKE_report.h"
72
73 #include "RE_pipeline.h"
74
75 #include "IMB_imbuf_types.h"
76
77 #include "ED_node.h"
78 #include "ED_image.h"
79 #include "ED_screen.h"
80 #include "ED_space_api.h"
81 #include "ED_render.h"
82
83 #include "RNA_access.h"
84 #include "RNA_define.h"
85 #include "RNA_enum_types.h"
86
87 #include "WM_api.h"
88 #include "WM_types.h"
89
90 #include "UI_interface.h"
91 #include "UI_resources.h"
92 #include "UI_view2d.h"
93
94 #include "IMB_imbuf.h"
95
96 #include "RNA_enum_types.h"
97
98 #include "GPU_material.h"
99
100 #include "node_intern.h"
101 #include "NOD_socket.h"
102
103 /* ***************** composite job manager ********************** */
104
105 typedef struct CompoJob {
106         Scene *scene;
107         bNodeTree *ntree;
108         bNodeTree *localtree;
109         short *stop;
110         short *do_update;
111         float *progress;
112 } CompoJob;
113
114 /* called by compo, only to check job 'stop' value */
115 static int compo_breakjob(void *cjv)
116 {
117         CompoJob *cj = cjv;
118         
119         return *(cj->stop);
120 }
121
122 /* called by compo, wmJob sends notifier */
123 static void compo_redrawjob(void *cjv, char *UNUSED(str))
124 {
125         CompoJob *cj = cjv;
126         
127         *(cj->do_update) = TRUE;
128 }
129
130 static void compo_freejob(void *cjv)
131 {
132         CompoJob *cj = cjv;
133
134         if (cj->localtree) {
135                 ntreeLocalMerge(cj->localtree, cj->ntree);
136         }
137         MEM_freeN(cj);
138 }
139
140 /* only now we copy the nodetree, so adding many jobs while
141  * sliding buttons doesn't frustrate */
142 static void compo_initjob(void *cjv)
143 {
144         CompoJob *cj = cjv;
145
146         cj->localtree = ntreeLocalize(cj->ntree);
147 }
148
149 /* called before redraw notifiers, it moves finished previews over */
150 static void compo_updatejob(void *cjv)
151 {
152         CompoJob *cj = cjv;
153         
154         ntreeLocalSync(cj->localtree, cj->ntree);
155 }
156
157 static void compo_progressjob(void *cjv, float progress)
158 {
159         CompoJob *cj = cjv;
160         
161         *(cj->progress) = progress;
162 }
163
164
165 /* only this runs inside thread */
166 static void compo_startjob(void *cjv, short *stop, short *do_update, float *progress)
167 {
168         CompoJob *cj = cjv;
169         bNodeTree *ntree = cj->localtree;
170
171         if (cj->scene->use_nodes == FALSE)
172                 return;
173         
174         cj->stop = stop;
175         cj->do_update = do_update;
176         cj->progress = progress;
177
178         ntree->test_break = compo_breakjob;
179         ntree->tbh = cj;
180         ntree->stats_draw = compo_redrawjob;
181         ntree->sdh = cj;
182         ntree->progress = compo_progressjob;
183         ntree->prh = cj;
184         
185         // XXX BIF_store_spare();
186         
187         ntreeCompositExecTree(ntree, &cj->scene->r, 0, 1);  /* 1 is do_previews */
188
189         ntree->test_break = NULL;
190         ntree->stats_draw = NULL;
191         ntree->progress = NULL;
192
193 }
194
195 void snode_composite_job(const bContext *C, ScrArea *sa)
196 {
197         SpaceNode *snode = sa->spacedata.first;
198         wmJob *steve;
199         CompoJob *cj;
200
201         steve = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Compositing", WM_JOB_EXCL_RENDER | WM_JOB_PROGRESS);
202         cj = MEM_callocN(sizeof(CompoJob), "compo job");
203         
204         /* customdata for preview thread */
205         cj->scene = CTX_data_scene(C);
206         cj->ntree = snode->nodetree;
207         
208         /* setup job */
209         WM_jobs_customdata(steve, cj, compo_freejob);
210         WM_jobs_timer(steve, 0.1, NC_SCENE, NC_SCENE | ND_COMPO_RESULT);
211         WM_jobs_callbacks(steve, compo_startjob, compo_initjob, compo_updatejob, NULL);
212         
213         WM_jobs_start(CTX_wm_manager(C), steve);
214         
215 }
216
217 /* ***************************************** */
218
219 /* operator poll callback */
220 int composite_node_active(bContext *C)
221 {
222         if (ED_operator_node_active(C)) {
223                 SpaceNode *snode = CTX_wm_space_node(C);
224                 if (snode->treetype == NTREE_COMPOSIT)
225                         return 1;
226         }
227         return 0;
228 }
229
230 /* also checks for edited groups */
231 bNode *editnode_get_active(bNodeTree *ntree)
232 {
233         bNode *node;
234         
235         /* check for edited group */
236         for (node = ntree->nodes.first; node; node = node->next)
237                 if (nodeGroupEditGet(node))
238                         break;
239         if (node)
240                 return nodeGetActive((bNodeTree *)node->id);
241         else
242                 return nodeGetActive(ntree);
243 }
244
245 static int has_nodetree(bNodeTree *ntree, bNodeTree *lookup)
246 {
247         bNode *node;
248         
249         if (ntree == lookup)
250                 return 1;
251         
252         for (node = ntree->nodes.first; node; node = node->next)
253                 if (node->type == NODE_GROUP && node->id)
254                         if (has_nodetree((bNodeTree *)node->id, lookup))
255                                 return 1;
256         
257         return 0;
258 }
259
260 static void snode_dag_update_group(void *calldata, ID *owner_id, bNodeTree *ntree)
261 {
262         if (has_nodetree(ntree, calldata))
263                 DAG_id_tag_update(owner_id, 0);
264 }
265
266 void snode_dag_update(bContext *C, SpaceNode *snode)
267 {
268         Main *bmain = CTX_data_main(C);
269
270         /* for groups, update all ID's using this */
271         if (snode->edittree != snode->nodetree) {
272                 bNodeTreeType *tti = ntreeGetType(snode->edittree->type);
273                 tti->foreach_nodetree(bmain, snode->edittree, snode_dag_update_group);
274         }
275
276         DAG_id_tag_update(snode->id, 0);
277 }
278
279 void snode_notify(bContext *C, SpaceNode *snode)
280 {
281         WM_event_add_notifier(C, NC_NODE | NA_EDITED, NULL);
282
283         if (snode->treetype == NTREE_SHADER)
284                 WM_event_add_notifier(C, NC_MATERIAL | ND_NODES, snode->id);
285         else if (snode->treetype == NTREE_COMPOSIT)
286                 WM_event_add_notifier(C, NC_SCENE | ND_NODES, snode->id);
287         else if (snode->treetype == NTREE_TEXTURE)
288                 WM_event_add_notifier(C, NC_TEXTURE | ND_NODES, snode->id);
289 }
290
291 bNode *node_tree_get_editgroup(bNodeTree *nodetree)
292 {
293         bNode *gnode;
294         
295         /* get the groupnode */
296         for (gnode = nodetree->nodes.first; gnode; gnode = gnode->next)
297                 if (nodeGroupEditGet(gnode))
298                         break;
299         return gnode;
300 }
301
302 /* assumes nothing being done in ntree yet, sets the default in/out node */
303 /* called from shading buttons or header */
304 void ED_node_shader_default(Scene *scene, ID *id)
305 {
306         bNode *in, *out;
307         bNodeSocket *fromsock, *tosock, *sock;
308         bNodeTree *ntree;
309         bNodeTemplate ntemp;
310         int output_type, shader_type;
311         float color[3], strength = 1.0f;
312         
313         ntree = ntreeAddTree("Shader Nodetree", NTREE_SHADER, 0);
314
315         switch (GS(id->name)) {
316                 case ID_MA: {
317                         Material *ma = (Material *)id;
318                         ma->nodetree = ntree;
319
320                         if (BKE_scene_use_new_shading_nodes(scene)) {
321                                 output_type = SH_NODE_OUTPUT_MATERIAL;
322                                 shader_type = SH_NODE_BSDF_DIFFUSE;
323                         }
324                         else {
325                                 output_type = SH_NODE_OUTPUT;
326                                 shader_type = SH_NODE_MATERIAL;
327                         }
328
329                         copy_v3_v3(color, &ma->r);
330                         strength = 0.0f;
331                         break;
332                 }
333                 case ID_WO: {
334                         World *wo = (World *)id;
335                         wo->nodetree = ntree;
336
337                         output_type = SH_NODE_OUTPUT_WORLD;
338                         shader_type = SH_NODE_BACKGROUND;
339
340                         copy_v3_v3(color, &wo->horr);
341                         strength = 1.0f;
342                         break;
343                 }
344                 case ID_LA: {
345                         Lamp *la = (Lamp *)id;
346                         la->nodetree = ntree;
347
348                         output_type = SH_NODE_OUTPUT_LAMP;
349                         shader_type = SH_NODE_EMISSION;
350
351                         copy_v3_v3(color, &la->r);
352                         if (la->type == LA_LOCAL || la->type == LA_SPOT || la->type == LA_AREA)
353                                 strength = 100.0f;
354                         else
355                                 strength = 1.0f;
356                         break;
357                 }
358                 default:
359                         printf("ED_node_shader_default called on wrong ID type.\n");
360                         return;
361         }
362         
363         ntemp.type = output_type;
364         out = nodeAddNode(ntree, &ntemp);
365         out->locx = 300.0f; out->locy = 300.0f;
366         
367         ntemp.type = shader_type;
368         in = nodeAddNode(ntree, &ntemp);
369         in->locx = 10.0f; in->locy = 300.0f;
370         nodeSetActive(ntree, in);
371         
372         /* only a link from color to color */
373         fromsock = in->outputs.first;
374         tosock = out->inputs.first;
375         nodeAddLink(ntree, in, fromsock, out, tosock);
376
377         /* default values */
378         if (BKE_scene_use_new_shading_nodes(scene)) {
379                 sock = in->inputs.first;
380                 copy_v3_v3(((bNodeSocketValueRGBA *)sock->default_value)->value, color);
381
382                 if (strength != 0.0f) {
383                         sock = in->inputs.last;
384                         ((bNodeSocketValueFloat *)sock->default_value)->value = strength;
385                 }
386         }
387         
388         ntreeUpdateTree(ntree);
389 }
390
391 /* assumes nothing being done in ntree yet, sets the default in/out node */
392 /* called from shading buttons or header */
393 void ED_node_composit_default(Scene *sce)
394 {
395         bNode *in, *out;
396         bNodeSocket *fromsock, *tosock;
397         bNodeTemplate ntemp;
398         
399         /* but lets check it anyway */
400         if (sce->nodetree) {
401                 if (G.debug & G_DEBUG)
402                         printf("error in composite initialize\n");
403                 return;
404         }
405         
406         sce->nodetree = ntreeAddTree("Compositing Nodetree", NTREE_COMPOSIT, 0);
407
408         sce->nodetree->chunksize = 256;
409         sce->nodetree->edit_quality = NTREE_QUALITY_HIGH;
410         sce->nodetree->render_quality = NTREE_QUALITY_HIGH;
411         
412         ntemp.type = CMP_NODE_COMPOSITE;
413         out = nodeAddNode(sce->nodetree, &ntemp);
414         out->locx = 300.0f; out->locy = 400.0f;
415         out->id = &sce->id;
416         id_us_plus(out->id);
417         
418         ntemp.type = CMP_NODE_R_LAYERS;
419         in = nodeAddNode(sce->nodetree, &ntemp);
420         in->locx = 10.0f; in->locy = 400.0f;
421         in->id = &sce->id;
422         id_us_plus(in->id);
423         nodeSetActive(sce->nodetree, in);
424         
425         /* links from color to color */
426         fromsock = in->outputs.first;
427         tosock = out->inputs.first;
428         nodeAddLink(sce->nodetree, in, fromsock, out, tosock);
429         
430         ntreeUpdateTree(sce->nodetree);
431         
432         // XXX ntreeCompositForceHidden(sce->nodetree);
433 }
434
435 /* assumes nothing being done in ntree yet, sets the default in/out node */
436 /* called from shading buttons or header */
437 void ED_node_texture_default(Tex *tx)
438 {
439         bNode *in, *out;
440         bNodeSocket *fromsock, *tosock;
441         bNodeTemplate ntemp;
442         
443         /* but lets check it anyway */
444         if (tx->nodetree) {
445                 if (G.debug & G_DEBUG)
446                         printf("error in texture initialize\n");
447                 return;
448         }
449         
450         tx->nodetree = ntreeAddTree("Texture Nodetree", NTREE_TEXTURE, 0);
451         
452         ntemp.type = TEX_NODE_OUTPUT;
453         out = nodeAddNode(tx->nodetree, &ntemp);
454         out->locx = 300.0f; out->locy = 300.0f;
455         
456         ntemp.type = TEX_NODE_CHECKER;
457         in = nodeAddNode(tx->nodetree, &ntemp);
458         in->locx = 10.0f; in->locy = 300.0f;
459         nodeSetActive(tx->nodetree, in);
460         
461         fromsock = in->outputs.first;
462         tosock = out->inputs.first;
463         nodeAddLink(tx->nodetree, in, fromsock, out, tosock);
464         
465         ntreeUpdateTree(tx->nodetree);
466 }
467
468 /* id is supposed to contain a node tree */
469 void node_tree_from_ID(ID *id, bNodeTree **ntree, bNodeTree **edittree, int *treetype)
470 {
471         if (id) {
472                 bNode *node = NULL;
473                 short idtype = GS(id->name);
474         
475                 if (idtype == ID_NT) {
476                         *ntree = (bNodeTree *)id;
477                         if (treetype) *treetype = (*ntree)->type;
478                 }
479                 else if (idtype == ID_MA) {
480                         *ntree = ((Material *)id)->nodetree;
481                         if (treetype) *treetype = NTREE_SHADER;
482                 }
483                 else if (idtype == ID_LA) {
484                         *ntree = ((Lamp *)id)->nodetree;
485                         if (treetype) *treetype = NTREE_SHADER;
486                 }
487                 else if (idtype == ID_WO) {
488                         *ntree = ((World *)id)->nodetree;
489                         if (treetype) *treetype = NTREE_SHADER;
490                 }
491                 else if (idtype == ID_SCE) {
492                         *ntree = ((Scene *)id)->nodetree;
493                         if (treetype) *treetype = NTREE_COMPOSIT;
494                 }
495                 else if (idtype == ID_TE) {
496                         *ntree = ((Tex *)id)->nodetree;
497                         if (treetype) *treetype = NTREE_TEXTURE;
498                 }
499                 else {
500                         if (treetype) *treetype = 0;
501                         return;
502                 }
503         
504                 /* find editable group */
505                 if (edittree) {
506                         if (*ntree)
507                                 for (node = (*ntree)->nodes.first; node; node = node->next)
508                                         if (nodeGroupEditGet(node))
509                                                 break;
510                         
511                         if (node && node->id)
512                                 *edittree = (bNodeTree *)node->id;
513                         else
514                                 *edittree = *ntree;
515                 }
516         }
517         else {
518                 *ntree = NULL;
519                 *edittree = NULL;
520                 if (treetype) *treetype = 0;
521         }
522 }
523
524 /* Here we set the active tree(s), even called for each redraw now, so keep it fast :) */
525 void snode_set_context(SpaceNode *snode, Scene *scene)
526 {
527         Object *ob = OBACT;
528         
529         snode->id = snode->from = NULL;
530         
531         if (snode->treetype == NTREE_SHADER) {
532                 /* need active object, or we allow pinning... */
533                 if (snode->shaderfrom == SNODE_SHADER_OBJECT) {
534                         if (ob) {
535                                 if (ob->type == OB_LAMP) {
536                                         snode->from = &ob->id;
537                                         snode->id = ob->data;
538                                 }
539                                 else {
540                                         Material *ma = give_current_material(ob, ob->actcol);
541                                         if (ma) {
542                                                 snode->from = &ob->id;
543                                                 snode->id = &ma->id;
544                                         }
545                                 }
546                         }
547                 }
548                 else { /* SNODE_SHADER_WORLD */
549                         if (scene->world) {
550                                 snode->from = NULL;
551                                 snode->id = &scene->world->id;
552                         }
553                 }
554         }
555         else if (snode->treetype == NTREE_COMPOSIT) {
556                 snode->id = &scene->id;
557                 
558                 /* update output sockets based on available layers */
559                 ntreeCompositForceHidden(scene->nodetree, scene);
560         }
561         else if (snode->treetype == NTREE_TEXTURE) {
562                 Tex *tx = NULL;
563
564                 if (snode->texfrom == SNODE_TEX_OBJECT) {
565                         if (ob) {
566                                 tx = give_current_object_texture(ob);
567
568                                 if (ob->type == OB_LAMP)
569                                         snode->from = (ID *)ob->data;
570                                 else
571                                         snode->from = (ID *)give_current_material(ob, ob->actcol);
572
573                                 /* from is not set fully for material nodes, should be ID + Node then */
574                                 snode->id = &tx->id;
575                         }
576                 }
577                 else if (snode->texfrom == SNODE_TEX_WORLD) {
578                         tx = give_current_world_texture(scene->world);
579                         snode->from = (ID *)scene->world;
580                         snode->id = &tx->id;
581                 }
582                 else {
583                         struct Brush *brush = NULL;
584                         
585                         if (ob && (ob->mode & OB_MODE_SCULPT))
586                                 brush = paint_brush(&scene->toolsettings->sculpt->paint);
587                         else
588                                 brush = paint_brush(&scene->toolsettings->imapaint.paint);
589
590                         if (brush) {
591                                 snode->from = (ID *)brush;
592                                 tx = give_current_brush_texture(brush);
593                                 snode->id = &tx->id;
594                         }
595                 }
596         }
597         else {
598                 if (snode->nodetree && snode->nodetree->type == snode->treetype)
599                         snode->id = &snode->nodetree->id;
600                 else
601                         snode->id = NULL;
602         }
603
604         node_tree_from_ID(snode->id, &snode->nodetree, &snode->edittree, NULL);
605 }
606
607 void snode_update(SpaceNode *snode, bNode *node)
608 {
609         bNode *gnode;
610         
611         if (node)
612                 nodeUpdate(snode->edittree, node);
613         
614         /* if inside group, tag entire group */
615         gnode = node_tree_get_editgroup(snode->nodetree);
616         if (gnode)
617                 nodeUpdateID(snode->nodetree, gnode->id);
618 }
619
620 void ED_node_set_active(Main *bmain, bNodeTree *ntree, bNode *node)
621 {
622         int was_active_texture = (node->flag & NODE_ACTIVE_TEXTURE);
623
624         nodeSetActive(ntree, node);
625         
626         if (node->type != NODE_GROUP) {
627                 int was_output = (node->flag & NODE_DO_OUTPUT);
628                 
629                 /* tree specific activate calls */
630                 if (ntree->type == NTREE_SHADER) {
631                         /* when we select a material, active texture is cleared, for buttons */
632                         if (node->id && ELEM3(GS(node->id->name), ID_MA, ID_LA, ID_WO))
633                                 nodeClearActiveID(ntree, ID_TE);
634                         
635                         if (node->type == SH_NODE_OUTPUT) {
636                                 bNode *tnode;
637                                 
638                                 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
639                                         if (tnode->type == SH_NODE_OUTPUT)
640                                                 tnode->flag &= ~NODE_DO_OUTPUT;
641                                 
642                                 node->flag |= NODE_DO_OUTPUT;
643                                 if (was_output == 0)
644                                         ED_node_generic_update(bmain, ntree, node);
645                         }
646
647                         /* if active texture changed, free glsl materials */
648                         if ((node->flag & NODE_ACTIVE_TEXTURE) && !was_active_texture) {
649                                 Material *ma;
650
651                                 for (ma = bmain->mat.first; ma; ma = ma->id.next)
652                                         if (ma->nodetree && ma->use_nodes && has_nodetree(ma->nodetree, ntree))
653                                                 GPU_material_free(ma);
654
655                                 WM_main_add_notifier(NC_IMAGE, NULL);
656                         }
657
658                         WM_main_add_notifier(NC_MATERIAL | ND_NODES, node->id);
659                 }
660                 else if (ntree->type == NTREE_COMPOSIT) {
661                         /* make active viewer, currently only 1 supported... */
662                         if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
663                                 bNode *tnode;
664                                 
665
666                                 for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
667                                         if (ELEM(tnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER))
668                                                 tnode->flag &= ~NODE_DO_OUTPUT;
669                                 
670                                 node->flag |= NODE_DO_OUTPUT;
671                                 if (was_output == 0)
672                                         ED_node_generic_update(bmain, ntree, node);
673                                 
674                                 /* addnode() doesnt link this yet... */
675                                 node->id = (ID *)BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
676                         }
677                         else if (node->type == CMP_NODE_R_LAYERS) {
678                                 Scene *scene;
679
680                                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
681                                         if (scene->nodetree && scene->use_nodes && has_nodetree(scene->nodetree, ntree)) {
682                                                 if (node->id == NULL || node->id == (ID *)scene) {
683                                                         scene->r.actlay = node->custom1;
684                                                 }
685                                         }
686                                 }
687                         }
688                         else if (node->type == CMP_NODE_COMPOSITE) {
689                                 if (was_output == 0) {
690                                         bNode *tnode;
691                                         
692                                         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next)
693                                                 if (tnode->type == CMP_NODE_COMPOSITE)
694                                                         tnode->flag &= ~NODE_DO_OUTPUT;
695                                         
696                                         node->flag |= NODE_DO_OUTPUT;
697                                         ED_node_generic_update(bmain, ntree, node);
698                                 }
699                         }
700                 }
701                 else if (ntree->type == NTREE_TEXTURE) {
702                         // XXX
703 #if 0
704                         if (node->id)
705                                 ;  // XXX BIF_preview_changed(-1);
706                         // allqueue(REDRAWBUTSSHADING, 1);
707                         // allqueue(REDRAWIPO, 0);
708 #endif
709                 }
710         }
711 }
712
713 void ED_node_post_apply_transform(bContext *UNUSED(C), bNodeTree *UNUSED(ntree))
714 {
715         /* XXX This does not work due to layout functions relying on node->block,
716          * which only exists during actual drawing. Can we rely on valid totr rects?
717          */
718         /* make sure nodes have correct bounding boxes after transform */
719         /* node_update_nodetree(C, ntree, 0.0f, 0.0f); */
720 }
721
722 /* ***************** generic operator functions for nodes ***************** */
723
724 #if 0 /* UNUSED */
725
726 static int edit_node_poll(bContext *C)
727 {
728         return ED_operator_node_active(C);
729 }
730
731 static void edit_node_properties(wmOperatorType *ot)
732 {
733         /* XXX could node be a context pointer? */
734         RNA_def_string(ot->srna, "node", "", MAX_NAME, "Node", "");
735         RNA_def_int(ot->srna, "socket", 0, 0, MAX_SOCKET, "Socket", "", 0, MAX_SOCKET);
736         RNA_def_enum(ot->srna, "in_out", socket_in_out_items, SOCK_IN, "Socket Side", "");
737 }
738
739 static int edit_node_invoke_properties(bContext *C, wmOperator *op)
740 {
741         if (!RNA_struct_property_is_set(op->ptr, "node")) {
742                 bNode *node = CTX_data_pointer_get_type(C, "node", &RNA_Node).data;
743                 if (!node)
744                         return 0;
745                 else
746                         RNA_string_set(op->ptr, "node", node->name);
747         }
748         
749         if (!RNA_struct_property_is_set(op->ptr, "in_out"))
750                 RNA_enum_set(op->ptr, "in_out", SOCK_IN);
751         
752         if (!RNA_struct_property_is_set(op->ptr, "socket"))
753                 RNA_int_set(op->ptr, "socket", 0);
754         
755         return 1;
756 }
757
758 static void edit_node_properties_get(wmOperator *op, bNodeTree *ntree, bNode **rnode, bNodeSocket **rsock, int *rin_out)
759 {
760         bNode *node;
761         bNodeSocket *sock = NULL;
762         char nodename[MAX_NAME];
763         int sockindex;
764         int in_out;
765         
766         RNA_string_get(op->ptr, "node", nodename);
767         node = nodeFindNodebyName(ntree, nodename);
768         
769         in_out = RNA_enum_get(op->ptr, "in_out");
770         
771         sockindex = RNA_int_get(op->ptr, "socket");
772         switch (in_out) {
773                 case SOCK_IN:   sock = BLI_findlink(&node->inputs, sockindex);  break;
774                 case SOCK_OUT:  sock = BLI_findlink(&node->outputs, sockindex); break;
775         }
776         
777         if (rnode)
778                 *rnode = node;
779         if (rsock)
780                 *rsock = sock;
781         if (rin_out)
782                 *rin_out = in_out;
783 }
784 #endif
785
786 /* ************************** Node generic ************** */
787
788 /* is rct in visible part of node? */
789 static bNode *visible_node(SpaceNode *snode, rctf *rct)
790 {
791         bNode *node;
792         
793         for (node = snode->edittree->nodes.last; node; node = node->prev) {
794                 if (BLI_rctf_isect(&node->totr, rct, NULL))
795                         break;
796         }
797         return node;
798 }
799
800 /* **************************** */
801
802 typedef struct NodeViewMove {
803         int mvalo[2];
804         int xmin, ymin, xmax, ymax;
805 } NodeViewMove;
806
807 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
808 {
809         SpaceNode *snode = CTX_wm_space_node(C);
810         ARegion *ar = CTX_wm_region(C);
811         NodeViewMove *nvm = op->customdata;
812
813         switch (event->type) {
814                 case MOUSEMOVE:
815                         
816                         snode->xof -= (nvm->mvalo[0] - event->mval[0]);
817                         snode->yof -= (nvm->mvalo[1] - event->mval[1]);
818                         nvm->mvalo[0] = event->mval[0];
819                         nvm->mvalo[1] = event->mval[1];
820                         
821                         /* prevent dragging image outside of the window and losing it! */
822                         CLAMP(snode->xof, nvm->xmin, nvm->xmax);
823                         CLAMP(snode->yof, nvm->ymin, nvm->ymax);
824                         
825                         ED_region_tag_redraw(ar);
826                         
827                         break;
828                         
829                 case LEFTMOUSE:
830                 case MIDDLEMOUSE:
831                 case RIGHTMOUSE:
832                         
833                         MEM_freeN(nvm);
834                         op->customdata = NULL;
835                         
836                         return OPERATOR_FINISHED;
837         }
838         
839         return OPERATOR_RUNNING_MODAL;
840 }
841
842 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
843 {
844         SpaceNode *snode = CTX_wm_space_node(C);
845         ARegion *ar = CTX_wm_region(C);
846         NodeViewMove *nvm;
847         Image *ima;
848         ImBuf *ibuf;
849         const float pad = 32.0f; /* better be bigger then scrollbars */
850
851         void *lock;
852         
853         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
854         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
855         
856         if (ibuf == NULL) {
857                 BKE_image_release_ibuf(ima, lock);
858                 return OPERATOR_CANCELLED;
859         }
860
861         nvm = MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
862         op->customdata = nvm;
863         nvm->mvalo[0] = event->mval[0];
864         nvm->mvalo[1] = event->mval[1];
865
866         nvm->xmin = -(ar->winx / 2) - (ibuf->x * (0.5f * snode->zoom)) + pad;
867         nvm->xmax =  (ar->winx / 2) + (ibuf->x * (0.5f * snode->zoom)) - pad;
868         nvm->ymin = -(ar->winy / 2) - (ibuf->y * (0.5f * snode->zoom)) + pad;
869         nvm->ymax =  (ar->winy / 2) + (ibuf->y * (0.5f * snode->zoom)) - pad;
870
871         BKE_image_release_ibuf(ima, lock);
872         
873         /* add modal handler */
874         WM_event_add_modal_handler(C, op);
875         
876         return OPERATOR_RUNNING_MODAL;
877 }
878
879 static int snode_bg_viewmove_cancel(bContext *UNUSED(C), wmOperator *op)
880 {
881         MEM_freeN(op->customdata);
882         op->customdata = NULL;
883
884         return OPERATOR_CANCELLED;
885 }
886
887 void NODE_OT_backimage_move(wmOperatorType *ot)
888 {
889         /* identifiers */
890         ot->name = "Background Image Move";
891         ot->description = "Move Node backdrop";
892         ot->idname = "NODE_OT_backimage_move";
893         
894         /* api callbacks */
895         ot->invoke = snode_bg_viewmove_invoke;
896         ot->modal = snode_bg_viewmove_modal;
897         ot->poll = composite_node_active;
898         ot->cancel = snode_bg_viewmove_cancel;
899         
900         /* flags */
901         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
902 }
903
904 static int backimage_zoom(bContext *C, wmOperator *op)
905 {
906         SpaceNode *snode = CTX_wm_space_node(C);
907         ARegion *ar = CTX_wm_region(C);
908         float fac = RNA_float_get(op->ptr, "factor");
909
910         snode->zoom *= fac;
911         ED_region_tag_redraw(ar);
912
913         return OPERATOR_FINISHED;
914 }
915
916
917 void NODE_OT_backimage_zoom(wmOperatorType *ot)
918 {
919         
920         /* identifiers */
921         ot->name = "Background Image Zoom";
922         ot->idname = "NODE_OT_backimage_zoom";
923         ot->description = "Zoom in/out the background image";
924         
925         /* api callbacks */
926         ot->exec = backimage_zoom;
927         ot->poll = composite_node_active;
928         
929         /* flags */
930         ot->flag = OPTYPE_BLOCKING;
931
932         /* internal */
933         RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
934 }
935
936 /******************** sample backdrop operator ********************/
937
938 typedef struct ImageSampleInfo {
939         ARegionType *art;
940         void *draw_handle;
941         int x, y;
942         int channels;
943         int color_manage;
944
945         unsigned char col[4];
946         float colf[4];
947
948         int draw;
949 } ImageSampleInfo;
950
951 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
952 {
953         Scene *scene = CTX_data_scene(C);
954         ImageSampleInfo *info = arg_info;
955
956         if (info->draw) {
957                 ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
958                                    info->x, info->y, info->col, info->colf,
959                                    NULL, NULL /* zbuf - unused for nodes */
960                                    );
961         }
962 }
963
964 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
965 {
966         SpaceNode *snode = CTX_wm_space_node(C);
967         ARegion *ar = CTX_wm_region(C);
968         ImageSampleInfo *info = op->customdata;
969         void *lock;
970         Image *ima;
971         ImBuf *ibuf;
972         float fx, fy, bufx, bufy;
973         
974         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
975         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
976         if (!ibuf) {
977                 info->draw = 0;
978                 return;
979         }
980         
981         if (!ibuf->rect) {
982                 if (info->color_manage)
983                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
984                 else
985                         ibuf->profile = IB_PROFILE_NONE;
986                 IMB_rect_from_float(ibuf);
987         }
988
989         /* map the mouse coords to the backdrop image space */
990         bufx = ibuf->x * snode->zoom;
991         bufy = ibuf->y * snode->zoom;
992         fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
993         fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
994
995         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
996                 float *fp;
997                 char *cp;
998                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
999
1000                 CLAMP(x, 0, ibuf->x - 1);
1001                 CLAMP(y, 0, ibuf->y - 1);
1002
1003                 info->x = x;
1004                 info->y = y;
1005                 info->draw = 1;
1006                 info->channels = ibuf->channels;
1007
1008                 if (ibuf->rect) {
1009                         cp = (char *)(ibuf->rect + y * ibuf->x + x);
1010
1011                         info->col[0] = cp[0];
1012                         info->col[1] = cp[1];
1013                         info->col[2] = cp[2];
1014                         info->col[3] = cp[3];
1015
1016                         info->colf[0] = (float)cp[0] / 255.0f;
1017                         info->colf[1] = (float)cp[1] / 255.0f;
1018                         info->colf[2] = (float)cp[2] / 255.0f;
1019                         info->colf[3] = (float)cp[3] / 255.0f;
1020                 }
1021                 if (ibuf->rect_float) {
1022                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
1023
1024                         info->colf[0] = fp[0];
1025                         info->colf[1] = fp[1];
1026                         info->colf[2] = fp[2];
1027                         info->colf[3] = fp[3];
1028                 }
1029
1030                 ED_node_sample_set(info->colf);
1031         }
1032         else {
1033                 info->draw = 0;
1034                 ED_node_sample_set(NULL);
1035         }
1036
1037         BKE_image_release_ibuf(ima, lock);
1038         
1039         ED_area_tag_redraw(CTX_wm_area(C));
1040 }
1041
1042 static void sample_exit(bContext *C, wmOperator *op)
1043 {
1044         ImageSampleInfo *info = op->customdata;
1045
1046         ED_node_sample_set(NULL);
1047         ED_region_draw_cb_exit(info->art, info->draw_handle);
1048         ED_area_tag_redraw(CTX_wm_area(C));
1049         MEM_freeN(info);
1050 }
1051
1052 static int sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
1053 {
1054         SpaceNode *snode = CTX_wm_space_node(C);
1055         ARegion *ar = CTX_wm_region(C);
1056         ImageSampleInfo *info;
1057
1058         if (snode->treetype != NTREE_COMPOSIT || !(snode->flag & SNODE_BACKDRAW))
1059                 return OPERATOR_CANCELLED;
1060         
1061         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
1062         info->art = ar->type;
1063         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
1064         op->customdata = info;
1065
1066         sample_apply(C, op, event);
1067
1068         WM_event_add_modal_handler(C, op);
1069
1070         return OPERATOR_RUNNING_MODAL;
1071 }
1072
1073 static int sample_modal(bContext *C, wmOperator *op, wmEvent *event)
1074 {
1075         switch (event->type) {
1076                 case LEFTMOUSE:
1077                 case RIGHTMOUSE: // XXX hardcoded
1078                         sample_exit(C, op);
1079                         return OPERATOR_CANCELLED;
1080                 case MOUSEMOVE:
1081                         sample_apply(C, op, event);
1082                         break;
1083         }
1084
1085         return OPERATOR_RUNNING_MODAL;
1086 }
1087
1088 static int sample_cancel(bContext *C, wmOperator *op)
1089 {
1090         sample_exit(C, op);
1091         return OPERATOR_CANCELLED;
1092 }
1093
1094 void NODE_OT_backimage_sample(wmOperatorType *ot)
1095 {
1096         /* identifiers */
1097         ot->name = "Backimage Sample";
1098         ot->idname = "NODE_OT_backimage_sample";
1099         ot->description = "Use mouse to sample background image";
1100         
1101         /* api callbacks */
1102         ot->invoke = sample_invoke;
1103         ot->modal = sample_modal;
1104         ot->cancel = sample_cancel;
1105         ot->poll = ED_operator_node_active;
1106
1107         /* flags */
1108         ot->flag = OPTYPE_BLOCKING;
1109 }
1110
1111 /* ********************** size widget operator ******************** */
1112
1113 typedef struct NodeSizeWidget {
1114         float mxstart, mystart;
1115         float oldlocx, oldlocy;
1116         float oldoffsetx, oldoffsety;
1117         float oldwidth, oldheight;
1118         float oldminiwidth;
1119         int directions;
1120 } NodeSizeWidget;
1121
1122 static void node_resize_init(bContext *C, wmOperator *op, wmEvent *UNUSED(event), bNode *node, int dir)
1123 {
1124         SpaceNode *snode = CTX_wm_space_node(C);
1125         
1126         NodeSizeWidget *nsw = MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
1127         
1128         op->customdata = nsw;
1129         nsw->mxstart = snode->mx;
1130         nsw->mystart = snode->my;
1131         
1132         /* store old */
1133         nsw->oldlocx = node->locx;
1134         nsw->oldlocy = node->locy;
1135         nsw->oldoffsetx = node->offsetx;
1136         nsw->oldoffsety = node->offsety;
1137         nsw->oldwidth = node->width;
1138         nsw->oldheight = node->height;
1139         nsw->oldminiwidth = node->miniwidth;
1140         nsw->directions = dir;
1141         
1142         WM_cursor_modal(CTX_wm_window(C), node_get_resize_cursor(dir));
1143         /* add modal handler */
1144         WM_event_add_modal_handler(C, op);
1145 }
1146
1147 static void node_resize_exit(bContext *C, wmOperator *op, int UNUSED(cancel))
1148 {
1149         WM_cursor_restore(CTX_wm_window(C));
1150         
1151         MEM_freeN(op->customdata);
1152         op->customdata = NULL;
1153 }
1154
1155 static int node_resize_modal(bContext *C, wmOperator *op, wmEvent *event)
1156 {
1157         SpaceNode *snode = CTX_wm_space_node(C);
1158         ARegion *ar = CTX_wm_region(C);
1159         bNode *node = editnode_get_active(snode->edittree);
1160         NodeSizeWidget *nsw = op->customdata;
1161         float mx, my, dx, dy;
1162         
1163         switch (event->type) {
1164                 case MOUSEMOVE:
1165                         
1166                         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &mx, &my);
1167                         dx = mx - nsw->mxstart;
1168                         dy = my - nsw->mystart;
1169                         
1170                         if (node) {
1171                                 if (node->flag & NODE_HIDDEN) {
1172                                         float widthmin = 0.0f;
1173                                         float widthmax = 100.0f;
1174                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
1175                                                 node->miniwidth = nsw->oldminiwidth + dx;
1176                                                 CLAMP(node->miniwidth, widthmin, widthmax);
1177                                         }
1178                                         if (nsw->directions & NODE_RESIZE_LEFT) {
1179                                                 float locmax = nsw->oldlocx + nsw->oldminiwidth;
1180                                                 
1181                                                 node->locx = nsw->oldlocx + dx;
1182                                                 CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
1183                                                 node->miniwidth = locmax - node->locx;
1184                                         }
1185                                 }
1186                                 else {
1187                                         float widthmin = UI_DPI_FAC * node->typeinfo->minwidth;
1188                                         float widthmax = UI_DPI_FAC * node->typeinfo->maxwidth;
1189                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
1190                                                 node->width = nsw->oldwidth + dx;
1191                                                 CLAMP(node->width, widthmin, widthmax);
1192                                         }
1193                                         if (nsw->directions & NODE_RESIZE_LEFT) {
1194                                                 float locmax = nsw->oldlocx + nsw->oldwidth;
1195                                                 
1196                                                 node->locx = nsw->oldlocx + dx;
1197                                                 CLAMP(node->locx, locmax - widthmax, locmax - widthmin);
1198                                                 node->width = locmax - node->locx;
1199                                         }
1200                                 }
1201                         
1202                                 /* height works the other way round ... */
1203                                 {
1204                                         float heightmin = UI_DPI_FAC * node->typeinfo->minheight;
1205                                         float heightmax = UI_DPI_FAC * node->typeinfo->maxheight;
1206                                         if (nsw->directions & NODE_RESIZE_TOP) {
1207                                                 float locmin = nsw->oldlocy - nsw->oldheight;
1208                                                 
1209                                                 node->locy = nsw->oldlocy + dy;
1210                                                 CLAMP(node->locy, locmin + heightmin, locmin + heightmax);
1211                                                 node->height = node->locy - locmin;
1212                                         }
1213                                         if (nsw->directions & NODE_RESIZE_BOTTOM) {
1214                                                 node->height = nsw->oldheight - dy;
1215                                                 CLAMP(node->height, heightmin, heightmax);
1216                                         }
1217                                 }
1218                                 
1219                                 /* XXX make callback? */
1220                                 if (node->type == NODE_FRAME) {
1221                                         /* keep the offset symmetric around center point */
1222                                         if (nsw->directions & NODE_RESIZE_LEFT) {
1223                                                 node->locx = nsw->oldlocx + 0.5f * dx;
1224                                                 node->offsetx = nsw->oldoffsetx + 0.5f * dx;
1225                                         }
1226                                         if (nsw->directions & NODE_RESIZE_RIGHT) {
1227                                                 node->locx = nsw->oldlocx + 0.5f * dx;
1228                                                 node->offsetx = nsw->oldoffsetx - 0.5f * dx;
1229                                         }
1230                                         if (nsw->directions & NODE_RESIZE_TOP) {
1231                                                 node->locy = nsw->oldlocy + 0.5f * dy;
1232                                                 node->offsety = nsw->oldoffsety + 0.5f * dy;
1233                                         }
1234                                         if (nsw->directions & NODE_RESIZE_BOTTOM) {
1235                                                 node->locy = nsw->oldlocy + 0.5f * dy;
1236                                                 node->offsety = nsw->oldoffsety - 0.5f * dy;
1237                                         }
1238                                 }
1239                         }
1240                                 
1241                         ED_region_tag_redraw(ar);
1242
1243                         break;
1244                         
1245                 case LEFTMOUSE:
1246                 case MIDDLEMOUSE:
1247                 case RIGHTMOUSE:
1248                         
1249                         node_resize_exit(C, op, 0);
1250                         ED_node_post_apply_transform(C, snode->edittree);
1251                         
1252                         return OPERATOR_FINISHED;
1253         }
1254         
1255         return OPERATOR_RUNNING_MODAL;
1256 }
1257
1258 static int node_resize_invoke(bContext *C, wmOperator *op, wmEvent *event)
1259 {
1260         SpaceNode *snode = CTX_wm_space_node(C);
1261         ARegion *ar = CTX_wm_region(C);
1262         bNode *node = editnode_get_active(snode->edittree);
1263         int dir;
1264         
1265         if (node) {
1266                 /* convert mouse coordinates to v2d space */
1267                 UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1],
1268                                          &snode->mx, &snode->my);
1269                 dir = node->typeinfo->resize_area_func(node, snode->mx, snode->my);
1270                 if (dir != 0) {
1271                         node_resize_init(C, op, event, node, dir);
1272                         return OPERATOR_RUNNING_MODAL;
1273                 }
1274         }
1275         return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
1276 }
1277
1278 static int node_resize_cancel(bContext *C, wmOperator *op)
1279 {
1280         node_resize_exit(C, op, 1);
1281
1282         return OPERATOR_CANCELLED;
1283 }
1284
1285 void NODE_OT_resize(wmOperatorType *ot)
1286 {
1287         /* identifiers */
1288         ot->name = "Resize Node";
1289         ot->idname = "NODE_OT_resize";
1290         ot->description = "Resize a node";
1291         
1292         /* api callbacks */
1293         ot->invoke = node_resize_invoke;
1294         ot->modal = node_resize_modal;
1295         ot->poll = ED_operator_node_active;
1296         ot->cancel = node_resize_cancel;
1297         
1298         /* flags */
1299         ot->flag = OPTYPE_BLOCKING;
1300 }
1301
1302
1303 /* ********************** hidden sockets ******************** */
1304
1305 int node_has_hidden_sockets(bNode *node)
1306 {
1307         bNodeSocket *sock;
1308         
1309         for (sock = node->inputs.first; sock; sock = sock->next)
1310                 if (sock->flag & SOCK_HIDDEN)
1311                         return 1;
1312         for (sock = node->outputs.first; sock; sock = sock->next)
1313                 if (sock->flag & SOCK_HIDDEN)
1314                         return 1;
1315         return 0;
1316 }
1317
1318 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
1319 {
1320         bNodeSocket *sock;
1321
1322         if (set == 0) {
1323                 for (sock = node->inputs.first; sock; sock = sock->next)
1324                         sock->flag &= ~SOCK_HIDDEN;
1325                 for (sock = node->outputs.first; sock; sock = sock->next)
1326                         sock->flag &= ~SOCK_HIDDEN;
1327         }
1328         else {
1329                 /* hide unused sockets */
1330                 for (sock = node->inputs.first; sock; sock = sock->next) {
1331                         if (sock->link == NULL)
1332                                 sock->flag |= SOCK_HIDDEN;
1333                 }
1334                 for (sock = node->outputs.first; sock; sock = sock->next) {
1335                         if (nodeCountSocketLinks(snode->edittree, sock) == 0)
1336                                 sock->flag |= SOCK_HIDDEN;
1337                 }
1338         }
1339 }
1340
1341 /* return 0, nothing done */
1342 static int UNUSED_FUNCTION(node_mouse_groupheader) (SpaceNode * snode)
1343 {
1344         bNode *gnode;
1345         float mx = 0, my = 0;
1346 // XXX  int mval[2];
1347         
1348         gnode = node_tree_get_editgroup(snode->nodetree);
1349         if (gnode == NULL) return 0;
1350         
1351 // XXX  getmouseco_areawin(mval);
1352 // XXX  areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1353         
1354         /* click in header or outside? */
1355         if (BLI_in_rctf(&gnode->totr, mx, my) == 0) {
1356                 rctf rect = gnode->totr;
1357                 
1358                 rect.ymax += NODE_DY;
1359                 if (BLI_in_rctf(&rect, mx, my) == 0)
1360                         snode_make_group_editable(snode, NULL);  /* toggles, so exits editmode */
1361 //              else
1362 // XXX                  transform_nodes(snode->nodetree, 'g', "Move group");
1363                 
1364                 return 1;
1365         }
1366         return 0;
1367 }
1368
1369 /* checks snode->mouse position, and returns found node/socket */
1370 /* type is SOCK_IN and/or SOCK_OUT */
1371 int node_find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **sockp, int in_out)
1372 {
1373         bNode *node;
1374         bNodeSocket *sock;
1375         rctf rect;
1376         
1377         *nodep = NULL;
1378         *sockp = NULL;
1379         
1380         /* check if we click in a socket */
1381         for (node = snode->edittree->nodes.first; node; node = node->next) {
1382                 
1383                 rect.xmin = snode->mx - (NODE_SOCKSIZE + 4);
1384                 rect.ymin = snode->my - (NODE_SOCKSIZE + 4);
1385                 rect.xmax = snode->mx + (NODE_SOCKSIZE + 4);
1386                 rect.ymax = snode->my + (NODE_SOCKSIZE + 4);
1387                 
1388                 if (!(node->flag & NODE_HIDDEN)) {
1389                         /* extra padding inside and out - allow dragging on the text areas too */
1390                         if (in_out == SOCK_IN) {
1391                                 rect.xmax += NODE_SOCKSIZE;
1392                                 rect.xmin -= NODE_SOCKSIZE * 4;
1393                         }
1394                         else if (in_out == SOCK_OUT) {
1395                                 rect.xmax += NODE_SOCKSIZE * 4;
1396                                 rect.xmin -= NODE_SOCKSIZE;
1397                         }
1398                 }
1399                 
1400                 if (in_out & SOCK_IN) {
1401                         for (sock = node->inputs.first; sock; sock = sock->next) {
1402                                 if (!nodeSocketIsHidden(sock)) {
1403                                         if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1404                                                 if (node == visible_node(snode, &rect)) {
1405                                                         *nodep = node;
1406                                                         *sockp = sock;
1407                                                         return 1;
1408                                                 }
1409                                         }
1410                                 }
1411                         }
1412                 }
1413                 if (in_out & SOCK_OUT) {
1414                         for (sock = node->outputs.first; sock; sock = sock->next) {
1415                                 if (!nodeSocketIsHidden(sock)) {
1416                                         if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1417                                                 if (node == visible_node(snode, &rect)) {
1418                                                         *nodep = node;
1419                                                         *sockp = sock;
1420                                                         return 1;
1421                                                 }
1422                                         }
1423                                 }
1424                         }
1425                 }
1426         }
1427         
1428         /* check group sockets
1429          * NB: using ngroup->outputs as input sockets and vice versa here!
1430          */
1431         if (in_out & SOCK_IN) {
1432                 for (sock = snode->edittree->outputs.first; sock; sock = sock->next) {
1433                         if (!nodeSocketIsHidden(sock)) {
1434                                 if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1435                                         *nodep = NULL;   /* NULL node pointer indicates group socket */
1436                                         *sockp = sock;
1437                                         return 1;
1438                                 }
1439                         }
1440                 }
1441         }
1442         if (in_out & SOCK_OUT) {
1443                 for (sock = snode->edittree->inputs.first; sock; sock = sock->next) {
1444                         if (!nodeSocketIsHidden(sock)) {
1445                                 if (BLI_in_rctf(&rect, sock->locx, sock->locy)) {
1446                                         *nodep = NULL;   /* NULL node pointer indicates group socket */
1447                                         *sockp = sock;
1448                                         return 1;
1449                                 }
1450                         }
1451                 }
1452         }
1453         
1454         return 0;
1455 }
1456
1457 /* ****************** Duplicate *********************** */
1458
1459 static void node_duplicate_reparent_recursive(bNode *node)
1460 {
1461         bNode *parent;
1462         
1463         node->flag |= NODE_TEST;
1464         
1465         /* find first selected parent */
1466         for (parent = node->parent; parent; parent = parent->parent) {
1467                 if (parent->flag & SELECT) {
1468                         if (!(parent->flag & NODE_TEST))
1469                                 node_duplicate_reparent_recursive(parent);
1470                         break;
1471                 }
1472         }
1473         /* reparent node copy to parent copy */
1474         if (parent) {
1475                 nodeDetachNode(node->new_node);
1476                 nodeAttachNode(node->new_node, parent->new_node);
1477         }
1478 }
1479
1480 static int node_duplicate_exec(bContext *C, wmOperator *op)
1481 {
1482         SpaceNode *snode = CTX_wm_space_node(C);
1483         bNodeTree *ntree = snode->edittree;
1484         bNode *node, *newnode, *lastnode;
1485         bNodeLink *link, *newlink, *lastlink;
1486         int keep_inputs = RNA_boolean_get(op->ptr, "keep_inputs");
1487         
1488         ED_preview_kill_jobs(C);
1489         
1490         lastnode = ntree->nodes.last;
1491         for (node = ntree->nodes.first; node; node = node->next) {
1492                 if (node->flag & SELECT) {
1493                         newnode = nodeCopyNode(ntree, node);
1494                         
1495                         if (newnode->id) {
1496                                 /* simple id user adjustment, node internal functions don't touch this
1497                                  * but operators and readfile.c do. */
1498                                 id_us_plus(newnode->id);
1499                                 /* to ensure redraws or rerenders happen */
1500                                 ED_node_changed_update(snode->id, newnode);
1501                         }
1502                 }
1503                 
1504                 /* make sure we don't copy new nodes again! */
1505                 if (node == lastnode)
1506                         break;
1507         }
1508         
1509         /* copy links between selected nodes
1510          * NB: this depends on correct node->new_node and sock->new_sock pointers from above copy!
1511          */
1512         lastlink = ntree->links.last;
1513         for (link = ntree->links.first; link; link = link->next) {
1514                 /* This creates new links between copied nodes.
1515                  * If keep_inputs is set, also copies input links from unselected (when fromnode==NULL)!
1516                  */
1517                 if (link->tonode && (link->tonode->flag & NODE_SELECT) &&
1518                     (keep_inputs || (link->fromnode && (link->fromnode->flag & NODE_SELECT))))
1519                 {
1520                         newlink = MEM_callocN(sizeof(bNodeLink), "bNodeLink");
1521                         newlink->flag = link->flag;
1522                         newlink->tonode = link->tonode->new_node;
1523                         newlink->tosock = link->tosock->new_sock;
1524                         if (link->fromnode && (link->fromnode->flag & NODE_SELECT)) {
1525                                 newlink->fromnode = link->fromnode->new_node;
1526                                 newlink->fromsock = link->fromsock->new_sock;
1527                         }
1528                         else {
1529                                 /* input node not copied, this keeps the original input linked */
1530                                 newlink->fromnode = link->fromnode;
1531                                 newlink->fromsock = link->fromsock;
1532                         }
1533                         
1534                         BLI_addtail(&ntree->links, newlink);
1535                 }
1536                 
1537                 /* make sure we don't copy new links again! */
1538                 if (link == lastlink)
1539                         break;
1540         }
1541         
1542         /* clear flags for recursive depth-first iteration */
1543         for (node = ntree->nodes.first; node; node = node->next)
1544                 node->flag &= ~NODE_TEST;
1545         /* reparent copied nodes */
1546         for (node = ntree->nodes.first; node; node = node->next) {
1547                 if ((node->flag & SELECT) && !(node->flag & NODE_TEST))
1548                         node_duplicate_reparent_recursive(node);
1549                 
1550                 /* only has to check old nodes */
1551                 if (node == lastnode)
1552                         break;
1553         }
1554         
1555         /* deselect old nodes, select the copies instead */
1556         for (node = ntree->nodes.first; node; node = node->next) {
1557                 if (node->flag & SELECT) {
1558                         /* has been set during copy above */
1559                         newnode = node->new_node;
1560                         
1561                         node_deselect(node);
1562                         node->flag &= ~NODE_ACTIVE;
1563                         node_select(newnode);
1564                 }
1565                 
1566                 /* make sure we don't copy new nodes again! */
1567                 if (node == lastnode)
1568                         break;
1569         }
1570         
1571         ntreeUpdateTree(snode->edittree);
1572         
1573         snode_notify(C, snode);
1574         snode_dag_update(C, snode);
1575
1576         return OPERATOR_FINISHED;
1577 }
1578
1579 void NODE_OT_duplicate(wmOperatorType *ot)
1580 {
1581         /* identifiers */
1582         ot->name = "Duplicate Nodes";
1583         ot->description = "Duplicate selected nodes";
1584         ot->idname = "NODE_OT_duplicate";
1585         
1586         /* api callbacks */
1587         ot->exec = node_duplicate_exec;
1588         ot->poll = ED_operator_node_active;
1589         
1590         /* flags */
1591         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1592         
1593         RNA_def_boolean(ot->srna, "keep_inputs", 0, "Keep Inputs", "Keep the input links to duplicated nodes");
1594 }
1595
1596 int ED_node_select_check(ListBase *lb)
1597 {
1598         bNode *node;
1599
1600         for (node = lb->first; node; node = node->next) {
1601                 if (node->flag & NODE_SELECT) {
1602                         return TRUE;
1603                 }
1604         }
1605
1606         return FALSE;
1607 }
1608
1609 /* ******************************** */
1610 // XXX some code needing updating to operators...
1611
1612
1613 /* goes over all scenes, reads render layers */
1614 static int node_read_renderlayers_exec(bContext *C, wmOperator *UNUSED(op))
1615 {
1616         Main *bmain = CTX_data_main(C);
1617         SpaceNode *snode = CTX_wm_space_node(C);
1618         Scene *curscene = CTX_data_scene(C), *scene;
1619         bNode *node;
1620
1621         ED_preview_kill_jobs(C);
1622
1623         /* first tag scenes unread */
1624         for (scene = bmain->scene.first; scene; scene = scene->id.next)
1625                 scene->id.flag |= LIB_DOIT;
1626
1627         for (node = snode->edittree->nodes.first; node; node = node->next) {
1628                 if (node->type == CMP_NODE_R_LAYERS) {
1629                         ID *id = node->id;
1630                         if (id->flag & LIB_DOIT) {
1631                                 RE_ReadRenderResult(curscene, (Scene *)id);
1632                                 ntreeCompositTagRender((Scene *)id);
1633                                 id->flag &= ~LIB_DOIT;
1634                         }
1635                 }
1636         }
1637         
1638         snode_notify(C, snode);
1639         snode_dag_update(C, snode);
1640
1641         return OPERATOR_FINISHED;
1642 }
1643
1644 void NODE_OT_read_renderlayers(wmOperatorType *ot)
1645 {
1646         
1647         ot->name = "Read Render Layers";
1648         ot->idname = "NODE_OT_read_renderlayers";
1649         ot->description = "Read all render layers of all used scenes";
1650         
1651         ot->exec = node_read_renderlayers_exec;
1652         
1653         ot->poll = composite_node_active;
1654         
1655         /* flags */
1656         ot->flag = 0;
1657 }
1658
1659 static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op))
1660 {
1661         Main *bmain = CTX_data_main(C);
1662         SpaceNode *snode = CTX_wm_space_node(C);
1663         Scene *curscene = CTX_data_scene(C);
1664         Render *re = RE_NewRender(curscene->id.name);
1665
1666         WM_cursor_wait(1);
1667         RE_MergeFullSample(re, bmain, curscene, snode->nodetree);
1668         WM_cursor_wait(0);
1669
1670         /* note we are careful to send the right notifier, as otherwise the
1671          * compositor would reexecute and overwrite the full sample result */
1672         WM_event_add_notifier(C, NC_SCENE | ND_COMPO_RESULT, NULL);
1673
1674         return OPERATOR_FINISHED;
1675 }
1676
1677
1678 void NODE_OT_read_fullsamplelayers(wmOperatorType *ot)
1679 {
1680         
1681         ot->name = "Read Full Sample Layers";
1682         ot->idname = "NODE_OT_read_fullsamplelayers";
1683         ot->description = "Read all render layers of current scene, in full sample";
1684         
1685         ot->exec = node_read_fullsamplelayers_exec;
1686         
1687         ot->poll = composite_node_active;
1688         
1689         /* flags */
1690         ot->flag = 0;
1691 }
1692
1693 int node_render_changed_exec(bContext *C, wmOperator *UNUSED(op))
1694 {
1695         Scene *sce = CTX_data_scene(C);
1696         bNode *node;
1697         
1698         for (node = sce->nodetree->nodes.first; node; node = node->next) {
1699                 if (node->id == (ID *)sce && node->need_exec) {
1700                         break;
1701                 }
1702         }
1703         if (node) {
1704                 SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
1705                 
1706                 if (srl) {
1707                         PointerRNA op_ptr;
1708                         
1709                         WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
1710                         RNA_string_set(&op_ptr, "layer", srl->name);
1711                         RNA_string_set(&op_ptr, "scene", sce->id.name + 2);
1712                         
1713                         /* to keep keypositions */
1714                         sce->r.scemode |= R_NO_FRAME_UPDATE;
1715                         
1716                         WM_operator_name_call(C, "RENDER_OT_render", WM_OP_INVOKE_DEFAULT, &op_ptr);
1717
1718                         WM_operator_properties_free(&op_ptr);
1719                         
1720                         return OPERATOR_FINISHED;
1721                 }
1722                    
1723         }
1724         return OPERATOR_CANCELLED;
1725 }
1726
1727 void NODE_OT_render_changed(wmOperatorType *ot)
1728 {
1729         ot->name = "Render Changed Layer";
1730         ot->idname = "NODE_OT_render_changed";
1731         ot->description = "Render current scene, when input node's layer has been changed";
1732         
1733         ot->exec = node_render_changed_exec;
1734         
1735         ot->poll = composite_node_active;
1736         
1737         /* flags */
1738         ot->flag = 0;
1739 }
1740
1741 /* ****************** Hide operator *********************** */
1742
1743 static void node_flag_toggle_exec(SpaceNode *snode, int toggle_flag)
1744 {
1745         bNode *node;
1746         int tot_eq = 0, tot_neq = 0;
1747
1748         /* Toggles the flag on all selected nodes.
1749          * If the flag is set on all nodes it is unset.
1750          * If the flag is not set on all nodes, it is set.
1751          */
1752         for (node = snode->edittree->nodes.first; node; node = node->next) {
1753                 if (node->flag & SELECT) {
1754                         
1755                         if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
1756                                 continue;
1757                         if (toggle_flag == NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS) == 0)
1758                                 continue;
1759                         
1760                         if (node->flag & toggle_flag)
1761                                 tot_eq++;
1762                         else
1763                                 tot_neq++;
1764                 }
1765         }
1766         for (node = snode->edittree->nodes.first; node; node = node->next) {
1767                 if (node->flag & SELECT) {
1768                         
1769                         if (toggle_flag == NODE_PREVIEW && (node->typeinfo->flag & NODE_PREVIEW) == 0)
1770                                 continue;
1771                         if (toggle_flag == NODE_OPTIONS && (node->typeinfo->flag & NODE_OPTIONS) == 0)
1772                                 continue;
1773                         
1774                         if ( (tot_eq && tot_neq) || tot_eq == 0)
1775                                 node->flag |= toggle_flag;
1776                         else
1777                                 node->flag &= ~toggle_flag;
1778                 }
1779         }
1780 }
1781
1782 static int node_hide_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1783 {
1784         SpaceNode *snode = CTX_wm_space_node(C);
1785         
1786         /* sanity checking (poll callback checks this already) */
1787         if ((snode == NULL) || (snode->edittree == NULL))
1788                 return OPERATOR_CANCELLED;
1789         
1790         node_flag_toggle_exec(snode, NODE_HIDDEN);
1791
1792         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1793
1794         return OPERATOR_FINISHED;
1795 }
1796
1797 void NODE_OT_hide_toggle(wmOperatorType *ot)
1798 {
1799         /* identifiers */
1800         ot->name = "Hide";
1801         ot->description = "Toggle hiding of selected nodes";
1802         ot->idname = "NODE_OT_hide_toggle";
1803         
1804         /* callbacks */
1805         ot->exec = node_hide_toggle_exec;
1806         ot->poll = ED_operator_node_active;
1807
1808         /* flags */
1809         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1810 }
1811
1812 static int node_preview_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1813 {
1814         SpaceNode *snode = CTX_wm_space_node(C);
1815
1816         /* sanity checking (poll callback checks this already) */
1817         if ((snode == NULL) || (snode->edittree == NULL))
1818                 return OPERATOR_CANCELLED;
1819
1820         ED_preview_kill_jobs(C);
1821
1822         node_flag_toggle_exec(snode, NODE_PREVIEW);
1823
1824         snode_notify(C, snode);
1825
1826         return OPERATOR_FINISHED;
1827 }
1828
1829 void NODE_OT_preview_toggle(wmOperatorType *ot)
1830 {
1831         /* identifiers */
1832         ot->name = "Toggle Node Preview";
1833         ot->description = "Toggle preview display for selected nodes";
1834         ot->idname = "NODE_OT_preview_toggle";
1835
1836         /* callbacks */
1837         ot->exec = node_preview_toggle_exec;
1838         ot->poll = ED_operator_node_active;
1839
1840         /* flags */
1841         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1842 }
1843
1844 static int node_options_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1845 {
1846         SpaceNode *snode = CTX_wm_space_node(C);
1847
1848         /* sanity checking (poll callback checks this already) */
1849         if ((snode == NULL) || (snode->edittree == NULL))
1850                 return OPERATOR_CANCELLED;
1851
1852         node_flag_toggle_exec(snode, NODE_OPTIONS);
1853
1854         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1855
1856         return OPERATOR_FINISHED;
1857 }
1858
1859 void NODE_OT_options_toggle(wmOperatorType *ot)
1860 {
1861         /* identifiers */
1862         ot->name = "Toggle Node Options";
1863         ot->description = "Toggle option buttons display for selected nodes";
1864         ot->idname = "NODE_OT_options_toggle";
1865
1866         /* callbacks */
1867         ot->exec = node_options_toggle_exec;
1868         ot->poll = ED_operator_node_active;
1869
1870         /* flags */
1871         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1872 }
1873
1874 static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1875 {
1876         SpaceNode *snode = CTX_wm_space_node(C);
1877         bNode *node;
1878         int hidden;
1879
1880         /* sanity checking (poll callback checks this already) */
1881         if ((snode == NULL) || (snode->edittree == NULL))
1882                 return OPERATOR_CANCELLED;
1883
1884         ED_preview_kill_jobs(C);
1885
1886         /* Toggle for all selected nodes */
1887         hidden = 0;
1888         for (node = snode->edittree->nodes.first; node; node = node->next) {
1889                 if (node->flag & SELECT) {
1890                         if (node_has_hidden_sockets(node)) {
1891                                 hidden = 1;
1892                                 break;
1893                         }
1894                 }
1895         }
1896         
1897         for (node = snode->edittree->nodes.first; node; node = node->next) {
1898                 if (node->flag & SELECT) {
1899                         node_set_hidden_sockets(snode, node, !hidden);
1900                 }
1901         }
1902
1903         ntreeUpdateTree(snode->edittree);
1904
1905         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
1906
1907         return OPERATOR_FINISHED;
1908 }
1909
1910 void NODE_OT_hide_socket_toggle(wmOperatorType *ot)
1911 {
1912         /* identifiers */
1913         ot->name = "Toggle Hidden Node Sockets";
1914         ot->description = "Toggle unused node socket display";
1915         ot->idname = "NODE_OT_hide_socket_toggle";
1916
1917         /* callbacks */
1918         ot->exec = node_socket_toggle_exec;
1919         ot->poll = ED_operator_node_active;
1920
1921         /* flags */
1922         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1923 }
1924
1925 /* ****************** Mute operator *********************** */
1926
1927 static int node_mute_exec(bContext *C, wmOperator *UNUSED(op))
1928 {
1929         SpaceNode *snode = CTX_wm_space_node(C);
1930         bNode *node;
1931
1932         ED_preview_kill_jobs(C);
1933
1934         for (node = snode->edittree->nodes.first; node; node = node->next) {
1935                 /* Only allow muting of nodes having a mute func! */
1936                 if ((node->flag & SELECT) && node->typeinfo->internal_connect) {
1937                         node->flag ^= NODE_MUTED;
1938                         snode_update(snode, node);
1939                 }
1940         }
1941         
1942         snode_notify(C, snode);
1943         snode_dag_update(C, snode);
1944         
1945         return OPERATOR_FINISHED;
1946 }
1947
1948 void NODE_OT_mute_toggle(wmOperatorType *ot)
1949 {
1950         /* identifiers */
1951         ot->name = "Toggle Node Mute";
1952         ot->description = "Toggle muting of the nodes";
1953         ot->idname = "NODE_OT_mute_toggle";
1954         
1955         /* callbacks */
1956         ot->exec = node_mute_exec;
1957         ot->poll = ED_operator_node_active;
1958         
1959         /* flags */
1960         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1961 }
1962
1963 /* ****************** Delete operator ******************* */
1964
1965 static int node_delete_exec(bContext *C, wmOperator *UNUSED(op))
1966 {
1967         SpaceNode *snode = CTX_wm_space_node(C);
1968         bNode *node, *next;
1969         
1970         ED_preview_kill_jobs(C);
1971
1972         for (node = snode->edittree->nodes.first; node; node = next) {
1973                 next = node->next;
1974                 if (node->flag & SELECT) {
1975                         /* check id user here, nodeFreeNode is called for free dbase too */
1976                         if (node->id)
1977                                 node->id->us--;
1978                         nodeFreeNode(snode->edittree, node);
1979                 }
1980         }
1981         
1982         ntreeUpdateTree(snode->edittree);
1983
1984         snode_notify(C, snode);
1985         snode_dag_update(C, snode);
1986         
1987         return OPERATOR_FINISHED;
1988 }
1989
1990 void NODE_OT_delete(wmOperatorType *ot)
1991 {
1992         /* identifiers */
1993         ot->name = "Delete";
1994         ot->description = "Delete selected nodes";
1995         ot->idname = "NODE_OT_delete";
1996         
1997         /* api callbacks */
1998         ot->exec = node_delete_exec;
1999         ot->poll = ED_operator_node_active;
2000         
2001         /* flags */
2002         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2003 }
2004
2005 /* ****************** Delete with reconnect ******************* */
2006 static int node_delete_reconnect_exec(bContext *C, wmOperator *UNUSED(op))
2007 {
2008         SpaceNode *snode = CTX_wm_space_node(C);
2009         bNode *node, *next;
2010
2011         ED_preview_kill_jobs(C);
2012
2013         for (node = snode->edittree->nodes.first; node; node = next) {
2014                 next = node->next;
2015                 if (node->flag & SELECT) {
2016                         nodeInternalRelink(snode->edittree, node);
2017                         
2018                         /* check id user here, nodeFreeNode is called for free dbase too */
2019                         if (node->id)
2020                                 node->id->us--;
2021                         nodeFreeNode(snode->edittree, node);
2022                 }
2023         }
2024
2025         ntreeUpdateTree(snode->edittree);
2026
2027         snode_notify(C, snode);
2028         snode_dag_update(C, snode);
2029
2030         return OPERATOR_FINISHED;
2031 }
2032
2033 void NODE_OT_delete_reconnect(wmOperatorType *ot)
2034 {
2035         /* identifiers */
2036         ot->name = "Delete with reconnect";
2037         ot->description = "Delete nodes; will reconnect nodes as if deletion was muted";
2038         ot->idname = "NODE_OT_delete_reconnect";
2039
2040         /* api callbacks */
2041         ot->exec = node_delete_reconnect_exec;
2042         ot->poll = ED_operator_node_active;
2043
2044         /* flags */
2045         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2046 }
2047
2048
2049 /* ****************** File Output Add Socket  ******************* */
2050
2051 static int node_output_file_add_socket_exec(bContext *C, wmOperator *op)
2052 {
2053         Scene *scene = CTX_data_scene(C);
2054         SpaceNode *snode = CTX_wm_space_node(C);
2055         PointerRNA ptr;
2056         bNodeTree *ntree;
2057         bNode *node;
2058         char file_path[MAX_NAME];
2059
2060         ptr = CTX_data_pointer_get(C, "node");
2061         if (!ptr.data)
2062                 return OPERATOR_CANCELLED;
2063         node = ptr.data;
2064         ntree = ptr.id.data;
2065
2066         RNA_string_get(op->ptr, "file_path", file_path);
2067         ntreeCompositOutputFileAddSocket(ntree, node, file_path, &scene->r.im_format);
2068
2069         snode_notify(C, snode);
2070
2071         return OPERATOR_FINISHED;
2072 }
2073
2074 void NODE_OT_output_file_add_socket(wmOperatorType *ot)
2075 {
2076         /* identifiers */
2077         ot->name = "Add File Node Socket";
2078         ot->description = "Add a new input to a file output node";
2079         ot->idname = "NODE_OT_output_file_add_socket";
2080
2081         /* callbacks */
2082         ot->exec = node_output_file_add_socket_exec;
2083         ot->poll = composite_node_active;
2084
2085         /* flags */
2086         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2087
2088         RNA_def_string(ot->srna, "file_path", "Image", MAX_NAME, "File Path", "Sub-path of the output file");
2089 }
2090
2091 /* ****************** Multi File Output Remove Socket  ******************* */
2092
2093 static int node_output_file_remove_active_socket_exec(bContext *C, wmOperator *UNUSED(op))
2094 {
2095         SpaceNode *snode = CTX_wm_space_node(C);
2096         PointerRNA ptr = CTX_data_pointer_get(C, "node");
2097         bNodeTree *ntree;
2098         bNode *node;
2099         
2100         if (!ptr.data)
2101                 return OPERATOR_CANCELLED;
2102         node = ptr.data;
2103         ntree = ptr.id.data;
2104         
2105         if (!ntreeCompositOutputFileRemoveActiveSocket(ntree, node))
2106                 return OPERATOR_CANCELLED;
2107         
2108         snode_notify(C, snode);
2109         
2110         return OPERATOR_FINISHED;
2111 }
2112
2113 void NODE_OT_output_file_remove_active_socket(wmOperatorType *ot)
2114 {
2115         /* identifiers */
2116         ot->name = "Remove File Node Socket";
2117         ot->description = "Remove active input from a file output node";
2118         ot->idname = "NODE_OT_output_file_remove_active_socket";
2119         
2120         /* callbacks */
2121         ot->exec = node_output_file_remove_active_socket_exec;
2122         ot->poll = composite_node_active;
2123         
2124         /* flags */
2125         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2126 }
2127
2128 /* ****************** Multi File Output Move Socket  ******************* */
2129
2130 static int node_output_file_move_active_socket_exec(bContext *C, wmOperator *op)
2131 {
2132         SpaceNode *snode = CTX_wm_space_node(C);
2133         PointerRNA ptr = CTX_data_pointer_get(C, "node");
2134         bNode *node;
2135         NodeImageMultiFile *nimf;
2136         bNodeSocket *sock;
2137         int direction;
2138         
2139         if (!ptr.data)
2140                 return OPERATOR_CANCELLED;
2141         node = ptr.data;
2142         nimf = node->storage;
2143         
2144         sock = BLI_findlink(&node->inputs, nimf->active_input);
2145         if (!sock)
2146                 return OPERATOR_CANCELLED;
2147         
2148         direction = RNA_enum_get(op->ptr, "direction");
2149         
2150         if (direction == 1) {
2151                 bNodeSocket *before = sock->prev;
2152                 if (!before)
2153                         return OPERATOR_CANCELLED;
2154                 BLI_remlink(&node->inputs, sock);
2155                 BLI_insertlinkbefore(&node->inputs, before, sock);
2156                 --nimf->active_input;
2157         }
2158         else {
2159                 bNodeSocket *after = sock->next;
2160                 if (!after)
2161                         return OPERATOR_CANCELLED;
2162                 BLI_remlink(&node->inputs, sock);
2163                 BLI_insertlinkafter(&node->inputs, after, sock);
2164                 ++nimf->active_input;
2165         }
2166         
2167         snode_notify(C, snode);
2168         
2169         return OPERATOR_FINISHED;
2170 }
2171
2172 void NODE_OT_output_file_move_active_socket(wmOperatorType *ot)
2173 {
2174         static EnumPropertyItem direction_items[] = {
2175                 {1, "UP", 0, "Up", ""},
2176                 {2, "DOWN", 0, "Down", ""},
2177                 { 0, NULL, 0, NULL, NULL }
2178         };
2179         
2180         /* identifiers */
2181         ot->name = "Move File Node Socket";
2182         ot->description = "Move the active input of a file output node up or down the list";
2183         ot->idname = "NODE_OT_output_file_move_active_socket";
2184         
2185         /* callbacks */
2186         ot->exec = node_output_file_move_active_socket_exec;
2187         ot->poll = composite_node_active;
2188         
2189         /* flags */
2190         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2191         
2192         RNA_def_enum(ot->srna, "direction", direction_items, 2, "Direction", "");
2193 }
2194
2195 /* ****************** Copy Node Color ******************* */
2196
2197 static int node_copy_color_exec(bContext *C, wmOperator *UNUSED(op))
2198 {
2199         SpaceNode *snode = CTX_wm_space_node(C);
2200         bNodeTree *ntree = snode->edittree;
2201         bNode *node, *tnode;
2202         
2203         if (!ntree)
2204                 return OPERATOR_CANCELLED;
2205         node = nodeGetActive(ntree);
2206         if (!node)
2207                 return OPERATOR_CANCELLED;
2208         
2209         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
2210                 if (tnode->flag & NODE_SELECT && tnode != node) {
2211                         if (node->flag & NODE_CUSTOM_COLOR) {
2212                                 tnode->flag |= NODE_CUSTOM_COLOR;
2213                                 copy_v3_v3(tnode->color, node->color);
2214                         }
2215                         else
2216                                 tnode->flag &= ~NODE_CUSTOM_COLOR;
2217                 }
2218         }
2219
2220         ED_node_sort(ntree);
2221         WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
2222
2223         return OPERATOR_FINISHED;
2224 }
2225
2226 void NODE_OT_node_copy_color(wmOperatorType *ot)
2227 {
2228         /* identifiers */
2229         ot->name = "Copy Color";
2230         ot->description = "Copy color to all selected nodes";
2231         ot->idname = "NODE_OT_node_copy_color";
2232
2233         /* api callbacks */
2234         ot->exec = node_copy_color_exec;
2235         ot->poll = ED_operator_node_active;
2236
2237         /* flags */
2238         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2239 }