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