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