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