Cycles: svn merge -r41467:41531 ^/trunk/blender
[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         uiLayout *row;
950         
951         uiItemL(layout, "Translation:", ICON_NONE);
952         row= uiLayoutRow(layout, 1);
953         uiItemR(row, ptr, "location", 0, "", ICON_NONE);
954         
955         uiItemL(layout, "Rotation:", ICON_NONE);
956         row= uiLayoutRow(layout, 1);
957         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
958         
959         uiItemL(layout, "Scale:", ICON_NONE);
960         row= uiLayoutRow(layout, 1);
961         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
962         
963 #if 0
964         row= uiLayoutRow(layout, 1);
965         uiItemR(row, ptr, "use_min", 0, "Min", ICON_NONE);
966         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
967         
968         row= uiLayoutRow(layout, 1);
969         uiItemR(row, ptr, "use_max", 0, "Max", ICON_NONE);
970         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
971 #endif  
972 }
973
974 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
975
976         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
977 }
978
979 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
980 {
981         PointerRNA obptr= CTX_data_pointer_get(C, "active_object");
982         uiLayout *col;
983
984         col= uiLayoutColumn(layout, 0);
985
986         if(obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
987                 PointerRNA dataptr= RNA_pointer_get(&obptr, "data");
988
989                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
990                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
991         }
992         else {
993                 uiItemR(col, ptr, "uv_layer", 0, "UV", ICON_NONE);
994                 uiItemR(col, ptr, "color_layer", 0, "VCol", ICON_NONE);
995         }
996 }
997
998 /*static void node_layout_prop(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname)
999 {
1000         if(C && CTX_wm_space_node(C)) {
1001                 uiItemR(layout, ptr, propname, 0, NULL, ICON_NONE);
1002         }
1003         else {
1004                 uiLayout *split = uiLayoutSplit(layout, 0.35f, 0);
1005                 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
1006
1007                 uiItemL(uiLayoutColumn(split, 0), RNA_property_ui_name(prop), ICON_NONE);
1008                 uiItemR(uiLayoutColumn(split, 0), ptr, propname, 0, "", ICON_NONE);
1009         }
1010 }*/
1011
1012 static void node_shader_buts_attribute(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1013 {
1014         uiItemR(layout, ptr, "attribute_name", 0, "Name", ICON_NONE);
1015 }
1016
1017 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1018 {
1019         //uiItemR(layout, ptr, "image", 0, "", ICON_NONE);
1020         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1021         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1022 }
1023
1024 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1025 {
1026         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
1027         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
1028 }
1029
1030 static void node_shader_buts_tex_blend(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1031 {
1032         uiItemR(layout, ptr, "progression", 0, "", ICON_NONE);
1033         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1034 }
1035
1036 static void node_shader_buts_tex_clouds(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1037 {
1038         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
1039         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
1040         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1041 }
1042
1043 static void node_shader_buts_tex_distnoise(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1044 {
1045         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
1046         uiItemR(layout, ptr, "noise_distortion", 0, "", ICON_NONE);
1047 }
1048
1049 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1050 {
1051         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1052 }
1053
1054 static void node_shader_buts_tex_marble(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1055 {
1056         uiItemR(layout, ptr, "marble_type", 0, "", ICON_NONE);
1057         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
1058         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
1059         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1060         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
1061 }
1062
1063 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1064 {
1065         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
1066         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
1067 }
1068
1069 static void node_shader_buts_tex_stucci(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1070 {
1071         uiItemR(layout, ptr, "stucci_type", 0, "", ICON_NONE);
1072         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
1073         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
1074 }
1075
1076 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1077 {
1078         uiItemR(layout, ptr, "distance_metric", 0, "", ICON_NONE);
1079         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
1080 }
1081
1082 static void node_shader_buts_tex_wood(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1083 {
1084         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
1085         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
1086         uiItemR(layout, ptr, "wood_type", 0, "", ICON_NONE);
1087         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
1088 }
1089
1090 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1091 {
1092         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
1093 }
1094
1095 static void node_shader_buts_dynamic(uiLayout *layout, bContext *C, PointerRNA *ptr)
1096
1097         Main *bmain= CTX_data_main(C);
1098         uiBlock *block= uiLayoutAbsoluteBlock(layout);
1099         bNode *node= ptr->data;
1100         bNodeTree *ntree= ptr->id.data;
1101         rctf *butr= &node->butr;
1102         uiBut *bt;
1103         // XXX SpaceNode *snode= curarea->spacedata.first;
1104         short dy= (short)butr->ymin;
1105         int xoff=0;
1106
1107         /* B_NODE_EXEC is handled in butspace.c do_node_buts */
1108         if(!node->id) {
1109                         const char *strp;
1110                         IDnames_to_pupstring(&strp, NULL, "", &(bmain->text), NULL, NULL);
1111                         node->menunr= 0;
1112                         bt= uiDefButS(block, MENU, B_NODE_EXEC/*+node->nr*/, strp, 
1113                                                         butr->xmin, dy, 19, 19, 
1114                                                         &node->menunr, 0, 0, 0, 0, "Browses existing choices");
1115                         uiButSetFunc(bt, node_browse_text_cb, ntree, node);
1116                         xoff=19;
1117                         if(strp) MEM_freeN((void *)strp);
1118         }
1119         else {
1120                 bt = uiDefBut(block, BUT, B_NOP, "Update",
1121                                 butr->xmin+xoff, butr->ymin+20, 50, 19,
1122                                 &node->menunr, 0.0, 19.0, 0, 0, "Refresh this node (and all others that use the same script)");
1123                 uiButSetFunc(bt, node_dynamic_update_cb, ntree, node);
1124
1125                 if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) {
1126                         // UI_ThemeColor(TH_REDALERT);
1127                         // XXX ui_rasterpos_safe(butr->xmin + xoff, butr->ymin + 5, snode->aspect);
1128                         // XXX snode_drawstring(snode, "Error! Check console...", butr->xmax - butr->xmin);
1129                         ;
1130                 }
1131         }
1132 }
1133
1134 /* only once called */
1135 static void node_shader_set_butfunc(bNodeType *ntype)
1136 {
1137         ntype->uifuncbut = NULL;
1138         switch(ntype->type) {
1139                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1140
1141                 case SH_NODE_MATERIAL:
1142                 case SH_NODE_MATERIAL_EXT:
1143                         ntype->uifunc= node_shader_buts_material;
1144                         break;
1145                 case SH_NODE_TEXTURE:
1146                         ntype->uifunc= node_buts_texture;
1147                         break;
1148                 case SH_NODE_NORMAL:
1149                         ntype->uifunc= node_buts_normal;
1150                         break;
1151                 case SH_NODE_CURVE_VEC:
1152                         ntype->uifunc= node_buts_curvevec;
1153                         break;
1154                 case SH_NODE_CURVE_RGB:
1155                         ntype->uifunc= node_buts_curvecol;
1156                         break;
1157                 case SH_NODE_MAPPING:
1158                         ntype->uifunc= node_shader_buts_mapping;
1159                         break;
1160                 case SH_NODE_VALUE:
1161                         ntype->uifunc= node_buts_value;
1162                         break;
1163                 case SH_NODE_RGB:
1164                         ntype->uifunc= node_buts_rgb;
1165                         break;
1166                 case SH_NODE_MIX_RGB:
1167                         ntype->uifunc= node_buts_mix_rgb;
1168                         break;
1169                 case SH_NODE_VALTORGB:
1170                         ntype->uifunc= node_buts_colorramp;
1171                         break;
1172                 case SH_NODE_MATH: 
1173                         ntype->uifunc= node_buts_math;
1174                         break; 
1175                 case SH_NODE_VECT_MATH: 
1176                         ntype->uifunc= node_shader_buts_vect_math;
1177                         break; 
1178                 case SH_NODE_GEOM:
1179                         ntype->uifunc= node_shader_buts_geometry;
1180                         break;
1181                 case SH_NODE_ATTRIBUTE:
1182                         ntype->uifunc= node_shader_buts_attribute;
1183                         break;
1184                 case SH_NODE_TEX_SKY:
1185                         ntype->uifunc= node_shader_buts_tex_sky;
1186                         break;
1187                 case SH_NODE_TEX_IMAGE:
1188                         ntype->uifunc= node_shader_buts_tex_image;
1189                         break;
1190                 case SH_NODE_TEX_ENVIRONMENT:
1191                         ntype->uifunc= node_shader_buts_tex_image;
1192                         break;
1193                 case SH_NODE_TEX_BLEND:
1194                         ntype->uifunc= node_shader_buts_tex_blend;
1195                         break;
1196                 case SH_NODE_TEX_CLOUDS:
1197                         ntype->uifunc= node_shader_buts_tex_clouds;
1198                         break;
1199                 case SH_NODE_TEX_DISTNOISE:
1200                         ntype->uifunc= node_shader_buts_tex_distnoise;
1201                         break;
1202                 case SH_NODE_TEX_MAGIC:
1203                         ntype->uifunc= node_shader_buts_tex_magic;
1204                         break;
1205                 case SH_NODE_TEX_MARBLE:
1206                         ntype->uifunc= node_shader_buts_tex_marble;
1207                         break;
1208                 case SH_NODE_TEX_MUSGRAVE:
1209                         ntype->uifunc= node_shader_buts_tex_musgrave;
1210                         break;
1211                 case SH_NODE_TEX_STUCCI:
1212                         ntype->uifunc= node_shader_buts_tex_stucci;
1213                         break;
1214                 case SH_NODE_TEX_VORONOI:
1215                         ntype->uifunc= node_shader_buts_tex_voronoi;
1216                         break;
1217                 case SH_NODE_TEX_WOOD:
1218                         ntype->uifunc= node_shader_buts_tex_wood;
1219                         break;
1220                 case SH_NODE_BSDF_GLOSSY:
1221                 case SH_NODE_BSDF_GLASS:
1222                         ntype->uifunc= node_shader_buts_glossy;
1223                         break;
1224                 case NODE_DYNAMIC:
1225                         ntype->uifunc= node_shader_buts_dynamic;
1226                         break;
1227         }
1228                 if (ntype->uifuncbut == NULL) ntype->uifuncbut = ntype->uifunc;
1229 }
1230
1231 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
1232
1233 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1234 {
1235         uiLayout *col;
1236         bNode *node= ptr->data;
1237         PointerRNA imaptr;
1238         PropertyRNA *prop;
1239         int source;
1240         
1241         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1242         
1243         if(!node->id) return;
1244         
1245         prop = RNA_struct_find_property(ptr, "image");
1246         if (!prop || RNA_property_type(prop) != PROP_POINTER) return;
1247         imaptr= RNA_property_pointer_get(ptr, prop);
1248         
1249         col= uiLayoutColumn(layout, 0);
1250         
1251         uiItemR(col, &imaptr, "source", 0, NULL, ICON_NONE);
1252         
1253         source= RNA_enum_get(&imaptr, "source");
1254
1255         if(source == IMA_SRC_SEQUENCE) {
1256                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
1257                 Scene *scene= CTX_data_scene(C);
1258                 ImageUser *iuser= node->storage;
1259                 char tstr[32];
1260                 const int framenr= BKE_image_user_get_frame(iuser, CFRA, 0);
1261                 BLI_snprintf(tstr, sizeof(tstr), "Frame: %d", framenr);
1262                 uiItemL(layout, tstr, ICON_NONE);
1263         }
1264
1265         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1266                 col= uiLayoutColumn(layout, 1);
1267                 uiItemR(col, ptr, "frame_duration", 0, NULL, ICON_NONE);
1268                 uiItemR(col, ptr, "frame_start", 0, NULL, ICON_NONE);
1269                 uiItemR(col, ptr, "frame_offset", 0, NULL, ICON_NONE);
1270                 uiItemR(col, ptr, "use_cyclic", 0, NULL, ICON_NONE);
1271                 uiItemR(col, ptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
1272         }
1273
1274         col= uiLayoutColumn(layout, 0);
1275         
1276         if (RNA_enum_get(&imaptr, "type")== IMA_TYPE_MULTILAYER)
1277                 uiItemR(col, ptr, "layer", 0, NULL, ICON_NONE);
1278 }
1279
1280 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
1281 {
1282         bNode *node= ptr->data;
1283         uiLayout *col, *row;
1284         PointerRNA op_ptr;
1285         PointerRNA scn_ptr;
1286         PropertyRNA *prop;
1287         const char *layer_name;
1288         char scene_name[MAX_ID_NAME-2];
1289         
1290         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
1291         
1292         if(!node->id) return;
1293
1294         col= uiLayoutColumn(layout, 0);
1295         row = uiLayoutRow(col, 0);
1296         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
1297         
1298         prop = RNA_struct_find_property(ptr, "layer");
1299         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
1300                 return;
1301         
1302         scn_ptr = RNA_pointer_get(ptr, "scene");
1303         RNA_string_get(&scn_ptr, "name", scene_name);
1304         
1305         WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
1306         RNA_string_set(&op_ptr, "layer", layer_name);
1307         RNA_string_set(&op_ptr, "scene", scene_name);
1308         uiItemFullO(row, "RENDER_OT_render", "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
1309
1310 }
1311
1312
1313 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1314 {
1315         uiLayout *col, *row;
1316         
1317         col= uiLayoutColumn(layout, 0);
1318         
1319         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
1320         if (RNA_enum_get(ptr, "filter_type")!= R_FILTER_FAST_GAUSS) {
1321                 uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
1322                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1323         }
1324         
1325         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
1326         
1327         if (RNA_boolean_get(ptr, "use_relative")) {
1328                 uiItemL(col, "Aspect Correction", 0);
1329                 row= uiLayoutRow(layout, 1);
1330                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, 0);
1331                 
1332                 col= uiLayoutColumn(layout, 1);
1333                 uiItemR(col, ptr, "factor_x", 0, "X", ICON_NONE);
1334                 uiItemR(col, ptr, "factor_y", 0, "Y", ICON_NONE);
1335         }
1336         else {
1337                 col= uiLayoutColumn(layout, 1);
1338                 uiItemR(col, ptr, "size_x", 0, "X", ICON_NONE);
1339                 uiItemR(col, ptr, "size_y", 0, "Y", ICON_NONE);
1340         }
1341 }
1342
1343 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1344 {
1345         uiLayout *col;
1346         
1347         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1348         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
1349         
1350         col= uiLayoutColumn(layout, 1);
1351         uiItemL(col, "Center:", ICON_NONE);
1352         uiItemR(col, ptr, "center_x", 0, "X", ICON_NONE);
1353         uiItemR(col, ptr, "center_y", 0, "Y", ICON_NONE);
1354         
1355         uiItemS(layout);
1356         
1357         col= uiLayoutColumn(layout, 1);
1358         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
1359         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1360         
1361         uiItemS(layout);
1362         
1363         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
1364         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
1365 }
1366
1367 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1368 {       
1369         uiLayout *col;
1370         
1371         col= uiLayoutColumn(layout, 1);
1372         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
1373         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
1374         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
1375 }
1376
1377 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1378 {
1379         uiLayout *sub, *col;
1380         
1381         col= uiLayoutColumn(layout, 0);
1382         uiItemL(col, "Bokeh Type:", ICON_NONE);
1383         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
1384         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1385
1386         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1387
1388         col = uiLayoutColumn(layout, 0);
1389         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer")==1);
1390         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
1391
1392         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
1393         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1394
1395         col = uiLayoutColumn(layout, 0);
1396         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
1397         sub = uiLayoutColumn(col, 0);
1398         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_preview"));
1399         uiItemR(sub, ptr, "samples", 0, NULL, ICON_NONE);
1400         
1401         col = uiLayoutColumn(layout, 0);
1402         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
1403         sub = uiLayoutColumn(col, 0);
1404         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer")==0);
1405         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
1406 }
1407
1408 /* qdn: glare node */
1409 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1410 {       
1411         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
1412         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
1413
1414         if (RNA_enum_get(ptr, "glare_type")!= 1) {
1415                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1416         
1417                 if (RNA_enum_get(ptr, "glare_type")!= 0) 
1418                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1419         }
1420         
1421         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
1422         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1423
1424         if (RNA_enum_get(ptr, "glare_type")== 2) {
1425                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
1426                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
1427         }
1428         if (RNA_enum_get(ptr, "glare_type")== 0 || RNA_enum_get(ptr, "glare_type")== 2) {
1429                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1430                 
1431                 if (RNA_enum_get(ptr, "glare_type")== 0) 
1432                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
1433         }
1434         if (RNA_enum_get(ptr, "glare_type")== 1) {
1435                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
1436         }
1437 }
1438
1439 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1440 {       
1441         uiLayout *col;
1442
1443         col = uiLayoutColumn(layout, 0);
1444         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
1445         if (RNA_enum_get(ptr, "tonemap_type")== 0) {
1446                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1447                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1448                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
1449         }
1450         else {
1451                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
1452                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1453                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1454                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1455         }
1456 }
1457
1458 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1459 {
1460         uiLayout *col;
1461
1462         col= uiLayoutColumn(layout, 0);
1463         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
1464
1465         col = uiLayoutColumn(col, 0);
1466         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector")==0);
1467         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
1468         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
1469 }
1470
1471 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1472 {
1473         uiLayout *col;
1474         
1475         col= uiLayoutColumn(layout, 0);
1476         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
1477         uiItemR(col, ptr, "factor", 0, "Blur", ICON_NONE);
1478         
1479         col= uiLayoutColumn(layout, 1);
1480         uiItemL(col, "Speed:", ICON_NONE);
1481         uiItemR(col, ptr, "speed_min", 0, "Min", ICON_NONE);
1482         uiItemR(col, ptr, "speed_max", 0, "Max", ICON_NONE);
1483
1484         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
1485 }
1486
1487 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1488 {
1489         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1490 }
1491
1492 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1493 {
1494         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1495 }
1496
1497 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1498 {
1499         uiLayout *col;
1500
1501         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
1502         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
1503
1504         col= uiLayoutColumn(layout, 1);
1505         if (RNA_boolean_get(ptr, "relative")){
1506                 uiItemR(col, ptr, "rel_min_x", 0, "Left", ICON_NONE);
1507                 uiItemR(col, ptr, "rel_max_x", 0, "Right", ICON_NONE);
1508                 uiItemR(col, ptr, "rel_min_y", 0, "Up", ICON_NONE);
1509                 uiItemR(col, ptr, "rel_max_y", 0, "Down", ICON_NONE);
1510         } else {
1511                 uiItemR(col, ptr, "min_x", 0, "Left", ICON_NONE);
1512                 uiItemR(col, ptr, "max_x", 0, "Right", ICON_NONE);
1513                 uiItemR(col, ptr, "min_y", 0, "Up", ICON_NONE);
1514                 uiItemR(col, ptr, "max_y", 0, "Down", ICON_NONE);
1515         }
1516 }
1517
1518 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1519 {
1520         uiLayout *row, *col;
1521         
1522         col= uiLayoutColumn(layout, 0);
1523         row= uiLayoutRow(col, 0);
1524         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1525         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
1526 }
1527
1528 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1529 {
1530         uiLayout *sub, *col;
1531         
1532         col =uiLayoutColumn(layout, 1);
1533         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1534         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
1535         
1536         col =uiLayoutColumn(layout, 1);
1537         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
1538         sub =uiLayoutColumn(col, 0);
1539         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1540         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
1541         
1542         col =uiLayoutColumn(layout, 1);
1543         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
1544         sub =uiLayoutColumn(col, 0);
1545         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1546         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
1547 }
1548
1549 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1550 {       
1551         uiLayout *col;
1552         
1553         col =uiLayoutColumn(layout, 1);
1554         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
1555         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
1556 }
1557
1558 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1559 {       
1560         uiLayout *col;
1561         
1562         col =uiLayoutColumn(layout, 1);
1563         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
1564 }
1565
1566
1567 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1568 {
1569         uiLayout *col;
1570         
1571         col =uiLayoutColumn(layout, 0);
1572         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1573         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1574         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1575 }
1576
1577 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1578 {
1579         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1580 }
1581
1582 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1583 {
1584         uiLayout *col;
1585         
1586         col =uiLayoutColumn(layout, 1);
1587         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1588         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1589 }
1590
1591 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1592 {
1593         uiLayout *col;
1594         
1595         col =uiLayoutColumn(layout, 1);
1596         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1597         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1598 }
1599
1600 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1601 {
1602         uiLayout *row, *col;
1603         
1604         uiItemL(layout, "Despill Channel:", ICON_NONE);
1605         row =uiLayoutRow(layout,0);
1606         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1607
1608         col= uiLayoutColumn(layout, 0);
1609         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1610
1611         if(RNA_enum_get(ptr, "limit_method")==0) {
1612                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1613                 row=uiLayoutRow(col,0);
1614                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1615         }
1616
1617         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1618         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1619         if (RNA_boolean_get(ptr, "use_unspill")== 1) {
1620                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1621                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1622                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1623         }
1624 }
1625
1626 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1627 {
1628         uiLayout *col;
1629         
1630         col= uiLayoutColumn(layout, 0);
1631         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1632         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1633         
1634         col= uiLayoutColumn(layout, 1);
1635         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1636         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1637         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1638 }
1639
1640 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1641 {
1642         uiLayout *col;
1643         
1644         col= uiLayoutColumn(layout, 1);
1645         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1646         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1647         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1648 }
1649
1650 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1651 {       
1652         uiLayout *col, *row;
1653
1654         uiItemL(layout, "Color Space:", ICON_NONE);
1655         row= uiLayoutRow(layout, 0);
1656         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1657
1658         col=uiLayoutColumn(layout, 0);
1659         uiItemL(col, "Key Channel:", ICON_NONE);
1660         row= uiLayoutRow(col, 0);
1661         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1662
1663         col =uiLayoutColumn(layout, 0);
1664
1665         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1666         if(RNA_enum_get(ptr, "limit_method")==0) {
1667                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1668                 row=uiLayoutRow(col,0);
1669                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1670         }
1671
1672         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1673         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1674 }
1675
1676 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1677 {
1678         uiLayout *col;
1679         
1680         col= uiLayoutColumn(layout, 1);
1681         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1682         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1683 }
1684
1685 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1686 {
1687         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1688 }
1689
1690 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1691 {
1692         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1693         uiItemR(layout, ptr, "use_smooth_mask", 0, NULL, ICON_NONE);
1694 }
1695
1696 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1697 {
1698         uiLayout *col, *row;
1699
1700         col= uiLayoutColumn(layout, 0);
1701         uiItemR(col, ptr, "filepath", 0, "", ICON_NONE);
1702         uiItemR(col, ptr, "image_type", 0, "", ICON_NONE);
1703         
1704         row= uiLayoutRow(layout, 0);
1705         if (RNA_enum_get(ptr, "image_type")== R_OPENEXR) {
1706                 uiItemR(row, ptr, "use_exr_half", 0, NULL, ICON_NONE);
1707                 uiItemR(row, ptr, "exr_codec", 0, "", ICON_NONE);
1708         }
1709         else if (RNA_enum_get(ptr, "image_type")== R_JPEG90) {
1710                 uiItemR(row, ptr, "quality", UI_ITEM_R_SLIDER, "Quality", ICON_NONE);
1711         }
1712         else if (RNA_enum_get(ptr, "image_type")== R_PNG) {
1713                 uiItemR(row, ptr, "quality", UI_ITEM_R_SLIDER, "Compression", ICON_NONE);
1714         }
1715         
1716         row= uiLayoutRow(layout, 1);
1717         uiItemR(row, ptr, "frame_start", 0, "Start", ICON_NONE);
1718         uiItemR(row, ptr, "frame_end", 0, "End", ICON_NONE);
1719 }
1720
1721 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1722 {
1723         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
1724 }
1725
1726 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1727 {
1728         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1729 }
1730
1731 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1732 {
1733         uiLayout *col;
1734         
1735         col= uiLayoutColumn(layout, 0);
1736         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
1737         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
1738 }
1739
1740 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1741 {
1742         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
1743 }
1744
1745 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1746 {
1747         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1748 }
1749
1750 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1751 {
1752         uiLayout *split, *col, *row;
1753         
1754         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1755         
1756         if (RNA_enum_get(ptr, "correction_method")== 0) {
1757         
1758                 split = uiLayoutSplit(layout, 0, 0);
1759                 col = uiLayoutColumn(split, 0);
1760                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
1761                 row = uiLayoutRow(col, 0);
1762                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
1763                 
1764                 col = uiLayoutColumn(split, 0);
1765                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
1766                 row = uiLayoutRow(col, 0);
1767                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
1768                 
1769                 col = uiLayoutColumn(split, 0);
1770                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
1771                 row = uiLayoutRow(col, 0);
1772                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
1773
1774         } else {
1775                 
1776                 split = uiLayoutSplit(layout, 0, 0);
1777                 col = uiLayoutColumn(split, 0);
1778                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
1779                 row = uiLayoutRow(col, 0);
1780                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
1781                 
1782                 col = uiLayoutColumn(split, 0);
1783                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
1784                 row = uiLayoutRow(col, 0);
1785                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
1786                 
1787                 col = uiLayoutColumn(split, 0);
1788                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
1789                 row = uiLayoutRow(col, 0);
1790                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
1791         }
1792
1793 }
1794 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1795 {
1796         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1797
1798         if (RNA_enum_get(ptr, "correction_method")== 0) {
1799
1800         uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
1801                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
1802
1803                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
1804                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
1805
1806                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
1807                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
1808         } else {
1809                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
1810                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
1811
1812                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
1813                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
1814
1815                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
1816                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
1817         }
1818 }
1819
1820
1821 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1822 {
1823         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
1824 }
1825
1826 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1827
1828         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
1829 }
1830
1831 /* only once called */
1832 static void node_composit_set_butfunc(bNodeType *ntype)
1833 {
1834         ntype->uifuncbut = NULL;
1835         switch(ntype->type) {
1836                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1837
1838                 case CMP_NODE_IMAGE:
1839                         ntype->uifunc= node_composit_buts_image;
1840                         break;
1841                 case CMP_NODE_R_LAYERS:
1842                         ntype->uifunc= node_composit_buts_renderlayers;
1843                         break;
1844                 case CMP_NODE_NORMAL:
1845                         ntype->uifunc= node_buts_normal;
1846                         break;
1847                 case CMP_NODE_CURVE_VEC:
1848                         ntype->uifunc= node_buts_curvevec;
1849                         break;
1850                 case CMP_NODE_CURVE_RGB:
1851                         ntype->uifunc= node_buts_curvecol;
1852                         break;
1853                 case CMP_NODE_VALUE:
1854                         ntype->uifunc= node_buts_value;
1855                         break;
1856                 case CMP_NODE_RGB:
1857                         ntype->uifunc= node_buts_rgb;
1858                         break;
1859                 case CMP_NODE_FLIP:
1860                         ntype->uifunc= node_composit_buts_flip;
1861                         break;
1862                 case CMP_NODE_SPLITVIEWER:
1863                         ntype->uifunc= node_composit_buts_splitviewer;
1864                         break;
1865                 case CMP_NODE_MIX_RGB:
1866                         ntype->uifunc= node_buts_mix_rgb;
1867                         break;
1868                 case CMP_NODE_VALTORGB:
1869                         ntype->uifunc= node_buts_colorramp;
1870                         break;
1871                 case CMP_NODE_CROP:
1872                         ntype->uifunc= node_composit_buts_crop;
1873                         break;
1874                 case CMP_NODE_BLUR:
1875                         ntype->uifunc= node_composit_buts_blur;
1876                         break;
1877                 case CMP_NODE_DBLUR:
1878                         ntype->uifunc= node_composit_buts_dblur;
1879                         break;
1880                 case CMP_NODE_BILATERALBLUR:
1881                         ntype->uifunc= node_composit_buts_bilateralblur;
1882                         break;
1883                 case CMP_NODE_DEFOCUS:
1884                         ntype->uifunc = node_composit_buts_defocus;
1885                         break;
1886                 case CMP_NODE_GLARE:
1887                         ntype->uifunc = node_composit_buts_glare;
1888                         break;
1889                 case CMP_NODE_TONEMAP:
1890                         ntype->uifunc = node_composit_buts_tonemap;
1891                         break;
1892                 case CMP_NODE_LENSDIST:
1893                         ntype->uifunc = node_composit_buts_lensdist;
1894                         break;
1895                 case CMP_NODE_VECBLUR:
1896                         ntype->uifunc= node_composit_buts_vecblur;
1897                         break;
1898                 case CMP_NODE_FILTER:
1899                         ntype->uifunc= node_composit_buts_filter;
1900                         break;
1901                 case CMP_NODE_MAP_VALUE:
1902                         ntype->uifunc= node_composit_buts_map_value;
1903                         break;
1904                 case CMP_NODE_TIME:
1905                         ntype->uifunc= node_buts_time;
1906                         break;
1907                 case CMP_NODE_ALPHAOVER:
1908                         ntype->uifunc= node_composit_buts_alphaover;
1909                         break;
1910                 case CMP_NODE_HUE_SAT:
1911                         ntype->uifunc= node_composit_buts_hue_sat;
1912                         break;
1913                 case CMP_NODE_TEXTURE:
1914                         ntype->uifunc= node_buts_texture;
1915                         break;
1916                 case CMP_NODE_DILATEERODE:
1917                         ntype->uifunc= node_composit_buts_dilateerode;
1918                         break;
1919                 case CMP_NODE_OUTPUT_FILE:
1920                         ntype->uifunc= node_composit_buts_file_output;
1921                         break;
1922                 case CMP_NODE_DIFF_MATTE:
1923                         ntype->uifunc=node_composit_buts_diff_matte;
1924                         break;
1925                 case CMP_NODE_DIST_MATTE:
1926                         ntype->uifunc=node_composit_buts_distance_matte;
1927                         break;
1928                 case CMP_NODE_COLOR_SPILL:
1929                         ntype->uifunc=node_composit_buts_color_spill;
1930                         break;
1931                 case CMP_NODE_CHROMA_MATTE:
1932                         ntype->uifunc=node_composit_buts_chroma_matte;
1933                         break;
1934                 case CMP_NODE_COLOR_MATTE:
1935                         ntype->uifunc=node_composit_buts_color_matte;
1936                         break;
1937                 case CMP_NODE_SCALE:
1938                         ntype->uifunc= node_composit_buts_scale;
1939                         break;
1940                 case CMP_NODE_ROTATE:
1941                         ntype->uifunc=node_composit_buts_rotate;
1942                         break;
1943                 case CMP_NODE_CHANNEL_MATTE:
1944                         ntype->uifunc= node_composit_buts_channel_matte;
1945                         break;
1946                 case CMP_NODE_LUMA_MATTE:
1947                         ntype->uifunc= node_composit_buts_luma_matte;
1948                         break;
1949                 case CMP_NODE_MAP_UV:
1950                         ntype->uifunc= node_composit_buts_map_uv;
1951                         break;
1952                 case CMP_NODE_ID_MASK:
1953                         ntype->uifunc= node_composit_buts_id_mask;
1954                         break;
1955                 case CMP_NODE_MATH:
1956                         ntype->uifunc= node_buts_math;
1957                         break;
1958                 case CMP_NODE_INVERT:
1959                         ntype->uifunc= node_composit_buts_invert;
1960                         break;
1961                 case CMP_NODE_PREMULKEY:
1962                         ntype->uifunc= node_composit_buts_premulkey;
1963                         break;
1964                 case CMP_NODE_VIEW_LEVELS:
1965                         ntype->uifunc=node_composit_buts_view_levels;
1966                         break;
1967                 case CMP_NODE_COLORBALANCE:
1968                         ntype->uifunc=node_composit_buts_colorbalance;
1969                         ntype->uifuncbut=node_composit_buts_colorbalance_but;
1970                         break;
1971                 case CMP_NODE_HUECORRECT:
1972                         ntype->uifunc=node_composit_buts_huecorrect;
1973                         break;
1974                 case CMP_NODE_ZCOMBINE:
1975                         ntype->uifunc=node_composit_buts_zcombine;
1976                         break;
1977                 case CMP_NODE_COMBYCCA:
1978                 case CMP_NODE_SEPYCCA:
1979                         ntype->uifunc=node_composit_buts_ycc;
1980                         break;
1981         }
1982         if (ntype->uifuncbut == NULL) ntype->uifuncbut = ntype->uifunc;
1983
1984 }
1985
1986 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
1987
1988 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1989 {
1990         uiLayout *col;
1991         
1992         col= uiLayoutColumn(layout, 1);
1993         uiItemR(col, ptr, "offset", 0, "Offset", ICON_NONE);
1994         uiItemR(col, ptr, "offset_frequency", 0, "Frequency", ICON_NONE);
1995         
1996         col= uiLayoutColumn(layout, 1);
1997         uiItemR(col, ptr, "squash", 0, "Squash", ICON_NONE);
1998         uiItemR(col, ptr, "squash_frequency", 0, "Frequency", ICON_NONE);
1999 }
2000
2001 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2002 {
2003         PointerRNA tex_ptr;
2004         bNode *node= ptr->data;
2005         ID *id= ptr->id.data;
2006         Tex *tex = (Tex *)node->storage;
2007         uiLayout *col, *row;
2008         
2009         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2010
2011         col= uiLayoutColumn(layout, 0);
2012
2013         switch( tex->type ) {
2014                 case TEX_BLEND:
2015                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2016                         row= uiLayoutRow(col, 0);
2017                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2018                         break;
2019
2020                 case TEX_MARBLE:
2021                         row= uiLayoutRow(col, 0);
2022                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2023                         row= uiLayoutRow(col, 0);
2024                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2025                         row= uiLayoutRow(col, 0);
2026                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2027                         row= uiLayoutRow(col, 0);
2028                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2029                         break;
2030
2031                 case TEX_MAGIC:
2032                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2033                         break;
2034
2035                 case TEX_STUCCI:
2036                         row= uiLayoutRow(col, 0);
2037                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2038                         row= uiLayoutRow(col, 0);
2039                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2040                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2041                         break;
2042
2043                 case TEX_WOOD:
2044                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2045                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2046                         row= uiLayoutRow(col, 0);
2047                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2048                         row= uiLayoutRow(col, 0);
2049                         uiLayoutSetActive(row, !(RNA_enum_get(&tex_ptr, "wood_type")==TEX_BAND || RNA_enum_get(&tex_ptr, "wood_type")==TEX_RING)); 
2050                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2051                         break;
2052                         
2053                 case TEX_CLOUDS:
2054                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2055                         row= uiLayoutRow(col, 0);
2056                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2057                         row= uiLayoutRow(col, 0);
2058                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2059                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, "Depth", ICON_NONE);
2060                         break;
2061                         
2062                 case TEX_DISTNOISE:
2063                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2064                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2065                         break;
2066
2067                 case TEX_MUSGRAVE:
2068                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2069                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2070                         break;
2071                 case TEX_VORONOI:
2072                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2073                         if(tex->vn_distm == TEX_MINKOVSKY) {
2074                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2075                         }
2076                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2077                         break;
2078         }
2079 }
2080
2081 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2082 {
2083         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2084 }
2085
2086 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2087 {
2088         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2089 }
2090
2091 /* only once called */
2092 static void node_texture_set_butfunc(bNodeType *ntype)
2093 {
2094         ntype->uifuncbut = NULL;
2095         if( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
2096                 ntype->uifunc = node_texture_buts_proc;
2097         }
2098         else switch(ntype->type) {
2099                 
2100                 case TEX_NODE_MATH:
2101                         ntype->uifunc = node_buts_math;
2102                         break;
2103                 
2104                 case TEX_NODE_MIX_RGB:
2105                         ntype->uifunc = node_buts_mix_rgb;
2106                         break;
2107                         
2108                 case TEX_NODE_VALTORGB:
2109                         ntype->uifunc = node_buts_colorramp;
2110                         break;
2111                         
2112                 case TEX_NODE_CURVE_RGB:
2113                         ntype->uifunc= node_buts_curvecol;
2114                         break;
2115                         
2116                 case TEX_NODE_CURVE_TIME:
2117                         ntype->uifunc = node_buts_time;
2118                         break;
2119                         
2120                 case TEX_NODE_TEXTURE:
2121                         ntype->uifunc = node_buts_texture;
2122                         break;
2123                         
2124                 case TEX_NODE_BRICKS:
2125                         ntype->uifunc = node_texture_buts_bricks;
2126                         break;
2127                         
2128                 case TEX_NODE_IMAGE:
2129                         ntype->uifunc = node_texture_buts_image;
2130                         break;
2131                         
2132                 case TEX_NODE_OUTPUT:
2133                         ntype->uifunc = node_texture_buts_output;
2134                         break;
2135         }
2136         if (ntype->uifuncbut == NULL) ntype->uifuncbut = ntype->uifunc;
2137 }
2138
2139 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2140
2141 void ED_init_node_butfuncs(void)
2142 {
2143         bNodeTreeType *treetype;
2144         bNodeType *ntype;
2145         bNodeSocketType *stype;
2146         int i;
2147         
2148         /* node type ui functions */
2149         for (i=0; i < NUM_NTREE_TYPES; ++i) {
2150                 treetype = ntreeGetType(i);
2151                 if (treetype) {
2152                         for (ntype= treetype->node_types.first; ntype; ntype= ntype->next) {
2153                                 /* default ui functions */
2154                                 ntype->drawfunc = node_draw_default;
2155                                 ntype->drawupdatefunc = node_update_default;
2156                                 ntype->uifunc = NULL;
2157                                 ntype->uifuncbut = NULL;
2158                                 ntype->resize_area_func = node_resize_area_default;
2159                                 
2160                                 node_common_set_butfunc(ntype);
2161                                 
2162                                 switch (i) {
2163                                 case NTREE_COMPOSIT:
2164                                         node_composit_set_butfunc(ntype);
2165                                         break;
2166                                 case NTREE_SHADER:
2167                                         node_shader_set_butfunc(ntype);
2168                                         break;
2169                                 case NTREE_TEXTURE:
2170                                         node_texture_set_butfunc(ntype);
2171                                         break;
2172                                 }
2173                         }
2174                 }
2175         }
2176         
2177         /* socket type ui functions */
2178         for (i=0; i < NUM_SOCKET_TYPES; ++i) {
2179                 stype = ntreeGetSocketType(i);
2180                 if (stype) {
2181                         switch(stype->type) {
2182                         case SOCK_FLOAT:
2183                         case SOCK_INT:
2184                         case SOCK_BOOLEAN:
2185                                 stype->buttonfunc = node_socket_button_default;
2186                                 break;
2187                         case SOCK_VECTOR:
2188                                 stype->buttonfunc = node_socket_button_components;
2189                                 break;
2190                         case SOCK_RGBA:
2191                                 stype->buttonfunc = node_socket_button_color;
2192                                 break;
2193                         case SOCK_SHADER:
2194                                 stype->buttonfunc = node_socket_button_label;
2195                                 break;
2196                         default:
2197                                 stype->buttonfunc = NULL;
2198                         }
2199                 }
2200         }
2201 }
2202
2203 /* ************** Generic drawing ************** */
2204
2205 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
2206 {
2207         
2208         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2209                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2210                 void *lock;
2211                 ImBuf *ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
2212                 if(ibuf) {
2213                         float x, y; 
2214                         
2215                         glMatrixMode(GL_PROJECTION);
2216                         glPushMatrix();
2217                         glMatrixMode(GL_MODELVIEW);
2218                         glPushMatrix();
2219
2220                         /* keep this, saves us from a version patch */
2221                         if(snode->zoom==0.0f) snode->zoom= 1.0f;
2222                         
2223                         /* somehow the offset has to be calculated inverse */
2224                         
2225                         glaDefine2DArea(&ar->winrct);
2226                         /* ortho at pixel level curarea */
2227                         wmOrtho2(-0.375, ar->winx-0.375, -0.375, ar->winy-0.375);
2228                         
2229                         x = (ar->winx-snode->zoom*ibuf->x)/2 + snode->xof;
2230                         y = (ar->winy-snode->zoom*ibuf->y)/2 + snode->yof;
2231                         
2232                         if(!ibuf->rect) {
2233                                 if(color_manage)
2234                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
2235                                 else
2236                                         ibuf->profile = IB_PROFILE_NONE;
2237                                 IMB_rect_from_float(ibuf);
2238                         }
2239
2240                         if(ibuf->rect) {
2241                                 if (snode->flag & SNODE_SHOW_ALPHA) {
2242                                         glPixelZoom(snode->zoom, snode->zoom);
2243                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2244                                         if(ENDIAN_ORDER == B_ENDIAN)
2245                                                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
2246                                         
2247                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
2248                                         
2249                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
2250                                         glPixelZoom(1.0f, 1.0f);
2251                                 } else if (snode->flag & SNODE_USE_ALPHA) {
2252                                         glEnable(GL_BLEND);
2253                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2254                                         glPixelZoom(snode->zoom, snode->zoom);
2255                                         
2256                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2257                                         
2258                                         glPixelZoom(1.0f, 1.0f);
2259                                         glDisable(GL_BLEND);
2260                                 } else {
2261                                         glPixelZoom(snode->zoom, snode->zoom);
2262                                         
2263                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2264                                         
2265                                         glPixelZoom(1.0f, 1.0f);
2266                                 }
2267                         }
2268                         
2269                         glMatrixMode(GL_PROJECTION);
2270                         glPopMatrix();
2271                         glMatrixMode(GL_MODELVIEW);
2272                         glPopMatrix();
2273                 }
2274
2275                 BKE_image_release_ibuf(ima, lock);
2276         }
2277 }
2278
2279 void draw_nodespace_color_info(ARegion *ar, int color_manage, int channels, int x, int y, char *cp, float *fp)
2280 {
2281         char str[256];
2282         float dx= 6;
2283         /* text colors */
2284         /* XXX colored text not allowed in Blender UI */
2285         #if 0
2286         unsigned char red[3] = {255, 50, 50};
2287         unsigned char green[3] = {0, 255, 0};
2288         unsigned char blue[3] = {100, 100, 255};
2289         #else
2290         unsigned char red[3] = {255, 255, 255};
2291         unsigned char green[3] = {255, 255, 255};
2292         unsigned char blue[3] = {255, 255, 255};
2293         #endif
2294         float hue=0, sat=0, val=0, lum=0, u=0, v=0;
2295         float col[4], finalcol[4];
2296
2297         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
2298         glEnable(GL_BLEND);
2299
2300         /* noisy, high contrast make impossible to read if lower alpha is used. */
2301         glColor4ub(0, 0, 0, 190);
2302         glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
2303         glDisable(GL_BLEND);
2304
2305         BLF_size(blf_mono_font, 11, 72);
2306
2307         glColor3ub(255, 255, 255);
2308         sprintf(str, "X:%-4d  Y:%-4d |", x, y);
2309         // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
2310         BLF_position(blf_mono_font, dx, 6, 0);
2311         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2312         dx += BLF_width(blf_mono_font, str);
2313
2314         #if 0   /* XXX no Z value in compo backdrop atm */
2315         if(zp) {
2316                 glColor3ub(255, 255, 255);
2317                 sprintf(str, " Z:%-.4f |", 0.5f+0.5f*(((float)*zp)/(float)0x7fffffff));
2318                 BLF_position(blf_mono_font, dx, 6, 0);
2319                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2320                 dx += BLF_width(blf_mono_font, str);
2321         }
2322         if(zpf) {
2323                 glColor3ub(255, 255, 255);
2324                 sprintf(str, " Z:%-.3f |", *zpf);
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         #endif
2330
2331         if(channels >= 3) {
2332                 glColor3ubv(red);
2333                 if (fp)
2334                         sprintf(str, "  R:%-.4f", fp[0]);
2335                 else if (cp)
2336                         sprintf(str, "  R:%-3d", cp[0]);
2337                 else
2338                         sprintf(str, "  R:-");
2339                 BLF_position(blf_mono_font, dx, 6, 0);
2340                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2341                 dx += BLF_width(blf_mono_font, str);
2342                 
2343                 glColor3ubv(green);
2344                 if (fp)
2345                         sprintf(str, "  G:%-.4f", fp[1]);
2346                 else if (cp)
2347                         sprintf(str, "  G:%-3d", cp[1]);
2348                 else
2349                         sprintf(str, "  G:-");
2350                 BLF_position(blf_mono_font, dx, 6, 0);
2351                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2352                 dx += BLF_width(blf_mono_font, str);
2353                 
2354                 glColor3ubv(blue);
2355                 if (fp)
2356                         sprintf(str, "  B:%-.4f", fp[2]);
2357                 else if (cp)
2358                         sprintf(str, "  B:%-3d", cp[2]);
2359                 else
2360                         sprintf(str, "  B:-");
2361                 BLF_position(blf_mono_font, dx, 6, 0);
2362                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2363                 dx += BLF_width(blf_mono_font, str);
2364                 
2365                 if(channels == 4) {
2366                         glColor3ub(255, 255, 255);
2367                         if (fp)
2368                                 sprintf(str, "  A:%-.4f", fp[3]);
2369                         else if (cp)
2370                                 sprintf(str, "  A:%-3d", cp[3]);
2371                         else
2372                                 sprintf(str, "- ");
2373                         BLF_position(blf_mono_font, dx, 6, 0);
2374                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2375                         dx += BLF_width(blf_mono_font, str);
2376                 }
2377         }
2378         
2379         /* color rectangle */
2380         if (channels==1) {
2381                 if (fp)
2382                         col[0] = col[1] = col[2] = fp[0];
2383                 else if (cp)
2384                         col[0] = col[1] = col[2] = (float)cp[0]/255.0f;
2385                 else
2386                         col[0] = col[1] = col[2] = 0.0f;
2387         }
2388         else if (channels==3) {
2389                 if (fp)
2390                         copy_v3_v3(col, fp);
2391                 else if (cp) {
2392                         col[0] = (float)cp[0]/255.0f;
2393                         col[1] = (float)cp[1]/255.0f;
2394                         col[2] = (float)cp[2]/255.0f;
2395                 }
2396                 else
2397                         zero_v3(col);
2398         }
2399         else if (channels==4) {
2400                 if (fp)
2401                         copy_v4_v4(col, fp);
2402                 else if (cp) {
2403                         col[0] = (float)cp[0]/255.0f;
2404                         col[1] = (float)cp[1]/255.0f;
2405                         col[2] = (float)cp[2]/255.0f;
2406                         col[3] = (float)cp[3]/255.0f;
2407                 }
2408                 else
2409                         zero_v4(col);
2410         }
2411         if (color_manage) {
2412                 linearrgb_to_srgb_v3_v3(finalcol, col);
2413                 finalcol[3] = col[3];
2414         }
2415         else {
2416                 copy_v4_v4(finalcol, col);
2417         }
2418         glDisable(GL_BLEND);
2419         glColor3fv(finalcol);
2420         dx += 5;
2421         glBegin(GL_QUADS);
2422         glVertex2f(dx, 3);
2423         glVertex2f(dx, 17);
2424         glVertex2f(dx+30, 17);
2425         glVertex2f(dx+30, 3);
2426         glEnd();
2427         dx += 35;
2428
2429         glColor3ub(255, 255, 255);
2430         if(channels == 1) {
2431                 if (fp) {
2432                         rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
2433                         rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
2434                 }
2435                 else if (cp) {
2436                         rgb_to_hsv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &hue, &sat, &val);
2437                         rgb_to_yuv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &lum, &u, &v);
2438                 }
2439                 
2440                 sprintf(str, "V:%-.4f", val);
2441                 BLF_position(blf_mono_font, dx, 6, 0);
2442                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2443                 dx += BLF_width(blf_mono_font, str);
2444
2445                 sprintf(str, "   L:%-.4f", lum);
2446                 BLF_position(blf_mono_font, dx, 6, 0);
2447                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2448                 dx += BLF_width(blf_mono_font, str);
2449         }
2450         else if(channels >= 3) {
2451                 if (fp) {
2452                         rgb_to_hsv(fp[0], fp[1], fp[2], &hue, &sat, &val);
2453                         rgb_to_yuv(fp[0], fp[1], fp[2], &lum, &u, &v);
2454                 }
2455                 else if (cp) {
2456                         rgb_to_hsv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &hue, &sat, &val);
2457                         rgb_to_yuv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &lum, &u, &v);
2458                 }
2459
2460                 sprintf(str, "H:%-.4f", hue);
2461                 BLF_position(blf_mono_font, dx, 6, 0);
2462                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2463                 dx += BLF_width(blf_mono_font, str);
2464
2465                 sprintf(str, "  S:%-.4f", sat);
2466                 BLF_position(blf_mono_font, dx, 6, 0);
2467                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2468                 dx += BLF_width(blf_mono_font, str);
2469
2470                 sprintf(str, "  V:%-.4f", val);
2471                 BLF_position(blf_mono_font, dx, 6, 0);
2472                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2473                 dx += BLF_width(blf_mono_font, str);
2474
2475                 sprintf(str, "   L:%-.4f", lum);
2476                 BLF_position(blf_mono_font, dx, 6, 0);
2477                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
2478                 dx += BLF_width(blf_mono_font, str);
2479         }
2480
2481         (void)dx;
2482 }
2483
2484 #if 0
2485 /* note: needs to be userpref or opengl profile option */
2486 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2487 {
2488
2489         draw_nodespace_grid(snode);
2490         
2491         if(snode->flag & SNODE_BACKDRAW) {
2492                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2493                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2494                 if(ibuf) {
2495                         int x, y;
2496                         float zoom = 1.0;
2497
2498                         glMatrixMode(GL_PROJECTION);
2499                         glPushMatrix();
2500                         glMatrixMode(GL_MODELVIEW);
2501                         glPushMatrix();
2502                         
2503                         glaDefine2DArea(&sa->winrct);
2504
2505                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2506                                 float zoomx, zoomy;
2507                                 zoomx= (float)sa->winx/ibuf->x;
2508                                 zoomy= (float)sa->winy/ibuf->y;
2509                                 zoom = MIN2(zoomx, zoomy);
2510                         }
2511                         
2512                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2513                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2514
2515                         glPixelZoom(zoom, zoom);
2516
2517                         glColor4f(1.0, 1.0, 1.0, 1.0);
2518                         if(ibuf->rect)
2519                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2520                         else if(ibuf->channels==4)
2521                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2522
2523                         glPixelZoom(1.0, 1.0);
2524
2525                         glMatrixMode(GL_PROJECTION);
2526                         glPopMatrix();
2527                         glMatrixMode(GL_MODELVIEW);
2528                         glPopMatrix();
2529                 }
2530         }
2531 }
2532 #endif
2533
2534 /* if v2d not NULL, it clips and returns 0 if not visible */
2535 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
2536 {
2537         float dist, vec[4][2];
2538         
2539         /* in v0 and v3 we put begin/end points */
2540         if(link->fromsock) {
2541                 vec[0][0]= link->fromsock->locx;
2542                 vec[0][1]= link->fromsock->locy;
2543         }
2544         else {
2545                 if(snode==NULL) return 0;
2546                 vec[0][0]= snode->mx;
2547                 vec[0][1]= snode->my;
2548         }
2549         if(link->tosock) {
2550                 vec[3][0]= link->tosock->locx;
2551                 vec[3][1]= link->tosock->locy;
2552         }
2553         else {
2554                 if(snode==NULL) return 0;
2555                 vec[3][0]= snode->mx;
2556                 vec[3][1]= snode->my;
2557         }
2558
2559         dist= UI_GetThemeValue(TH_NODE_CURVING)*0.10f*ABS(vec[0][0] - vec[3][0]);
2560         
2561         /* check direction later, for top sockets */
2562         vec[1][0]= vec[0][0]+dist;
2563         vec[1][1]= vec[0][1];
2564         
2565         vec[2][0]= vec[3][0]-dist;
2566         vec[2][1]= vec[3][1];
2567         
2568         if(v2d && MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax); /* clipped */      
2569         else if (v2d && MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin); /* clipped */
2570         else {
2571                 
2572                 /* always do all three, to prevent data hanging around */
2573                 forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2574                 forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
2575                 
2576                 return 1;
2577         }
2578         return 0;
2579 }
2580
2581 #define LINK_RESOL      24
2582 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 )
2583 {
2584         float coord_array[LINK_RESOL+1][2];
2585         
2586         if(node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
2587                 float dist, spline_step = 0.0f;
2588                 int i;
2589                 
2590                 /* store current linewidth */
2591                 float linew;
2592                 glGetFloatv(GL_LINE_WIDTH, &linew);
2593                 
2594                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2595                 dist = 1.0f/(float)LINK_RESOL;
2596                 
2597                 glEnable(GL_LINE_SMOOTH);
2598                 
2599                 if(do_triple) {
2600                         UI_ThemeColorShadeAlpha(th_col3, -80, -120);
2601                         glLineWidth(4.0f);
2602                         
2603                         glBegin(GL_LINE_STRIP);
2604                         for(i=0; i<=LINK_RESOL; i++) {
2605                                 glVertex2fv(coord_array[i]);
2606                         }
2607                         glEnd();
2608                 }
2609                 
2610                 UI_ThemeColor(th_col1);
2611                 glLineWidth(1.5f);
2612                 
2613                 glBegin(GL_LINE_STRIP);
2614                 for(i=0; i<=LINK_RESOL; i++) {
2615                         if(do_shaded) {
2616                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step);
2617                                 spline_step += dist;
2618                         }
2619                         glVertex2fv(coord_array[i]);
2620                 }
2621                 glEnd();
2622                 
2623                 glDisable(GL_LINE_SMOOTH);
2624                 
2625                 /* restore previuos linewidth */
2626                 glLineWidth(linew);
2627         }
2628 }
2629
2630 static void node_link_straight_points(View2D *UNUSED(v2d), SpaceNode *snode, bNodeLink *link, float coord_array[][2])
2631 {
2632         if(link->fromsock) {
2633                 coord_array[0][0]= link->fromsock->locx;
2634                 coord_array[0][1]= link->fromsock->locy;
2635         }
2636         else {
2637                 if(snode==NULL) return;
2638                 coord_array[0][0]= snode->mx;
2639                 coord_array[0][1]= snode->my;
2640         }
2641         if(link->tosock) {
2642                 coord_array[1][0]= link->tosock->locx;
2643                 coord_array[1][1]= link->tosock->locy;
2644         }
2645         else {
2646                 if(snode==NULL) return;
2647                 coord_array[1][0]= snode->mx;
2648                 coord_array[1][1]= snode->my;
2649         }
2650 }
2651
2652 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 )
2653 {
2654         float coord_array[2][2];
2655         float linew;
2656         int i;
2657         
2658         node_link_straight_points(v2d, snode, link, coord_array);
2659         
2660         /* store current linewidth */
2661         glGetFloatv(GL_LINE_WIDTH, &linew);
2662         
2663         glEnable(GL_LINE_SMOOTH);
2664         
2665         if(do_triple) {
2666                 UI_ThemeColorShadeAlpha(th_col3, -80, -120);
2667                 glLineWidth(4.0f);
2668                 
2669                 glBegin(GL_LINES);
2670                 glVertex2fv(coord_array[0]);
2671                 glVertex2fv(coord_array[1]);
2672                 glEnd();
2673         }
2674         
2675         UI_ThemeColor(th_col1);
2676         glLineWidth(1.5f);
2677         
2678         glBegin(GL_LINE_STRIP);
2679         for (i=0; i < LINK_RESOL; ++i) {
2680                 float t= (float)i/(float)(LINK_RESOL-1);
2681                 if(do_shaded)
2682                         UI_ThemeColorBlend(th_col1, th_col2, t);
2683                 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]);
2684         }
2685         glEnd();
2686         
2687         glDisable(GL_LINE_SMOOTH);
2688         
2689         /* restore previuos linewidth */
2690         glLineWidth(linew);
2691 }
2692
2693 /* note; this is used for fake links in groups too */
2694 void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link)
2695 {
2696         int do_shaded= 0, th_col1= TH_HEADER, th_col2= TH_HEADER;
2697         int do_triple= 0, th_col3= TH_WIRE;
2698         
2699         if(link->fromsock==NULL && link->tosock==NULL)
2700                 return;
2701         
2702         /* new connection */
2703         if(!link->fromsock || !link->tosock) {
2704                 th_col1 = TH_ACTIVE;
2705                 do_triple = 1;
2706         }
2707         else {
2708                 /* going to give issues once... */
2709                 if(link->tosock->flag & SOCK_UNAVAIL)
2710                         return;
2711                 if(link->fromsock->flag & SOCK_UNAVAIL)
2712                         return;
2713                 
2714                 /* a bit ugly... but thats how we detect the internal group links */
2715                 if(!link->fromnode || !link->tonode) {
2716                         UI_ThemeColorBlend(TH_BACK, TH_WIRE, 0.5f);
2717                         do_shaded= 0;
2718                 }
2719                 else {
2720                         /* check cyclic */
2721                         if((link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) && (link->flag & NODE_LINK_VALID)) {
2722                                 /* special indicated link, on drop-node */
2723                                 if(link->flag & NODE_LINKFLAG_HILITE) {
2724                                         th_col1= th_col2= TH_ACTIVE;
2725                                 }
2726                                 else {
2727                                         /* regular link */
2728                                         if(link->fromnode->flag & SELECT)
2729                                                 th_col1= TH_EDGE_SELECT;
2730                                         if(link->tonode->flag & SELECT)
2731                                                 th_col2= TH_EDGE_SELECT;
2732                                 }
2733                                 do_shaded= 1;
2734                                 do_triple= 1;
2735                         }                               
2736                         else {
2737                                 th_col1 = TH_REDALERT;
2738                         }
2739                 }
2740         }
2741         
2742         node_draw_link_bezier(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
2743 //      node_draw_link_straight(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
2744 }