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