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