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