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