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