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