Cycles: svn merge -r41182:41205 ^/trunk/blender
[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_COMPO_RESULT:
184                                         ED_area_tag_redraw(sa);
185                                         break;
186                                 case ND_TRANSFORM_DONE:
187                                         if(type==NTREE_COMPOSIT) {
188                                                 if(snode->flag & SNODE_AUTO_RENDER) {
189                                                         snode->recalc= 1;
190                                                         ED_area_tag_refresh(sa);
191                                                 }
192                                         }
193                                         break;
194                         }
195                         break;
196                 case NC_WM:
197                         if(wmn->data==ND_FILEREAD)
198                                 ED_area_tag_refresh(sa);
199                         break;
200                 
201                 /* future: add ID checks? */
202                 case NC_MATERIAL:
203                         if(type==NTREE_SHADER) {
204                                 if(wmn->data==ND_SHADING)
205                                         ED_area_tag_refresh(sa);
206                                 else if(wmn->data==ND_SHADING_DRAW)
207                                         ED_area_tag_refresh(sa);
208                                 else if(wmn->action==NA_ADDED && snode->edittree)
209                                         nodeSetActiveID(snode->edittree, ID_MA, wmn->reference);
210                                         
211                         }
212                         break;
213                 case NC_TEXTURE:
214                         if(type==NTREE_SHADER || type==NTREE_TEXTURE) {
215                                 if(wmn->data==ND_NODES)
216                                         ED_area_tag_refresh(sa);
217                         }
218                         break;
219                 case NC_OBJECT:
220                         if(type==NTREE_SHADER) {
221                                 if(wmn->data==ND_OB_SHADING)
222                                         ED_area_tag_refresh(sa);
223                         }
224                         break;
225                 case NC_TEXT:
226                         /* pynodes */
227                         if(wmn->data==ND_SHADING)
228                                 ED_area_tag_refresh(sa);
229                         break;
230                 case NC_SPACE:
231                         if(wmn->data==ND_SPACE_NODE)
232                                 ED_area_tag_refresh(sa);
233                         else if(wmn->data==ND_SPACE_NODE_VIEW)
234                                 ED_area_tag_redraw(sa);
235                         break;
236                 case NC_NODE:
237                         if (wmn->action == NA_EDITED)
238                                 ED_area_tag_refresh(sa);
239                         else if (wmn->action == NA_SELECTED)
240                                 ED_area_tag_redraw(sa);
241                         break;
242                 case NC_SCREEN:
243                         switch(wmn->data) {
244                                 case ND_ANIMPLAY:
245                                         ED_area_tag_refresh(sa);
246                                         break;
247                         }
248                         break;
249
250                 case NC_IMAGE:
251                         if (wmn->action == NA_EDITED) {
252                                 if(type==NTREE_COMPOSIT) {
253                                         Scene *scene= wmn->window->screen->scene;
254                                         
255                                         /* note that nodeUpdateID is already called by BKE_image_signal() on all
256                                          * scenes so really this is just to know if the images is used in the compo else
257                                          * painting on images could become very slow when the compositor is open. */
258                                         if(nodeUpdateID(scene->nodetree, wmn->reference))
259                                                 ED_area_tag_refresh(sa);
260                                 }
261                         }
262                         break;
263         }
264 }
265
266 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
267 {
268         /* default now: refresh node is starting preview */
269         SpaceNode *snode= sa->spacedata.first;
270
271         snode_set_context(snode, CTX_data_scene(C));
272         
273         if(snode->nodetree) {
274                 if(snode->treetype==NTREE_SHADER) {
275                         if(GS(snode->id->name) == ID_MA) {
276                                 Material *ma= (Material *)snode->id;
277                                 if(ma->use_nodes)
278                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
279                         }
280                         else if(GS(snode->id->name) == ID_LA) {
281                                 Lamp *la= (Lamp *)snode->id;
282                                 if(la->use_nodes)
283                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
284                         }
285                         else if(GS(snode->id->name) == ID_WO) {
286                                 World *wo= (World *)snode->id;
287                                 if(wo->use_nodes)
288                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
289                         }
290                 }
291                 else if(snode->treetype==NTREE_COMPOSIT) {
292                         Scene *scene= (Scene *)snode->id;
293                         if(scene->use_nodes) {
294                                 /* recalc is set on 3d view changes for auto compo */
295                                 if(snode->recalc) {
296                                         snode->recalc= 0;
297                                         node_render_changed_exec((struct bContext*)C, NULL);
298                                 }
299                                 else 
300                                         snode_composite_job(C, sa);
301                         }
302                 }
303                 else if(snode->treetype==NTREE_TEXTURE) {
304                         Tex *tex= (Tex *)snode->id;
305                         if(tex->use_nodes) {
306                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
307                         }
308                 }
309         }
310 }
311
312 static SpaceLink *node_duplicate(SpaceLink *sl)
313 {
314         SpaceNode *snoden= MEM_dupallocN(sl);
315         
316         /* clear or remove stuff from old */
317         snoden->nodetree= NULL;
318         snoden->linkdrag.first= snoden->linkdrag.last= NULL;
319         
320         return (SpaceLink *)snoden;
321 }
322
323
324 /* add handlers, stuff you only do once or on area/region changes */
325 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
326 {
327         wmKeyMap *keymap;
328
329         ED_region_panels_init(wm, ar);
330         
331         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
332         WM_event_add_keymap_handler(&ar->handlers, keymap);
333 }
334
335 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
336 {
337         ED_region_panels(C, ar, 1, NULL, -1);
338 }
339
340 /* Initialise main area, setting handlers. */
341 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
342 {
343         wmKeyMap *keymap;
344         ListBase *lb;
345         
346         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
347         
348         /* own keymaps */
349         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
350         WM_event_add_keymap_handler(&ar->handlers, keymap);
351         
352         keymap= WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
353         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
354         
355         /* add drop boxes */
356         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
357         
358         WM_event_add_dropbox_handler(&ar->handlers, lb);
359 }
360
361 static void node_main_area_draw(const bContext *C, ARegion *ar)
362 {
363         View2D *v2d= &ar->v2d;
364         
365         drawnodespace(C, ar, v2d);
366 }
367
368
369 /* ************* dropboxes ************* */
370
371 static int node_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
372 {
373         if(drag->type==WM_DRAG_ID) {
374                 ID *id= (ID *)drag->poin;
375                 if( GS(id->name)==ID_IM )
376                         return 1;
377         }
378         else if(drag->type==WM_DRAG_PATH){
379                 if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
380                         return 1;
381         }
382         return 0;
383 }
384
385 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
386 {
387         ID *id= (ID *)drag->poin;
388         
389         if(id) {
390                 RNA_string_set(drop->ptr, "name", id->name+2);
391         }
392         if (drag->path[0]) {
393                 RNA_string_set(drop->ptr, "filepath", drag->path);
394         }
395 }
396
397 /* this region dropbox definition */
398 static void node_dropboxes(void)
399 {
400         ListBase *lb= WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
401         
402         WM_dropbox_add(lb, "NODE_OT_add_file", node_drop_poll, node_id_path_drop_copy);
403         
404 }
405
406 /* ************* end drop *********** */
407
408
409 /* add handlers, stuff you only do once or on area/region changes */
410 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
411 {
412         ED_region_header_init(ar);
413 }
414
415 static void node_header_area_draw(const bContext *C, ARegion *ar)
416 {
417         SpaceNode *snode= CTX_wm_space_node(C);
418         Scene *scene= CTX_data_scene(C);
419
420         /* find and set the context */
421         snode_set_context(snode, scene);
422
423         ED_region_header(C, ar);
424 }
425
426 /* used for header + main area */
427 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
428 {
429         /* context changes */
430         switch(wmn->category) {
431                 case NC_SPACE:
432                         if(wmn->data==ND_SPACE_NODE)
433                                 ED_region_tag_redraw(ar);
434                         break;
435                 case NC_SCREEN:
436                         if(wmn->data == ND_GPENCIL)     
437                                 ED_region_tag_redraw(ar);
438                         break;
439                 case NC_SCENE:
440                 case NC_MATERIAL:
441                 case NC_TEXTURE:
442                 case NC_NODE:
443                         ED_region_tag_redraw(ar);
444                         break;
445                 case NC_OBJECT:
446                         if(wmn->data==ND_OB_SHADING)
447                                 ED_region_tag_redraw(ar);
448                         break;
449                 case NC_ID:
450                         if(wmn->action == NA_RENAME)
451                                 ED_region_tag_redraw(ar);
452                         break;
453         }
454 }
455
456 const char *node_context_dir[] = {"selected_nodes", NULL};
457
458 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
459 {
460         SpaceNode *snode= CTX_wm_space_node(C);
461         
462         if(CTX_data_dir(member)) {
463                 CTX_data_dir_set(result, node_context_dir);
464                 return 1;
465         }
466         else if(CTX_data_equals(member, "selected_nodes")) {
467                 bNode *node;
468                 
469                 if(snode->edittree) {
470                         for(node=snode->edittree->nodes.last; node; node=node->prev) {
471                                 if(node->flag & NODE_SELECT) {
472                                         CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
473                                 }
474                         }
475                 }
476                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
477                 return 1;
478         }
479         else if(CTX_data_equals(member, "active_node")) {
480                 bNode *node;
481                 
482                 if(snode->edittree) {
483                         for(node=snode->edittree->nodes.last; node; node=node->prev) {
484                                 if(node->flag & NODE_ACTIVE) {
485                                         CTX_data_pointer_set(result, &snode->edittree->id, &RNA_Node, node);
486                                         break;
487                                 }
488                         }
489                 }
490                 CTX_data_type_set(result, CTX_DATA_TYPE_POINTER);
491                 return 1;
492         }
493         
494         return 0;
495 }
496
497 /* only called once, from space/spacetypes.c */
498 void ED_spacetype_node(void)
499 {
500         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
501         ARegionType *art;
502         
503         st->spaceid= SPACE_NODE;
504         strncpy(st->name, "Node", BKE_ST_MAXNAME);
505         
506         st->new= node_new;
507         st->free= node_free;
508         st->init= node_init;
509         st->duplicate= node_duplicate;
510         st->operatortypes= node_operatortypes;
511         st->keymap= node_keymap;
512         st->listener= node_area_listener;
513         st->refresh= node_area_refresh;
514         st->context= node_context;
515         st->dropboxes = node_dropboxes;
516         
517         /* regions: main window */
518         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
519         art->regionid = RGN_TYPE_WINDOW;
520         art->init= node_main_area_init;
521         art->draw= node_main_area_draw;
522         art->listener= node_region_listener;
523         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
524
525         BLI_addhead(&st->regiontypes, art);
526         
527         /* regions: header */
528         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
529         art->regionid = RGN_TYPE_HEADER;
530         art->prefsizey= HEADERY;
531         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
532         art->listener= node_region_listener;
533         art->init= node_header_area_init;
534         art->draw= node_header_area_draw;
535         
536         BLI_addhead(&st->regiontypes, art);
537
538         node_menus_register();
539         
540         /* regions: listview/buttons */
541         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
542         art->regionid = RGN_TYPE_UI;
543         art->prefsizex= 180; // XXX
544         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
545         art->listener= node_region_listener;
546         art->init= node_buttons_area_init;
547         art->draw= node_buttons_area_draw;
548         BLI_addhead(&st->regiontypes, art);
549         
550         node_buttons_register(art);
551         
552         BKE_spacetype_register(st);
553 }
554