9f2e0b71b0ba1991cd4ce0878614ac5d73d9b7b5
[blender-staging.git] / source / blender / editors / space_node / drawnode.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) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Bob Holcomb, Thomas Dinges
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_node/drawnode.c
29  *  \ingroup spnode
30  *  \brief lower level node drawing for nodes (boarders, headers etc), also node layout.
31  */
32
33 #include "BLI_blenlib.h"
34 #include "BLI_math.h"
35
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_space_types.h"
39 #include "DNA_screen_types.h"
40
41 #include "BKE_context.h"
42 #include "BKE_curve.h"
43 #include "BKE_image.h"
44 #include "BKE_main.h"
45 #include "BKE_node.h"
46 #include "BKE_scene.h"
47 #include "BKE_tracking.h"
48
49 #include "BLF_api.h"
50 #include "BLF_translation.h"
51
52 #include "NOD_texture.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56
57 #include "BLF_translation.h"
58 #include "MEM_guardedalloc.h"
59
60 #include "RNA_access.h"
61 #include "RNA_define.h"
62 #include "RNA_enum_types.h"
63
64 #include "ED_node.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "UI_resources.h"
70
71 #include "IMB_colormanagement.h"
72 #include "IMB_imbuf.h"
73 #include "IMB_imbuf_types.h"
74
75 #include "node_intern.h"  /* own include */
76 #include "NOD_composite.h"
77 #include "NOD_shader.h"
78 #include "NOD_texture.h"
79
80 /* ****************** MENU FUNCTIONS ***************** */
81
82 static void node_add_menu_class(bContext *C, uiLayout *layout, void *arg_nodeclass)
83 {
84         Scene *scene = CTX_data_scene(C);
85         SpaceNode *snode = CTX_wm_space_node(C);
86         bNodeTree *ntree;
87         int nodeclass = GET_INT_FROM_POINTER(arg_nodeclass);
88         int event, compatibility = 0;
89         
90         ntree = snode->nodetree;
91         
92         if (!ntree) {
93                 uiItemS(layout);
94                 return;
95         }
96
97         if (ntree->type == NTREE_SHADER) {
98                 if (BKE_scene_use_new_shading_nodes(scene))
99                         compatibility = NODE_NEW_SHADING;
100                 else
101                         compatibility = NODE_OLD_SHADING;
102         }
103         
104         if (nodeclass == NODE_CLASS_GROUP) {
105                 Main *bmain = CTX_data_main(C);
106                 bNodeTree *ngroup;
107                 const char *ngroup_type, *node_type;
108                 PointerRNA ptr;
109                 
110                 NODE_TYPES_BEGIN(ntype)
111                         if (ntype->nclass != nodeclass || !ntype->ui_name)
112                                 continue;
113                         if (!ntype->poll(ntype, ntree))
114                                 continue;
115                         
116                         switch (ntree->type) {
117                                 case NTREE_COMPOSIT:
118                                         ngroup_type = "CompositorNodeTree";
119                                         node_type = "CompositorNodeGroup";
120                                         break;
121                                 case NTREE_SHADER:
122                                         ngroup_type = "ShaderNodeTree";
123                                         node_type = "ShaderNodeGroup";
124                                         break;
125                                 case NTREE_TEXTURE:
126                                         ngroup_type = "TextureNodeTree";
127                                         node_type = "TextureNodeGroup";
128                                         break;
129                         }
130                         
131                         ptr = uiItemFullO(layout, "NODE_OT_group_make", "New Group", ntype->ui_icon, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
132                         RNA_string_set(&ptr, "node_type", node_type);
133                         
134                         uiItemS(layout);
135
136                         for (ngroup = bmain->nodetree.first, event = 0; ngroup; ngroup = ngroup->id.next, ++event) {
137                                 /* only use group trees of the right type */
138                                 if (STRNEQ(ngroup->idname, ngroup_type))
139                                         continue;
140                                 if (!nodeGroupPoll(ntree, ngroup))
141                                         continue;
142                                 
143                                 ptr = uiItemFullO(layout, "NODE_OT_add_group_node", ngroup->id.name + 2, ntype->ui_icon, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
144                                 RNA_string_set(&ptr, "type", ntype->idname);
145                                 RNA_string_set(&ptr, "grouptree", ngroup->id.name + 2);
146                         }
147                 NODE_TYPES_END
148         }
149         else if (nodeclass == NODE_DYNAMIC) {
150                 /* disabled */
151         }
152         else {
153                 PointerRNA ptr;
154                 
155                 NODE_TYPES_BEGIN(ntype)
156                         if (ntype->nclass != nodeclass || !ntype->ui_name)
157                                 continue;
158                         if (!ntype->poll(ntype, ntree))
159                                 continue;
160                         if (compatibility && (ntype->compatibility & compatibility) == 0)
161                                 continue;
162                         
163                         ptr = uiItemFullO(layout, "NODE_OT_add_node", IFACE_(ntype->ui_name), ntype->ui_icon, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
164                         RNA_string_set(&ptr, "type", ntype->idname);
165                 NODE_TYPES_END
166         }
167 }
168
169 static void node_add_menu_foreach_class_cb(void *calldata, int nclass, const char *name)
170 {
171         uiLayout *layout = calldata;
172         uiItemMenuF(layout, IFACE_(name), 0, node_add_menu_class, SET_INT_IN_POINTER(nclass));
173 }
174
175 static void node_add_menu_default(const bContext *C, uiLayout *layout, bNodeTree *ntree)
176 {
177         Scene *scene = CTX_data_scene(C);
178         
179         if (ntree->typeinfo->foreach_nodeclass)
180                 ntree->typeinfo->foreach_nodeclass(scene, layout, node_add_menu_foreach_class_cb);
181 }
182
183 /* ****************** SOCKET BUTTON DRAW FUNCTIONS ***************** */
184
185 static void node_socket_button_label(bContext *UNUSED(C), uiLayout *layout, PointerRNA *ptr, PointerRNA *UNUSED(node_ptr))
186 {
187         bNodeSocket *sock = (bNodeSocket *)ptr->data;
188         uiItemL(layout, sock->name, 0);
189 }
190
191 static void node_draw_input_default(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *node_ptr, int linked)
192 {
193         bNodeSocket *sock = (bNodeSocket *)ptr->data;
194         if (linked || (sock->flag & SOCK_HIDE_VALUE))
195                 node_socket_button_label(C, layout, ptr, node_ptr);
196         else
197                 sock->typeinfo->draw(C, layout, ptr, node_ptr);
198 }
199
200 static void node_draw_output_default(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *node_ptr, int UNUSED(linked))
201 {
202         node_socket_button_label(C, layout, ptr, node_ptr);
203 }
204
205
206 /* ****************** BASE DRAW FUNCTIONS FOR NEW OPERATOR NODES ***************** */
207
208 #if 0 /* UNUSED */
209 static void node_draw_socket_new(bNodeSocket *sock, float size)
210 {
211         float x = sock->locx, y = sock->locy;
212         
213         /* 16 values of sin function */
214         static float si[16] = {
215                 0.00000000f, 0.39435585f, 0.72479278f, 0.93775213f,
216                 0.99871650f, 0.89780453f, 0.65137248f, 0.29936312f,
217                 -0.10116832f, -0.48530196f, -0.79077573f, -0.96807711f,
218                 -0.98846832f, -0.84864425f, -0.57126821f, -0.20129852f
219         };
220         /* 16 values of cos function */
221         static float co[16] = {
222                 1.00000000f, 0.91895781f, 0.68896691f, 0.34730525f,
223                 -0.05064916f, -0.44039415f, -0.75875812f, -0.95413925f,
224                 -0.99486932f, -0.87434661f, -0.61210598f, -0.25065253f,
225                 0.15142777f, 0.52896401f, 0.82076344f, 0.97952994f,
226         };
227         int a;
228         
229         glColor3ub(180, 180, 180);
230         
231         glBegin(GL_POLYGON);
232         for (a = 0; a < 16; a++)
233                 glVertex2f(x + size * si[a], y + size * co[a]);
234         glEnd();
235         
236         glColor4ub(0, 0, 0, 150);
237         glEnable(GL_BLEND);
238         glEnable(GL_LINE_SMOOTH);
239         glBegin(GL_LINE_LOOP);
240         for (a = 0; a < 16; a++)
241                 glVertex2f(x + size * si[a], y + size * co[a]);
242         glEnd();
243         glDisable(GL_LINE_SMOOTH);
244         glDisable(GL_BLEND);
245 }
246 #endif
247
248 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
249
250 static void node_buts_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
251 {
252         bNode *node = ptr->data;
253         /* first output stores value */
254         bNodeSocket *output = node->outputs.first;
255         PointerRNA sockptr;
256         RNA_pointer_create(ptr->id.data, &RNA_NodeSocket, output, &sockptr);
257         
258         uiItemR(layout, &sockptr, "default_value", 0, "", ICON_NONE);
259 }
260
261 static void node_buts_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
262 {
263         bNode *node = ptr->data;
264         /* first output stores value */
265         bNodeSocket *output = node->outputs.first;
266         PointerRNA sockptr;
267         uiLayout *col;
268         RNA_pointer_create(ptr->id.data, &RNA_NodeSocket, output, &sockptr);
269         
270         col = uiLayoutColumn(layout, FALSE);
271         uiTemplateColorPicker(col, &sockptr, "default_value", 1, 0, 0, 0);
272         uiItemR(col, &sockptr, "default_value", 0, "", ICON_NONE);
273 }
274
275 static void node_buts_mix_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
276 {       
277         uiLayout *row, *col;
278
279         bNodeTree *ntree = (bNodeTree *)ptr->id.data;
280
281         col = uiLayoutColumn(layout, FALSE);
282         row = uiLayoutRow(col, TRUE);
283         uiItemR(row, ptr, "blend_type", 0, "", ICON_NONE);
284         if (ntree->type == NTREE_COMPOSIT)
285                 uiItemR(row, ptr, "use_alpha", 0, "", ICON_IMAGE_RGB_ALPHA);
286
287         uiItemR(col, ptr, "use_clamp", 0, NULL, ICON_NONE);
288 }
289
290 static void node_buts_time(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
291 {
292         uiLayout *row;
293 #if 0
294         /* XXX no context access here .. */
295         bNode *node = ptr->data;
296         CurveMapping *cumap = node->storage;
297         
298         if (cumap) {
299                 cumap->flag |= CUMA_DRAW_CFRA;
300                 if (node->custom1 < node->custom2)
301                         cumap->sample[0] = (float)(CFRA - node->custom1) / (float)(node->custom2 - node->custom1);
302         }
303 #endif
304
305         uiTemplateCurveMapping(layout, ptr, "curve", 's', 0, 0);
306
307         row = uiLayoutRow(layout, TRUE);
308         uiItemR(row, ptr, "frame_start", 0, IFACE_("Sta"), ICON_NONE);
309         uiItemR(row, ptr, "frame_end", 0, IFACE_("End"), ICON_NONE);
310 }
311
312 static void node_buts_colorramp(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
313 {
314         uiTemplateColorRamp(layout, ptr, "color_ramp", 0);
315 }
316
317 static void node_buts_curvevec(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
318 {
319         uiTemplateCurveMapping(layout, ptr, "mapping", 'v', 0, 0);
320 }
321
322 #define SAMPLE_FLT_ISNONE FLT_MAX
323 static float _sample_col[4] = {SAMPLE_FLT_ISNONE};  /* bad bad, 2.5 will do better?... no it won't... */
324 void ED_node_sample_set(const float col[4])
325 {
326         if (col) {
327                 copy_v4_v4(_sample_col, col);
328         }
329         else {
330                 copy_v4_fl(_sample_col, SAMPLE_FLT_ISNONE);
331         }
332 }
333
334 static void node_buts_curvecol(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
335 {
336         bNode *node = ptr->data;
337         CurveMapping *cumap = node->storage;
338
339         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
340                 cumap->flag |= CUMA_DRAW_SAMPLE;
341                 copy_v3_v3(cumap->sample, _sample_col);
342         }
343         else {
344                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
345         }
346
347         uiTemplateCurveMapping(layout, ptr, "mapping", 'c', 0, 0);
348 }
349
350 static void node_buts_normal(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
351 {
352         bNode *node = ptr->data;
353         /* first output stores normal */
354         bNodeSocket *output = node->outputs.first;
355         PointerRNA sockptr;
356         RNA_pointer_create(ptr->id.data, &RNA_NodeSocket, output, &sockptr);
357         
358         uiItemR(layout, &sockptr, "default_value", 0, "", ICON_NONE);
359 }
360
361 #if 0 /* not used in 2.5x yet */
362 static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
363 {
364         Main *bmain = CTX_data_main(C);
365         bNodeTree *ntree = ntree_v;
366         bNode *node = node_v;
367         Tex *tex;
368         
369         if (node->menunr < 1) return;
370         
371         if (node->id) {
372                 node->id->us--;
373                 node->id = NULL;
374         }
375         tex = BLI_findlink(&bmain->tex, node->menunr - 1);
376
377         node->id = &tex->id;
378         id_us_plus(node->id);
379         BLI_strncpy(node->name, node->id->name + 2, sizeof(node->name));
380         
381         nodeSetActive(ntree, node);
382         
383         if (ntree->type == NTREE_TEXTURE)
384                 ntreeTexCheckCyclics(ntree);
385         
386         // allqueue(REDRAWBUTSSHADING, 0);
387         // allqueue(REDRAWNODE, 0);
388         NodeTagChanged(ntree, node); 
389         
390         node->menunr = 0;
391 }
392 #endif
393
394 static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
395 {
396         bNode *node = ptr->data;
397
398         short multi = (
399             node->id &&
400             ((Tex *)node->id)->use_nodes &&
401             (node->type != CMP_NODE_TEXTURE) &&
402             (node->type != TEX_NODE_TEXTURE)
403             );
404         
405         uiItemR(layout, ptr, "texture", 0, "", ICON_NONE);
406         
407         if (multi) {
408                 /* Number Drawing not optimal here, better have a list*/
409                 uiItemR(layout, ptr, "node_output", 0, "", ICON_NONE);
410         }
411 }
412
413 static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
414
415         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
416         uiItemR(layout, ptr, "use_clamp", 0, NULL, ICON_NONE);
417 }
418
419 static int node_resize_area_default(bNode *node, int x, int y)
420 {
421         if (node->flag & NODE_HIDDEN) {
422                 rctf totr = node->totr;
423                 /* right part of node */
424                 totr.xmin = node->totr.xmax - 20.0f;
425                 if (BLI_rctf_isect_pt(&totr, x, y))
426                         return NODE_RESIZE_RIGHT;
427                 else
428                         return 0;
429         }
430         else {
431                 const float size = 10.0f;
432                 rctf totr = node->totr;
433                 int dir = 0;
434                 
435                 if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
436                         dir |= NODE_RESIZE_RIGHT;
437                 if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
438                         dir |= NODE_RESIZE_LEFT;
439                 return dir;
440         }
441 }
442
443 /* ****************** BUTTON CALLBACKS FOR COMMON NODES ***************** */
444
445
446 static void node_uifunc_group(uiLayout *layout, bContext *C, PointerRNA *ptr)
447 {
448         uiTemplateIDBrowse(layout, C, ptr, "node_tree", NULL, NULL, NULL);
449 }
450
451 /* XXX Does a bounding box update by iterating over all children.
452  * Not ideal to do this in every draw call, but doing as transform callback doesn't work,
453  * since the child node totr rects are not updated properly at that point.
454  */
455 static void node_update_frame(const bContext *UNUSED(C), bNodeTree *ntree, bNode *node)
456 {
457         const float margin = 1.5f * U.widget_unit;
458         NodeFrame *data = (NodeFrame *)node->storage;
459         int bbinit;
460         bNode *tnode;
461         rctf rect, noderect;
462         float xmax, ymax;
463         
464         /* init rect from current frame size */
465         node_to_view(node, node->offsetx, node->offsety, &rect.xmin, &rect.ymax);
466         node_to_view(node, node->offsetx + node->width, node->offsety - node->height, &rect.xmax, &rect.ymin);
467         
468         /* frame can be resized manually only if shrinking is disabled or no children are attached */
469         data->flag |= NODE_FRAME_RESIZEABLE;
470         /* for shrinking bbox, initialize the rect from first child node */
471         bbinit = (data->flag & NODE_FRAME_SHRINK);
472         /* fit bounding box to all children */
473         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
474                 if (tnode->parent != node)
475                         continue;
476                 
477                 /* add margin to node rect */
478                 noderect = tnode->totr;
479                 noderect.xmin -= margin;
480                 noderect.xmax += margin;
481                 noderect.ymin -= margin;
482                 noderect.ymax += margin;
483                 
484                 /* first child initializes frame */
485                 if (bbinit) {
486                         bbinit = 0;
487                         rect = noderect;
488                         data->flag &= ~NODE_FRAME_RESIZEABLE;
489                 }
490                 else
491                         BLI_rctf_union(&rect, &noderect);
492         }
493         
494         /* now adjust the frame size from view-space bounding box */
495         node_from_view(node, rect.xmin, rect.ymax, &node->offsetx, &node->offsety);
496         node_from_view(node, rect.xmax, rect.ymin, &xmax, &ymax);
497         node->width = xmax - node->offsetx;
498         node->height = -ymax + node->offsety;
499         
500         node->totr = rect;
501 }
502
503 static void node_draw_frame_label(bNode *node, const float aspect)
504 {
505         /* XXX font id is crap design */
506         const int fontid = UI_GetStyle()->widgetlabel.uifont_id;
507         NodeFrame *data = (NodeFrame *)node->storage;
508         rctf *rct = &node->totr;
509         int color_id = node_get_colorid(node);
510         const char *label = nodeLabel(node);
511         /* XXX a bit hacky, should use separate align values for x and y */
512         float width, ascender;
513         float x, y;
514         const int font_size = data->label_size / aspect;
515
516         BLF_enable(fontid, BLF_ASPECT);
517         BLF_aspect(fontid, aspect, aspect, 1.0f);
518         BLF_size(fontid, MIN2(24, font_size), U.dpi); /* clamp otherwise it can suck up a LOT of memory */
519         
520         /* title color */
521         UI_ThemeColorBlendShade(TH_TEXT, color_id, 0.8f, 10);
522
523         width = BLF_width(fontid, label);
524         ascender = BLF_ascender(fontid);
525         
526         /* 'x' doesn't need aspect correction */
527         x = BLI_rctf_cent_x(rct) - (0.5f * width);
528         y = rct->ymax - (((NODE_DY / 4) / aspect) + (ascender * aspect));
529
530         BLF_position(fontid, x, y, 0);
531         BLF_draw(fontid, label, BLF_DRAW_STR_DUMMY_MAX);
532
533         BLF_disable(fontid, BLF_ASPECT);
534 }
535
536 static void node_draw_frame(const bContext *C, ARegion *ar, SpaceNode *snode,
537                             bNodeTree *UNUSED(ntree), bNode *node, bNodeInstanceKey UNUSED(key))
538 {
539         rctf *rct = &node->totr;
540         int color_id = node_get_colorid(node);
541         unsigned char color[4];
542         float alpha;
543         
544         /* skip if out of view */
545         if (BLI_rctf_isect(&node->totr, &ar->v2d.cur, NULL) == FALSE) {
546                 uiEndBlock(C, node->block);
547                 node->block = NULL;
548                 return;
549         }
550
551         UI_GetThemeColor4ubv(TH_NODE_FRAME, color);
552         alpha = (float)(color[3]) / 255.0f;
553         
554         /* shadow */
555         node_draw_shadow(snode, node, BASIS_RAD, alpha);
556         
557         /* body */
558         if (node->flag & NODE_CUSTOM_COLOR)
559                 glColor4f(node->color[0], node->color[1], node->color[2], alpha);
560         else
561                 UI_ThemeColor4(TH_NODE_FRAME);
562         glEnable(GL_BLEND);
563         uiSetRoundBox(UI_CNR_ALL);
564         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
565         glDisable(GL_BLEND);
566
567         /* outline active and selected emphasis */
568         if (node->flag & SELECT) {
569                 glEnable(GL_BLEND);
570                 glEnable(GL_LINE_SMOOTH);
571                 
572                 if (node->flag & NODE_ACTIVE)
573                         UI_ThemeColorShadeAlpha(TH_ACTIVE, 0, -40);
574                 else
575                         UI_ThemeColorShadeAlpha(TH_SELECT, 0, -40);
576                 uiSetRoundBox(UI_CNR_ALL);
577                 uiDrawBox(GL_LINE_LOOP,
578                           rct->xmin, rct->ymin,
579                           rct->xmax, rct->ymax, BASIS_RAD);
580                 
581                 glDisable(GL_LINE_SMOOTH);
582                 glDisable(GL_BLEND);
583         }
584         
585         /* label */
586         node_draw_frame_label(node, snode->aspect);
587         
588         UI_ThemeClearColor(color_id);
589                 
590         uiEndBlock(C, node->block);
591         uiDrawBlock(C, node->block);
592         node->block = NULL;
593 }
594
595 static int node_resize_area_frame(bNode *node, int x, int y)
596 {
597         const float size = 10.0f;
598         NodeFrame *data = (NodeFrame *)node->storage;
599         rctf totr = node->totr;
600         int dir = 0;
601         
602         /* shrinking frame size is determined by child nodes */
603         if (!(data->flag & NODE_FRAME_RESIZEABLE))
604                 return 0;
605         
606         if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
607                 dir |= NODE_RESIZE_RIGHT;
608         if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
609                 dir |= NODE_RESIZE_LEFT;
610         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymax - size && y < totr.ymax)
611                 dir |= NODE_RESIZE_TOP;
612         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymin && y < totr.ymin + size)
613                 dir |= NODE_RESIZE_BOTTOM;
614         
615         return dir;
616 }
617
618 static void node_buts_frame_details(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
619 {
620         uiItemR(layout, ptr, "label_size", 0, IFACE_("Label Size"), ICON_NONE);
621         uiItemR(layout, ptr, "shrink", 0, IFACE_("Shrink"), ICON_NONE);
622 }
623
624
625 #define NODE_REROUTE_SIZE   8.0f
626
627 static void node_update_reroute(const bContext *UNUSED(C), bNodeTree *UNUSED(ntree), bNode *node)
628 {
629         bNodeSocket *nsock;
630         float locx, locy;
631         float size = NODE_REROUTE_SIZE;
632         
633         /* get "global" coords */
634         node_to_view(node, 0.0f, 0.0f, &locx, &locy);
635         
636         /* reroute node has exactly one input and one output, both in the same place */
637         nsock = node->outputs.first;
638         nsock->locx = locx;
639         nsock->locy = locy;
640
641         nsock = node->inputs.first;
642         nsock->locx = locx;
643         nsock->locy = locy;
644
645         node->width = size * 2;
646         node->totr.xmin = locx - size;
647         node->totr.xmax = locx + size;
648         node->totr.ymax = locy + size;
649         node->totr.ymin = locy - size;
650 }
651
652 static void node_draw_reroute(const bContext *C, ARegion *ar, SpaceNode *UNUSED(snode),
653                               bNodeTree *ntree, bNode *node, bNodeInstanceKey UNUSED(key))
654 {
655         bNodeSocket *sock;
656         char showname[128]; /* 128 used below */
657         rctf *rct = &node->totr;
658
659 #if 0   /* UNUSED */
660         float size = NODE_REROUTE_SIZE;
661 #endif
662         float socket_size = NODE_SOCKSIZE;
663
664         /* skip if out of view */
665         if (node->totr.xmax < ar->v2d.cur.xmin || node->totr.xmin > ar->v2d.cur.xmax ||
666             node->totr.ymax < ar->v2d.cur.ymin || node->totr.ymin > ar->v2d.cur.ymax)
667         {
668                 uiEndBlock(C, node->block);
669                 node->block = NULL;
670                 return;
671         }
672
673         /* XXX only kept for debugging
674          * selection state is indicated by socket outline below!
675          */
676 #if 0
677         /* body */
678         uiSetRoundBox(15);
679         UI_ThemeColor4(TH_NODE);
680         glEnable(GL_BLEND);
681         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
682         glDisable(GL_BLEND);
683
684         /* outline active and selected emphasis */
685         if (node->flag & SELECT) {
686                 glEnable(GL_BLEND);
687                 glEnable(GL_LINE_SMOOTH);
688                 /* using different shades of TH_TEXT_HI for the empasis, like triangle */
689                 if (node->flag & NODE_ACTIVE)
690                         UI_ThemeColorShadeAlpha(TH_TEXT_HI, 0, -40);
691                 else
692                         UI_ThemeColorShadeAlpha(TH_TEXT_HI, -20, -120);
693                 uiDrawBox(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
694
695                 glDisable(GL_LINE_SMOOTH);
696                 glDisable(GL_BLEND);
697         }
698 #endif
699
700         if (node->label[0] != '\0') {
701                 /* draw title (node label) */
702                 BLI_strncpy(showname, node->label, sizeof(showname));
703                 uiDefBut(node->block, LABEL, 0, showname,
704                          (int)(rct->xmin - NODE_DYS), (int)(rct->ymax),
705                          (short)512, (short)NODE_DY,
706                          NULL, 0, 0, 0, 0, NULL);
707         }
708
709         /* only draw input socket. as they all are placed on the same position.
710          * highlight also if node itself is selected, since we don't display the node body separately!
711          */
712         for (sock = node->inputs.first; sock; sock = sock->next) {
713                 node_socket_circle_draw(C, ntree, node, sock, socket_size, (sock->flag & SELECT) || (node->flag & SELECT));
714         }
715
716         uiEndBlock(C, node->block);
717         uiDrawBlock(C, node->block);
718         node->block = NULL;
719 }
720
721 /* Special tweak area for reroute node.
722  * Since this node is quite small, we use a larger tweak area for grabbing than for selection.
723  */
724 static int node_tweak_area_reroute(bNode *node, int x, int y)
725 {
726         /* square of tweak radius */
727         static const float tweak_radius_sq = 576;  /* 24 * 24 */
728         
729         bNodeSocket *sock = node->inputs.first;
730         float dx = sock->locx - x;
731         float dy = sock->locy - y;
732         return (dx * dx + dy * dy <= tweak_radius_sq);
733 }
734
735 static void node_common_set_butfunc(bNodeType *ntype)
736 {
737         switch (ntype->type) {
738                 case NODE_GROUP:
739                         ntype->uifunc = node_uifunc_group;
740                         break;
741                 case NODE_FRAME:
742                         ntype->drawfunc = node_draw_frame;
743                         ntype->drawupdatefunc = node_update_frame;
744                         ntype->uifuncbut = node_buts_frame_details;
745                         ntype->resize_area_func = node_resize_area_frame;
746                         break;
747                 case NODE_REROUTE:
748                         ntype->drawfunc = node_draw_reroute;
749                         ntype->drawupdatefunc = node_update_reroute;
750                         ntype->tweak_area_func = node_tweak_area_reroute;
751                         break;
752         }
753 }
754
755 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
756
757 static void node_buts_image_user(uiLayout *layout, bContext *C, PointerRNA *ptr,
758                                  PointerRNA *imaptr, PointerRNA *iuserptr)
759 {
760         uiLayout *col;
761         int source;
762
763         if (!imaptr->data)
764                 return;
765
766         col = uiLayoutColumn(layout, FALSE);
767         
768         uiItemR(col, imaptr, "source", 0, "", ICON_NONE);
769         
770         source = RNA_enum_get(imaptr, "source");
771
772         if (source == IMA_SRC_SEQUENCE) {
773                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
774                 Scene *scene = CTX_data_scene(C);
775                 ImageUser *iuser = iuserptr->data;
776                 char numstr[32];
777                 const int framenr = BKE_image_user_frame_get(iuser, CFRA, 0, NULL);
778                 BLI_snprintf(numstr, sizeof(numstr), IFACE_("Frame: %d"), framenr);
779                 uiItemL(layout, numstr, ICON_NONE);
780         }
781
782         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
783                 col = uiLayoutColumn(layout, TRUE);
784                 uiItemR(col, ptr, "frame_duration", 0, NULL, ICON_NONE);
785                 uiItemR(col, ptr, "frame_start", 0, NULL, ICON_NONE);
786                 uiItemR(col, ptr, "frame_offset", 0, NULL, ICON_NONE);
787                 uiItemR(col, ptr, "use_cyclic", 0, NULL, ICON_NONE);
788                 uiItemR(col, ptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
789         }
790
791         col = uiLayoutColumn(layout, FALSE);
792
793         if (RNA_enum_get(imaptr, "type") == IMA_TYPE_MULTILAYER)
794                 uiItemR(col, ptr, "layer", 0, NULL, ICON_NONE);
795 }
796
797 static void node_shader_buts_material(uiLayout *layout, bContext *C, PointerRNA *ptr)
798 {
799         bNode *node = ptr->data;
800         uiLayout *col;
801         
802         uiTemplateID(layout, C, ptr, "material", "MATERIAL_OT_new", NULL, NULL);
803         
804         if (!node->id) return;
805         
806         col = uiLayoutColumn(layout, FALSE);
807         uiItemR(col, ptr, "use_diffuse", 0, NULL, ICON_NONE);
808         uiItemR(col, ptr, "use_specular", 0, NULL, ICON_NONE);
809         uiItemR(col, ptr, "invert_normal", 0, NULL, ICON_NONE);
810 }
811
812 static void node_shader_buts_mapping(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
813 {
814         uiLayout *row;
815         
816         uiItemL(layout, IFACE_("Location:"), ICON_NONE);
817         row = uiLayoutRow(layout, TRUE);
818         uiItemR(row, ptr, "translation", 0, "", ICON_NONE);
819         
820         uiItemL(layout, IFACE_("Rotation:"), ICON_NONE);
821         row = uiLayoutRow(layout, TRUE);
822         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
823         
824         uiItemL(layout, IFACE_("Scale:"), ICON_NONE);
825         row = uiLayoutRow(layout, TRUE);
826         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
827         
828         row = uiLayoutRow(layout, TRUE);
829         uiItemR(row, ptr, "use_min", 0, IFACE_("Min"), ICON_NONE);
830         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
831         
832         row = uiLayoutRow(layout, TRUE);
833         uiItemR(row, ptr, "use_max", 0, IFACE_("Max"), ICON_NONE);
834         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
835 }
836
837 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
838
839         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
840 }
841
842 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
843 {
844         PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
845         uiLayout *col;
846
847         col = uiLayoutColumn(layout, FALSE);
848
849         if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
850                 PointerRNA dataptr = RNA_pointer_get(&obptr, "data");
851
852                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
853                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
854         }
855         else {
856                 uiItemR(col, ptr, "uv_layer", 0, IFACE_("UV"), ICON_NONE);
857                 uiItemR(col, ptr, "color_layer", 0, IFACE_("VCol"), ICON_NONE);
858         }
859 }
860
861 static void node_shader_buts_attribute(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
862 {
863         uiItemR(layout, ptr, "attribute_name", 0, IFACE_("Name"), ICON_NONE);
864 }
865
866 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
867 {
868         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
869         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
870
871         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
872         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
873         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
874
875         if (RNA_enum_get(ptr, "projection") == SHD_PROJ_BOX) {
876                 uiItemR(layout, ptr, "projection_blend", 0, "Blend", ICON_NONE);
877         }
878
879         /* note: image user properties used directly here, unlike compositor image node,
880          * which redefines them in the node struct RNA to get proper updates.
881          */
882         node_buts_image_user(layout, C, &iuserptr, &imaptr, &iuserptr);
883 }
884
885 static void node_shader_buts_tex_environment(uiLayout *layout, bContext *C, PointerRNA *ptr)
886 {
887         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
888         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
889
890         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
891         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
892         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
893
894         node_buts_image_user(layout, C, &iuserptr, &imaptr, &iuserptr);
895 }
896
897 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
898 {
899         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
900         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
901 }
902
903 static void node_shader_buts_tex_gradient(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
904 {
905         uiItemR(layout, ptr, "gradient_type", 0, "", ICON_NONE);
906 }
907
908 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
909 {
910         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
911 }
912
913 static void node_shader_buts_tex_brick(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
914 {
915         uiLayout *col;
916         
917         col = uiLayoutColumn(layout, TRUE);
918         uiItemR(col, ptr, "offset", 0, IFACE_("Offset"), ICON_NONE);
919         uiItemR(col, ptr, "offset_frequency", 0, IFACE_("Frequency"), ICON_NONE);
920         
921         col = uiLayoutColumn(layout, TRUE);
922         uiItemR(col, ptr, "squash", 0, IFACE_("Squash"), ICON_NONE);
923         uiItemR(col, ptr, "squash_frequency", 0, IFACE_("Frequency"), ICON_NONE);
924 }
925
926 static void node_shader_buts_tex_wave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
927 {
928         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
929 }
930
931 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
932 {
933         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
934 }
935
936 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
937 {
938         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
939 }
940
941 static void node_shader_buts_tex_coord(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
942 {
943         uiItemR(layout, ptr, "from_dupli", 0, NULL, 0);
944 }
945
946 static void node_shader_buts_normal_map(uiLayout *layout, bContext *C, PointerRNA *ptr)
947 {
948         uiItemR(layout, ptr, "space", 0, "", 0);
949
950         if (RNA_enum_get(ptr, "space") == SHD_NORMAL_MAP_TANGENT) {
951                 PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
952
953                 if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
954                         PointerRNA dataptr = RNA_pointer_get(&obptr, "data");
955                         uiItemPointerR(layout, ptr, "uv_map", &dataptr, "uv_textures", "", ICON_NONE);
956                 }
957                 else
958                         uiItemR(layout, ptr, "uv_map", 0, "", 0);
959         }
960 }
961
962 static void node_shader_buts_tangent(uiLayout *layout, bContext *C, PointerRNA *ptr)
963 {
964         uiLayout *split, *row;
965
966         split = uiLayoutSplit(layout, 0.0f, FALSE);
967
968         uiItemR(split, ptr, "direction_type", 0, "", 0);
969
970         row = uiLayoutRow(split, FALSE);
971
972         if (RNA_enum_get(ptr, "direction_type") == SHD_TANGENT_UVMAP) {
973                 PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
974
975                 if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
976                         PointerRNA dataptr = RNA_pointer_get(&obptr, "data");
977                         uiItemPointerR(row, ptr, "uv_map", &dataptr, "uv_textures", "", ICON_NONE);
978                 }
979                 else
980                         uiItemR(row, ptr, "uv_map", 0, "", 0);
981         }
982         else
983                 uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, 0);
984 }
985
986 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
987 {
988         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
989 }
990
991 static void node_shader_buts_script(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
992 {
993         uiLayout *row;
994
995         row = uiLayoutRow(layout, FALSE);
996         uiItemR(row, ptr, "mode", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
997
998         row = uiLayoutRow(layout, TRUE);
999
1000         if (RNA_enum_get(ptr, "mode") == NODE_SCRIPT_INTERNAL)
1001                 uiItemR(row, ptr, "script", 0, "", ICON_NONE);
1002         else
1003                 uiItemR(row, ptr, "filepath", 0, "", ICON_NONE);
1004
1005         uiItemO(row, "", ICON_FILE_REFRESH, "node.shader_script_update");
1006 }
1007
1008 static void node_shader_buts_script_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1009 {
1010         uiItemS(layout);
1011
1012         node_shader_buts_script(layout, C, ptr);
1013
1014 #if 0  /* not implemented yet */
1015         if (RNA_enum_get(ptr, "mode") == NODE_SCRIPT_EXTERNAL)
1016                 uiItemR(layout, ptr, "use_auto_update", 0, NULL, ICON_NONE);
1017 #endif
1018 }
1019
1020 /* only once called */
1021 static void node_shader_set_butfunc(bNodeType *ntype)
1022 {
1023         switch (ntype->type) {
1024                 case SH_NODE_MATERIAL:
1025                 case SH_NODE_MATERIAL_EXT:
1026                         ntype->uifunc = node_shader_buts_material;
1027                         break;
1028                 case SH_NODE_TEXTURE:
1029                         ntype->uifunc = node_buts_texture;
1030                         break;
1031                 case SH_NODE_NORMAL:
1032                         ntype->uifunc = node_buts_normal;
1033                         break;
1034                 case SH_NODE_CURVE_VEC:
1035                         ntype->uifunc = node_buts_curvevec;
1036                         break;
1037                 case SH_NODE_CURVE_RGB:
1038                         ntype->uifunc = node_buts_curvecol;
1039                         break;
1040                 case SH_NODE_MAPPING:
1041                         ntype->uifunc = node_shader_buts_mapping;
1042                         break;
1043                 case SH_NODE_VALUE:
1044                         ntype->uifunc = node_buts_value;
1045                         break;
1046                 case SH_NODE_RGB:
1047                         ntype->uifunc = node_buts_rgb;
1048                         break;
1049                 case SH_NODE_MIX_RGB:
1050                         ntype->uifunc = node_buts_mix_rgb;
1051                         break;
1052                 case SH_NODE_VALTORGB:
1053                         ntype->uifunc = node_buts_colorramp;
1054                         break;
1055                 case SH_NODE_MATH: 
1056                         ntype->uifunc = node_buts_math;
1057                         break; 
1058                 case SH_NODE_VECT_MATH: 
1059                         ntype->uifunc = node_shader_buts_vect_math;
1060                         break; 
1061                 case SH_NODE_GEOMETRY:
1062                         ntype->uifunc = node_shader_buts_geometry;
1063                         break;
1064                 case SH_NODE_ATTRIBUTE:
1065                         ntype->uifunc = node_shader_buts_attribute;
1066                         break;
1067                 case SH_NODE_TEX_SKY:
1068                         ntype->uifunc = node_shader_buts_tex_sky;
1069                         break;
1070                 case SH_NODE_TEX_IMAGE:
1071                         ntype->uifunc = node_shader_buts_tex_image;
1072                         break;
1073                 case SH_NODE_TEX_ENVIRONMENT:
1074                         ntype->uifunc = node_shader_buts_tex_environment;
1075                         break;
1076                 case SH_NODE_TEX_GRADIENT:
1077                         ntype->uifunc = node_shader_buts_tex_gradient;
1078                         break;
1079                 case SH_NODE_TEX_MAGIC:
1080                         ntype->uifunc = node_shader_buts_tex_magic;
1081                         break;
1082                 case SH_NODE_TEX_BRICK:
1083                         ntype->uifunc = node_shader_buts_tex_brick;
1084                         break;
1085                 case SH_NODE_TEX_WAVE:
1086                         ntype->uifunc = node_shader_buts_tex_wave;
1087                         break;
1088                 case SH_NODE_TEX_MUSGRAVE:
1089                         ntype->uifunc = node_shader_buts_tex_musgrave;
1090                         break;
1091                 case SH_NODE_TEX_VORONOI:
1092                         ntype->uifunc = node_shader_buts_tex_voronoi;
1093                         break;
1094                 case SH_NODE_TEX_COORD:
1095                         ntype->uifunc = node_shader_buts_tex_coord;
1096                         break;
1097                 case SH_NODE_NORMAL_MAP:
1098                         ntype->uifunc = node_shader_buts_normal_map;
1099                         break;
1100                 case SH_NODE_TANGENT:
1101                         ntype->uifunc = node_shader_buts_tangent;
1102                         break;
1103                 case SH_NODE_BSDF_GLOSSY:
1104                 case SH_NODE_BSDF_GLASS:
1105                 case SH_NODE_BSDF_REFRACTION:
1106                         ntype->uifunc = node_shader_buts_glossy;
1107                         break;
1108                 case SH_NODE_SCRIPT:
1109                         ntype->uifunc = node_shader_buts_script;
1110                         ntype->uifuncbut = node_shader_buts_script_details;
1111                         break;
1112         }
1113 }
1114
1115 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
1116
1117 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1118 {
1119         bNode *node = ptr->data;
1120         PointerRNA imaptr, iuserptr;
1121         
1122         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1123         
1124         if (!node->id) return;
1125         
1126         imaptr = RNA_pointer_get(ptr, "image");
1127         RNA_pointer_create((ID *)ptr->id.data, &RNA_ImageUser, node->storage, &iuserptr);
1128         
1129         node_buts_image_user(layout, C, ptr, &imaptr, &iuserptr);
1130 }
1131
1132 static void node_composit_buts_image_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1133 {
1134         bNode *node = ptr->data;
1135         PointerRNA imaptr;
1136
1137         node_composit_buts_image(layout, C, ptr);
1138
1139         uiItemR(layout, ptr, "use_straight_alpha_output", 0, NULL, 0);
1140
1141         if (!node->id)
1142                 return;
1143
1144         imaptr = RNA_pointer_get(ptr, "image");
1145
1146         uiTemplateColorspaceSettings(layout, &imaptr, "colorspace_settings");
1147         uiItemR(layout, &imaptr, "alpha_mode", 0, NULL, 0);
1148 }
1149
1150 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
1151 {
1152         bNode *node = ptr->data;
1153         uiLayout *col, *row;
1154         PointerRNA op_ptr;
1155         PointerRNA scn_ptr;
1156         PropertyRNA *prop;
1157         const char *layer_name;
1158         char scene_name[MAX_ID_NAME - 2];
1159         wmOperatorType *ot = WM_operatortype_find("RENDER_OT_render", 1);
1160
1161         BLI_assert(ot != 0);
1162
1163         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
1164         
1165         if (!node->id) return;
1166
1167         col = uiLayoutColumn(layout, FALSE);
1168         row = uiLayoutRow(col, TRUE);
1169         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
1170         
1171         prop = RNA_struct_find_property(ptr, "layer");
1172         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
1173                 return;
1174         
1175         scn_ptr = RNA_pointer_get(ptr, "scene");
1176         RNA_string_get(&scn_ptr, "name", scene_name);
1177         
1178         WM_operator_properties_create_ptr(&op_ptr, ot);
1179         RNA_string_set(&op_ptr, "layer", layer_name);
1180         RNA_string_set(&op_ptr, "scene", scene_name);
1181         uiItemFullO_ptr(row, ot, "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
1182
1183 }
1184
1185
1186 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1187 {
1188         uiLayout *col, *row;
1189         int reference;
1190         int filter;
1191         
1192         col = uiLayoutColumn(layout, FALSE);
1193         filter = RNA_enum_get(ptr, "filter_type");
1194         reference = RNA_boolean_get(ptr, "use_variable_size");
1195
1196         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
1197         if (filter != R_FILTER_FAST_GAUSS) {
1198                 uiItemR(col, ptr, "use_variable_size", 0, NULL, ICON_NONE);
1199                 if (!reference) {
1200                         uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
1201                 }
1202                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1203         }
1204         
1205         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
1206         
1207         if (RNA_boolean_get(ptr, "use_relative")) {
1208                 uiItemL(col, IFACE_("Aspect Correction"), ICON_NONE);
1209                 row = uiLayoutRow(layout, TRUE);
1210                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1211                 
1212                 col = uiLayoutColumn(layout, TRUE);
1213                 uiItemR(col, ptr, "factor_x", 0, IFACE_("X"), ICON_NONE);
1214                 uiItemR(col, ptr, "factor_y", 0, IFACE_("Y"), ICON_NONE);
1215         }
1216         else {
1217                 col = uiLayoutColumn(layout, TRUE);
1218                 uiItemR(col, ptr, "size_x", 0, IFACE_("X"), ICON_NONE);
1219                 uiItemR(col, ptr, "size_y", 0, IFACE_("Y"), ICON_NONE);
1220         }
1221 }
1222
1223 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1224 {
1225         uiLayout *col;
1226         
1227         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1228         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
1229         
1230         col = uiLayoutColumn(layout, TRUE);
1231         uiItemL(col, IFACE_("Center:"), ICON_NONE);
1232         uiItemR(col, ptr, "center_x", 0, IFACE_("X"), ICON_NONE);
1233         uiItemR(col, ptr, "center_y", 0, IFACE_("Y"), ICON_NONE);
1234         
1235         uiItemS(layout);
1236         
1237         col = uiLayoutColumn(layout, TRUE);
1238         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
1239         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1240         
1241         uiItemS(layout);
1242         
1243         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
1244         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
1245 }
1246
1247 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1248 {       
1249         uiLayout *col;
1250         
1251         col = uiLayoutColumn(layout, TRUE);
1252         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
1253         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
1254         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
1255 }
1256
1257 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1258 {
1259         uiLayout *sub, *col;
1260         
1261         col = uiLayoutColumn(layout, FALSE);
1262         uiItemL(col, IFACE_("Bokeh Type:"), ICON_NONE);
1263         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
1264         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1265
1266         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1267
1268         col = uiLayoutColumn(layout, FALSE);
1269         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer") == TRUE);
1270         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
1271
1272         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
1273         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1274
1275         col = uiLayoutColumn(layout, FALSE);
1276         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
1277         
1278         col = uiLayoutColumn(layout, FALSE);
1279         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
1280         sub = uiLayoutColumn(col, FALSE);
1281         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer") == FALSE);
1282         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
1283 }
1284
1285 /* qdn: glare node */
1286 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1287 {       
1288         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
1289         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
1290
1291         if (RNA_enum_get(ptr, "glare_type") != 1) {
1292                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1293         
1294                 if (RNA_enum_get(ptr, "glare_type") != 0)
1295                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1296         }
1297         
1298         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
1299         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1300
1301         if (RNA_enum_get(ptr, "glare_type") == 2) {
1302                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
1303                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
1304         }
1305         if (RNA_enum_get(ptr, "glare_type") == 0 || RNA_enum_get(ptr, "glare_type") == 2) {
1306                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1307                 
1308                 if (RNA_enum_get(ptr, "glare_type") == 0)
1309                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
1310         }
1311         if (RNA_enum_get(ptr, "glare_type") == 1) {
1312                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
1313         }
1314 }
1315
1316 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1317 {       
1318         uiLayout *col;
1319
1320         col = uiLayoutColumn(layout, FALSE);
1321         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
1322         if (RNA_enum_get(ptr, "tonemap_type") == 0) {
1323                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1324                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1325                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
1326         }
1327         else {
1328                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
1329                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1330                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1331                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1332         }
1333 }
1334
1335 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1336 {
1337         uiLayout *col;
1338
1339         col = uiLayoutColumn(layout, FALSE);
1340         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
1341
1342         col = uiLayoutColumn(col, FALSE);
1343         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector") == FALSE);
1344         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
1345         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
1346 }
1347
1348 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1349 {
1350         uiLayout *col;
1351         
1352         col = uiLayoutColumn(layout, FALSE);
1353         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
1354         uiItemR(col, ptr, "factor", 0, IFACE_("Blur"), ICON_NONE);
1355         
1356         col = uiLayoutColumn(layout, TRUE);
1357         uiItemL(col, IFACE_("Speed:"), ICON_NONE);
1358         uiItemR(col, ptr, "speed_min", 0, IFACE_("Min"), ICON_NONE);
1359         uiItemR(col, ptr, "speed_max", 0, IFACE_("Max"), ICON_NONE);
1360
1361         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
1362 }
1363
1364 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1365 {
1366         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1367 }
1368
1369 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1370 {
1371         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1372 }
1373
1374 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1375 {
1376         uiLayout *col;
1377
1378         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
1379         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
1380
1381         col = uiLayoutColumn(layout, TRUE);
1382         if (RNA_boolean_get(ptr, "relative")) {
1383                 uiItemR(col, ptr, "rel_min_x", 0, IFACE_("Left"), ICON_NONE);
1384                 uiItemR(col, ptr, "rel_max_x", 0, IFACE_("Right"), ICON_NONE);
1385                 uiItemR(col, ptr, "rel_min_y", 0, IFACE_("Up"), ICON_NONE);
1386                 uiItemR(col, ptr, "rel_max_y", 0, IFACE_("Down"), ICON_NONE);
1387         }
1388         else {
1389                 uiItemR(col, ptr, "min_x", 0, IFACE_("Left"), ICON_NONE);
1390                 uiItemR(col, ptr, "max_x", 0, IFACE_("Right"), ICON_NONE);
1391                 uiItemR(col, ptr, "min_y", 0, IFACE_("Up"), ICON_NONE);
1392                 uiItemR(col, ptr, "max_y", 0, IFACE_("Down"), ICON_NONE);
1393         }
1394 }
1395
1396 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1397 {
1398         uiLayout *row, *col;
1399         
1400         col = uiLayoutColumn(layout, FALSE);
1401         row = uiLayoutRow(col, FALSE);
1402         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1403         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
1404 }
1405
1406 static void node_composit_buts_double_edge_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1407 {
1408         uiLayout *col;
1409
1410         col = uiLayoutColumn(layout, FALSE);
1411
1412         uiItemL(col, IFACE_("Inner Edge:"), ICON_NONE);
1413         uiItemR(col, ptr, "inner_mode", 0, "", ICON_NONE);
1414         uiItemL(col, IFACE_("Buffer Edge:"), ICON_NONE);
1415         uiItemR(col, ptr, "edge_mode", 0, "", ICON_NONE);
1416 }
1417
1418 static void node_composit_buts_map_range(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1419 {
1420         uiLayout *col;
1421
1422         col = uiLayoutColumn(layout, TRUE);
1423         uiItemR(col, ptr, "use_clamp", 0, NULL, ICON_NONE);
1424 }
1425
1426 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1427 {
1428         uiLayout *sub, *col;
1429         
1430         col = uiLayoutColumn(layout, TRUE);
1431         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1432         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
1433         
1434         col = uiLayoutColumn(layout, TRUE);
1435         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
1436         sub = uiLayoutColumn(col, FALSE);
1437         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1438         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
1439         
1440         col = uiLayoutColumn(layout, TRUE);
1441         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
1442         sub = uiLayoutColumn(col, FALSE);
1443         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1444         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
1445 }
1446
1447 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1448 {       
1449         uiLayout *col;
1450         
1451         col = uiLayoutColumn(layout, TRUE);
1452         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
1453         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
1454 }
1455
1456 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1457 {       
1458         uiLayout *col;
1459         
1460         col = uiLayoutColumn(layout, TRUE);
1461         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
1462 }
1463
1464
1465 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1466 {
1467         uiLayout *col;
1468         
1469         col = uiLayoutColumn(layout, FALSE);
1470         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1471         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1472         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1473 }
1474
1475 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1476 {
1477         uiItemR(layout, ptr, "mode", 0, NULL, ICON_NONE);
1478         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1479         switch (RNA_enum_get(ptr, "mode")) {
1480                 case CMP_NODE_DILATEERODE_DISTANCE_THRESH:
1481                         uiItemR(layout, ptr, "edge", 0, NULL, ICON_NONE);
1482                         break;
1483                 case CMP_NODE_DILATEERODE_DISTANCE_FEATHER:
1484                         uiItemR(layout, ptr, "falloff", 0, NULL, ICON_NONE);
1485                         break;
1486         }
1487 }
1488
1489 static void node_composit_buts_inpaint(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1490 {
1491         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1492 }
1493
1494 static void node_composit_buts_despeckle(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1495 {
1496         uiLayout *col;
1497
1498         col = uiLayoutColumn(layout, FALSE);
1499         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1500         uiItemR(col, ptr, "threshold_neighbour", 0, NULL, ICON_NONE);
1501 }
1502
1503 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1504 {
1505         uiLayout *col;
1506         
1507         col = uiLayoutColumn(layout, TRUE);
1508         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1509         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1510 }
1511
1512 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1513 {
1514         uiLayout *col, *row;
1515         
1516         col = uiLayoutColumn(layout, TRUE);
1517
1518         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1519         row = uiLayoutRow(layout, FALSE);
1520         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1521
1522         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1523         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1524 }
1525
1526 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1527 {
1528         uiLayout *row, *col;
1529         
1530         uiItemL(layout, IFACE_("Despill Channel:"), ICON_NONE);
1531         row = uiLayoutRow(layout, FALSE);
1532         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1533
1534         col = uiLayoutColumn(layout, FALSE);
1535         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1536
1537         if (RNA_enum_get(ptr, "limit_method") == 0) {
1538                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1539                 row = uiLayoutRow(col, FALSE);
1540                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1541         }
1542
1543         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1544         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1545         if (RNA_boolean_get(ptr, "use_unspill") == TRUE) {
1546                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1547                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1548                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1549         }
1550 }
1551
1552 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1553 {
1554         uiLayout *col;
1555         
1556         col = uiLayoutColumn(layout, FALSE);
1557         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1558         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1559         
1560         col = uiLayoutColumn(layout, TRUE);
1561         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1562         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1563         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1564 }
1565
1566 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1567 {
1568         uiLayout *col;
1569         
1570         col = uiLayoutColumn(layout, TRUE);
1571         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1572         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1573         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1574 }
1575
1576 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1577 {       
1578         uiLayout *col, *row;
1579
1580         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1581         row = uiLayoutRow(layout, FALSE);
1582         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1583
1584         col = uiLayoutColumn(layout, FALSE);
1585         uiItemL(col, IFACE_("Key Channel:"), ICON_NONE);
1586         row = uiLayoutRow(col, FALSE);
1587         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1588
1589         col = uiLayoutColumn(layout, FALSE);
1590
1591         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1592         if (RNA_enum_get(ptr, "limit_method") == 0) {
1593                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1594                 row = uiLayoutRow(col, FALSE);
1595                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1596         }
1597
1598         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1599         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1600 }
1601
1602 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1603 {
1604         uiLayout *col;
1605         
1606         col = uiLayoutColumn(layout, TRUE);
1607         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1608         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1609 }
1610
1611 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1612 {
1613         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1614 }
1615
1616 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1617 {
1618         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1619         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
1620 }
1621
1622 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1623 static void node_draw_input_file_output(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *node_ptr, int UNUSED(linked))
1624 {
1625         bNodeTree *ntree = ptr->id.data;
1626         bNodeSocket *sock = ptr->data;
1627         uiLayout *row;
1628         PointerRNA inputptr, imfptr;
1629         int imtype;
1630         
1631         row = uiLayoutRow(layout, FALSE);
1632         
1633         imfptr = RNA_pointer_get(node_ptr, "format");
1634         imtype = RNA_enum_get(&imfptr, "file_format");
1635         if (imtype == R_IMF_IMTYPE_MULTILAYER) {
1636                 NodeImageMultiFileSocket *input = sock->storage;
1637                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotLayer, input, &inputptr);
1638                 
1639                 uiItemL(row, input->layer, ICON_NONE);
1640         }
1641         else {
1642                 NodeImageMultiFileSocket *input = sock->storage;
1643                 PropertyRNA *imtype_prop;
1644                 const char *imtype_name;
1645                 uiBlock *block;
1646                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotFile, input, &inputptr);
1647                 
1648                 uiItemL(row, input->path, ICON_NONE);
1649                 
1650                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1651                         imfptr = RNA_pointer_get(&inputptr, "format");
1652                 
1653                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1654                 RNA_property_enum_name((bContext *)C, &imfptr, imtype_prop,
1655                                        RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1656                 block = uiLayoutGetBlock(row);
1657                 uiBlockSetEmboss(block, UI_EMBOSSP);
1658                 uiItemL(row, imtype_name, ICON_NONE);
1659                 uiBlockSetEmboss(block, UI_EMBOSSN);
1660         }
1661 }
1662 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1663 {
1664         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1665         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1666         
1667         if (multilayer)
1668                 uiItemL(layout, IFACE_("Path:"), ICON_NONE);
1669         else
1670                 uiItemL(layout, IFACE_("Base Path:"), ICON_NONE);
1671         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1672 }
1673 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1674 {
1675         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1676         PointerRNA active_input_ptr, op_ptr;
1677         uiLayout *row, *col;
1678         int active_index;
1679         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1680         
1681         node_composit_buts_file_output(layout, C, ptr);
1682         uiTemplateImageSettings(layout, &imfptr, FALSE);
1683         
1684         uiItemS(layout);
1685         
1686         uiItemO(layout, IFACE_("Add Input"), ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
1687         
1688         row = uiLayoutRow(layout, FALSE);
1689         col = uiLayoutColumn(row, TRUE);
1690         
1691         active_index = RNA_int_get(ptr, "active_input_index");
1692         /* using different collection properties if multilayer format is enabled */
1693         if (multilayer) {
1694                 uiTemplateList(col, C, "UI_UL_list", "file_output_node", ptr, "layer_slots", ptr, "active_input_index", 0, 0, 0);
1695                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "layer_slots"),
1696                                                    active_index, &active_input_ptr);
1697         }
1698         else {
1699                 uiTemplateList(col, C, "UI_UL_list", "file_output_node", ptr, "file_slots", ptr, "active_input_index", 0, 0, 0);
1700                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_slots"),
1701                                                    active_index, &active_input_ptr);
1702         }
1703         /* XXX collection lookup does not return the ID part of the pointer, setting this manually here */
1704         active_input_ptr.id.data = ptr->id.data;
1705         
1706         col = uiLayoutColumn(row, TRUE);
1707         op_ptr = uiItemFullO(col, "NODE_OT_output_file_move_active_socket", "",
1708                              ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1709         RNA_enum_set(&op_ptr, "direction", 1);
1710         op_ptr = uiItemFullO(col, "NODE_OT_output_file_move_active_socket", "",
1711                              ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1712         RNA_enum_set(&op_ptr, "direction", 2);
1713         
1714         if (active_input_ptr.data) {
1715                 if (multilayer) {
1716                         col = uiLayoutColumn(layout, TRUE);
1717                         
1718                         uiItemL(col, IFACE_("Layer:"), ICON_NONE);
1719                         row = uiLayoutRow(col, FALSE);
1720                         uiItemR(row, &active_input_ptr, "name", 0, "", ICON_NONE);
1721                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "",
1722                                     ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
1723                 }
1724                 else {
1725                         col = uiLayoutColumn(layout, TRUE);
1726                         
1727                         uiItemL(col, IFACE_("File Path:"), ICON_NONE);
1728                         row = uiLayoutRow(col, FALSE);
1729                         uiItemR(row, &active_input_ptr, "path", 0, "", ICON_NONE);
1730                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "",
1731                                     ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
1732                         
1733                         /* format details for individual files */
1734                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
1735                         
1736                         col = uiLayoutColumn(layout, TRUE);
1737                         uiItemL(col, IFACE_("Format:"), ICON_NONE);
1738                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, ICON_NONE);
1739                         
1740                         col = uiLayoutColumn(layout, FALSE);
1741                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format") == FALSE);
1742                         uiTemplateImageSettings(col, &imfptr, FALSE);
1743                 }
1744         }
1745 }
1746
1747 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1748 {
1749         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
1750
1751         if (RNA_enum_get(ptr, "space") == CMP_SCALE_RENDERPERCENT) {
1752                 uiLayout *row;
1753                 uiItemR(layout, ptr, "frame_method", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1754                 row = uiLayoutRow(layout, TRUE);
1755                 uiItemR(row, ptr, "offset_x", 0, "X", ICON_NONE);
1756                 uiItemR(row, ptr, "offset_y", 0, "Y", ICON_NONE);
1757         }
1758 }
1759
1760 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1761 {
1762         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1763 }
1764
1765 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1766 {
1767         uiLayout *col;
1768         
1769         col = uiLayoutColumn(layout, FALSE);
1770         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
1771         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
1772 }
1773
1774 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1775 {
1776         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
1777 }
1778
1779 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1780 {
1781         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1782 }
1783
1784 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1785 {
1786         uiLayout *split, *col, *row;
1787         
1788         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1789         
1790         if (RNA_enum_get(ptr, "correction_method") == 0) {
1791         
1792                 split = uiLayoutSplit(layout, 0.0f, FALSE);
1793                 col = uiLayoutColumn(split, FALSE);
1794                 uiTemplateColorPicker(col, ptr, "lift", 1, 1, 0, 1);
1795                 row = uiLayoutRow(col, FALSE);
1796                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
1797                 
1798                 col = uiLayoutColumn(split, FALSE);
1799                 uiTemplateColorPicker(col, ptr, "gamma", 1, 1, 1, 1);
1800                 row = uiLayoutRow(col, FALSE);
1801                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
1802                 
1803                 col = uiLayoutColumn(split, FALSE);
1804                 uiTemplateColorPicker(col, ptr, "gain", 1, 1, 1, 1);
1805                 row = uiLayoutRow(col, FALSE);
1806                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
1807
1808         }
1809         else {
1810                 
1811                 split = uiLayoutSplit(layout, 0.0f, FALSE);
1812                 col = uiLayoutColumn(split, FALSE);
1813                 uiTemplateColorPicker(col, ptr, "offset", 1, 1, 0, 1);
1814                 row = uiLayoutRow(col, FALSE);
1815                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
1816                 
1817                 col = uiLayoutColumn(split, FALSE);
1818                 uiTemplateColorPicker(col, ptr, "power", 1, 1, 0, 1);
1819                 row = uiLayoutRow(col, FALSE);
1820                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
1821                 
1822                 col = uiLayoutColumn(split, FALSE);
1823                 uiTemplateColorPicker(col, ptr, "slope", 1, 1, 0, 1);
1824                 row = uiLayoutRow(col, FALSE);
1825                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
1826         }
1827
1828 }
1829 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1830 {
1831         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1832
1833         if (RNA_enum_get(ptr, "correction_method") == 0) {
1834
1835                 uiTemplateColorPicker(layout, ptr, "lift", 1, 1, 0, 1);
1836                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
1837
1838                 uiTemplateColorPicker(layout, ptr, "gamma", 1, 1, 1, 1);
1839                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
1840
1841                 uiTemplateColorPicker(layout, ptr, "gain", 1, 1, 1, 1);
1842                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
1843         }
1844         else {
1845                 uiTemplateColorPicker(layout, ptr, "offset", 1, 1, 0, 1);
1846                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
1847
1848                 uiTemplateColorPicker(layout, ptr, "power", 1, 1, 0, 1);
1849                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
1850
1851                 uiTemplateColorPicker(layout, ptr, "slope", 1, 1, 0, 1);
1852                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
1853         }
1854 }
1855
1856
1857 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1858 {
1859         bNode *node = ptr->data;
1860         CurveMapping *cumap = node->storage;
1861
1862         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
1863                 cumap->flag |= CUMA_DRAW_SAMPLE;
1864                 copy_v3_v3(cumap->sample, _sample_col);
1865         }
1866         else {
1867                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
1868         }
1869
1870         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
1871 }
1872
1873 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1874
1875         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
1876 }
1877
1878 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
1879 {
1880         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1881 }
1882
1883 static void node_composit_buts_movieclip_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1884 {
1885         bNode *node = ptr->data;
1886         PointerRNA clipptr;
1887
1888         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1889
1890         if (!node->id)
1891                 return;
1892
1893         clipptr = RNA_pointer_get(ptr, "clip");
1894
1895         uiTemplateColorspaceSettings(layout, &clipptr, "colorspace_settings");
1896 }
1897
1898 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
1899 {
1900         bNode *node = ptr->data;
1901
1902         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1903
1904         if (!node->id)
1905                 return;
1906
1907         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1908 }
1909
1910 static void node_composit_buts_translate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1911 {
1912         uiItemR(layout, ptr, "use_relative", 0, NULL, ICON_NONE);
1913         uiItemR(layout, ptr, "wrap_axis", 0, NULL, ICON_NONE);
1914 }
1915
1916 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1917 {
1918         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1919 }
1920
1921 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
1922 {
1923         bNode *node = ptr->data;
1924
1925         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
1926
1927         if (!node->id)
1928                 return;
1929
1930         uiItemR(layout, ptr, "distortion_type", 0, "", ICON_NONE);
1931 }
1932
1933 static void node_composit_buts_colorcorrection(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1934 {
1935         uiLayout *row;
1936         
1937         row = uiLayoutRow(layout, FALSE);
1938         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
1939         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
1940         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
1941
1942         row = uiLayoutRow(layout, FALSE);
1943         uiItemL(row, "", ICON_NONE);
1944         uiItemL(row, IFACE_("Saturation"), ICON_NONE);
1945         uiItemL(row, IFACE_("Contrast"), ICON_NONE);
1946         uiItemL(row, IFACE_("Gamma"), ICON_NONE);
1947         uiItemL(row, IFACE_("Gain"), ICON_NONE);
1948         uiItemL(row, IFACE_("Lift"), ICON_NONE);
1949
1950         row = uiLayoutRow(layout, FALSE);
1951         uiItemL(row, IFACE_("Master"), ICON_NONE);
1952         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
1953         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
1954         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
1955         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
1956         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
1957
1958         row = uiLayoutRow(layout, FALSE);
1959         uiItemL(row, IFACE_("Highlights"), ICON_NONE);
1960         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
1961         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
1962         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
1963         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
1964         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
1965
1966         row = uiLayoutRow(layout, FALSE);
1967         uiItemL(row, IFACE_("Midtones"), ICON_NONE);
1968         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
1969         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
1970         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
1971         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
1972         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
1973
1974         row = uiLayoutRow(layout, FALSE);
1975         uiItemL(row, IFACE_("Shadows"), ICON_NONE);
1976         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
1977         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
1978         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
1979         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
1980         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
1981
1982         row = uiLayoutRow(layout, FALSE);
1983         uiItemR(row, ptr, "midtones_start", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1984         uiItemR(row, ptr, "midtones_end", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1985 }
1986
1987 static void node_composit_buts_colorcorrection_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1988 {
1989         uiLayout *row;
1990         
1991         row = uiLayoutRow(layout, FALSE);
1992         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
1993         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
1994         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
1995         row = layout;
1996         uiItemL(row, IFACE_("Saturation"), ICON_NONE);
1997         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1998         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1999         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2000         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2001
2002         uiItemL(row, IFACE_("Contrast"), ICON_NONE);
2003         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2004         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2005         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2006         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2007
2008         uiItemL(row, IFACE_("Gamma"), ICON_NONE);
2009         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2010         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2011         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2012         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2013
2014         uiItemL(row, IFACE_("Gain"), ICON_NONE);
2015         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2016         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2017         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2018         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2019         
2020         uiItemL(row, IFACE_("Lift"), ICON_NONE);
2021         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2022         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2023         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2024         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2025
2026         row = uiLayoutRow(layout, FALSE);
2027         uiItemR(row, ptr, "midtones_start", 0, NULL, ICON_NONE);
2028         uiItemR(row, ptr, "midtones_end", 0, NULL, ICON_NONE);
2029 }
2030
2031 static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2032 {
2033         uiItemR(layout, ptr, "check", 0, NULL, ICON_NONE);
2034 }
2035
2036 static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2037 {
2038         uiLayout *row;
2039         
2040         row = uiLayoutRow(layout, TRUE);
2041         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2042         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2043         
2044         row = uiLayoutRow(layout, TRUE);
2045         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2046         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2047
2048         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2049         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2050 }
2051
2052 static void node_composit_buts_bokehimage(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2053 {
2054         uiItemR(layout, ptr, "flaps", 0, NULL, ICON_NONE);
2055         uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
2056         uiItemR(layout, ptr, "rounding", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2057         uiItemR(layout, ptr, "catadioptric", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2058         uiItemR(layout, ptr, "shift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2059 }
2060
2061 static void node_composit_buts_bokehblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2062 {
2063         uiItemR(layout, ptr, "use_variable_size", 0, NULL, ICON_NONE);
2064         // uiItemR(layout, ptr, "f_stop", 0, NULL, ICON_NONE);  // UNUSED
2065         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
2066 }
2067
2068 static void node_composit_backdrop_viewer(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2069 {
2070 //      node_composit_backdrop_canvas(snode, backdrop, node, x, y);
2071         if (node->custom1 == 0) {
2072                 const float backdropWidth = backdrop->x;
2073                 const float backdropHeight = backdrop->y;
2074                 const float cx  = x + snode->zoom * backdropWidth * node->custom3;
2075                 const float cy = y + snode->zoom * backdropHeight * node->custom4;
2076
2077                 glColor3f(1.0, 1.0, 1.0);
2078
2079                 glBegin(GL_LINES);
2080                 glVertex2f(cx - 25, cy - 25);
2081                 glVertex2f(cx + 25, cy + 25);
2082                 glVertex2f(cx + 25, cy - 25);
2083                 glVertex2f(cx - 25, cy + 25);
2084                 glEnd();
2085         }
2086 }
2087
2088 static void node_composit_backdrop_boxmask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2089 {
2090         NodeBoxMask *boxmask = node->storage;
2091         const float backdropWidth = backdrop->x;
2092         const float backdropHeight = backdrop->y;
2093         const float aspect = backdropWidth / backdropHeight;
2094         const float rad = DEG2RADF(-boxmask->rotation);
2095         const float cosine = cosf(rad);
2096         const float sine = sinf(rad);
2097         const float halveBoxWidth = backdropWidth * (boxmask->width / 2.0f);
2098         const float halveBoxHeight = backdropHeight * (boxmask->height / 2.0f) * aspect;
2099
2100         float cx, cy, x1, x2, x3, x4;
2101         float y1, y2, y3, y4;
2102
2103
2104         /* keep this, saves us from a version patch */
2105         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2106
2107         glColor3f(1.0, 1.0, 1.0);
2108
2109         cx  = x + snode->zoom * backdropWidth * boxmask->x;
2110         cy = y + snode->zoom * backdropHeight * boxmask->y;
2111
2112         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2113         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2114         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2115         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2116         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2117         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2118         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2119         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2120
2121         glBegin(GL_LINE_LOOP);
2122         glVertex2f(x1, y1);
2123         glVertex2f(x2, y2);
2124         glVertex2f(x3, y3);
2125         glVertex2f(x4, y4);
2126         glEnd();
2127 }
2128
2129 static void node_composit_backdrop_ellipsemask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2130 {
2131         NodeEllipseMask *ellipsemask = node->storage;
2132         const float backdropWidth = backdrop->x;
2133         const float backdropHeight = backdrop->y;
2134         const float aspect = backdropWidth / backdropHeight;
2135         const float rad = DEG2RADF(-ellipsemask->rotation);
2136         const float cosine = cosf(rad);
2137         const float sine = sinf(rad);
2138         const float halveBoxWidth = backdropWidth * (ellipsemask->width / 2.0f);
2139         const float halveBoxHeight = backdropHeight * (ellipsemask->height / 2.0f) * aspect;
2140
2141         float cx, cy, x1, x2, x3, x4;
2142         float y1, y2, y3, y4;
2143
2144
2145         /* keep this, saves us from a version patch */
2146         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2147
2148         glColor3f(1.0, 1.0, 1.0);
2149
2150         cx  = x + snode->zoom * backdropWidth * ellipsemask->x;
2151         cy = y + snode->zoom * backdropHeight * ellipsemask->y;
2152
2153         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2154         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2155         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2156         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2157         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2158         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2159         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2160         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2161
2162         glBegin(GL_LINE_LOOP);
2163
2164         glVertex2f(x1, y1);
2165         glVertex2f(x2, y2);
2166         glVertex2f(x3, y3);
2167         glVertex2f(x4, y4);
2168         glEnd();
2169 }
2170
2171 static void node_composit_buts_ellipsemask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2172 {
2173         uiLayout *row;
2174         row = uiLayoutRow(layout, TRUE);
2175         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2176         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2177         row = uiLayoutRow(layout, TRUE);
2178         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2179         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2180
2181         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2182         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2183 }
2184
2185 static void node_composit_buts_composite(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2186 {
2187         uiItemR(layout, ptr, "use_alpha", 0, NULL, ICON_NONE);
2188 }
2189
2190 static void node_composit_buts_viewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2191 {
2192         uiItemR(layout, ptr, "use_alpha", 0, NULL, ICON_NONE);
2193 }
2194
2195 static void node_composit_buts_viewer_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2196 {
2197         uiLayout *col;
2198         
2199         uiItemR(layout, ptr, "use_alpha", 0, NULL, ICON_NONE);
2200         uiItemR(layout, ptr, "tile_order", 0, NULL, ICON_NONE);
2201         if (RNA_enum_get(ptr, "tile_order") == 0) {
2202                 col = uiLayoutColumn(layout, TRUE);
2203                 uiItemR(col, ptr, "center_x", 0, NULL, ICON_NONE);
2204                 uiItemR(col, ptr, "center_y", 0, NULL, ICON_NONE);
2205         }
2206 }
2207
2208 static void node_composit_buts_mask(uiLayout *layout, bContext *C, PointerRNA *ptr)
2209 {
2210         bNode *node = ptr->data;
2211
2212         uiTemplateID(layout, C, ptr, "mask", NULL, NULL, NULL);
2213         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
2214         uiItemR(layout, ptr, "use_feather", 0, NULL, ICON_NONE);
2215
2216         uiItemR(layout, ptr, "size_source", 0, "", ICON_NONE);
2217
2218         if (node->custom1 & (CMP_NODEFLAG_MASK_FIXED | CMP_NODEFLAG_MASK_FIXED_SCENE)) {
2219                 uiItemR(layout, ptr, "size_x", 0, NULL, ICON_NONE);
2220                 uiItemR(layout, ptr, "size_y", 0, NULL, ICON_NONE);
2221         }
2222
2223         uiItemR(layout, ptr, "use_motion_blur", 0, NULL, ICON_NONE);
2224         if (node->custom1 & CMP_NODEFLAG_MASK_MOTION_BLUR) {
2225                 uiItemR(layout, ptr, "motion_blur_samples", 0, NULL, ICON_NONE);
2226                 uiItemR(layout, ptr, "motion_blur_shutter", 0, NULL, ICON_NONE);
2227         }
2228 }
2229
2230 static void node_composit_buts_keyingscreen(uiLayout *layout, bContext *C, PointerRNA *ptr)
2231 {
2232         bNode *node = ptr->data;
2233
2234         uiTemplateID(layout, C, ptr, "clip", NULL, NULL, NULL);
2235
2236         if (node->id) {
2237                 MovieClip *clip = (MovieClip *) node->id;
2238                 uiLayout *col;
2239                 PointerRNA tracking_ptr;
2240
2241                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, &clip->tracking, &tracking_ptr);
2242
2243                 col = uiLayoutColumn(layout, TRUE);
2244                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2245         }
2246 }
2247
2248 static void node_composit_buts_keying(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2249 {
2250         /* bNode *node = ptr->data; */ /* UNUSED */
2251
2252         uiItemR(layout, ptr, "blur_pre", 0, NULL, ICON_NONE);
2253         uiItemR(layout, ptr, "screen_balance", 0, NULL, ICON_NONE);
2254         uiItemR(layout, ptr, "despill_factor", 0, NULL, ICON_NONE);
2255         uiItemR(layout, ptr, "despill_balance", 0, NULL, ICON_NONE);
2256         uiItemR(layout, ptr, "edge_kernel_radius", 0, NULL, ICON_NONE);
2257         uiItemR(layout, ptr, "edge_kernel_tolerance", 0, NULL, ICON_NONE);
2258         uiItemR(layout, ptr, "clip_black", 0, NULL, ICON_NONE);
2259         uiItemR(layout, ptr, "clip_white", 0, NULL, ICON_NONE);
2260         uiItemR(layout, ptr, "dilate_distance", 0, NULL, ICON_NONE);
2261         uiItemR(layout, ptr, "feather_falloff", 0, NULL, ICON_NONE);
2262         uiItemR(layout, ptr, "feather_distance", 0, NULL, ICON_NONE);
2263         uiItemR(layout, ptr, "blur_post", 0, NULL, ICON_NONE);
2264 }
2265
2266 static void node_composit_buts_trackpos(uiLayout *layout, bContext *C, PointerRNA *ptr)
2267 {
2268         bNode *node = ptr->data;
2269
2270         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2271
2272         if (node->id) {
2273                 MovieClip *clip = (MovieClip *) node->id;
2274                 MovieTracking *tracking = &clip->tracking;
2275                 MovieTrackingObject *object;
2276                 uiLayout *col;
2277                 PointerRNA tracking_ptr;
2278                 NodeTrackPosData *data = node->storage;
2279
2280                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, tracking, &tracking_ptr);
2281
2282                 col = uiLayoutColumn(layout, FALSE);
2283                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2284
2285                 object = BKE_tracking_object_get_named(tracking, data->tracking_object);
2286                 if (object) {
2287                         PointerRNA object_ptr;
2288
2289                         RNA_pointer_create(&clip->id, &RNA_MovieTrackingObject, object, &object_ptr);
2290
2291                         uiItemPointerR(col, ptr, "track_name", &object_ptr, "tracks", "", ICON_ANIM_DATA);
2292                 }
2293                 else {
2294                         uiItemR(layout, ptr, "track_name", 0, "", ICON_ANIM_DATA);
2295                 }
2296
2297                 uiItemR(layout, ptr, "position", 0, NULL, ICON_NONE);
2298
2299                 if (node->custom1 == 2) {
2300                         uiItemR(layout, ptr, "frame_relative", 0, NULL, ICON_NONE);
2301                 }
2302         }
2303 }
2304
2305 /* only once called */
2306 static void node_composit_set_butfunc(bNodeType *ntype)
2307 {
2308         switch (ntype->type) {
2309                 case CMP_NODE_IMAGE:
2310                         ntype->uifunc = node_composit_buts_image;
2311                         ntype->uifuncbut = node_composit_buts_image_details;
2312                         break;
2313                 case CMP_NODE_R_LAYERS:
2314                         ntype->uifunc = node_composit_buts_renderlayers;
2315                         break;
2316                 case CMP_NODE_NORMAL:
2317                         ntype->uifunc = node_buts_normal;
2318                         break;
2319                 case CMP_NODE_CURVE_VEC:
2320                         ntype->uifunc = node_buts_curvevec;
2321                         break;
2322                 case CMP_NODE_CURVE_RGB:
2323                         ntype->uifunc = node_buts_curvecol;
2324                         break;
2325                 case CMP_NODE_VALUE:
2326                         ntype->uifunc = node_buts_value;
2327                         break;
2328                 case CMP_NODE_RGB:
2329                         ntype->uifunc = node_buts_rgb;
2330                         break;
2331                 case CMP_NODE_FLIP:
2332                         ntype->uifunc = node_composit_buts_flip;
2333                         break;
2334                 case CMP_NODE_SPLITVIEWER:
2335                         ntype->uifunc = node_composit_buts_splitviewer;
2336                         break;
2337                 case CMP_NODE_MIX_RGB:
2338                         ntype->uifunc = node_buts_mix_rgb;
2339                         break;
2340                 case CMP_NODE_VALTORGB:
2341                         ntype->uifunc = node_buts_colorramp;
2342                         break;
2343                 case CMP_NODE_CROP:
2344                         ntype->uifunc = node_composit_buts_crop;
2345                         break;
2346                 case CMP_NODE_BLUR:
2347                         ntype->uifunc = node_composit_buts_blur;
2348                         break;
2349                 case CMP_NODE_DBLUR:
2350                         ntype->uifunc = node_composit_buts_dblur;
2351                         break;
2352                 case CMP_NODE_BILATERALBLUR:
2353                         ntype->uifunc = node_composit_buts_bilateralblur;
2354                         break;
2355                 case CMP_NODE_DEFOCUS:
2356                         ntype->uifunc = node_composit_buts_defocus;
2357                         break;
2358                 case CMP_NODE_GLARE:
2359                         ntype->uifunc = node_composit_buts_glare;
2360                         break;
2361                 case CMP_NODE_TONEMAP:
2362                         ntype->uifunc = node_composit_buts_tonemap;
2363                         break;
2364                 case CMP_NODE_LENSDIST:
2365                         ntype->uifunc = node_composit_buts_lensdist;
2366                         break;
2367                 case CMP_NODE_VECBLUR:
2368                         ntype->uifunc = node_composit_buts_vecblur;
2369                         break;
2370                 case CMP_NODE_FILTER:
2371                         ntype->uifunc = node_composit_buts_filter;
2372                         break;
2373                 case CMP_NODE_MAP_VALUE:
2374                         ntype->uifunc = node_composit_buts_map_value;
2375                         break;
2376                 case CMP_NODE_MAP_RANGE:
2377                         ntype->uifunc = node_composit_buts_map_range;
2378                         break;
2379                 case CMP_NODE_TIME:
2380                         ntype->uifunc = node_buts_time;
2381                         break;
2382                 case CMP_NODE_ALPHAOVER:
2383                         ntype->uifunc = node_composit_buts_alphaover;
2384                         break;
2385                 case CMP_NODE_HUE_SAT:
2386                         ntype->uifunc = node_composit_buts_hue_sat;
2387                         break;
2388                 case CMP_NODE_TEXTURE:
2389                         ntype->uifunc = node_buts_texture;
2390                         break;
2391                 case CMP_NODE_DILATEERODE:
2392                         ntype->uifunc = node_composit_buts_dilateerode;
2393                         break;
2394                 case CMP_NODE_INPAINT:
2395                         ntype->uifunc = node_composit_buts_inpaint;
2396                         break;
2397                 case CMP_NODE_DESPECKLE:
2398                         ntype->uifunc = node_composit_buts_despeckle;
2399                         break;
2400                 case CMP_NODE_OUTPUT_FILE:
2401                         ntype->uifunc = node_composit_buts_file_output;
2402                         ntype->uifuncbut = node_composit_buts_file_output_details;
2403                         ntype->drawinputfunc = node_draw_input_file_output;
2404                         break;
2405                 case CMP_NODE_DIFF_MATTE:
2406                         ntype->uifunc = node_composit_buts_diff_matte;
2407                         break;
2408                 case CMP_NODE_DIST_MATTE:
2409                         ntype->uifunc = node_composit_buts_distance_matte;
2410                         break;
2411                 case CMP_NODE_COLOR_SPILL:
2412                         ntype->uifunc = node_composit_buts_color_spill;
2413                         break;
2414                 case CMP_NODE_CHROMA_MATTE:
2415                         ntype->uifunc = node_composit_buts_chroma_matte;
2416                         break;
2417                 case CMP_NODE_COLOR_MATTE:
2418                         ntype->uifunc = node_composit_buts_color_matte;
2419                         break;
2420                 case CMP_NODE_SCALE:
2421                         ntype->uifunc = node_composit_buts_scale;
2422                         break;
2423                 case CMP_NODE_ROTATE:
2424                         ntype->uifunc = node_composit_buts_rotate;
2425                         break;
2426                 case CMP_NODE_CHANNEL_MATTE:
2427                         ntype->uifunc = node_composit_buts_channel_matte;
2428                         break;
2429                 case CMP_NODE_LUMA_MATTE:
2430                         ntype->uifunc = node_composit_buts_luma_matte;
2431                         break;
2432                 case CMP_NODE_MAP_UV:
2433                         ntype->uifunc = node_composit_buts_map_uv;
2434                         break;
2435                 case CMP_NODE_ID_MASK:
2436                         ntype->uifunc = node_composit_buts_id_mask;
2437                         break;
2438                 case CMP_NODE_DOUBLEEDGEMASK:
2439                         ntype->uifunc = node_composit_buts_double_edge_mask;
2440                         break;
2441                 case CMP_NODE_MATH:
2442                         ntype->uifunc = node_buts_math;
2443                         break;
2444                 case CMP_NODE_INVERT:
2445                         ntype->uifunc = node_composit_buts_invert;
2446                         break;
2447                 case CMP_NODE_PREMULKEY:
2448                         ntype->uifunc = node_composit_buts_premulkey;
2449                         break;
2450                 case CMP_NODE_VIEW_LEVELS:
2451                         ntype->uifunc = node_composit_buts_view_levels;
2452                         break;
2453                 case CMP_NODE_COLORBALANCE:
2454                         ntype->uifunc = node_composit_buts_colorbalance;
2455                         ntype->uifuncbut = node_composit_buts_colorbalance_but;
2456                         break;
2457                 case CMP_NODE_HUECORRECT:
2458                         ntype->uifunc = node_composit_buts_huecorrect;
2459                         break;
2460                 case CMP_NODE_ZCOMBINE:
2461                         ntype->uifunc = node_composit_buts_zcombine;
2462                         break;
2463                 case CMP_NODE_COMBYCCA:
2464                 case CMP_NODE_SEPYCCA:
2465                         ntype->uifunc = node_composit_buts_ycc;
2466                         break;
2467                 case CMP_NODE_MOVIECLIP:
2468                         ntype->uifunc = node_composit_buts_movieclip;
2469                         ntype->uifuncbut = node_composit_buts_movieclip_details;
2470                         break;
2471                 case CMP_NODE_STABILIZE2D:
2472                         ntype->uifunc = node_composit_buts_stabilize2d;
2473                         break;
2474                 case CMP_NODE_TRANSFORM:
2475                         ntype->uifunc = node_composit_buts_transform;
2476                         break;
2477                 case CMP_NODE_TRANSLATE:
2478                         ntype->uifunc = node_composit_buts_translate;
2479                         break;
2480                 case CMP_NODE_MOVIEDISTORTION:
2481                         ntype->uifunc = node_composit_buts_moviedistortion;
2482                         break;
2483                 case CMP_NODE_COLORCORRECTION:
2484                         ntype->uifunc = node_composit_buts_colorcorrection;
2485                         ntype->uifuncbut = node_composit_buts_colorcorrection_but;
2486                         break;
2487                 case CMP_NODE_SWITCH:
2488                         ntype->uifunc = node_composit_buts_switch;
2489                         break;
2490                 case CMP_NODE_MASK_BOX:
2491                         ntype->uifunc = node_composit_buts_boxmask;
2492                         ntype->uibackdropfunc = node_composit_backdrop_boxmask;
2493                         break;
2494                 case CMP_NODE_MASK_ELLIPSE:
2495                         ntype->uifunc = node_composit_buts_ellipsemask;
2496                         ntype->uibackdropfunc = node_composit_backdrop_ellipsemask;
2497                         break;
2498                 case CMP_NODE_BOKEHIMAGE:
2499                         ntype->uifunc = node_composit_buts_bokehimage;
2500                         break;
2501                 case CMP_NODE_BOKEHBLUR:
2502                         ntype->uifunc = node_composit_buts_bokehblur;
2503                         break;
2504                 case CMP_NODE_VIEWER:
2505                         ntype->uifunc = node_composit_buts_viewer;
2506                         ntype->uifuncbut = node_composit_buts_viewer_but;
2507                         ntype->uibackdropfunc = node_composit_backdrop_viewer;
2508                         break;
2509                 case CMP_NODE_COMPOSITE:
2510                         ntype->uifunc = node_composit_buts_composite;
2511                         break;
2512                 case CMP_NODE_MASK:
2513                         ntype->uifunc = node_composit_buts_mask;
2514                         break;
2515                 case CMP_NODE_KEYINGSCREEN:
2516                         ntype->uifunc = node_composit_buts_keyingscreen;
2517                         break;
2518                 case CMP_NODE_KEYING:
2519                         ntype->uifunc = node_composit_buts_keying;
2520                         break;
2521                 case CMP_NODE_TRACKPOS:
2522                         ntype->uifunc = node_composit_buts_trackpos;
2523                         break;
2524         }
2525 }
2526
2527 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2528
2529 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2530 {
2531         uiLayout *col;
2532         
2533         col = uiLayoutColumn(layout, TRUE);
2534         uiItemR(col, ptr, "offset", 0, IFACE_("Offset"), ICON_NONE);
2535         uiItemR(col, ptr, "offset_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2536         
2537         col = uiLayoutColumn(layout, TRUE);
2538         uiItemR(col, ptr, "squash", 0, IFACE_("Squash"), ICON_NONE);
2539         uiItemR(col, ptr, "squash_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2540 }
2541
2542 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2543 {
2544         PointerRNA tex_ptr;
2545         bNode *node = ptr->data;
2546         ID *id = ptr->id.data;
2547         Tex *tex = (Tex *)node->storage;
2548         uiLayout *col, *row;
2549         
2550         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2551
2552         col = uiLayoutColumn(layout, FALSE);
2553
2554         switch (tex->type) {
2555                 case TEX_BLEND:
2556                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2557                         row = uiLayoutRow(col, FALSE);
2558                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2559                         break;
2560
2561                 case TEX_MARBLE:
2562                         row = uiLayoutRow(col, FALSE);
2563                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2564                         row = uiLayoutRow(col, FALSE);
2565                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2566                         row = uiLayoutRow(col, FALSE);
2567                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2568                         row = uiLayoutRow(col, FALSE);
2569                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2570                         break;
2571
2572                 case TEX_MAGIC:
2573                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2574                         break;
2575
2576                 case TEX_STUCCI:
2577                         row = uiLayoutRow(col, FALSE);
2578                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2579                         row = uiLayoutRow(col, FALSE);
2580                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2581                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2582                         break;
2583
2584                 case TEX_WOOD:
2585                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2586                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2587                         row = uiLayoutRow(col, FALSE);
2588                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2589                         row = uiLayoutRow(col, FALSE);
2590                         uiLayoutSetActive(row, !(ELEM(tex->stype, TEX_BAND, TEX_RING)));
2591                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2592                         break;
2593                         
2594                 case TEX_CLOUDS:
2595                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2596                         row = uiLayoutRow(col, FALSE);
2597                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2598                         row = uiLayoutRow(col, FALSE);
2599                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2600                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, IFACE_("Depth"), ICON_NONE);
2601                         break;
2602                         
2603                 case TEX_DISTNOISE:
2604                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2605                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2606                         break;
2607
2608                 case TEX_MUSGRAVE:
2609                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2610                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2611                         break;
2612                 case TEX_VORONOI:
2613                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2614                         if (tex->vn_distm == TEX_MINKOVSKY) {
2615                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2616                         }
2617                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2618                         break;
2619         }
2620 }
2621
2622 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2623 {
2624         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2625 }
2626
2627 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2628 {
2629         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2630 }
2631
2632 /* only once called */
2633 static void node_texture_set_butfunc(bNodeType *ntype)
2634 {
2635         if (ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX) {
2636                 ntype->uifunc = node_texture_buts_proc;
2637         }
2638         else {
2639                 switch (ntype->type) {
2640
2641                         case TEX_NODE_MATH:
2642                                 ntype->uifunc = node_buts_math;
2643                                 break;
2644
2645                         case TEX_NODE_MIX_RGB:
2646                                 ntype->uifunc = node_buts_mix_rgb;
2647                                 break;
2648
2649                         case TEX_NODE_VALTORGB:
2650                                 ntype->uifunc = node_buts_colorramp;
2651                                 break;
2652
2653                         case TEX_NODE_CURVE_RGB:
2654                                 ntype->uifunc = node_buts_curvecol;
2655                                 break;
2656
2657                         case TEX_NODE_CURVE_TIME:
2658                                 ntype->uifunc = node_buts_time;
2659                                 break;
2660
2661                         case TEX_NODE_TEXTURE:
2662                                 ntype->uifunc = node_buts_texture;
2663                                 break;
2664
2665                         case TEX_NODE_BRICKS:
2666                                 ntype->uifunc = node_texture_buts_bricks;
2667                                 break;
2668
2669                         case TEX_NODE_IMAGE:
2670                                 ntype->uifunc = node_texture_buts_image;
2671                                 break;
2672
2673                         case TEX_NODE_OUTPUT:
2674                                 ntype->uifunc = node_texture_buts_output;
2675                                 break;
2676                 }
2677         }
2678 }
2679
2680 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2681
2682 static void node_property_update_default(Main *bmain, Scene *UNUSED(scene), PointerRNA *ptr)
2683 {
2684         bNodeTree *ntree = ptr->id.data;
2685         ED_node_tag_update_nodetree(bmain, ntree);
2686 }
2687
2688 static void node_socket_template_properties_update(bNodeType *ntype, bNodeSocketTemplate *stemp)
2689 {
2690         StructRNA *srna = ntype->ext.srna;
2691         PropertyRNA *prop = RNA_struct_type_find_property(srna, stemp->identifier);
2692         
2693         if (prop)
2694                 RNA_def_property_update_runtime(prop, node_property_update_default);
2695 }
2696
2697 static void node_template_properties_update(bNodeType *ntype)
2698 {
2699         bNodeSocketTemplate *stemp;
2700         
2701         if (ntype->inputs) {
2702                 for (stemp = ntype->inputs; stemp->type >= 0; ++stemp)
2703                         node_socket_template_properties_update(ntype, stemp);
2704         }
2705         if (ntype->outputs) {
2706                 for (stemp = ntype->outputs; stemp->type >= 0; ++stemp)
2707                         node_socket_template_properties_update(ntype, stemp);
2708         }
2709 }
2710
2711 static void node_socket_undefined_draw(bContext *UNUSED(C), uiLayout *layout, PointerRNA *UNUSED(ptr), PointerRNA *UNUSED(node_ptr))
2712 {
2713         uiItemL(layout, "Undefined Socket Type", ICON_ERROR);
2714 }
2715
2716 static void node_socket_undefined_draw_color(bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PointerRNA *UNUSED(node_ptr), float *r_color)
2717 {
2718         r_color[0] = 1.0f;
2719         r_color[1] = 0.0f;
2720         r_color[2] = 0.0f;
2721         r_color[3] = 1.0f;
2722 }
2723
2724 static void node_socket_undefined_interface_draw(bContext *UNUSED(C), uiLayout *layout, PointerRNA *UNUSED(ptr))
2725 {
2726         uiItemL(layout, "Undefined Socket Type", ICON_ERROR);
2727 }
2728
2729 static void node_socket_undefined_interface_draw_color(bContext *UNUSED(C), PointerRNA *UNUSED(ptr), float *r_color)
2730 {
2731         r_color[0] = 1.0f;
2732         r_color[1] = 0.0f;
2733         r_color[2] = 0.0f;
2734         r_color[3] = 1.0f;
2735 }
2736
2737 void ED_node_init_butfuncs(void)
2738 {
2739         /* Fallback types for undefined tree, nodes, sockets
2740          * Defined in blenkernel, but not registered in type hashes.
2741          */
2742         /*extern bNodeTreeType NodeTreeTypeUndefined;*/
2743         extern bNodeType NodeTypeUndefined;
2744         extern bNodeSocketType NodeSocketTypeUndefined;
2745         
2746         /* default ui functions */
2747         NodeTypeUndefined.drawfunc = node_draw_default;
2748         NodeTypeUndefined.drawupdatefunc = node_update_default;
2749         NodeTypeUndefined.select_area_func = node_select_area_default;
2750         NodeTypeUndefined.tweak_area_func = node_tweak_area_default;
2751         NodeTypeUndefined.uifunc = NULL;
2752         NodeTypeUndefined.uifuncbut = NULL;
2753         NodeTypeUndefined.drawinputfunc = node_draw_input_default;
2754         NodeTypeUndefined.drawoutputfunc = node_draw_output_default;
2755         NodeTypeUndefined.resize_area_func = node_resize_area_default;
2756         
2757         NodeSocketTypeUndefined.draw = node_socket_undefined_draw;
2758         NodeSocketTypeUndefined.draw_color = node_socket_undefined_draw_color;
2759         NodeSocketTypeUndefined.interface_draw = node_socket_undefined_interface_draw;
2760         NodeSocketTypeUndefined.interface_draw_color = node_socket_undefined_interface_draw_color;
2761         
2762         /* node type ui functions */
2763         NODE_TYPES_BEGIN(ntype)
2764                 /* default ui functions */
2765                 ntype->drawfunc = node_draw_default;
2766                 ntype->drawupdatefunc = node_update_default;
2767                 ntype->select_area_func = node_select_area_default;
2768                 ntype->tweak_area_func = node_tweak_area_default;
2769                 ntype->uifunc = NULL;
2770                 ntype->uifuncbut = NULL;
2771                 ntype->drawinputfunc = node_draw_input_default;
2772                 ntype->drawoutputfunc = node_draw_output_default;
2773                 ntype->resize_area_func = node_resize_area_default;
2774                 
2775                 node_common_set_butfunc(ntype);
2776                 
2777                 node_composit_set_butfunc(ntype);
2778                 node_shader_set_butfunc(ntype);
2779                 node_texture_set_butfunc(ntype);
2780                 
2781                 /* define update callbacks for socket properties */
2782                 node_template_properties_update(ntype);
2783         NODE_TYPES_END
2784         
2785         /* tree type icons */
2786         ntreeType_Composite->ui_icon = ICON_RENDERLAYERS;
2787         ntreeType_Composite->draw_add_menu = node_add_menu_default;
2788         ntreeType_Shader->ui_icon = ICON_MATERIAL;
2789         ntreeType_Shader->draw_add_menu = node_add_menu_default;
2790         ntreeType_Texture->ui_icon = ICON_TEXTURE;
2791         ntreeType_Texture->draw_add_menu = node_add_menu_default;
2792 }
2793
2794 void ED_init_custom_node_type(bNodeType *ntype)
2795 {
2796         /* default ui functions */
2797         ntype->drawfunc = node_draw_default;
2798         ntype->drawupdatefunc = node_update_default;
2799         ntype->drawinputfunc = node_draw_input_default;
2800         ntype->drawoutputfunc = node_draw_output_default;
2801         ntype->resize_area_func = node_resize_area_default;
2802         ntype->select_area_func = node_select_area_default;
2803         ntype->tweak_area_func = node_tweak_area_default;
2804 }
2805
2806 void ED_init_custom_node_socket_type(bNodeSocketType *stype)
2807 {
2808         /* default ui functions */
2809         stype->draw = node_socket_button_label;
2810 }
2811
2812 /* maps standard socket integer type to a color */
2813 static const float std_node_socket_colors[][4] = {
2814     {0.63, 0.63, 0.63, 1.0},    /* SOCK_FLOAT */
2815     {0.39, 0.39, 0.78, 1.0},    /* SOCK_VECTOR */
2816     {0.78, 0.78, 0.16, 1.0},    /* SOCK_RGBA */
2817     {0.39, 0.78, 0.39, 1.0},    /* SOCK_SHADER */
2818     {0.70, 0.65, 0.19, 1.0},    /* SOCK_BOOLEAN */
2819     {0.0, 0.0, 0.0, 1.0},       /*__SOCK_MESH (deprecated) */
2820     {0.06, 0.52, 0.15, 1.0},    /* SOCK_INT */
2821     {1.0, 1.0, 1.0, 1.0},       /* SOCK_STRING */
2822 };
2823
2824 /* common color callbacks for standard types */
2825 static void std_node_socket_draw_color(bContext *UNUSED(C), PointerRNA *ptr, PointerRNA *UNUSED(node_ptr), float *r_color)
2826 {
2827         bNodeSocket *sock = ptr->data;
2828         int type = sock->typeinfo->type;
2829         copy_v4_v4(r_color, std_node_socket_colors[type]);
2830 }
2831 static void std_node_socket_interface_draw_color(bContext *UNUSED(C), PointerRNA *ptr, float *r_color)
2832 {
2833         bNodeSocket *sock = ptr->data;
2834         int type = sock->typeinfo->type;
2835         copy_v4_v4(r_color, std_node_socket_colors[type]);
2836 }
2837
2838 static void std_node_socket_draw(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *node_ptr)
2839 {
2840         bNodeSocket *sock = ptr->data;
2841         int type = sock->typeinfo->type;
2842         /*int subtype = sock->typeinfo->subtype;*/
2843         
2844         switch (type) {
2845                 case SOCK_FLOAT:
2846                 case SOCK_INT:
2847                 case SOCK_BOOLEAN:
2848                         uiItemR(layout, ptr, "default_value", 0, sock->name, 0);
2849                         break;
2850                 case SOCK_VECTOR:
2851                         uiTemplateComponentMenu(layout, ptr, "default_value", sock->name);
2852                         break;
2853                 case SOCK_RGBA: {
2854                         uiLayout *row = uiLayoutRow(layout, false);
2855                         uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_LEFT);
2856                         /* draw the socket name right of the actual button */
2857                         uiItemR(row, ptr, "default_value", 0, "", 0);
2858                         uiItemL(row, sock->name, 0);
2859                         break;
2860                 }
2861                 case SOCK_STRING: {
2862                         uiLayout *row = uiLayoutRow(layout, true);
2863                         /* draw the socket name right of the actual button */
2864                         uiItemR(row, ptr, "default_value", 0, "", 0);
2865                         uiItemL(row, sock->name, 0);
2866                         break;
2867                 }
2868                 
2869                 default:
2870                         node_socket_button_label(C, layout, ptr, node_ptr);
2871                         break;
2872         }
2873 }
2874
2875 void ED_init_standard_node_socket_type(bNodeSocketType *stype)
2876 {
2877         stype->draw = std_node_socket_draw;
2878         stype->draw_color = std_node_socket_draw_color;
2879         stype->interface_draw_color = std_node_socket_interface_draw_color;
2880 }
2881
2882 static void node_socket_virtual_draw_color(bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PointerRNA *UNUSED(node_ptr), float *r_color)
2883 {
2884         /* alpha = 0, empty circle */
2885         zero_v4(r_color);
2886 }
2887
2888 void ED_init_node_socket_type_virtual(bNodeSocketType *stype)
2889 {
2890         stype->draw = node_socket_button_label;
2891         stype->draw_color = node_socket_virtual_draw_color;
2892 }
2893
2894 /* ************** Generic drawing ************** */
2895
2896 void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode)
2897 {
2898         if ((snode->flag & SNODE_BACKDRAW) && ED_node_is_compositor(snode)) {
2899                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2900                 void *lock;
2901                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
2902                 if (ibuf) {
2903                         float x, y; 
2904                         unsigned char *display_buffer;
2905                         void *cache_handle;
2906                         
2907                         glMatrixMode(GL_PROJECTION);
2908                         glPushMatrix();
2909                         glMatrixMode(GL_MODELVIEW);
2910                         glPushMatrix();
2911
2912                         /* keep this, saves us from a version patch */
2913                         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2914                         
2915                         /* somehow the offset has to be calculated inverse */
2916                         
2917                         glaDefine2DArea(&ar->winrct);
2918                         /* ortho at pixel level curarea */
2919                         wmOrtho2(-GLA_PIXEL_OFS, ar->winx - GLA_PIXEL_OFS, -GLA_PIXEL_OFS, ar->winy - GLA_PIXEL_OFS);
2920                         
2921                         x = (ar->winx - snode->zoom * ibuf->x) / 2 + snode->xof;
2922                         y = (ar->winy - snode->zoom * ibuf->y) / 2 + snode->yof;
2923                         
2924
2925                         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
2926
2927                         if (display_buffer) {
2928                                 if (snode->flag & (SNODE_SHOW_R | SNODE_SHOW_G | SNODE_SHOW_B)) {
2929                                         int ofs;
2930
2931 #ifdef __BIG_ENDIAN__
2932                                         if      (snode->flag & SNODE_SHOW_R) ofs = 2;
2933                                         else if (snode->flag & SNODE_SHOW_G) ofs = 1;
2934                                         else                                 ofs = 0;
2935 #else
2936                                         if      (snode->flag & SNODE_SHOW_R) ofs = 1;
2937                                         else if (snode->flag & SNODE_SHOW_G) ofs = 2;
2938                                         else                                 ofs = 3;
2939 #endif
2940
2941                                         glPixelZoom(snode->zoom, snode->zoom);
2942                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2943
2944                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT,
2945                                                           display_buffer + ofs);
2946
2947                                         glPixelZoom(1.0f, 1.0f);
2948                                 }
2949                                 else if (snode->flag & SNODE_SHOW_ALPHA) {
2950                                         glPixelZoom(snode->zoom, snode->zoom);
2951                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2952 #ifdef __BIG_ENDIAN__
2953                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
2954 #endif
2955                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, display_buffer);
2956
2957 #ifdef __BIG_ENDIAN__
2958                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
2959 #endif
2960                                         glPixelZoom(1.0f, 1.0f);
2961                                 }
2962                                 else if (snode->flag & SNODE_USE_ALPHA) {
2963                                         glEnable(GL_BLEND);
2964                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2965                                         glPixelZoom(snode->zoom, snode->zoom);
2966                                         
2967                                         glaDrawPixelsAuto(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer);
2968                                         
2969                                         glPixelZoom(1.0f, 1.0f);
2970                                         glDisable(GL_BLEND);
2971                                 }
2972                                 else {
2973                                         glPixelZoom(snode->zoom, snode->zoom);
2974
2975                                         glaDrawPixelsAuto(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, GL_NEAREST, display_buffer);
2976                                         
2977                                         glPixelZoom(1.0f, 1.0f);
2978                                 }
2979                         }
2980
2981                         IMB_display_buffer_release(cache_handle);
2982
2983                         /** @note draw selected info on backdrop */
2984                         if (snode->edittree) {
2985                                 bNode *node = snode->edittree->nodes.first;
2986                                 rctf *viewer_border = &snode->edittree->viewer_border;
2987                                 while (node) {
2988                                         if (node->flag & NODE_SELECT) {
2989                                                 if (node->typeinfo->uibackdropfunc) {
2990                                                         node->typeinfo->uibackdropfunc(snode, ibuf, node, x, y);
2991                                                 }
2992                                         }
2993                                         node = node->next;
2994                                 }
2995
2996                                 if ((snode->edittree->flag & NTREE_VIEWER_BORDER) &&
2997                                     viewer_border->xmin < viewer_border->xmax &&
2998                                     viewer_border->ymin < viewer_border->ymax)
2999                                 {
3000                                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
3001                                         setlinestyle(3);
3002                                         cpack(0x4040FF);
3003
3004                                         glRectf(x + snode->zoom * viewer_border->xmin * ibuf->x,
3005                                                 y + snode->zoom * viewer_border->ymin * ibuf->y,
3006                                                 x + snode->zoom * viewer_border->xmax * ibuf->x,
3007                                                 y + snode->zoom * viewer_border->ymax * ibuf->y);
3008
3009                                         setlinestyle(0);
3010                                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
3011                                 }
3012                         }
3013                         
3014                         glMatrixMode(GL_PROJECTION);
3015                         glPopMatrix();
3016                         glMatrixMode(GL_MODELVIEW);
3017                         glPopMatrix();
3018                 }
3019
3020                 BKE_image_release_ibuf(ima, ibuf, lock);
3021         }
3022 }
3023
3024
3025 /* if v2d not NULL, it clips and returns 0 if not visible */
3026 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
3027 {
3028         float dist, vec[4][2];
3029         float deltax, deltay;
3030         int toreroute, fromreroute;
3031         /* in v0 and v3 we put begin/end points */
3032         if (link->fromsock) {
3033                 vec[0][0] = link->fromsock->locx;
3034                 vec[0][1] = link->fromsock->locy;
3035                 fromreroute = (link->fromnode && link->fromnode->type == NODE_REROUTE);
3036         }
3037         else {
3038                 if (snode == NULL) return 0;
3039                 copy_v2_v2(vec[0], snode->cursor);
3040                 fromreroute = 0;
3041         }
3042         if (link->tosock) {
3043                 vec[3][0] = link->tosock->locx;
3044                 vec[3][1] = link->tosock->locy;
3045                 toreroute = (link->tonode && link->tonode->type == NODE_REROUTE);
3046         }
3047         else {
3048                 if (snode == NULL) return 0;
3049                 copy_v2_v2(vec[3], snode->cursor);
3050                 toreroute = 0;
3051         }
3052
3053         dist = UI_GetThemeValue(TH_NODE_CURVING) * 0.10f * ABS(vec[0][0] - vec[3][0]);
3054         deltax = vec[3][0] - vec[0][0];
3055         deltay = vec[3][1] - vec[0][1];
3056         /* check direction later, for top sockets */
3057         if (fromreroute) {
3058                 if (ABS(deltax) > ABS(deltay)) {
3059                         vec[1][1] = vec[0][1];
3060                         vec[1][0] = vec[0][0] + (deltax > 0 ? dist : -dist);
3061                 }
3062                 else {
3063                         vec[1][0] = vec[0][0];
3064                         vec[1][1] = vec[0][1] + (deltay > 0 ? dist : -dist);
3065                 }
3066         }
3067         else {
3068                 vec[1][0] = vec[0][0] + dist;
3069                 vec[1][1] = vec[0][1];
3070         }
3071         if (toreroute) {
3072                 if (ABS(deltax) > ABS(deltay)) {
3073                         vec[2][1] = vec[3][1];
3074                         vec[2][0] = vec[3][0] + (deltax > 0 ? -dist : dist);
3075                 }
3076                 else {
3077                         vec[2][0] = vec[3][0];
3078                         vec[2][1] = vec[3][1] + (deltay > 0 ? -dist : dist);
3079                 }
3080
3081         }
3082         else {
3083                 vec[2][0] = vec[3][0] - dist;
3084                 vec[2][1] = vec[3][1];
3085         }
3086         if (v2d && min_ffff(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax) {
3087                 /* clipped */
3088         }
3089         else if (v2d && max_ffff(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin) {
3090                 /* clipped */
3091         }
3092         else {
3093                 /* always do all three, to prevent data hanging around */
3094                 BKE_curve_forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0],
3095                                               coord_array[0] + 0, resol, sizeof(float) * 2);
3096                 BKE_curve_forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1],
3097                                               coord_array[0] + 1, resol, sizeof(float) * 2);
3098                 
3099                 return 1;
3100         }
3101         return 0;
3102 }
3103
3104 #define LINK_RESOL  24
3105 #define LINK_ARROW  12  /* position of arrow on the link, LINK_RESOL/2 */
3106 #define ARROW_SIZE 7
3107 void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link,
3108                            int th_col1, int do_shaded, int th_col2, int do_triple, int th_col3)
3109 {
3110         float coord_array[LINK_RESOL + 1][2];
3111         
3112         if (node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
3113                 float dist, spline_step = 0.0f;
3114                 int i;
3115                 int drawarrow;