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