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