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