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