4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
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"
40 #include "MEM_guardedalloc.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_arithb.h"
46 #include "BKE_colortools.h"
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
50 #include "ED_previewrender.h"
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
59 #include "UI_interface.h"
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
63 #include "RNA_access.h"
65 #include "node_intern.h" // own include
67 /* ******************** default callbacks for node space ***************** */
69 static SpaceLink *node_new(const bContext *C)
74 snode= MEM_callocN(sizeof(SpaceNode), "initnode");
75 snode->spacetype= SPACE_NODE;
78 ar= MEM_callocN(sizeof(ARegion), "header for node");
80 BLI_addtail(&snode->regionbase, ar);
81 ar->regiontype= RGN_TYPE_HEADER;
82 ar->alignment= RGN_ALIGN_BOTTOM;
86 ar= MEM_callocN(sizeof(ARegion), "nodetree area for node");
88 BLI_addtail(&snode->regionbase, ar);
89 ar->regiontype= RGN_TYPE_CHANNELS;
90 ar->alignment= RGN_ALIGN_LEFT;
92 //ar->v2d.scroll = (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
96 ar= MEM_callocN(sizeof(ARegion), "main area for node");
98 BLI_addtail(&snode->regionbase, ar);
99 ar->regiontype= RGN_TYPE_WINDOW;
101 ar->v2d.tot.xmin= -10.0f;
102 ar->v2d.tot.ymin= -10.0f;
103 ar->v2d.tot.xmax= 512.0f;
104 ar->v2d.tot.ymax= 512.0f;
106 ar->v2d.cur.xmin= 0.0f;
107 ar->v2d.cur.ymin= 0.0f;
108 ar->v2d.cur.xmax= 512.0f;
109 ar->v2d.cur.ymax= 512.0f;
111 ar->v2d.min[0]= 1.0f;
112 ar->v2d.min[1]= 1.0f;
114 ar->v2d.max[0]= 32000.0f;
115 ar->v2d.max[1]= 32000.0f;
117 ar->v2d.minzoom= 0.5f;
118 ar->v2d.maxzoom= 1.21f;
120 ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
121 ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
124 return (SpaceLink *)snode;
127 /* not spacelink itself */
128 static void node_free(SpaceLink *sl)
130 // SpaceNode *snode= (SpaceNode*) sl;
132 // XXX if(snode->gpd) free_gpencil_data(snode->gpd);
136 /* spacetype; init callback */
137 static void node_init(struct wmWindowManager *wm, ScrArea *sa)
142 static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
145 /* preview renders */
146 switch(wmn->category) {
148 if(wmn->data==ND_NODES)
149 ED_area_tag_refresh(sa);
152 if(wmn->data==ND_FILEREAD)
153 ED_area_tag_refresh(sa);
157 /* future: add ID check? */
158 if(wmn->data==ND_SHADING)
159 ED_area_tag_refresh(sa);
164 static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
166 /* default now: refresh node is starting preview */
167 SpaceNode *snode= sa->spacedata.first;
169 if(snode->nodetree) {
170 if(snode->treetype==NTREE_SHADER) {
171 Material *ma= (Material *)snode->id;
173 ED_preview_shader_job(C, sa, snode->id, NULL, 100, 100);
175 else if(snode->treetype==NTREE_COMPOSIT) {
176 Scene *scene= (Scene *)snode->id;
178 snode_composite_job(C, sa);
183 static SpaceLink *node_duplicate(SpaceLink *sl)
185 SpaceNode *snoden= MEM_dupallocN(sl);
187 /* clear or remove stuff from old */
188 snoden->nodetree= NULL;
189 // XXX snoden->gpd= gpencil_data_duplicate(snode->gpd);
191 return (SpaceLink *)snoden;
195 static void node_channel_area_init(wmWindowManager *wm, ARegion *ar)
197 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
200 static void node_channel_area_draw(const bContext *C, ARegion *ar)
202 View2D *v2d= &ar->v2d;
203 View2DScrollers *scrollers;
206 /* clear and setup matrix */
207 UI_GetThemeColor3fv(TH_BACK, col);
208 glClearColor(col[0], col[1], col[2], 0.0);
209 glClear(GL_COLOR_BUFFER_BIT);
211 UI_view2d_view_ortho(C, v2d);
215 /* reset view matrix */
216 UI_view2d_view_restore(C);
219 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
220 UI_view2d_scrollers_draw(C, v2d, scrollers);
221 UI_view2d_scrollers_free(scrollers);
225 /* Initialise main area, setting handlers. */
226 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
230 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
233 keymap= WM_keymap_listbase(wm, "Node", SPACE_NODE, 0); /* XXX weak? */
234 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
237 static void node_main_area_draw(const bContext *C, ARegion *ar)
239 View2D *v2d= &ar->v2d;
241 drawnodespace(C, ar, v2d);
244 /* add handlers, stuff you only do once or on area/region changes */
245 static void node_header_area_init(wmWindowManager *wm, ARegion *ar)
247 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
250 static void node_header_area_draw(const bContext *C, ARegion *ar)
255 if(ED_screen_area_active(C))
256 UI_GetThemeColor3fv(TH_HEADER, col);
258 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
260 glClearColor(col[0], col[1], col[2], 0.0);
261 glClear(GL_COLOR_BUFFER_BIT);
263 /* set view2d view matrix for scrolling (without scrollers) */
264 UI_view2d_view_ortho(C, &ar->v2d);
266 node_header_buttons(C, ar);
268 /* restore view matrix? */
269 UI_view2d_view_restore(C);
272 /* used for header + main area */
273 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
275 /* context changes */
276 switch(wmn->category) {
278 ED_region_tag_redraw(ar);
281 ED_region_tag_redraw(ar);
286 static int node_context(const bContext *C, const char *member, bContextDataResult *result)
288 SpaceNode *snode= CTX_wm_space_node(C);
290 if(CTX_data_dir(member)) {
291 static const char *dir[] = {"selected_nodes", NULL};
292 CTX_data_dir_set(result, dir);
295 else if(CTX_data_equals(member, "selected_nodes")) {
298 for(next_node(snode->edittree); (node=next_node(NULL));) {
299 if(node->flag & SELECT) {
300 CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
309 /* only called once, from space/spacetypes.c */
310 void ED_spacetype_node(void)
312 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype node");
315 st->spaceid= SPACE_NODE;
320 st->duplicate= node_duplicate;
321 st->operatortypes= node_operatortypes;
322 st->keymap= node_keymap;
323 st->listener= node_area_listener;
324 st->refresh= node_area_refresh;
325 st->context= node_context;
327 /* regions: main window */
328 art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
329 art->regionid = RGN_TYPE_WINDOW;
330 art->init= node_main_area_init;
331 art->draw= node_main_area_draw;
332 art->listener= node_region_listener;
333 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
335 BLI_addhead(&st->regiontypes, art);
337 /* regions: header */
338 art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
339 art->regionid = RGN_TYPE_HEADER;
340 art->minsizey= HEADERY;
341 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
342 art->listener= node_region_listener;
343 art->init= node_header_area_init;
344 art->draw= node_header_area_draw;
346 BLI_addhead(&st->regiontypes, art);
349 /* regions: channels */
350 art= MEM_callocN(sizeof(ARegionType), "spacetype node region");
351 art->regionid = RGN_TYPE_CHANNELS;
353 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
355 art->init= node_channel_area_init;
356 art->draw= node_channel_area_draw;
358 BLI_addhead(&st->regiontypes, art);
362 BKE_spacetype_register(st);