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