patch [#20342] Notifier patch for modifier rename and particle system rename
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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                         if(wmn->data==ND_NODES)
174                                 ED_area_tag_refresh(sa);
175                         break;
176                 case NC_WM:
177                         if(wmn->data==ND_FILEREAD)
178                                 ED_area_tag_refresh(sa);
179                         break;
180                         
181                 /* future: add ID checks? */
182                 case NC_MATERIAL:
183                         if(wmn->data==ND_SHADING)
184                                 ED_area_tag_refresh(sa);
185                         else if(wmn->data==ND_SHADING_DRAW)
186                                 ED_area_tag_refresh(sa);
187                         break;
188                 case NC_TEXTURE:
189                         if(wmn->data==ND_NODES)
190                                 ED_area_tag_refresh(sa);
191                         break;
192                 case NC_TEXT:
193                         /* pynodes */
194                         if(wmn->data==ND_SHADING)
195                                 ED_area_tag_refresh(sa);
196                         break;
197                 case NC_SPACE:
198                         if(wmn->data==ND_SPACE_NODE)
199                                 ED_area_tag_refresh(sa);
200                         else if(wmn->data==ND_SPACE_NODE_VIEW)
201                                 ED_area_tag_redraw(sa);
202                         break;
203                 case NC_NODE:
204                         ED_area_tag_refresh(sa);
205                         break;
206         }
207 }
208
209 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
210 {
211         /* default now: refresh node is starting preview */
212         SpaceNode *snode= sa->spacedata.first;
213
214         snode_set_context(snode, CTX_data_scene(C));
215         
216         if(snode->nodetree) {
217                 if(snode->treetype==NTREE_SHADER) {
218                         Material *ma= (Material *)snode->id;
219                         if(ma->use_nodes)
220                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
221                 }
222                 else if(snode->treetype==NTREE_COMPOSIT) {
223                         Scene *scene= (Scene *)snode->id;
224                         if(scene->use_nodes)
225                                 snode_composite_job(C, sa);
226                 }
227                 else if(snode->treetype==NTREE_TEXTURE) {
228                         Tex *tex= (Tex *)snode->id;
229                         if(tex->use_nodes) {
230                                 ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
231                         }
232                 }
233         }
234 }
235
236 static SpaceLink *node_duplicate(SpaceLink *sl)
237 {
238         SpaceNode *snoden= MEM_dupallocN(sl);
239         
240         /* clear or remove stuff from old */
241         snoden->nodetree= NULL;
242         
243         return (SpaceLink *)snoden;
244 }
245
246
247 /* add handlers, stuff you only do once or on area/region changes */
248 static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
249 {
250         wmKeyMap *keymap;
251
252         ED_region_panels_init(wm, ar);
253         
254         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
255         WM_event_add_keymap_handler(&ar->handlers, keymap);
256 }
257
258 static void node_buttons_area_draw(const bContext *C, ARegion *ar)
259 {
260         ED_region_panels(C, ar, 1, NULL, -1);
261 }
262
263 /* Initialise main area, setting handlers. */
264 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
265 {
266         wmKeyMap *keymap;
267         
268         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
269         
270         /* own keymaps */
271         keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
272         WM_event_add_keymap_handler(&ar->handlers, keymap);
273         
274         keymap= WM_keymap_find(wm->defaultconf, "Node", SPACE_NODE, 0);
275         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
276 }
277
278 static void node_main_area_draw(const bContext *C, ARegion *ar)
279 {
280         View2D *v2d= &ar->v2d;
281         
282         drawnodespace(C, ar, v2d);
283 }
284
285 /* add handlers, stuff you only do once or on area/region changes */
286 static void node_header_area_init(wmWindowManager *wm, ARegion *ar)
287 {
288         ED_region_header_init(ar);
289 }
290
291 static void node_header_area_draw(const bContext *C, ARegion *ar)
292 {
293         SpaceNode *snode= CTX_wm_space_node(C);
294         Scene *scene= CTX_data_scene(C);
295
296     /* find and set the context */
297         snode_set_context(snode, scene);
298
299         ED_region_header(C, ar);
300 }
301
302 /* used for header + main area */
303 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
304 {
305         /* context changes */
306         switch(wmn->category) {
307                 case NC_SPACE:
308                         if(wmn->data==ND_SPACE_NODE)
309                                 ED_region_tag_redraw(ar);
310                         break;
311                 case NC_SCENE:
312                 case NC_MATERIAL:
313                 case NC_TEXTURE:
314                 case NC_NODE:
315                         ED_region_tag_redraw(ar);
316                         break;
317                 case NC_ID:
318                         if(wmn->action == NA_RENAME)
319                                 ED_region_tag_redraw(ar);
320                         break;
321         }
322 }
323
324 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
325 {
326         SpaceNode *snode= CTX_wm_space_node(C);
327         
328         if(CTX_data_dir(member)) {
329                 static const char *dir[] = {"selected_nodes", NULL};
330                 CTX_data_dir_set(result, dir);
331                 return 1;
332         }
333         else if(CTX_data_equals(member, "selected_nodes")) {
334                 bNode *node;
335                 
336                 for(next_node(snode->edittree); (node=next_node(NULL));) {
337                         if(node->flag & SELECT) {
338                                 CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
339                         }
340                 }
341                 return 1;
342         }
343         
344         return 0;
345 }
346
347 /* only called once, from space/spacetypes.c */
348 void ED_spacetype_node(void)
349 {
350         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
351         ARegionType *art;
352         
353         st->spaceid= SPACE_NODE;
354         
355         st->new= node_new;
356         st->free= node_free;
357         st->init= node_init;
358         st->duplicate= node_duplicate;
359         st->operatortypes= node_operatortypes;
360         st->keymap= node_keymap;
361         st->listener= node_area_listener;
362         st->refresh= node_area_refresh;
363         st->context= node_context;
364         
365         /* regions: main window */
366         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
367         art->regionid = RGN_TYPE_WINDOW;
368         art->init= node_main_area_init;
369         art->draw= node_main_area_draw;
370         art->listener= node_region_listener;
371         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
372
373         BLI_addhead(&st->regiontypes, art);
374         
375         /* regions: header */
376         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
377         art->regionid = RGN_TYPE_HEADER;
378         art->minsizey= HEADERY;
379         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
380         art->listener= node_region_listener;
381         art->init= node_header_area_init;
382         art->draw= node_header_area_draw;
383         
384         BLI_addhead(&st->regiontypes, art);
385
386         node_menus_register(art);
387         
388         /* regions: listview/buttons */
389         art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
390         art->regionid = RGN_TYPE_UI;
391         art->minsizex= 180; // XXX
392         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
393         art->listener= node_region_listener;
394         art->init= node_buttons_area_init;
395         art->draw= node_buttons_area_draw;
396         BLI_addhead(&st->regiontypes, art);
397         
398         node_buttons_register(art);
399         
400         BKE_spacetype_register(st);
401 }
402