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