d9a4dda9d6e03009eab91eee12095536d062c3e0
[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
43 #include "BKE_context.h"
44 #include "BKE_screen.h"
45
46 #include "ED_render.h"
47 #include "ED_screen.h"
48
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "UI_resources.h"
54 #include "UI_view2d.h"
55
56 #include "RNA_access.h"
57
58 #include "node_intern.h"        // own include
59
60 /* ******************** manage regions ********************* */
61
62 ARegion *node_has_buttons_region(ScrArea *sa)
63 {
64         ARegion *ar, *arnew;
65         
66         for(ar= sa->regionbase.first; ar; ar= ar->next)
67                 if(ar->regiontype==RGN_TYPE_UI)
68                         return ar;
69         
70         /* add subdiv level; after header */
71         for(ar= sa->regionbase.first; ar; ar= ar->next)
72                 if(ar->regiontype==RGN_TYPE_HEADER)
73                         break;
74         
75         /* is error! */
76         if(ar==NULL) return NULL;
77         
78         arnew= MEM_callocN(sizeof(ARegion), "buttons for node");
79         
80         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
81         arnew->regiontype= RGN_TYPE_UI;
82         arnew->alignment= RGN_ALIGN_RIGHT;
83         
84         arnew->flag = RGN_FLAG_HIDDEN;
85         
86         return arnew;
87 }
88
89 /* ******************** default callbacks for node space ***************** */
90
91 static SpaceLink *node_new(const bContext *C)
92 {
93         ARegion *ar;
94         SpaceNode *snode;
95         
96         snode= MEM_callocN(sizeof(SpaceNode), "initnode");
97         snode->spacetype= SPACE_NODE;   
98         
99         /* header */
100         ar= MEM_callocN(sizeof(ARegion), "header for node");
101         
102         BLI_addtail(&snode->regionbase, ar);
103         ar->regiontype= RGN_TYPE_HEADER;
104         ar->alignment= RGN_ALIGN_BOTTOM;
105         
106         /* buttons/list view */
107         ar= MEM_callocN(sizeof(ARegion), "buttons for node");
108         
109         BLI_addtail(&snode->regionbase, ar);
110         ar->regiontype= RGN_TYPE_UI;
111         ar->alignment= RGN_ALIGN_RIGHT;
112         ar->flag = RGN_FLAG_HIDDEN;
113         
114         /* main area */
115         ar= MEM_callocN(sizeof(ARegion), "main area for node");
116         
117         BLI_addtail(&snode->regionbase, ar);
118         ar->regiontype= RGN_TYPE_WINDOW;
119         
120         ar->v2d.tot.xmin=  -10.0f;
121         ar->v2d.tot.ymin=  -10.0f;
122         ar->v2d.tot.xmax= 512.0f;
123         ar->v2d.tot.ymax= 512.0f;
124         
125         ar->v2d.cur.xmin=  0.0f;
126         ar->v2d.cur.ymin=  0.0f;
127         ar->v2d.cur.xmax= 512.0f;
128         ar->v2d.cur.ymax= 512.0f;
129         
130         ar->v2d.min[0]= 1.0f;
131         ar->v2d.min[1]= 1.0f;
132         
133         ar->v2d.max[0]= 32000.0f;
134         ar->v2d.max[1]= 32000.0f;
135         
136         ar->v2d.minzoom= 0.09f;
137         ar->v2d.maxzoom= 2.31f;
138         
139         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
140         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
141         ar->v2d.keeptot= 0;
142         
143         return (SpaceLink *)snode;
144 }
145
146 /* not spacelink itself */
147 static void node_free(SpaceLink *sl)
148 {       
149         
150 }
151
152
153 /* spacetype; init callback */
154 static void node_init(struct wmWindowManager *wm, ScrArea *sa)
155 {
156
157 }
158
159 static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
160 {
161         
162         /* preview renders */
163         switch(wmn->category) {
164                 case NC_SCENE:
165                         switch (wmn->data) {
166                                 case ND_NODES:
167                                 case ND_FRAME:
168                                         ED_area_tag_refresh(sa);
169                                         break;
170                         }
171                         break;
172                 case NC_WM:
173                         if(wmn->data==ND_FILEREAD)
174                                 ED_area_tag_refresh(sa);
175                         break;
176                         
177                 /* future: add ID checks? */
178                 case NC_MATERIAL:
179                         if(wmn->data==ND_SHADING)
180                                 ED_area_tag_refresh(sa);
181                         else if(wmn->data==ND_SHADING_DRAW)
182                                 ED_area_tag_refresh(sa);
183                         break;
184                 case NC_TEXTURE:
185                         if(wmn->data==ND_NODES)
186                                 ED_area_tag_refresh(sa);
187                         break;
188                 case NC_TEXT:
189                         /* pynodes */
190                         if(wmn->data==ND_SHADING)
191                                 ED_area_tag_refresh(sa);
192                         break;
193                 case NC_SPACE:
194                         if(wmn->data==ND_SPACE_NODE)
195                                 ED_area_tag_refresh(sa);
196                         else if(wmn->data==ND_SPACE_NODE_VIEW)
197                                 ED_area_tag_redraw(sa);
198                         break;
199                 case NC_NODE:
200                         if (wmn->action == NA_EDITED)
201                                 ED_area_tag_refresh(sa);
202                         break;
203         }
204 }
205
206 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
207 {
208         /* default now: refresh node is starting preview */
209         SpaceNode *snode= sa->spacedata.first;
210
211         snode_set_context(snode, CTX_data_scene(C));
212         
213         if(snode->nodetree) {
214                 if(snode->treetype==NTREE_SHADER) {
215                         Material *ma= (Material *)snode->id;
216                         if(ma->use_nodes)
217                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
218                 }
219                 else if(snode->treetype==NTREE_COMPOSIT) {
220                         Scene *scene= (Scene *)snode->id;
221                         if(scene->use_nodes)
222                                 snode_composite_job(C, sa);
223                 }
224                 else if(snode->treetype==NTREE_TEXTURE) {
225                         Tex *tex= (Tex *)snode->id;
226                         if(tex->use_nodes) {
227                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
228                         }
229                 }
230         }
231 }
232
233 static SpaceLink *node_duplicate(SpaceLink *sl)
234 {
235         SpaceNode *snoden= MEM_dupallocN(sl);
236         
237         /* clear or remove stuff from old */
238         snoden->nodetree= NULL;
239         
240         return (SpaceLink *)snoden;
241 }
242
243
244 /* add handlers, stuff you only do once or on area/region changes */
245 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
246 {
247         wmKeyMap *keymap;
248
249         ED_region_panels_init(wm, ar);
250         
251         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
252         WM_event_add_keymap_handler(&ar->handlers, keymap);
253 }
254
255 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
256 {
257         ED_region_panels(C, ar, 1, NULL, -1);
258 }
259
260 /* Initialise main area, setting handlers. */
261 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
262 {
263         wmKeyMap *keymap;
264         ListBase *lb;
265         
266         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
267         
268         /* own keymaps */
269         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
270         WM_event_add_keymap_handler(&ar->handlers, keymap);
271         
272         keymap= WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
273         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
274         
275         /* add drop boxes */
276         lb = WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
277         
278         WM_event_add_dropbox_handler(&ar->handlers, lb);
279 }
280
281 static void node_main_area_draw(const bContext *C, ARegion *ar)
282 {
283         View2D *v2d= &ar->v2d;
284         
285         drawnodespace(C, ar, v2d);
286 }
287
288
289 /* ************* dropboxes ************* */
290
291 static int node_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
292 {
293         if(drag->type==WM_DRAG_ID) {
294                 ID *id= (ID *)drag->poin;
295                 if( GS(id->name)==ID_IM )
296                         return 1;
297         }
298         else if(drag->type==WM_DRAG_PATH){
299                 if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
300                         return 1;
301         }
302         return 0;
303 }
304
305 static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
306 {
307         ID *id= (ID *)drag->poin;
308         
309         if(id) {
310                 RNA_string_set(drop->ptr, "name", id->name+2);
311         }
312         if (drag->path[0]) {
313                 RNA_string_set(drop->ptr, "filepath", drag->path);
314         }
315 }
316
317 /* this region dropbox definition */
318 static void node_dropboxes(void)
319 {
320         ListBase *lb= WM_dropboxmap_find("Node Editor", SPACE_NODE, RGN_TYPE_WINDOW);
321         
322         WM_dropbox_add(lb, "NODE_OT_add_file", node_drop_poll, node_id_path_drop_copy);
323         
324 }
325
326 /* ************* end drop *********** */
327
328
329 /* add handlers, stuff you only do once or on area/region changes */
330 static void node_header_area_init(wmWindowManager *wm, ARegion *ar)
331 {
332         ED_region_header_init(ar);
333 }
334
335 static void node_header_area_draw(const bContext *C, ARegion *ar)
336 {
337         SpaceNode *snode= CTX_wm_space_node(C);
338         Scene *scene= CTX_data_scene(C);
339
340         /* find and set the context */
341         snode_set_context(snode, scene);
342
343         ED_region_header(C, ar);
344 }
345
346 /* used for header + main area */
347 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
348 {
349         /* context changes */
350         switch(wmn->category) {
351                 case NC_SPACE:
352                         if(wmn->data==ND_SPACE_NODE)
353                                 ED_region_tag_redraw(ar);
354                         break;
355                 case NC_SCREEN:
356                         if(wmn->data == ND_GPENCIL)     
357                                 ED_region_tag_redraw(ar);
358                         break;
359                 case NC_SCENE:
360                 case NC_MATERIAL:
361                 case NC_TEXTURE:
362                 case NC_NODE:
363                         ED_region_tag_redraw(ar);
364                         break;
365                 case NC_ID:
366                         if(wmn->action == NA_RENAME)
367                                 ED_region_tag_redraw(ar);
368                         break;
369         }
370 }
371
372 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
373 {
374         SpaceNode *snode= CTX_wm_space_node(C);
375         
376         if(CTX_data_dir(member)) {
377                 static const char *dir[] = {"selected_nodes", NULL};
378                 CTX_data_dir_set(result, dir);
379                 return 1;
380         }
381         else if(CTX_data_equals(member, "selected_nodes")) {
382                 bNode *node;
383                 
384                 for(next_node(snode->edittree); (node=next_node(NULL));) {
385                         if(node->flag & SELECT) {
386                                 CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
387                         }
388                 }
389                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
390                 return 1;
391         }
392         
393         return 0;
394 }
395
396 /* only called once, from space/spacetypes.c */
397 void ED_spacetype_node(void)
398 {
399         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
400         ARegionType *art;
401         
402         st->spaceid= SPACE_NODE;
403         strncpy(st->name, "Node", BKE_ST_MAXNAME);
404         
405         st->new= node_new;
406         st->free= node_free;
407         st->init= node_init;
408         st->duplicate= node_duplicate;
409         st->operatortypes= node_operatortypes;
410         st->keymap= node_keymap;
411         st->listener= node_area_listener;
412         st->refresh= node_area_refresh;
413         st->context= node_context;
414         st->dropboxes = node_dropboxes;
415         
416         /* regions: main window */
417         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
418         art->regionid = RGN_TYPE_WINDOW;
419         art->init= node_main_area_init;
420         art->draw= node_main_area_draw;
421         art->listener= node_region_listener;
422         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
423
424         BLI_addhead(&st->regiontypes, art);
425         
426         /* regions: header */
427         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
428         art->regionid = RGN_TYPE_HEADER;
429         art->prefsizey= HEADERY;
430         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
431         art->listener= node_region_listener;
432         art->init= node_header_area_init;
433         art->draw= node_header_area_draw;
434         
435         BLI_addhead(&st->regiontypes, art);
436
437         node_menus_register(art);
438         
439         /* regions: listview/buttons */
440         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
441         art->regionid = RGN_TYPE_UI;
442         art->prefsizex= 180; // XXX
443         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
444         art->listener= node_region_listener;
445         art->init= node_buttons_area_init;
446         art->draw= node_buttons_area_draw;
447         BLI_addhead(&st->regiontypes, art);
448         
449         node_buttons_register(art);
450         
451         BKE_spacetype_register(st);
452 }
453