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