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