svn merge ^/trunk/blender -r48140:48147
[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         if (RNA_enum_get(ptr, "type") == CMP_NODE_DILATEERODE_DISTANCE_THRESH) {
1773                 uiItemR(layout, ptr, "edge", 0, NULL, ICON_NONE);
1774         }
1775 }
1776
1777 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1778 {
1779         uiLayout *col;
1780         
1781         col = uiLayoutColumn(layout, TRUE);
1782         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1783         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1784 }
1785
1786 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1787 {
1788         uiLayout *col, *row;
1789         
1790         col = uiLayoutColumn(layout, TRUE);
1791    
1792         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1793         row = uiLayoutRow(layout, FALSE);
1794         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1795
1796         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1797         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1798 }
1799
1800 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1801 {
1802         uiLayout *row, *col;
1803         
1804         uiItemL(layout, IFACE_("Despill Channel:"), ICON_NONE);
1805         row = uiLayoutRow(layout, FALSE);
1806         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1807
1808         col = uiLayoutColumn(layout, FALSE);
1809         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1810
1811         if (RNA_enum_get(ptr, "limit_method") == 0) {
1812                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1813                 row = uiLayoutRow(col, FALSE);
1814                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1815         }
1816
1817         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1818         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1819         if (RNA_boolean_get(ptr, "use_unspill") == TRUE) {
1820                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1821                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1822                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1823         }
1824 }
1825
1826 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1827 {
1828         uiLayout *col;
1829         
1830         col = uiLayoutColumn(layout, FALSE);
1831         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1832         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1833         
1834         col = uiLayoutColumn(layout, TRUE);
1835         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1836         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1837         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1838 }
1839
1840 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1841 {
1842         uiLayout *col;
1843         
1844         col = uiLayoutColumn(layout, TRUE);
1845         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1846         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1847         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1848 }
1849
1850 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1851 {       
1852         uiLayout *col, *row;
1853
1854         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1855         row = uiLayoutRow(layout, FALSE);
1856         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1857
1858         col = uiLayoutColumn(layout, FALSE);
1859         uiItemL(col, IFACE_("Key Channel:"), ICON_NONE);
1860         row = uiLayoutRow(col, FALSE);
1861         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1862
1863         col = uiLayoutColumn(layout, FALSE);
1864
1865         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1866         if (RNA_enum_get(ptr, "limit_method") == 0) {
1867                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1868                 row = uiLayoutRow(col, FALSE);
1869                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1870         }
1871
1872         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1873         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1874 }
1875
1876 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1877 {
1878         uiLayout *col;
1879         
1880         col = uiLayoutColumn(layout, TRUE);
1881         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1882         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1883 }
1884
1885 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1886 {
1887         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1888 }
1889
1890 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1891 {
1892         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1893         uiItemR(layout, ptr, "use_smooth_mask", 0, NULL, ICON_NONE);
1894 }
1895
1896 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1897 static void node_draw_input_file_output(const bContext *C, uiBlock *block,
1898                                         bNodeTree *ntree, bNode *node, bNodeSocket *sock,
1899                                         const char *UNUSED(name), int x, int y, int width)
1900 {
1901         uiLayout *layout, *row;
1902         PointerRNA nodeptr, inputptr, imfptr;
1903         int imtype;
1904         int rx, ry;
1905         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
1906         
1907         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, x, y + NODE_DY, width, 20, UI_GetStyle());
1908         row = uiLayoutRow(layout, FALSE);
1909         
1910         imfptr = RNA_pointer_get(&nodeptr, "format");
1911         imtype = RNA_enum_get(&imfptr, "file_format");
1912         if (imtype == R_IMF_IMTYPE_MULTILAYER) {
1913                 NodeImageMultiFileSocket *input = sock->storage;
1914                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotLayer, input, &inputptr);
1915                 
1916                 uiItemL(row, input->layer, 0);
1917         }
1918         else {
1919                 NodeImageMultiFileSocket *input = sock->storage;
1920                 PropertyRNA *imtype_prop;
1921                 const char *imtype_name;
1922                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotFile, input, &inputptr);
1923                 
1924                 uiItemL(row, input->path, 0);
1925                 
1926                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1927                         imfptr = RNA_pointer_get(&inputptr, "format");
1928                 
1929                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1930                 RNA_property_enum_name((bContext *)C, &imfptr, imtype_prop, RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1931                 uiBlockSetEmboss(block, UI_EMBOSSP);
1932                 uiItemL(row, imtype_name, 0);
1933                 uiBlockSetEmboss(block, UI_EMBOSSN);
1934         }
1935         
1936         uiBlockLayoutResolve(block, &rx, &ry);
1937 }
1938 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1939 {
1940         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1941         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1942         
1943         if (multilayer)
1944                 uiItemL(layout, IFACE_("Path:"), 0);
1945         else
1946                 uiItemL(layout, IFACE_("Base Path:"), 0);
1947         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1948 }
1949 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1950 {
1951         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1952         PointerRNA active_input_ptr, op_ptr;
1953         uiLayout *row;
1954         int active_index;
1955         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1956         
1957         node_composit_buts_file_output(layout, C, ptr);
1958         uiTemplateImageSettings(layout, &imfptr);
1959         
1960         uiItemS(layout);
1961         
1962         uiItemO(layout, IFACE_("Add Input"), ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
1963         
1964         active_index = RNA_int_get(ptr, "active_input_index");
1965         /* using different collection properties if multilayer format is enabled */
1966         if (multilayer) {
1967                 uiTemplateList(layout, C, ptr, "layer_slots", ptr, "active_input_index", NULL, 0, 0, 0);
1968                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "layer_slots"), active_index, &active_input_ptr);
1969         }
1970         else {
1971                 uiTemplateList(layout, C, ptr, "file_slots", ptr, "active_input_index", NULL, 0, 0, 0);
1972                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_slots"), active_index, &active_input_ptr);
1973         }
1974         /* XXX collection lookup does not return the ID part of the pointer, setting this manually here */
1975         active_input_ptr.id.data = ptr->id.data;
1976         
1977         row = uiLayoutRow(layout, TRUE);
1978         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1979         RNA_enum_set(&op_ptr, "direction", 1);
1980         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1981         RNA_enum_set(&op_ptr, "direction", 2);
1982         
1983         if (active_input_ptr.data) {
1984                 if (multilayer) {
1985                         uiLayout *row, *col;
1986                         col = uiLayoutColumn(layout, TRUE);
1987                         
1988                         uiItemL(col, IFACE_("Layer:"), 0);
1989                         row = uiLayoutRow(col, FALSE);
1990                         uiItemR(row, &active_input_ptr, "name", 0, "", 0);
1991                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
1992                 }
1993                 else {
1994                         uiLayout *row, *col;
1995                         col = uiLayoutColumn(layout, TRUE);
1996                         
1997                         uiItemL(col, IFACE_("File Path:"), 0);
1998                         row = uiLayoutRow(col, FALSE);
1999                         uiItemR(row, &active_input_ptr, "path", 0, "", 0);
2000                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2001                         
2002                         /* format details for individual files */
2003                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
2004                         
2005                         col = uiLayoutColumn(layout, TRUE);
2006                         uiItemL(col, IFACE_("Format:"), 0);
2007                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, 0);
2008                         
2009                         col = uiLayoutColumn(layout, FALSE);
2010                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format") == FALSE);
2011                         uiTemplateImageSettings(col, &imfptr);
2012                 }
2013         }
2014 }
2015
2016 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2017 {
2018         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
2019
2020         if (RNA_enum_get(ptr, "space") == CMP_SCALE_RENDERPERCENT) {
2021                 uiLayout *row;
2022                 uiItemR(layout, ptr, "frame_method", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2023                 row = uiLayoutRow(layout, TRUE);
2024                 uiItemR(row, ptr, "offset_x", 0, "X", ICON_NONE);
2025                 uiItemR(row, ptr, "offset_y", 0, "Y", ICON_NONE);
2026         }
2027 }
2028
2029 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2030 {
2031         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
2032 }
2033
2034 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2035 {
2036         uiLayout *col;
2037         
2038         col = uiLayoutColumn(layout, FALSE);
2039         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
2040         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
2041 }
2042
2043 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2044 {
2045         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
2046 }
2047
2048 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2049 {
2050         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2051 }
2052
2053 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2054 {
2055         uiLayout *split, *col, *row;
2056         
2057         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2058         
2059         if (RNA_enum_get(ptr, "correction_method") == 0) {
2060         
2061                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2062                 col = uiLayoutColumn(split, FALSE);
2063                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
2064                 row = uiLayoutRow(col, FALSE);
2065                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
2066                 
2067                 col = uiLayoutColumn(split, FALSE);
2068                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
2069                 row = uiLayoutRow(col, FALSE);
2070                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
2071                 
2072                 col = uiLayoutColumn(split, FALSE);
2073                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
2074                 row = uiLayoutRow(col, FALSE);
2075                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
2076
2077         }
2078         else {
2079                 
2080                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2081                 col = uiLayoutColumn(split, FALSE);
2082                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
2083                 row = uiLayoutRow(col, FALSE);
2084                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
2085                 
2086                 col = uiLayoutColumn(split, FALSE);
2087                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
2088                 row = uiLayoutRow(col, FALSE);
2089                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
2090                 
2091                 col = uiLayoutColumn(split, FALSE);
2092                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
2093                 row = uiLayoutRow(col, FALSE);
2094                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
2095         }
2096
2097 }
2098 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2099 {
2100         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2101
2102         if (RNA_enum_get(ptr, "correction_method") == 0) {
2103
2104                 uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
2105                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
2106
2107                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
2108                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
2109
2110                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
2111                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
2112         }
2113         else {
2114                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
2115                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
2116
2117                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
2118                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
2119
2120                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
2121                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
2122         }
2123 }
2124
2125
2126 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2127 {
2128         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
2129 }
2130
2131 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2132
2133         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
2134 }
2135
2136 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
2137 {
2138         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2139 }
2140
2141 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
2142 {
2143         bNode *node = ptr->data;
2144
2145         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2146
2147         if (!node->id)
2148                 return;
2149
2150         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2151 }
2152
2153 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2154 {
2155         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2156 }
2157
2158 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
2159 {
2160         bNode *node = ptr->data;
2161
2162         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2163
2164         if (!node->id)
2165                 return;
2166
2167         uiItemR(layout, ptr, "distortion_type", 0, "", 0);
2168 }
2169
2170 static void node_composit_buts_colorcorrection(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2171 {
2172         uiLayout *row;
2173         
2174         row = uiLayoutRow(layout, FALSE);
2175         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2176         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2177         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2178
2179         row = uiLayoutRow(layout, FALSE);
2180         uiItemL(row, "", 0);
2181         uiItemL(row, IFACE_("Saturation"), 0);
2182         uiItemL(row, IFACE_("Contrast"), 0);
2183         uiItemL(row, IFACE_("Gamma"), 0);
2184         uiItemL(row, IFACE_("Gain"), 0);
2185         uiItemL(row, IFACE_("Lift"), 0);
2186
2187         row = uiLayoutRow(layout, FALSE);
2188         uiItemL(row, IFACE_("Master"), 0);
2189         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2190         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2191         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2192         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2193         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2194
2195         row = uiLayoutRow(layout, FALSE);
2196         uiItemL(row, IFACE_("Highlights"), 0);
2197         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2198         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2199         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2200         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2201         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2202
2203         row = uiLayoutRow(layout, FALSE);
2204         uiItemL(row, IFACE_("Midtones"), 0);
2205         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2206         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2207         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2208         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2209         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2210
2211         row = uiLayoutRow(layout, FALSE);
2212         uiItemL(row, IFACE_("Shadows"), 0);
2213         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2214         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2215         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2216         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2217         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2218
2219         row = uiLayoutRow(layout, FALSE);
2220         uiItemR(row, ptr, "midtones_start", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2221         uiItemR(row, ptr, "midtones_end", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2222 }
2223
2224 static void node_composit_buts_colorcorrection_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2225 {
2226         uiLayout *row;
2227         
2228         row = uiLayoutRow(layout, FALSE);
2229         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2230         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2231         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2232         row = layout;
2233         uiItemL(row, IFACE_("Saturation"), 0);
2234         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2235         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2236         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2237         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2238
2239         uiItemL(row, IFACE_("Contrast"), 0);
2240         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2241         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2242         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2243         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2244
2245         uiItemL(row, IFACE_("Gamma"), 0);
2246         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2247         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2248         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2249         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2250
2251         uiItemL(row, IFACE_("Gain"), 0);
2252         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2253         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2254         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2255         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2256         
2257         uiItemL(row, IFACE_("Lift"), 0);
2258         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2259         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2260         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2261         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2262
2263         row = uiLayoutRow(layout, FALSE);
2264         uiItemR(row, ptr, "midtones_start", 0, NULL, ICON_NONE);
2265         uiItemR(row, ptr, "midtones_end", 0, NULL, ICON_NONE);
2266 }
2267
2268 static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2269 {
2270         uiItemR(layout, ptr, "check", 0, NULL, ICON_NONE);
2271 }
2272
2273 static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2274 {
2275         uiLayout *row;
2276         
2277         row = uiLayoutRow(layout, TRUE);
2278         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2279         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2280         
2281         row = uiLayoutRow(layout, TRUE);
2282         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2283         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2284
2285         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2286         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2287 }
2288
2289 static void node_composit_buts_bokehimage(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2290 {
2291         uiItemR(layout, ptr, "flaps", 0, NULL, ICON_NONE);
2292         uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
2293         uiItemR(layout, ptr, "rounding", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2294         uiItemR(layout, ptr, "catadioptric", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2295         uiItemR(layout, ptr, "shift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2296 }
2297
2298 void node_composit_backdrop_viewer(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2299 {
2300 //      node_composit_backdrop_canvas(snode, backdrop, node, x, y);
2301         if (node->custom1 == 0) {
2302                 const float backdropWidth = backdrop->x;
2303                 const float backdropHeight = backdrop->y;
2304                 const float cx  = x + snode->zoom * backdropWidth * node->custom3;
2305                 const float cy = y + snode->zoom * backdropHeight * node->custom4;
2306
2307                 glColor3f(1.0, 1.0, 1.0);
2308
2309                 glBegin(GL_LINES);
2310                 glVertex2f(cx - 25, cy - 25);
2311                 glVertex2f(cx + 25, cy + 25);
2312                 glVertex2f(cx + 25, cy - 25);
2313                 glVertex2f(cx - 25, cy + 25);
2314                 glEnd();
2315         }
2316 }
2317
2318 void node_composit_backdrop_boxmask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2319 {
2320         NodeBoxMask *boxmask = node->storage;
2321         const float backdropWidth = backdrop->x;
2322         const float backdropHeight = backdrop->y;
2323         const float aspect = backdropWidth / backdropHeight;
2324         const float rad = DEG2RADF(-boxmask->rotation);
2325         const float cosine = cosf(rad);
2326         const float sine = sinf(rad);
2327         const float halveBoxWidth = backdropWidth * (boxmask->width / 2.0f);
2328         const float halveBoxHeight = backdropHeight * (boxmask->height / 2.0f) * aspect;
2329
2330         float cx, cy, x1, x2, x3, x4;
2331         float y1, y2, y3, y4;
2332
2333
2334         /* keep this, saves us from a version patch */
2335         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2336
2337         glColor3f(1.0, 1.0, 1.0);
2338
2339         cx  = x + snode->zoom * backdropWidth * boxmask->x;
2340         cy = y + snode->zoom * backdropHeight * boxmask->y;
2341
2342         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2343         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2344         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2345         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2346         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2347         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2348         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2349         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2350
2351         glBegin(GL_LINE_LOOP);
2352         glVertex2f(x1, y1);
2353         glVertex2f(x2, y2);
2354         glVertex2f(x3, y3);
2355         glVertex2f(x4, y4);
2356         glEnd();
2357 }
2358
2359 void node_composit_backdrop_ellipsemask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2360 {
2361         NodeEllipseMask *ellipsemask = node->storage;
2362         const float backdropWidth = backdrop->x;
2363         const float backdropHeight = backdrop->y;
2364         const float aspect = backdropWidth / backdropHeight;
2365         const float rad = DEG2RADF(-ellipsemask->rotation);
2366         const float cosine = cosf(rad);
2367         const float sine = sinf(rad);
2368         const float halveBoxWidth = backdropWidth * (ellipsemask->width / 2.0f);
2369         const float halveBoxHeight = backdropHeight * (ellipsemask->height / 2.0f) * aspect;
2370
2371         float cx, cy, x1, x2, x3, x4;
2372         float y1, y2, y3, y4;
2373
2374
2375         /* keep this, saves us from a version patch */
2376         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2377
2378         glColor3f(1.0, 1.0, 1.0);
2379
2380         cx  = x + snode->zoom * backdropWidth * ellipsemask->x;
2381         cy = y + snode->zoom * backdropHeight * ellipsemask->y;
2382
2383         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2384         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2385         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2386         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2387         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2388         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2389         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2390         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2391
2392         glBegin(GL_LINE_LOOP);
2393
2394         glVertex2f(x1, y1);
2395         glVertex2f(x2, y2);
2396         glVertex2f(x3, y3);
2397         glVertex2f(x4, y4);
2398         glEnd();
2399 }
2400
2401 static void node_composit_buts_ellipsemask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2402 {
2403         uiLayout *row;
2404         row = uiLayoutRow(layout, TRUE);
2405         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2406         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2407         row = uiLayoutRow(layout, TRUE);
2408         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2409         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2410
2411         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2412         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2413 }
2414
2415 static void node_composit_buts_viewer_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2416 {
2417         uiLayout *col;
2418         
2419         uiItemR(layout, ptr, "tile_order", 0, NULL, ICON_NONE);
2420         if (RNA_enum_get(ptr, "tile_order") == 0) {
2421                 col = uiLayoutColumn(layout, TRUE);
2422                 uiItemR(col, ptr, "center_x", 0, NULL, ICON_NONE);
2423                 uiItemR(col, ptr, "center_y", 0, NULL, ICON_NONE);
2424         }
2425 }
2426
2427 static void node_composit_buts_mask(uiLayout *layout, bContext *C, PointerRNA *ptr)
2428 {
2429         uiTemplateID(layout, C, ptr, "mask", NULL, NULL, NULL);
2430         uiItemR(layout, ptr, "smooth_mask", 0, NULL, ICON_NONE);
2431
2432 }
2433
2434 static void node_composit_buts_keyingscreen(uiLayout *layout, bContext *C, PointerRNA *ptr)
2435 {
2436         bNode *node= ptr->data;
2437
2438         uiTemplateID(layout, C, ptr, "clip", NULL, NULL, NULL);
2439
2440         if (node->id) {
2441                 MovieClip *clip = (MovieClip *) node->id;
2442                 uiLayout *col;
2443                 PointerRNA tracking_ptr;
2444
2445                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, &clip->tracking, &tracking_ptr);
2446
2447                 col = uiLayoutColumn(layout, TRUE);
2448                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2449         }
2450 }
2451
2452 static void node_composit_buts_keying(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2453 {
2454         /* bNode *node= ptr->data; */ /* UNUSED */
2455
2456         uiItemR(layout, ptr, "blur_pre", 0, NULL, ICON_NONE);
2457         uiItemR(layout, ptr, "screen_balance", 0, NULL, ICON_NONE);
2458         uiItemR(layout, ptr, "despill_factor", 0, NULL, ICON_NONE);
2459         uiItemR(layout, ptr, "edge_kernel_radius", 0, NULL, ICON_NONE);
2460         uiItemR(layout, ptr, "edge_kernel_tolerance", 0, NULL, ICON_NONE);
2461         uiItemR(layout, ptr, "clip_black", 0, NULL, ICON_NONE);
2462         uiItemR(layout, ptr, "clip_white", 0, NULL, ICON_NONE);
2463         uiItemR(layout, ptr, "dilate_distance", 0, NULL, ICON_NONE);
2464         uiItemR(layout, ptr, "blur_post", 0, NULL, ICON_NONE);
2465 }
2466
2467 static void node_composit_buts_trackpos(uiLayout *layout, bContext *C, PointerRNA *ptr)
2468 {
2469         bNode *node= ptr->data;
2470
2471         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2472
2473         if (node->id) {
2474                 MovieClip *clip = (MovieClip *) node->id;
2475                 MovieTracking *tracking = &clip->tracking;
2476                 MovieTrackingObject *object;
2477                 uiLayout *col;
2478                 PointerRNA tracking_ptr;
2479                 NodeTrackPosData *data = node->storage;
2480
2481                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, tracking, &tracking_ptr);
2482
2483                 col = uiLayoutColumn(layout, 0);
2484                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2485
2486                 object = BKE_tracking_object_get_named(tracking, data->tracking_object);
2487                 if (object) {
2488                         PointerRNA object_ptr;
2489
2490                         RNA_pointer_create(&clip->id, &RNA_MovieTrackingObject, object, &object_ptr);
2491
2492                         uiItemPointerR(col, ptr, "track_name", &object_ptr, "tracks", "", ICON_ANIM_DATA);
2493                 }
2494                 else {
2495                         uiItemR(layout, ptr, "track_name", 0, "", ICON_ANIM_DATA);
2496                 }
2497
2498                 uiItemR(layout, ptr, "use_relative", 0, NULL, ICON_NONE);
2499         }
2500 }
2501
2502 /* only once called */
2503 static void node_composit_set_butfunc(bNodeType *ntype)
2504 {
2505         switch (ntype->type) {
2506                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2507
2508                 case CMP_NODE_IMAGE:
2509                         ntype->uifunc = node_composit_buts_image;
2510                         break;
2511                 case CMP_NODE_R_LAYERS:
2512                         ntype->uifunc = node_composit_buts_renderlayers;
2513                         break;
2514                 case CMP_NODE_NORMAL:
2515                         ntype->uifunc = node_buts_normal;
2516                         break;
2517                 case CMP_NODE_CURVE_VEC:
2518                         ntype->uifunc = node_buts_curvevec;
2519                         break;
2520                 case CMP_NODE_CURVE_RGB:
2521                         ntype->uifunc = node_buts_curvecol;
2522                         break;
2523                 case CMP_NODE_VALUE:
2524                         ntype->uifunc = node_buts_value;
2525                         break;
2526                 case CMP_NODE_RGB:
2527                         ntype->uifunc = node_buts_rgb;
2528                         break;
2529                 case CMP_NODE_FLIP:
2530                         ntype->uifunc = node_composit_buts_flip;
2531                         break;
2532                 case CMP_NODE_SPLITVIEWER:
2533                         ntype->uifunc = node_composit_buts_splitviewer;
2534                         break;
2535                 case CMP_NODE_MIX_RGB:
2536                         ntype->uifunc = node_buts_mix_rgb;
2537                         break;
2538                 case CMP_NODE_VALTORGB:
2539                         ntype->uifunc = node_buts_colorramp;
2540                         break;
2541                 case CMP_NODE_CROP:
2542                         ntype->uifunc = node_composit_buts_crop;
2543                         break;
2544                 case CMP_NODE_BLUR:
2545                         ntype->uifunc = node_composit_buts_blur;
2546                         break;
2547                 case CMP_NODE_DBLUR:
2548                         ntype->uifunc = node_composit_buts_dblur;
2549                         break;
2550                 case CMP_NODE_BILATERALBLUR:
2551                         ntype->uifunc = node_composit_buts_bilateralblur;
2552                         break;
2553                 case CMP_NODE_DEFOCUS:
2554                         ntype->uifunc = node_composit_buts_defocus;
2555                         break;
2556                 case CMP_NODE_GLARE:
2557                         ntype->uifunc = node_composit_buts_glare;
2558                         break;
2559                 case CMP_NODE_TONEMAP:
2560                         ntype->uifunc = node_composit_buts_tonemap;
2561                         break;
2562                 case CMP_NODE_LENSDIST:
2563                         ntype->uifunc = node_composit_buts_lensdist;
2564                         break;
2565                 case CMP_NODE_VECBLUR:
2566                         ntype->uifunc = node_composit_buts_vecblur;
2567                         break;
2568                 case CMP_NODE_FILTER:
2569                         ntype->uifunc = node_composit_buts_filter;
2570                         break;
2571                 case CMP_NODE_MAP_VALUE:
2572                         ntype->uifunc = node_composit_buts_map_value;
2573                         break;
2574                 case CMP_NODE_TIME:
2575                         ntype->uifunc = node_buts_time;
2576                         break;
2577                 case CMP_NODE_ALPHAOVER:
2578                         ntype->uifunc = node_composit_buts_alphaover;
2579                         break;
2580                 case CMP_NODE_HUE_SAT:
2581                         ntype->uifunc = node_composit_buts_hue_sat;
2582                         break;
2583                 case CMP_NODE_TEXTURE:
2584                         ntype->uifunc = node_buts_texture;
2585                         break;
2586                 case CMP_NODE_DILATEERODE:
2587                         ntype->uifunc = node_composit_buts_dilateerode;
2588                         break;
2589                 case CMP_NODE_OUTPUT_FILE:
2590                         ntype->uifunc = node_composit_buts_file_output;
2591                         ntype->uifuncbut = node_composit_buts_file_output_details;
2592                         ntype->drawinputfunc = node_draw_input_file_output;
2593                         break;
2594                 case CMP_NODE_DIFF_MATTE:
2595                         ntype->uifunc = node_composit_buts_diff_matte;
2596                         break;
2597                 case CMP_NODE_DIST_MATTE:
2598                         ntype->uifunc = node_composit_buts_distance_matte;
2599                         break;
2600                 case CMP_NODE_COLOR_SPILL:
2601                         ntype->uifunc = node_composit_buts_color_spill;
2602                         break;
2603                 case CMP_NODE_CHROMA_MATTE:
2604                         ntype->uifunc = node_composit_buts_chroma_matte;
2605                         break;
2606                 case CMP_NODE_COLOR_MATTE:
2607                         ntype->uifunc = node_composit_buts_color_matte;
2608                         break;
2609                 case CMP_NODE_SCALE:
2610                         ntype->uifunc = node_composit_buts_scale;
2611                         break;
2612                 case CMP_NODE_ROTATE:
2613                         ntype->uifunc = node_composit_buts_rotate;
2614                         break;
2615                 case CMP_NODE_CHANNEL_MATTE:
2616                         ntype->uifunc = node_composit_buts_channel_matte;
2617                         break;
2618                 case CMP_NODE_LUMA_MATTE:
2619                         ntype->uifunc = node_composit_buts_luma_matte;
2620                         break;
2621                 case CMP_NODE_MAP_UV:
2622                         ntype->uifunc = node_composit_buts_map_uv;
2623                         break;
2624                 case CMP_NODE_ID_MASK:
2625                         ntype->uifunc = node_composit_buts_id_mask;
2626                         break;
2627                 case CMP_NODE_DOUBLEEDGEMASK:
2628                         ntype->uifunc = node_composit_buts_double_edge_mask;
2629                         break;
2630                 case CMP_NODE_MATH:
2631                         ntype->uifunc = node_buts_math;
2632                         break;
2633                 case CMP_NODE_INVERT:
2634                         ntype->uifunc = node_composit_buts_invert;
2635                         break;
2636                 case CMP_NODE_PREMULKEY:
2637                         ntype->uifunc = node_composit_buts_premulkey;
2638                         break;
2639                 case CMP_NODE_VIEW_LEVELS:
2640                         ntype->uifunc = node_composit_buts_view_levels;
2641                         break;
2642                 case CMP_NODE_COLORBALANCE:
2643                         ntype->uifunc = node_composit_buts_colorbalance;
2644                         ntype->uifuncbut = node_composit_buts_colorbalance_but;
2645                         break;
2646                 case CMP_NODE_HUECORRECT:
2647                         ntype->uifunc = node_composit_buts_huecorrect;
2648                         break;
2649                 case CMP_NODE_ZCOMBINE:
2650                         ntype->uifunc = node_composit_buts_zcombine;
2651                         break;
2652                 case CMP_NODE_COMBYCCA:
2653                 case CMP_NODE_SEPYCCA:
2654                         ntype->uifunc = node_composit_buts_ycc;
2655                         break;
2656                 case CMP_NODE_MOVIECLIP:
2657                         ntype->uifunc = node_composit_buts_movieclip;
2658                         break;
2659                 case CMP_NODE_STABILIZE2D:
2660                         ntype->uifunc = node_composit_buts_stabilize2d;
2661                         break;
2662                 case CMP_NODE_TRANSFORM:
2663                         ntype->uifunc = node_composit_buts_transform;
2664                         break;
2665                 case CMP_NODE_MOVIEDISTORTION:
2666                         ntype->uifunc = node_composit_buts_moviedistortion;
2667                         break;
2668                 case CMP_NODE_COLORCORRECTION:
2669                         ntype->uifunc = node_composit_buts_colorcorrection;
2670                         ntype->uifuncbut = node_composit_buts_colorcorrection_but;
2671                         break;
2672                 case CMP_NODE_SWITCH:
2673                         ntype->uifunc = node_composit_buts_switch;
2674                         break;
2675                 case CMP_NODE_MASK_BOX:
2676                         ntype->uifunc = node_composit_buts_boxmask;
2677                         ntype->uibackdropfunc = node_composit_backdrop_boxmask;
2678                         break;
2679                 case CMP_NODE_MASK_ELLIPSE:
2680                         ntype->uifunc = node_composit_buts_ellipsemask;
2681                         ntype->uibackdropfunc = node_composit_backdrop_ellipsemask;
2682                         break;
2683                 case CMP_NODE_BOKEHIMAGE:
2684                         ntype->uifunc = node_composit_buts_bokehimage;
2685                         break;
2686                 case CMP_NODE_VIEWER:
2687                         ntype->uifunc = NULL;
2688                         ntype->uifuncbut = node_composit_buts_viewer_but;
2689                         ntype->uibackdropfunc = node_composit_backdrop_viewer;
2690                         break;
2691                 case CMP_NODE_MASK:
2692                         ntype->uifunc= node_composit_buts_mask;
2693                         break;
2694                 case CMP_NODE_KEYINGSCREEN:
2695                         ntype->uifunc = node_composit_buts_keyingscreen;
2696                         break;
2697                 case CMP_NODE_KEYING:
2698                         ntype->uifunc = node_composit_buts_keying;
2699                         break;
2700                 case CMP_NODE_TRACKPOS:
2701                         ntype->uifunc = node_composit_buts_trackpos;
2702                         break;
2703                 default:
2704                         ntype->uifunc = NULL;
2705         }
2706 }
2707
2708 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2709
2710 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2711 {
2712         uiLayout *col;
2713         
2714         col = uiLayoutColumn(layout, TRUE);
2715         uiItemR(col, ptr, "offset", 0, IFACE_("Offset"), ICON_NONE);
2716         uiItemR(col, ptr, "offset_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2717         
2718         col = uiLayoutColumn(layout, TRUE);
2719         uiItemR(col, ptr, "squash", 0, IFACE_("Squash"), ICON_NONE);
2720         uiItemR(col, ptr, "squash_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2721 }
2722
2723 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2724 {
2725         PointerRNA tex_ptr;
2726         bNode *node = ptr->data;
2727         ID *id = ptr->id.data;
2728         Tex *tex = (Tex *)node->storage;
2729         uiLayout *col, *row;
2730         
2731         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2732
2733         col = uiLayoutColumn(layout, FALSE);
2734
2735         switch (tex->type) {
2736                 case TEX_BLEND:
2737                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2738                         row = uiLayoutRow(col, FALSE);
2739                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2740                         break;
2741
2742                 case TEX_MARBLE:
2743                         row = uiLayoutRow(col, FALSE);
2744                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2745                         row = uiLayoutRow(col, FALSE);
2746                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2747                         row = uiLayoutRow(col, FALSE);
2748                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2749                         row = uiLayoutRow(col, FALSE);
2750                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2751                         break;
2752
2753                 case TEX_MAGIC:
2754                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2755                         break;
2756
2757                 case TEX_STUCCI:
2758                         row = uiLayoutRow(col, FALSE);
2759                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2760                         row = uiLayoutRow(col, FALSE);
2761                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2762                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2763                         break;
2764
2765                 case TEX_WOOD:
2766                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2767                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2768                         row = uiLayoutRow(col, FALSE);
2769                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2770                         row = uiLayoutRow(col, FALSE);
2771                         uiLayoutSetActive(row, !(RNA_enum_get(&tex_ptr, "wood_type") == TEX_BAND || RNA_enum_get(&tex_ptr, "wood_type") == TEX_RING));
2772                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2773                         break;
2774                         
2775                 case TEX_CLOUDS:
2776                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2777                         row = uiLayoutRow(col, FALSE);
2778                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2779                         row = uiLayoutRow(col, FALSE);
2780                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2781                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, IFACE_("Depth"), ICON_NONE);
2782                         break;
2783                         
2784                 case TEX_DISTNOISE:
2785                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2786                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2787                         break;
2788
2789                 case TEX_MUSGRAVE:
2790                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2791                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2792                         break;
2793                 case TEX_VORONOI:
2794                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2795                         if (tex->vn_distm == TEX_MINKOVSKY) {
2796                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2797                         }
2798                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2799                         break;
2800         }
2801 }
2802
2803 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2804 {
2805         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2806 }
2807
2808 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2809 {
2810         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2811 }
2812
2813 /* only once called */
2814 static void node_texture_set_butfunc(bNodeType *ntype)
2815 {
2816         if (ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX) {
2817                 ntype->uifunc = node_texture_buts_proc;
2818         }
2819         else {
2820                 switch (ntype->type) {
2821
2822                         case TEX_NODE_MATH:
2823                                 ntype->uifunc = node_buts_math;
2824                                 break;
2825
2826                         case TEX_NODE_MIX_RGB:
2827                                 ntype->uifunc = node_buts_mix_rgb;
2828                                 break;
2829
2830                         case TEX_NODE_VALTORGB:
2831                                 ntype->uifunc = node_buts_colorramp;
2832                                 break;
2833
2834                         case TEX_NODE_CURVE_RGB:
2835                                 ntype->uifunc = node_buts_curvecol;
2836                                 break;
2837
2838                         case TEX_NODE_CURVE_TIME:
2839                                 ntype->uifunc = node_buts_time;
2840                                 break;
2841
2842                         case TEX_NODE_TEXTURE:
2843                                 ntype->uifunc = node_buts_texture;
2844                                 break;
2845
2846                         case TEX_NODE_BRICKS:
2847                                 ntype->uifunc = node_texture_buts_bricks;
2848                                 break;
2849
2850                         case TEX_NODE_IMAGE:
2851                                 ntype->uifunc = node_texture_buts_image;
2852                                 break;
2853
2854                         case TEX_NODE_OUTPUT:
2855                                 ntype->uifunc = node_texture_buts_output;
2856                                 break;
2857                 }
2858         }
2859 }
2860
2861 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2862
2863 void ED_init_node_butfuncs(void)
2864 {
2865         bNodeTreeType *treetype;
2866         bNodeType *ntype;
2867         bNodeSocketType *stype;
2868         int i;
2869         
2870         /* node type ui functions */
2871         for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2872                 treetype = ntreeGetType(i);
2873                 if (treetype) {
2874                         for (ntype = treetype->node_types.first; ntype; ntype = ntype->next) {
2875                                 /* default ui functions */
2876                                 ntype->drawfunc = node_draw_default;
2877                                 ntype->drawupdatefunc = node_update_default;
2878                                 ntype->select_area_func = node_select_area_default;
2879                                 ntype->tweak_area_func = node_tweak_area_default;
2880                                 ntype->uifunc = NULL;
2881                                 ntype->uifuncbut = NULL;
2882                                 ntype->drawinputfunc = node_draw_input_default;
2883                                 ntype->drawoutputfunc = node_draw_output_default;
2884                                 ntype->resize_area_func = node_resize_area_default;
2885                                 
2886                                 node_common_set_butfunc(ntype);
2887                                 
2888                                 switch (i) {
2889                                         case NTREE_COMPOSIT:
2890                                                 node_composit_set_butfunc(ntype);
2891                                                 break;
2892                                         case NTREE_SHADER:
2893                                                 node_shader_set_butfunc(ntype);
2894                                                 break;
2895                                         case NTREE_TEXTURE:
2896                                                 node_texture_set_butfunc(ntype);
2897                                                 break;
2898                                 }
2899                         }
2900                 }
2901         }
2902         
2903         /* socket type ui functions */
2904         for (i = 0; i < NUM_SOCKET_TYPES; ++i) {
2905                 stype = ntreeGetSocketType(i);
2906                 if (stype) {
2907                         switch (stype->type) {
2908                                 case SOCK_FLOAT:
2909                                 case SOCK_INT:
2910                                 case SOCK_BOOLEAN:
2911                                         stype->buttonfunc = node_socket_button_default;
2912                                         break;
2913                                 case SOCK_VECTOR:
2914                                         stype->buttonfunc = node_socket_button_components;
2915                                         break;
2916                                 case SOCK_RGBA:
2917                                         stype->buttonfunc = node_socket_button_color;
2918                                         break;
2919                                 case SOCK_SHADER:
2920                                         stype->buttonfunc = node_socket_button_label;
2921                                         break;
2922                                 default:
2923                                         stype->buttonfunc = NULL;
2924                         }
2925                 }
2926         }
2927 }
2928
2929 /* ************** Generic drawing ************** */
2930
2931 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
2932 {
2933         
2934         if ((snode->flag & SNODE_BACKDRAW) && snode->treetype == NTREE_COMPOSIT) {
2935                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2936                 void *lock;
2937                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
2938                 if (ibuf) {
2939                         float x, y; 
2940                         
2941                         glMatrixMode(GL_PROJECTION);
2942                         glPushMatrix();
2943                         glMatrixMode(GL_MODELVIEW);
2944                         glPushMatrix();
2945
2946                         /* keep this, saves us from a version patch */
2947                         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2948                         
2949                         /* somehow the offset has to be calculated inverse */
2950                         
2951                         glaDefine2DArea(&ar->winrct);
2952                         /* ortho at pixel level curarea */
2953                         wmOrtho2(-0.375, ar->winx - 0.375, -0.375, ar->winy - 0.375);
2954                         
2955                         x = (ar->winx - snode->zoom * ibuf->x) / 2 + snode->xof;
2956                         y = (ar->winy - snode->zoom * ibuf->y) / 2 + snode->yof;
2957                         
2958                         if (!ibuf->rect) {
2959                                 if (color_manage)
2960                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
2961                                 else
2962                                         ibuf->profile = IB_PROFILE_NONE;
2963                                 IMB_rect_from_float(ibuf);
2964                         }
2965
2966                         if (ibuf->rect) {
2967                                 if (snode->flag & SNODE_SHOW_ALPHA) {
2968                                         glPixelZoom(snode->zoom, snode->zoom);
2969                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2970                                         if (ENDIAN_ORDER == B_ENDIAN)
2971                                                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
2972                                         
2973                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
2974                                         
2975                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
2976                                         glPixelZoom(1.0f, 1.0f);
2977                                 }
2978                                 else if (snode->flag & SNODE_USE_ALPHA) {
2979                                         glEnable(GL_BLEND);
2980                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2981                                         glPixelZoom(snode->zoom, snode->zoom);
2982                                         
2983                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2984                                         
2985                                         glPixelZoom(1.0f, 1.0f);
2986                                         glDisable(GL_BLEND);
2987                                 }
2988                                 else {
2989                                         glPixelZoom(snode->zoom, snode->zoom);
2990                                         
2991                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2992                                         
2993                                         glPixelZoom(1.0f, 1.0f);
2994                                 }
2995                         }
2996
2997                         /// @note draw selected info on backdrop
2998                         if (snode->edittree) {
2999                                 bNode *node = snode->edittree->nodes.first;
3000                                 while (node) {
3001                                         if (node->flag & NODE_SELECT) {
3002                                                 if (node->typeinfo->uibackdropfunc) {
3003                                                         node->typeinfo->uibackdropfunc(snode, ibuf, node, x, y);
3004                                                 }
3005                                         }
3006                                         node = node->next;
3007                                 }
3008                         }
3009                         
3010                         glMatrixMode(GL_PROJECTION);
3011                         glPopMatrix();
3012                         glMatrixMode(GL_MODELVIEW);
3013                         glPopMatrix();
3014                 }
3015
3016                 BKE_image_release_ibuf(ima, lock);
3017         }
3018 }
3019
3020 #if 0
3021 /* note: needs to be userpref or opengl profile option */
3022 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
3023 {
3024
3025         draw_nodespace_grid(snode);
3026         
3027         if (snode->flag & SNODE_BACKDRAW) {
3028                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
3029                 ImBuf *ibuf = BKE_image_get_ibuf(ima, NULL);
3030                 if (ibuf) {
3031                         int x, y;
3032                         float zoom = 1.0;
3033
3034                         glMatrixMode(GL_PROJECTION);
3035                         glPushMatrix();
3036                         glMatrixMode(GL_MODELVIEW);
3037                         glPushMatrix();
3038                         
3039                         glaDefine2DArea(&sa->winrct);
3040
3041                         if (ibuf->x > sa->winx || ibuf->y > sa->winy) {
3042                                 float zoomx, zoomy;
3043                                 zoomx = (float)sa->winx / ibuf->x;
3044                                 zoomy = (float)sa->winy / ibuf->y;
3045                                 zoom = MIN2(zoomx, zoomy);
3046                         }
3047                         
3048                         x = (sa->winx - zoom * ibuf->x) / 2 + snode->xof;
3049                         y = (sa->winy - zoom * ibuf->y) / 2 + snode->yof;
3050
3051                         glPixelZoom(zoom, zoom);
3052
3053                         glColor4f(1.0, 1.0, 1.0, 1.0);
3054                         if (ibuf->rect)
3055                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
3056                         else if (ibuf->channels == 4)
3057                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
3058
3059                         glPixelZoom(1.0, 1.0);
3060
3061                         glMatrixMode(GL_PROJECTION);
3062                         glPopMatrix();
3063                         glMatrixMode(GL_MODELVIEW);
3064                         glPopMatrix();
3065                 }
3066         }
3067 }
3068 #endif
3069
3070 /* if v2d not NULL, it clips and returns 0 if not visible */
3071 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
3072 {
3073         float dist, vec[4][2];
3074         float deltax, deltay;
3075         int toreroute, fromreroute;
3076         /* in v0 and v3 we put begin/end points */
3077         if (link->fromsock) {
3078                 vec[0][0] = link->fromsock->locx;
3079                 vec[0][1] = link->fromsock->locy;
3080                 fromreroute = (link->fromnode && link->fromnode->type == NODE_REROUTE);
3081         }
3082         else {
3083                 if (snode == NULL) return 0;
3084                 vec[0][0] = snode->mx;
3085                 vec[0][1] = snode->my;
3086                 fromreroute = 0;
3087         }
3088         if (link->tosock) {
3089                 vec[3][0] = link->tosock->locx;
3090                 vec[3][1] = link->tosock->locy;
3091                 toreroute = (link->tonode && link->tonode->type == NODE_REROUTE);
3092         }
3093         else {
3094                 if (snode == NULL) return 0;
3095                 vec[3][0] = snode->mx;
3096                 vec[3][1] = snode->my;
3097                 toreroute = 0;
3098         }
3099
3100         dist = UI_GetThemeValue(TH_NODE_CURVING) * 0.10f * ABS(vec[0][0] - vec[3][0]);
3101         deltax = vec[3][0] - vec[0][0];
3102         deltay = vec[3][1] - vec[0][1];
3103         /* check direction later, for top sockets */
3104         if (fromreroute) {
3105                 if (ABS(deltax)>ABS(deltay)) {
3106                         vec[1][1]= vec[0][1];
3107                         vec[1][0]= vec[0][0]+(deltax>0?dist:-dist);
3108                 } else {
3109                         vec[1][0]= vec[0][0];