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