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