7f7c074c823c7a5bbf3c3cfd711837b4b101ccb3
[blender.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 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_lamp_types.h"
36 #include "DNA_material_types.h"
37 #include "DNA_node_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_world_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_rand.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_context.h"
50 #include "BKE_screen.h"
51 #include "BKE_node.h"
52
53 #include "ED_space_api.h"
54 #include "ED_render.h"
55 #include "ED_screen.h"
56
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64 #include "RNA_access.h"
65
66 #include "node_intern.h"        // own include
67
68 /* ******************** manage regions ********************* */
69
70 ARegion *node_has_buttons_region(ScrArea *sa)
71 {
72         ARegion *ar, *arnew;
73
74         ar= BKE_area_find_region_type(sa, RGN_TYPE_UI);
75         if (ar) return ar;
76         
77         /* add subdiv level; after header */
78         ar= BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
79
80         /* is error! */
81         if (ar==NULL) return NULL;
82         
83         arnew= MEM_callocN(sizeof(ARegion), "buttons for node");
84         
85         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
86         arnew->regiontype= RGN_TYPE_UI;
87         arnew->alignment= RGN_ALIGN_RIGHT;
88         
89         arnew->flag = RGN_FLAG_HIDDEN;
90         
91         return arnew;
92 }
93
94 /* ******************** default callbacks for node space ***************** */
95
96 static SpaceLink *node_new(const bContext *UNUSED(C))
97 {
98         ARegion *ar;
99         SpaceNode *snode;
100         
101         snode= MEM_callocN(sizeof(SpaceNode), "initnode");
102         snode->spacetype= SPACE_NODE;   
103         
104         /* backdrop */
105         snode->zoom = 1.0f;
106
107
108         /* OCIO_TODO: use default view transform here when OCIO is completely integrated
109          *             and proper versioning stuff is added.
110          *             for now use NONE to be compatible with all current files
111          */
112         BLI_strncpy(snode->view_transform, "NONE", sizeof(snode->view_transform));
113
114         /* header */
115         ar= MEM_callocN(sizeof(ARegion), "header for node");
116         
117         BLI_addtail(&snode->regionbase, ar);
118         ar->regiontype= RGN_TYPE_HEADER;
119         ar->alignment= RGN_ALIGN_BOTTOM;
120         
121         /* buttons/list view */
122         ar= MEM_callocN(sizeof(ARegion), "buttons for node");
123         
124         BLI_addtail(&snode->regionbase, ar);
125         ar->regiontype= RGN_TYPE_UI;
126         ar->alignment= RGN_ALIGN_RIGHT;
127         ar->flag = RGN_FLAG_HIDDEN;
128         
129         /* main area */
130         ar= MEM_callocN(sizeof(ARegion), "main area for node");
131         
132         BLI_addtail(&snode->regionbase, ar);
133         ar->regiontype= RGN_TYPE_WINDOW;
134         
135         ar->v2d.tot.xmin =  -256.0f;
136         ar->v2d.tot.ymin =  -256.0f;
137         ar->v2d.tot.xmax = 768.0f;
138         ar->v2d.tot.ymax = 768.0f;
139         
140         ar->v2d.cur.xmin =  -256.0f;
141         ar->v2d.cur.ymin =  -256.0f;
142         ar->v2d.cur.xmax = 768.0f;
143         ar->v2d.cur.ymax = 768.0f;
144         
145         ar->v2d.min[0]= 1.0f;
146         ar->v2d.min[1]= 1.0f;
147         
148         ar->v2d.max[0]= 32000.0f;
149         ar->v2d.max[1]= 32000.0f;
150         
151         ar->v2d.minzoom= 0.09f;
152         ar->v2d.maxzoom= 2.31f;
153         
154         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
155         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
156         ar->v2d.keeptot= 0;
157         
158         return (SpaceLink *)snode;
159 }
160
161 /* not spacelink itself */
162 static void node_free(SpaceLink *UNUSED(sl))
163 {       
164         
165 }
166
167
168 /* spacetype; init callback */
169 static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
170 {
171
172 }
173
174 static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
175 {
176         /* note, ED_area_tag_refresh will re-execute compositor */
177         SpaceNode *snode= sa->spacedata.first;
178         int type= snode->treetype;
179         short shader_type = snode->shaderfrom;
180         
181         /* preview renders */
182         switch (wmn->category) {
183                 case NC_SCENE:
184                         switch (wmn->data) {
185                                 case ND_NODES:
186                                 case ND_FRAME:
187                                         ED_area_tag_refresh(sa);
188                                         break;
189                                 case ND_COMPO_RESULT:
190                                         ED_area_tag_redraw(sa);
191                                         break;
192                                 case ND_TRANSFORM_DONE:
193                                         if (type==NTREE_COMPOSIT) {
194                                                 if (snode->flag & SNODE_AUTO_RENDER) {
195                                                         snode->recalc= 1;
196                                                         ED_area_tag_refresh(sa);
197                                                 }
198                                         }
199                                         break;
200                         }
201                         break;
202                 case NC_WM:
203                         if (wmn->data==ND_FILEREAD)
204                                 ED_area_tag_refresh(sa);
205                         break;
206                 
207                 /* future: add ID checks? */
208                 case NC_MATERIAL:
209                         if (type==NTREE_SHADER) {
210                                 if (wmn->data==ND_SHADING)
211                                         ED_area_tag_refresh(sa);
212                                 else if (wmn->data==ND_SHADING_DRAW)
213                                         ED_area_tag_refresh(sa);
214                                 else if (wmn->action==NA_ADDED && snode->edittree)
215                                         nodeSetActiveID(snode->edittree, ID_MA, wmn->reference);
216                                         
217                         }
218                         break;
219                 case NC_TEXTURE:
220                         if (type==NTREE_SHADER || type==NTREE_TEXTURE) {
221                                 if (wmn->data==ND_NODES)
222                                         ED_area_tag_refresh(sa);
223                         }
224                         break;
225                 case NC_WORLD:
226                         if (type==NTREE_SHADER && shader_type==SNODE_SHADER_WORLD) {
227                                 ED_area_tag_refresh(sa);        
228                         }
229                         break;
230                 case NC_OBJECT:
231                         if (type==NTREE_SHADER) {
232                                 if (wmn->data==ND_OB_SHADING)
233                                         ED_area_tag_refresh(sa);
234                         }
235                         break;
236                 case NC_SPACE:
237                         if (wmn->data==ND_SPACE_NODE)
238                                 ED_area_tag_refresh(sa);
239                         else if (wmn->data==ND_SPACE_NODE_VIEW)
240                                 ED_area_tag_redraw(sa);
241                         break;
242                 case NC_NODE:
243                         if (wmn->action == NA_EDITED)
244                                 ED_area_tag_refresh(sa);
245                         else if (wmn->action == NA_SELECTED)
246                                 ED_area_tag_redraw(sa);
247                         break;
248                 case NC_SCREEN:
249                         switch (wmn->data) {
250                                 case ND_ANIMPLAY:
251                                         ED_area_tag_refresh(sa);
252                                         break;
253                         }
254                         break;
255                 case NC_MASK:
256                         if (wmn->action == NA_EDITED) {
257                                 if (type==NTREE_COMPOSIT) {
258                                         ED_area_tag_refresh(sa);
259                                 }
260                         }
261                         break;
262
263                 case NC_IMAGE:
264                         if (wmn->action == NA_EDITED) {
265                                 if (type==NTREE_COMPOSIT) {
266                                         /* note that nodeUpdateID is already called by BKE_image_signal() on all
267                                          * scenes so really this is just to know if the images is used in the compo else
268                                          * painting on images could become very slow when the compositor is open. */
269                                         if (nodeUpdateID(snode->nodetree, wmn->reference))
270                                                 ED_area_tag_refresh(sa);
271                                 }
272                         }
273                         break;
274         }
275 }
276
277 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
278 {
279         /* default now: refresh node is starting preview */
280         SpaceNode *snode= sa->spacedata.first;
281
282         snode_set_context(snode, CTX_data_scene(C));
283         
284         if (snode->nodetree) {
285                 if (snode->treetype==NTREE_SHADER) {
286                         if (GS(snode->id->name) == ID_MA) {
287                                 Material *ma= (Material *)snode->id;
288                                 if (ma->use_nodes)
289                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
290                         }
291                         else if (GS(snode->id->name) == ID_LA) {
292                                 Lamp *la= (Lamp *)snode->id;
293                                 if (la->use_nodes)
294                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
295                         }
296                         else if (GS(snode->id->name) == ID_WO) {
297                                 World *wo= (World *)snode->id;
298                                 if (wo->use_nodes)
299                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
300                         }
301                 }
302                 else if (snode->treetype==NTREE_COMPOSIT) {
303                         Scene *scene= (Scene *)snode->id;
304                         if (scene->use_nodes) {
305                                 /* recalc is set on 3d view changes for auto compo */
306                                 if (snode->recalc) {
307                                         snode->recalc= 0;
308                                         node_render_changed_exec((struct bContext*)C, NULL);
309                                 }
310                                 else 
311                                         snode_composite_job(C, sa);
312                         }
313                 }
314                 else if (snode->treetype==NTREE_TEXTURE) {
315                         Tex *tex= (Tex *)snode->id;
316                         if (tex->use_nodes) {
317                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
318                         }
319                 }
320         }
321 }
322
323 static SpaceLink *node_duplicate(SpaceLink *sl)
324 {
325         SpaceNode *snoden= MEM_dupallocN(sl);
326         
327         /* clear or remove stuff from old */
328         snoden->nodetree= NULL;
329         snoden->linkdrag.first= snoden->linkdrag.last= NULL;
330
331         BLI_strncpy(snoden->view_transform, snoden->view_transform, sizeof(snoden->view_transform));
332
333         return (SpaceLink *)snoden;
334 }
335
336
337 /* add handlers, stuff you only do once or on area/region changes */
338 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
339 {
340         wmKeyMap *keymap;
341
342         ED_region_panels_init(wm, ar);
343         
344         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
345         WM_event_add_keymap_handler(&ar->handlers, keymap);
346 }
347
348 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
349 {
350         ED_region_panels(C, ar, 1, NULL, -1);
351 }
352
353 static void node_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
354 {
355         SpaceNode *snode= sa->spacedata.first;
356         
357         /* convert mouse coordinates to v2d space */
358         UI_view2d_region_to_view(&ar->v2d, win->eventstate->x - ar->winrct.xmin, win->eventstate->y - ar->winrct.ymin,
359                                  &snode->mx, &snode->my);
360         
361         node_set_cursor(win, snode);
362 }
363
364 /* Initialize main area, setting handlers. */
365 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
366 {
367         wmKeyMap *keymap;
368         ListBase *lb;
369         
370         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
371         
372         /* own keymaps */
373         keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
374         WM_event_add_keymap_handler(&ar->handlers, keymap);
375         
376         keymap = WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
377         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
378         
379         /* add drop boxes */
380         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
381         
382         WM_event_add_dropbox_handler(&ar->handlers, lb);
383 }
384
385 static void node_main_area_draw(const bContext *C, ARegion *ar)
386 {
387         View2D *v2d= &ar->v2d;
388         
389         drawnodespace(C, ar, v2d);
390 }
391
392
393 /* ************* dropboxes ************* */
394
395 static int node_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
396 {
397         if (drag->type==WM_DRAG_ID) {
398                 ID *id= (ID *)drag->poin;
399                 if ( GS(id->name)==ID_IM )
400                         return 1;
401         }
402         else if (drag->type==WM_DRAG_PATH) {
403                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE))       /* rule might not work? */
404                         return 1;
405         }
406         return 0;
407 }
408
409 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
410 {
411         ID *id= (ID *)drag->poin;
412         
413         if (id) {
414                 RNA_string_set(drop->ptr, "name", id->name+2);
415         }
416         if (drag->path[0]) {
417                 RNA_string_set(drop->ptr, "filepath", drag->path);
418         }
419 }
420
421 /* this region dropbox definition */
422 static void node_dropboxes(void)
423 {
424         ListBase *lb= WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
425         
426         WM_dropbox_add(lb, "NODE_OT_add_file", node_drop_poll, node_id_path_drop_copy);
427         
428 }
429
430 /* ************* end drop *********** */
431
432
433 /* add handlers, stuff you only do once or on area/region changes */
434 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
435 {
436         ED_region_header_init(ar);
437 }
438
439 static void node_header_area_draw(const bContext *C, ARegion *ar)
440 {
441         SpaceNode *snode= CTX_wm_space_node(C);
442         Scene *scene= CTX_data_scene(C);
443
444         /* find and set the context */
445         snode_set_context(snode, scene);
446
447         ED_region_header(C, ar);
448 }
449
450 /* used for header + main area */
451 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
452 {
453         /* context changes */
454         switch (wmn->category) {
455                 case NC_SPACE:
456                         if (wmn->data==ND_SPACE_NODE)
457                                 ED_region_tag_redraw(ar);
458                         break;
459                 case NC_SCREEN:
460                         if (wmn->data == ND_GPENCIL)    
461                                 ED_region_tag_redraw(ar);
462                         break;
463                 case NC_SCENE:
464                 case NC_MATERIAL:
465                 case NC_TEXTURE:
466                 case NC_NODE:
467                         ED_region_tag_redraw(ar);
468                         break;
469                 case NC_OBJECT:
470                         if (wmn->data==ND_OB_SHADING)
471                                 ED_region_tag_redraw(ar);
472                         break;
473                 case NC_ID:
474                         if (wmn->action == NA_RENAME)
475                                 ED_region_tag_redraw(ar);
476                         break;
477         }
478 }
479
480 const char *node_context_dir[] = {"selected_nodes", "active_node", NULL};
481
482 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
483 {
484         SpaceNode *snode= CTX_wm_space_node(C);
485         
486         if (CTX_data_dir(member)) {
487                 CTX_data_dir_set(result, node_context_dir);
488                 return 1;
489         }
490         else if (CTX_data_equals(member, "selected_nodes")) {
491                 bNode *node;
492                 
493                 if (snode->edittree) {
494                         for (node=snode->edittree->nodes.last; node; node=node->prev) {
495                                 if (node->flag & NODE_SELECT) {
496                                         CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
497                                 }
498                         }
499                 }
500                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
501                 return 1;
502         }
503         else if (CTX_data_equals(member, "active_node")) {
504                 if (snode->edittree) {
505                         bNode *node = nodeGetActive(snode->edittree);
506                         CTX_data_pointer_set(result, &snode->edittree->id, &RNA_Node, node);
507                 }
508
509                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
510                 return 1;
511         }
512         
513         return 0;
514 }
515
516 /* only called once, from space/spacetypes.c */
517 void ED_spacetype_node(void)
518 {
519         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
520         ARegionType *art;
521         
522         st->spaceid= SPACE_NODE;
523         strncpy(st->name, "Node", BKE_ST_MAXNAME);
524         
525         st->new= node_new;
526         st->free= node_free;
527         st->init= node_init;
528         st->duplicate= node_duplicate;
529         st->operatortypes= node_operatortypes;
530         st->keymap= node_keymap;
531         st->listener= node_area_listener;
532         st->refresh= node_area_refresh;
533         st->context= node_context;
534         st->dropboxes = node_dropboxes;
535         
536         /* regions: main window */
537         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
538         art->regionid = RGN_TYPE_WINDOW;
539         art->init= node_main_area_init;
540         art->draw= node_main_area_draw;
541         art->listener= node_region_listener;
542         art->cursor = node_cursor;
543         art->event_cursor = TRUE;
544         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
545
546         BLI_addhead(&st->regiontypes, art);
547         
548         /* regions: header */
549         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
550         art->regionid = RGN_TYPE_HEADER;
551         art->prefsizey= HEADERY;
552         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
553         art->listener= node_region_listener;
554         art->init= node_header_area_init;
555         art->draw= node_header_area_draw;
556         
557         BLI_addhead(&st->regiontypes, art);
558
559         node_menus_register();
560         
561         /* regions: listview/buttons */
562         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
563         art->regionid = RGN_TYPE_UI;
564         art->prefsizex= 180; // XXX
565         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
566         art->listener= node_region_listener;
567         art->init= node_buttons_area_init;
568         art->draw= node_buttons_area_draw;
569         BLI_addhead(&st->regiontypes, art);
570         
571         node_buttons_register(art);
572         
573         BKE_spacetype_register(st);
574 }
575