Cycles: svn merge -r36495:36747 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         for(ar= sa->regionbase.first; ar; ar= ar->next)
77                 if(ar->regiontype==RGN_TYPE_UI)
78                         return ar;
79         
80         /* add subdiv level; after header */
81         for(ar= sa->regionbase.first; ar; ar= ar->next)
82                 if(ar->regiontype==RGN_TYPE_HEADER)
83                         break;
84         
85         /* is error! */
86         if(ar==NULL) return NULL;
87         
88         arnew= MEM_callocN(sizeof(ARegion), "buttons for node");
89         
90         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
91         arnew->regiontype= RGN_TYPE_UI;
92         arnew->alignment= RGN_ALIGN_RIGHT;
93         
94         arnew->flag = RGN_FLAG_HIDDEN;
95         
96         return arnew;
97 }
98
99 /* ******************** default callbacks for node space ***************** */
100
101 static SpaceLink *node_new(const bContext *UNUSED(C))
102 {
103         ARegion *ar;
104         SpaceNode *snode;
105         
106         snode= MEM_callocN(sizeof(SpaceNode), "initnode");
107         snode->spacetype= SPACE_NODE;   
108         
109         /* backdrop */
110         snode->zoom = 1.0f;
111         
112         /* header */
113         ar= MEM_callocN(sizeof(ARegion), "header for node");
114         
115         BLI_addtail(&snode->regionbase, ar);
116         ar->regiontype= RGN_TYPE_HEADER;
117         ar->alignment= RGN_ALIGN_BOTTOM;
118         
119         /* buttons/list view */
120         ar= MEM_callocN(sizeof(ARegion), "buttons for node");
121         
122         BLI_addtail(&snode->regionbase, ar);
123         ar->regiontype= RGN_TYPE_UI;
124         ar->alignment= RGN_ALIGN_RIGHT;
125         ar->flag = RGN_FLAG_HIDDEN;
126         
127         /* main area */
128         ar= MEM_callocN(sizeof(ARegion), "main area for node");
129         
130         BLI_addtail(&snode->regionbase, ar);
131         ar->regiontype= RGN_TYPE_WINDOW;
132         
133         ar->v2d.tot.xmin=  -256.0f;
134         ar->v2d.tot.ymin=  -256.0f;
135         ar->v2d.tot.xmax= 768.0f;
136         ar->v2d.tot.ymax= 768.0f;
137         
138         ar->v2d.cur.xmin=  -256.0f;
139         ar->v2d.cur.ymin=  -256.0f;
140         ar->v2d.cur.xmax= 768.0f;
141         ar->v2d.cur.ymax= 768.0f;
142         
143         ar->v2d.min[0]= 1.0f;
144         ar->v2d.min[1]= 1.0f;
145         
146         ar->v2d.max[0]= 32000.0f;
147         ar->v2d.max[1]= 32000.0f;
148         
149         ar->v2d.minzoom= 0.09f;
150         ar->v2d.maxzoom= 2.31f;
151         
152         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
153         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
154         ar->v2d.keeptot= 0;
155         
156         return (SpaceLink *)snode;
157 }
158
159 /* not spacelink itself */
160 static void node_free(SpaceLink *UNUSED(sl))
161 {       
162         
163 }
164
165
166 /* spacetype; init callback */
167 static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
168 {
169
170 }
171
172 static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
173 {
174         /* note, ED_area_tag_refresh will re-execute compositor */
175         SpaceNode *snode= sa->spacedata.first;
176         int type= snode->treetype;
177         
178         /* preview renders */
179         switch(wmn->category) {
180                 case NC_SCENE:
181                         switch (wmn->data) {
182                                 case ND_NODES:
183                                 case ND_FRAME:
184                                         ED_area_tag_refresh(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                         }
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                         break;
231
232                 case NC_IMAGE:
233                         if (wmn->action == NA_EDITED) {
234                                 if(type==NTREE_COMPOSIT) {
235                                         Scene *scene= wmn->window->screen->scene;
236                                         
237                                         /* note that NodeTagIDChanged is already called by BKE_image_signal() on all
238                                          * scenes so really this is just to know if the images is used in the compo else
239                                          * painting on images could become very slow when the compositor is open. */
240                                         if(NodeTagIDChanged(scene->nodetree, wmn->reference))
241                                                 ED_area_tag_refresh(sa);
242                                 }
243                         }
244                         break;
245         }
246 }
247
248 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
249 {
250         /* default now: refresh node is starting preview */
251         SpaceNode *snode= sa->spacedata.first;
252
253         snode_set_context(snode, CTX_data_scene(C));
254         
255         if(snode->nodetree) {
256                 if(snode->treetype==NTREE_SHADER) {
257                         if(GS(snode->id->name) == ID_MA) {
258                                 Material *ma= (Material *)snode->id;
259                                 if(ma->use_nodes)
260                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
261                         }
262                         else if(GS(snode->id->name) == ID_LA) {
263                                 Lamp *la= (Lamp *)snode->id;
264                                 if(la->use_nodes)
265                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
266                         }
267                         else if(GS(snode->id->name) == ID_WO) {
268                                 World *wo= (World *)snode->id;
269                                 if(wo->use_nodes)
270                                         ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
271                         }
272                 }
273                 else if(snode->treetype==NTREE_COMPOSIT) {
274                         Scene *scene= (Scene *)snode->id;
275                         if(scene->use_nodes) {
276                                 /* recalc is set on 3d view changes for auto compo */
277                                 if(snode->recalc) {
278                                         snode->recalc= 0;
279                                         node_render_changed_exec((struct bContext*)C, NULL);
280                                 }
281                                 else 
282                                         snode_composite_job(C, sa);
283                         }
284                 }
285                 else if(snode->treetype==NTREE_TEXTURE) {
286                         Tex *tex= (Tex *)snode->id;
287                         if(tex->use_nodes) {
288                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
289                         }
290                 }
291         }
292 }
293
294 static SpaceLink *node_duplicate(SpaceLink *sl)
295 {
296         SpaceNode *snoden= MEM_dupallocN(sl);
297         
298         /* clear or remove stuff from old */
299         snoden->nodetree= NULL;
300         snoden->linkdrag.first= snoden->linkdrag.last= NULL;
301         
302         return (SpaceLink *)snoden;
303 }
304
305
306 /* add handlers, stuff you only do once or on area/region changes */
307 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
308 {
309         wmKeyMap *keymap;
310
311         ED_region_panels_init(wm, ar);
312         
313         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
314         WM_event_add_keymap_handler(&ar->handlers, keymap);
315 }
316
317 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
318 {
319         ED_region_panels(C, ar, 1, NULL, -1);
320 }
321
322 /* Initialise main area, setting handlers. */
323 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
324 {
325         wmKeyMap *keymap;
326         ListBase *lb;
327         
328         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
329         
330         /* own keymaps */
331         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
332         WM_event_add_keymap_handler(&ar->handlers, keymap);
333         
334         keymap= WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
335         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
336         
337         /* add drop boxes */
338         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
339         
340         WM_event_add_dropbox_handler(&ar->handlers, lb);
341 }
342
343 static void node_main_area_draw(const bContext *C, ARegion *ar)
344 {
345         View2D *v2d= &ar->v2d;
346         
347         drawnodespace(C, ar, v2d);
348 }
349
350
351 /* ************* dropboxes ************* */
352
353 static int node_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
354 {
355         if(drag->type==WM_DRAG_ID) {
356                 ID *id= (ID *)drag->poin;
357                 if( GS(id->name)==ID_IM )
358                         return 1;
359         }
360         else if(drag->type==WM_DRAG_PATH){
361                 if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
362                         return 1;
363         }
364         return 0;
365 }
366
367 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
368 {
369         ID *id= (ID *)drag->poin;
370         
371         if(id) {
372                 RNA_string_set(drop->ptr, "name", id->name+2);
373         }
374         if (drag->path[0]) {
375                 RNA_string_set(drop->ptr, "filepath", drag->path);
376         }
377 }
378
379 /* this region dropbox definition */
380 static void node_dropboxes(void)
381 {
382         ListBase *lb= WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
383         
384         WM_dropbox_add(lb, "NODE_OT_add_file", node_drop_poll, node_id_path_drop_copy);
385         
386 }
387
388 /* ************* end drop *********** */
389
390
391 /* add handlers, stuff you only do once or on area/region changes */
392 static void node_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
393 {
394         ED_region_header_init(ar);
395 }
396
397 static void node_header_area_draw(const bContext *C, ARegion *ar)
398 {
399         SpaceNode *snode= CTX_wm_space_node(C);
400         Scene *scene= CTX_data_scene(C);
401
402         /* find and set the context */
403         snode_set_context(snode, scene);
404
405         ED_region_header(C, ar);
406 }
407
408 /* used for header + main area */
409 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
410 {
411         /* context changes */
412         switch(wmn->category) {
413                 case NC_SPACE:
414                         if(wmn->data==ND_SPACE_NODE)
415                                 ED_region_tag_redraw(ar);
416                         break;
417                 case NC_SCREEN:
418                         if(wmn->data == ND_GPENCIL)     
419                                 ED_region_tag_redraw(ar);
420                         break;
421                 case NC_SCENE:
422                 case NC_MATERIAL:
423                 case NC_TEXTURE:
424                 case NC_NODE:
425                         ED_region_tag_redraw(ar);
426                         break;
427                 case NC_ID:
428                         if(wmn->action == NA_RENAME)
429                                 ED_region_tag_redraw(ar);
430                         break;
431         }
432 }
433
434 const char *node_context_dir[] = {"selected_nodes", NULL};
435
436 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
437 {
438         SpaceNode *snode= CTX_wm_space_node(C);
439         
440         if(CTX_data_dir(member)) {
441                 CTX_data_dir_set(result, node_context_dir);
442                 return 1;
443         }
444         else if(CTX_data_equals(member, "selected_nodes")) {
445                 bNode *node;
446                 
447                 for(next_node(snode->edittree); (node=next_node(NULL));) {
448                         if(node->flag & NODE_SELECT) {
449                                 CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
450                         }
451                 }
452                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
453                 return 1;
454         }
455         
456         return 0;
457 }
458
459 /* only called once, from space/spacetypes.c */
460 void ED_spacetype_node(void)
461 {
462         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
463         ARegionType *art;
464         
465         st->spaceid= SPACE_NODE;
466         strncpy(st->name, "Node", BKE_ST_MAXNAME);
467         
468         st->new= node_new;
469         st->free= node_free;
470         st->init= node_init;
471         st->duplicate= node_duplicate;
472         st->operatortypes= node_operatortypes;
473         st->keymap= node_keymap;
474         st->listener= node_area_listener;
475         st->refresh= node_area_refresh;
476         st->context= node_context;
477         st->dropboxes = node_dropboxes;
478         
479         /* regions: main window */
480         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
481         art->regionid = RGN_TYPE_WINDOW;
482         art->init= node_main_area_init;
483         art->draw= node_main_area_draw;
484         art->listener= node_region_listener;
485         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
486
487         BLI_addhead(&st->regiontypes, art);
488         
489         /* regions: header */
490         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
491         art->regionid = RGN_TYPE_HEADER;
492         art->prefsizey= HEADERY;
493         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
494         art->listener= node_region_listener;
495         art->init= node_header_area_init;
496         art->draw= node_header_area_draw;
497         
498         BLI_addhead(&st->regiontypes, art);
499
500         node_menus_register();
501         
502         /* regions: listview/buttons */
503         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
504         art->regionid = RGN_TYPE_UI;
505         art->prefsizex= 180; // XXX
506         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
507         art->listener= node_region_listener;
508         art->init= node_buttons_area_init;
509         art->draw= node_buttons_area_draw;
510         BLI_addhead(&st->regiontypes, art);
511         
512         node_buttons_register(art);
513         
514         BKE_spacetype_register(st);
515 }
516