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