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