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