Implemented a basic framework for node-based shaders.
[blender-staging.git] / source / blender / editors / space_node / space_node.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) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_node/space_node.c
28  *  \ingroup spnode
29  */
30
31
32
33 #include "DNA_lamp_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_node_types.h"
36 #include "DNA_world_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42
43 #include "BKE_context.h"
44 #include "BKE_library.h"
45 #include "BKE_scene.h"
46 #include "BKE_screen.h"
47 #include "BKE_node.h"
48
49 #include "ED_space_api.h"
50 #include "ED_node.h"
51 #include "ED_render.h"
52 #include "ED_screen.h"
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "UI_resources.h"
57 #include "UI_view2d.h"
58
59 #include "RNA_access.h"
60
61 #include "node_intern.h"  /* own include */
62
63
64 /* ******************** tree path ********************* */
65
66 void ED_node_tree_start(SpaceNode *snode, bNodeTree *ntree, ID *id, ID *from)
67 {
68         bNodeTreePath *path, *path_next;
69         for (path = snode->treepath.first; path; path = path_next) {
70                 path_next = path->next;
71                 MEM_freeN(path);
72         }
73         BLI_listbase_clear(&snode->treepath);
74         
75         if (ntree) {
76                 path = MEM_callocN(sizeof(bNodeTreePath), "node tree path");
77                 path->nodetree = ntree;
78                 path->parent_key = NODE_INSTANCE_KEY_BASE;
79                 
80                 /* copy initial offset from bNodeTree */
81                 copy_v2_v2(path->view_center, ntree->view_center);
82                 
83                 if (id)
84                         BLI_strncpy(path->node_name, id->name + 2, sizeof(path->node_name));
85                 
86                 BLI_addtail(&snode->treepath, path);
87                 
88                 id_us_ensure_real(&ntree->id);
89         }
90         
91         /* update current tree */
92         snode->nodetree = snode->edittree = ntree;
93         snode->id = id;
94         snode->from = from;
95         
96         ED_node_set_active_viewer_key(snode);
97         
98         WM_main_add_notifier(NC_SCENE | ND_NODES, NULL);
99 }
100
101 void ED_node_tree_push(SpaceNode *snode, bNodeTree *ntree, bNode *gnode)
102 {
103         bNodeTreePath *path = MEM_callocN(sizeof(bNodeTreePath), "node tree path");
104         bNodeTreePath *prev_path = snode->treepath.last;
105         path->nodetree = ntree;
106         if (gnode) {
107                 if (prev_path)
108                         path->parent_key = BKE_node_instance_key(prev_path->parent_key, prev_path->nodetree, gnode);
109                 else
110                         path->parent_key = NODE_INSTANCE_KEY_BASE;
111                 
112                 BLI_strncpy(path->node_name, gnode->name, sizeof(path->node_name));
113         }
114         else
115                 path->parent_key = NODE_INSTANCE_KEY_BASE;
116         
117         /* copy initial offset from bNodeTree */
118         copy_v2_v2(path->view_center, ntree->view_center);
119         
120         BLI_addtail(&snode->treepath, path);
121         
122         id_us_ensure_real(&ntree->id);
123         
124         /* update current tree */
125         snode->edittree = ntree;
126         
127         ED_node_set_active_viewer_key(snode);
128         
129         WM_main_add_notifier(NC_SCENE | ND_NODES, NULL);
130 }
131
132 void ED_node_tree_pop(SpaceNode *snode)
133 {
134         bNodeTreePath *path = snode->treepath.last;
135         
136         /* don't remove root */
137         if (path == snode->treepath.first)
138                 return;
139         
140         BLI_remlink(&snode->treepath, path);
141         MEM_freeN(path);
142         
143         /* update current tree */
144         path = snode->treepath.last;
145         snode->edittree = path->nodetree;
146         
147         ED_node_set_active_viewer_key(snode);
148         
149         /* listener updates the View2D center from edittree */
150         WM_main_add_notifier(NC_SCENE | ND_NODES, NULL);
151 }
152
153 int ED_node_tree_depth(SpaceNode *snode)
154 {
155         return BLI_countlist(&snode->treepath);
156 }
157
158 bNodeTree *ED_node_tree_get(SpaceNode *snode, int level)
159 {
160         bNodeTreePath *path;
161         int i;
162         for (path = snode->treepath.last, i = 0; path; path = path->prev, ++i) {
163                 if (i == level)
164                         return path->nodetree;
165         }
166         return NULL;
167 }
168
169 int ED_node_tree_path_length(SpaceNode *snode)
170 {
171         bNodeTreePath *path;
172         int length = 0;
173         int i;
174         for (path = snode->treepath.first, i = 0; path; path = path->next, ++i) {
175                 length += strlen(path->node_name);
176                 if (i > 0)
177                         length += 1;    /* for separator char */
178         }
179         return length;
180 }
181
182 void ED_node_tree_path_get(SpaceNode *snode, char *value)
183 {
184         bNodeTreePath *path;
185         int i;
186         
187         value[0] = '\0';
188         for (path = snode->treepath.first, i = 0; path; path = path->next, ++i) {
189                 if (i == 0) {
190                         strcpy(value, path->node_name);
191                         value += strlen(path->node_name);
192                 }
193                 else {
194                         sprintf(value, "/%s", path->node_name);
195                         value += strlen(path->node_name) + 1;
196                 }
197         }
198 }
199
200 void ED_node_tree_path_get_fixedbuf(SpaceNode *snode, char *value, int max_length)
201 {
202         bNodeTreePath *path;
203         int size, i;
204         
205         value[0] = '\0';
206         for (path = snode->treepath.first, i = 0; path; path = path->next, ++i) {
207                 if (i == 0) {
208                         BLI_strncpy(value, path->node_name, max_length);
209                         size = strlen(path->node_name);
210                 }
211                 else {
212                         BLI_snprintf(value, max_length, "/%s", path->node_name);
213                         size = strlen(path->node_name) + 1;
214                 }
215                 max_length -= size;
216                 if (max_length <= 0)
217                         break;
218                 value += size;
219         }
220 }
221
222 void ED_node_set_active_viewer_key(SpaceNode *snode)
223 {
224         bNodeTreePath *path = snode->treepath.last;
225         if (snode->nodetree && path) {
226                 snode->nodetree->active_viewer_key = path->parent_key;
227         }
228 }
229
230 void snode_group_offset(SpaceNode *snode, float *x, float *y)
231 {
232         bNodeTreePath *path = snode->treepath.last;
233         
234         if (path && path->prev) {
235                 float dcenter[2];
236                 sub_v2_v2v2(dcenter, path->view_center, path->prev->view_center);
237                 *x = dcenter[0];
238                 *y = dcenter[1];
239         }
240         else
241                 *x = *y = 0.0f;
242 }
243
244 /* ******************** manage regions ********************* */
245
246 ARegion *node_has_buttons_region(ScrArea *sa)
247 {
248         ARegion *ar, *arnew;
249
250         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
251         if (ar) return ar;
252
253         /* add subdiv level; after header */
254         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
255
256         /* is error! */
257         if (ar == NULL) return NULL;
258
259         arnew = MEM_callocN(sizeof(ARegion), "buttons for node");
260
261         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
262         arnew->regiontype = RGN_TYPE_UI;
263         arnew->alignment = RGN_ALIGN_RIGHT;
264
265         arnew->flag = RGN_FLAG_HIDDEN;
266
267         return arnew;
268 }
269
270 ARegion *node_has_tools_region(ScrArea *sa)
271 {
272         ARegion *ar, *arnew;
273         
274         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
275         if (ar) return ar;
276         
277         /* add subdiv level; after header */
278         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
279         
280         /* is error! */
281         if (ar == NULL) return NULL;
282         
283         arnew = MEM_callocN(sizeof(ARegion), "node tools");
284         
285         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
286         arnew->regiontype = RGN_TYPE_TOOLS;
287         arnew->alignment = RGN_ALIGN_LEFT;
288         
289         arnew->flag = RGN_FLAG_HIDDEN;
290         
291         return arnew;
292 }
293
294 /* ******************** default callbacks for node space ***************** */
295
296 static SpaceLink *node_new(const bContext *UNUSED(C))
297 {
298         ARegion *ar;
299         SpaceNode *snode;
300
301         snode = MEM_callocN(sizeof(SpaceNode), "initnode");
302         snode->spacetype = SPACE_NODE;
303
304         snode->flag = SNODE_SHOW_GPENCIL | SNODE_USE_ALPHA;
305
306         /* backdrop */
307         snode->zoom = 1.0f;
308
309         /* select the first tree type for valid type */
310         NODE_TREE_TYPES_BEGIN (treetype)
311         {
312                 strcpy(snode->tree_idname, treetype->idname);
313                 break;
314         }
315         NODE_TREE_TYPES_END;
316
317         /* header */
318         ar = MEM_callocN(sizeof(ARegion), "header for node");
319
320         BLI_addtail(&snode->regionbase, ar);
321         ar->regiontype = RGN_TYPE_HEADER;
322         ar->alignment = RGN_ALIGN_BOTTOM;
323
324         /* buttons/list view */
325         ar = MEM_callocN(sizeof(ARegion), "buttons for node");
326
327         BLI_addtail(&snode->regionbase, ar);
328         ar->regiontype = RGN_TYPE_UI;
329         ar->alignment = RGN_ALIGN_RIGHT;
330
331         /* toolbar */
332         ar = MEM_callocN(sizeof(ARegion), "node tools");
333
334         BLI_addtail(&snode->regionbase, ar);
335         ar->regiontype = RGN_TYPE_TOOLS;
336         ar->alignment = RGN_ALIGN_LEFT;
337
338         ar->flag = RGN_FLAG_HIDDEN;
339
340         /* main area */
341         ar = MEM_callocN(sizeof(ARegion), "main area for node");
342
343         BLI_addtail(&snode->regionbase, ar);
344         ar->regiontype = RGN_TYPE_WINDOW;
345
346         ar->v2d.tot.xmin =  -12.8f * U.widget_unit;
347         ar->v2d.tot.ymin =  -12.8f * U.widget_unit;
348         ar->v2d.tot.xmax = 38.4f * U.widget_unit;
349         ar->v2d.tot.ymax = 38.4f * U.widget_unit;
350
351         ar->v2d.cur =  ar->v2d.tot;
352
353         ar->v2d.min[0] = 1.0f;
354         ar->v2d.min[1] = 1.0f;
355
356         ar->v2d.max[0] = 32000.0f;
357         ar->v2d.max[1] = 32000.0f;
358
359         ar->v2d.minzoom = 0.09f;
360         ar->v2d.maxzoom = 2.31f;
361
362         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
363         ar->v2d.keepzoom = V2D_LIMITZOOM | V2D_KEEPASPECT;
364         ar->v2d.keeptot = 0;
365
366         return (SpaceLink *)snode;
367 }
368
369 static void node_free(SpaceLink *sl)
370 {
371         SpaceNode *snode = (SpaceNode *)sl;
372         bNodeTreePath *path, *path_next;
373
374         for (path = snode->treepath.first; path; path = path_next) {
375                 path_next = path->next;
376                 MEM_freeN(path);
377         }
378 }
379
380
381 /* spacetype; init callback */
382 static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
383 {
384
385 }
386
387 static void node_area_listener(bScreen *sc, ScrArea *sa, wmNotifier *wmn)
388 {
389         /* note, ED_area_tag_refresh will re-execute compositor */
390         SpaceNode *snode = sa->spacedata.first;
391         /* shaderfrom is only used for new shading nodes, otherwise all shaders are from objects */
392         short shader_type = BKE_scene_use_new_shading_nodes(sc->scene) ? snode->shaderfrom : SNODE_SHADER_OBJECT;
393
394         /* preview renders */
395         switch (wmn->category) {
396                 case NC_SCENE:
397                         switch (wmn->data) {
398                                 case ND_NODES:
399                                 {
400                                         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
401                                         bNodeTreePath *path = snode->treepath.last;
402                                         /* shift view to node tree center */
403                                         if (ar && path)
404                                                 UI_view2d_center_set(&ar->v2d, path->view_center[0], path->view_center[1]);
405                                         
406                                         ED_area_tag_refresh(sa);
407                                         break;
408                                 }
409                                 case ND_FRAME:
410                                         ED_area_tag_refresh(sa);
411                                         break;
412                                 case ND_COMPO_RESULT:
413                                         ED_area_tag_redraw(sa);
414                                         break;
415                                 case ND_TRANSFORM_DONE:
416                                         if (ED_node_is_compositor(snode)) {
417                                                 if (snode->flag & SNODE_AUTO_RENDER) {
418                                                         snode->recalc = 1;
419                                                         ED_area_tag_refresh(sa);
420                                                 }
421                                         }
422                                         break;
423                         }
424                         break;
425
426                 /* future: add ID checks? */
427                 case NC_MATERIAL:
428                         if (ED_node_is_shader(snode)) {
429                                 if (wmn->data == ND_SHADING)
430                                         ED_area_tag_refresh(sa);
431                                 else if (wmn->data == ND_SHADING_DRAW)
432                                         ED_area_tag_refresh(sa);
433                                 else if (wmn->data == ND_SHADING_LINKS)
434                                         ED_area_tag_refresh(sa);
435                                 else if (wmn->action == NA_ADDED && snode->edittree)
436                                         nodeSetActiveID(snode->edittree, ID_MA, wmn->reference);
437
438                         }
439                         break;
440                 case NC_TEXTURE:
441                         if (ED_node_is_shader(snode) || ED_node_is_texture(snode)) {
442                                 if (wmn->data == ND_NODES)
443                                         ED_area_tag_refresh(sa);
444                         }
445                         break;
446                 case NC_WORLD:
447                         if (ED_node_is_shader(snode) && shader_type == SNODE_SHADER_WORLD) {
448                                 ED_area_tag_refresh(sa);
449                         }
450                         break;
451                 case NC_OBJECT:
452                         if (ED_node_is_shader(snode)) {
453                                 if (wmn->data == ND_OB_SHADING)
454                                         ED_area_tag_refresh(sa);
455                         }
456                         break;
457                 case NC_SPACE:
458                         if (wmn->data == ND_SPACE_NODE)
459                                 ED_area_tag_refresh(sa);
460                         else if (wmn->data == ND_SPACE_NODE_VIEW)
461                                 ED_area_tag_redraw(sa);
462                         break;
463                 case NC_NODE:
464                         if (wmn->action == NA_EDITED)
465                                 ED_area_tag_refresh(sa);
466                         else if (wmn->action == NA_SELECTED)
467                                 ED_area_tag_redraw(sa);
468                         break;
469                 case NC_SCREEN:
470                         switch (wmn->data) {
471                                 case ND_ANIMPLAY:
472                                         ED_area_tag_refresh(sa);
473                                         break;
474                         }
475                         break;
476                 case NC_MASK:
477                         if (wmn->action == NA_EDITED) {
478                                 if (snode->nodetree && snode->nodetree->type == NTREE_COMPOSIT) {
479                                         ED_area_tag_refresh(sa);
480                                 }
481                         }
482                         break;
483
484                 case NC_IMAGE:
485                         if (wmn->action == NA_EDITED) {
486                                 if (ED_node_is_compositor(snode)) {
487                                         /* note that nodeUpdateID is already called by BKE_image_signal() on all
488                                          * scenes so really this is just to know if the images is used in the compo else
489                                          * painting on images could become very slow when the compositor is open. */
490                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
491                                                 ED_area_tag_refresh(sa);
492                                 }
493                         }
494                         break;
495
496                 case NC_MOVIECLIP:
497                         if (wmn->action == NA_EDITED) {
498                                 if (ED_node_is_compositor(snode)) {
499                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
500                                                 ED_area_tag_refresh(sa);
501                                 }
502                         }
503                         break;
504
505                 case NC_LINESTYLE:
506                         if (ED_node_is_shader(snode) && shader_type == SNODE_SHADER_LINESTYLE) {
507                                 ED_area_tag_refresh(sa);
508                         }
509                         break;
510         }
511 }
512
513 static void node_area_refresh(const struct bContext *C, ScrArea *sa)
514 {
515         /* default now: refresh node is starting preview */
516         SpaceNode *snode = sa->spacedata.first;
517         
518         snode_set_context(C);
519
520         if (snode->nodetree) {
521                 if (snode->nodetree->type == NTREE_SHADER) {
522                         if (GS(snode->id->name) == ID_MA) {
523                                 Material *ma = (Material *)snode->id;
524                                 if (ma->use_nodes)
525                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
526                         }
527                         else if (GS(snode->id->name) == ID_LA) {
528                                 Lamp *la = (Lamp *)snode->id;
529                                 if (la->use_nodes)
530                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
531                         }
532                         else if (GS(snode->id->name) == ID_WO) {
533                                 World *wo = (World *)snode->id;
534                                 if (wo->use_nodes)
535                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
536                         }
537                 }
538                 else if (snode->nodetree->type == NTREE_COMPOSIT) {
539                         Scene *scene = (Scene *)snode->id;
540                         if (scene->use_nodes) {
541                                 /* recalc is set on 3d view changes for auto compo */
542                                 if (snode->recalc) {
543                                         snode->recalc = 0;
544                                         node_render_changed_exec((struct bContext *)C, NULL);
545                                 }
546                                 else {
547                                         ED_node_composite_job(C, snode->nodetree, scene);
548                                 }
549                         }
550                 }
551                 else if (snode->nodetree->type == NTREE_TEXTURE) {
552                         Tex *tex = (Tex *)snode->id;
553                         if (tex->use_nodes) {
554                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
555                         }
556                 }
557         }
558 }
559
560 static SpaceLink *node_duplicate(SpaceLink *sl)
561 {
562         SpaceNode *snode = (SpaceNode *)sl;
563         SpaceNode *snoden = MEM_dupallocN(snode);
564
565         BLI_duplicatelist(&snoden->treepath, &snode->treepath);
566
567         /* clear or remove stuff from old */
568         BLI_listbase_clear(&snoden->linkdrag);
569
570         /* Note: no need to set node tree user counts,
571          * the editor only keeps at least 1 (id_us_ensure_real),
572          * which is already done by the original SpaceNode.
573          */
574
575         return (SpaceLink *)snoden;
576 }
577
578
579 /* add handlers, stuff you only do once or on area/region changes */
580 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
581 {
582         wmKeyMap *keymap;
583
584         ED_region_panels_init(wm, ar);
585
586         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
587         WM_event_add_keymap_handler(&ar->handlers, keymap);
588 }
589
590 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
591 {
592         ED_region_panels(C, ar, 1, NULL, -1);
593 }
594
595 /* add handlers, stuff you only do once or on area/region changes */
596 static void node_toolbar_area_init(wmWindowManager *wm, ARegion *ar)
597 {
598         wmKeyMap *keymap;
599
600         ED_region_panels_init(wm, ar);
601
602         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
603         WM_event_add_keymap_handler(&ar->handlers, keymap);
604 }
605
606 static void node_toolbar_area_draw(const bContext *C, ARegion *ar)
607 {
608         ED_region_panels(C, ar, 1, NULL, -1);
609 }
610
611 static void node_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
612 {
613         SpaceNode *snode = sa->spacedata.first;
614
615         /* convert mouse coordinates to v2d space */
616         UI_view2d_region_to_view(&ar->v2d, win->eventstate->x - ar->winrct.xmin, win->eventstate->y - ar->winrct.ymin,
617                                  &snode->cursor[0], &snode->cursor[1]);
618         
619         /* here snode->cursor is used to detect the node edge for sizing */
620         node_set_cursor(win, snode, snode->cursor);
621
622         /* XXX snode->cursor is in placing new nodes space */
623         snode->cursor[0] /= UI_DPI_FAC;
624         snode->cursor[1] /= UI_DPI_FAC;
625         
626 }
627
628 /* Initialize main area, setting handlers. */
629 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
630 {
631         wmKeyMap *keymap;
632         ListBase *lb;
633
634         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
635
636         /* own keymaps */
637         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
638         WM_event_add_keymap_handler(&ar->handlers, keymap);
639
640         keymap = WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
641         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
642
643         /* add drop boxes */
644         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
645
646         WM_event_add_dropbox_handler(&ar->handlers, lb);
647 }
648
649 static void node_main_area_draw(const bContext *C, ARegion *ar)
650 {
651         drawnodespace(C, ar);
652 }
653
654
655 /* ************* dropboxes ************* */
656
657 static int node_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
658 {
659         if (drag->type == WM_DRAG_ID) {
660                 ID *id = (ID *)drag->poin;
661                 if (GS(id->name) == ID_IM)
662                         return 1;
663         }
664         else if (drag->type == WM_DRAG_PATH) {
665                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE))   /* rule might not work? */
666                         return 1;
667         }
668         return 0;
669 }
670
671 static int node_mask_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
672 {
673         if (drag->type == WM_DRAG_ID) {
674                 ID *id = (ID *)drag->poin;
675                 if (GS(id->name) == ID_MSK)
676                         return 1;
677         }
678         return 0;
679 }
680
681 static void node_id_drop_copy(wmDrag *drag, wmDropBox *drop)
682 {
683         ID *id = (ID *)drag->poin;
684
685         RNA_string_set(drop->ptr, "name", id->name + 2);
686 }
687
688 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
689 {
690         ID *id = (ID *)drag->poin;
691
692         if (id) {
693                 RNA_string_set(drop->ptr, "name", id->name + 2);
694         }
695         if (drag->path[0]) {
696                 RNA_string_set(drop->ptr, "filepath", drag->path);
697         }
698 }
699
700 /* this region dropbox definition */
701 static void node_dropboxes(void)
702 {
703         ListBase *lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
704
705         WM_dropbox_add(lb, "NODE_OT_add_file", node_ima_drop_poll, node_id_path_drop_copy);
706         WM_dropbox_add(lb, "NODE_OT_add_mask", node_mask_drop_poll, node_id_drop_copy);
707
708 }
709
710 /* ************* end drop *********** */
711
712
713 /* add handlers, stuff you only do once or on area/region changes */
714 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
715 {
716         ED_region_header_init(ar);
717 }
718
719 static void node_header_area_draw(const bContext *C, ARegion *ar)
720 {
721         /* find and set the context */
722         snode_set_context(C);
723
724         ED_region_header(C, ar);
725 }
726
727 /* used for header + main area */
728 static void node_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
729 {
730         /* context changes */
731         switch (wmn->category) {
732                 case NC_SPACE:
733                         if (wmn->data == ND_SPACE_NODE)
734                                 ED_region_tag_redraw(ar);
735                         break;
736                 case NC_SCREEN:
737                         switch (wmn->data) {
738                                 case ND_SCREENCAST:
739                                 case ND_ANIMPLAY:
740                                         ED_region_tag_redraw(ar);
741                                         break;
742                         }
743                         break;
744                 case NC_SCENE:
745                 case NC_MATERIAL:
746                 case NC_TEXTURE:
747                 case NC_WORLD:
748                 case NC_NODE:
749                 case NC_LINESTYLE:
750                         ED_region_tag_redraw(ar);
751                         break;
752                 case NC_OBJECT:
753                         if (wmn->data == ND_OB_SHADING)
754                                 ED_region_tag_redraw(ar);
755                         break;
756                 case NC_ID:
757                         if (wmn->action == NA_RENAME)
758                                 ED_region_tag_redraw(ar);
759                         break;
760                 case NC_GPENCIL:
761                         if (wmn->action == NA_EDITED)
762                                 ED_region_tag_redraw(ar);
763                         break;
764         }
765 }
766
767 const char *node_context_dir[] = {"selected_nodes", "active_node", NULL};
768
769 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
770 {
771         SpaceNode *snode = CTX_wm_space_node(C);
772
773         if (CTX_data_dir(member)) {
774                 CTX_data_dir_set(result, node_context_dir);
775                 return 1;
776         }
777         else if (CTX_data_equals(member, "selected_nodes")) {
778                 bNode *node;
779
780                 if (snode->edittree) {
781                         for (node = snode->edittree->nodes.last; node; node = node->prev) {
782                                 if (node->flag & NODE_SELECT) {
783                                         CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
784                                 }
785                         }
786                 }
787                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
788                 return 1;
789         }
790         else if (CTX_data_equals(member, "active_node")) {
791                 if (snode->edittree) {
792                         bNode *node = nodeGetActive(snode->edittree);
793                         CTX_data_pointer_set(result, &snode->edittree->id, &RNA_Node, node);
794                 }
795
796                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
797                 return 1;
798         }
799         else if (CTX_data_equals(member, "node_previews")) {
800                 if (snode->nodetree) {
801                         CTX_data_pointer_set(result, &snode->nodetree->id, &RNA_NodeInstanceHash, snode->nodetree->previews);
802                 }
803
804                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
805                 return 1;
806         }
807
808         return 0;
809 }
810
811 /* only called once, from space/spacetypes.c */
812 void ED_spacetype_node(void)
813 {
814         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype node");
815         ARegionType *art;
816
817         st->spaceid = SPACE_NODE;
818         strncpy(st->name, "Node", BKE_ST_MAXNAME);
819
820         st->new = node_new;
821         st->free = node_free;
822         st->init = node_init;
823         st->duplicate = node_duplicate;
824         st->operatortypes = node_operatortypes;
825         st->keymap = node_keymap;
826         st->listener = node_area_listener;
827         st->refresh = node_area_refresh;
828         st->context = node_context;
829         st->dropboxes = node_dropboxes;
830
831         /* regions: main window */
832         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
833         art->regionid = RGN_TYPE_WINDOW;
834         art->init = node_main_area_init;
835         art->draw = node_main_area_draw;
836         art->listener = node_region_listener;
837         art->cursor = node_cursor;
838         art->event_cursor = true;
839         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
840
841         BLI_addhead(&st->regiontypes, art);
842
843         /* regions: header */
844         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
845         art->regionid = RGN_TYPE_HEADER;
846         art->prefsizey = HEADERY;
847         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
848         art->listener = node_region_listener;
849         art->init = node_header_area_init;
850         art->draw = node_header_area_draw;
851
852         BLI_addhead(&st->regiontypes, art);
853
854         /* regions: listview/buttons */
855         art = MEM_callocN(sizeof(ARegionType), "spacetype node region");
856         art->regionid = RGN_TYPE_UI;
857         art->prefsizex = 180; // XXX
858         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
859         art->listener = node_region_listener;
860         art->init = node_buttons_area_init;
861         art->draw = node_buttons_area_draw;
862         BLI_addhead(&st->regiontypes, art);
863
864         node_buttons_register(art);
865
866         /* regions: toolbar */
867         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
868         art->regionid = RGN_TYPE_TOOLS;
869         art->prefsizex = 160; /* XXX */
870         art->prefsizey = 50; /* XXX */
871         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
872         art->listener = node_region_listener;
873         art->init = node_toolbar_area_init;
874         art->draw = node_toolbar_area_draw;
875         BLI_addhead(&st->regiontypes, art);
876         
877         node_toolbar_register(art);
878
879         BKE_spacetype_register(st);
880 }
881