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