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