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