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