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