Cycles: svn merge -r37904:37957 https://svn.blender.org/svnroot/bf-blender/trunk...
[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
57 #include "CMP_node.h"
58 #include "SHD_node.h"
59
60 #include "BIF_gl.h"
61 #include "BIF_glutil.h"
62
63 #include "BLF_api.h"
64
65 #include "MEM_guardedalloc.h"
66
67
68 #include "RNA_access.h"
69
70 #include "ED_node.h"
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74
75 #include "UI_interface.h"
76 #include "UI_resources.h"
77
78 #include "IMB_imbuf.h"
79 #include "IMB_imbuf_types.h"
80
81 #include "node_intern.h"
82
83
84 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
85
86 static void node_buts_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
87 {
88         PointerRNA sockptr;
89         PropertyRNA *prop;
90         
91         /* first socket stores value */
92         prop = RNA_struct_find_property(ptr, "outputs");
93         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
94         
95         uiItemR(layout, &sockptr, "default_value", 0, "", ICON_NONE);
96 }
97
98 static void node_buts_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
99 {
100         uiLayout *col;
101         PointerRNA sockptr;
102         PropertyRNA *prop;
103         
104         /* first socket stores value */
105         prop = RNA_struct_find_property(ptr, "outputs");
106         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
107         
108         col = uiLayoutColumn(layout, 0);
109         uiTemplateColorWheel(col, &sockptr, "default_value", 1, 0, 0, 0);
110         uiItemR(col, &sockptr, "default_value", 0, "", ICON_NONE);
111 }
112
113 static void node_buts_mix_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
114 {       
115         uiLayout *row;
116
117         bNodeTree *ntree= (bNodeTree*)ptr->id.data;
118
119         row= uiLayoutRow(layout, 1);
120         uiItemR(row, ptr, "blend_type", 0, "", ICON_NONE);
121         if(ntree->type == NTREE_COMPOSIT)
122                 uiItemR(row, ptr, "use_alpha", 0, "", ICON_IMAGE_RGB_ALPHA);
123 }
124
125 static void node_buts_time(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
126 {
127         uiLayout *row;
128 #if 0
129         /* XXX no context access here .. */
130         bNode *node= ptr->data;
131         CurveMapping *cumap= node->storage;
132         
133         if(cumap) {
134                 cumap->flag |= CUMA_DRAW_CFRA;
135                 if(node->custom1<node->custom2)
136                         cumap->sample[0]= (float)(CFRA - node->custom1)/(float)(node->custom2-node->custom1);
137         }
138 #endif
139
140         uiTemplateCurveMapping(layout, ptr, "curve", 's', 0, 0);
141
142         row= uiLayoutRow(layout, 1);
143         uiItemR(row, ptr, "frame_start", 0, "Sta", ICON_NONE);
144         uiItemR(row, ptr, "frame_end", 0, "End", ICON_NONE);
145 }
146
147 static void node_buts_colorramp(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
148 {
149         uiTemplateColorRamp(layout, ptr, "color_ramp", 0);
150 }
151
152 static void node_buts_curvevec(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
153 {
154         uiTemplateCurveMapping(layout, ptr, "mapping", 'v', 0, 0);
155 }
156
157 static float *_sample_col= NULL;        // bad bad, 2.5 will do better?
158 #if 0
159 static void node_curvemap_sample(float *col)
160 {
161         _sample_col= col;
162 }
163 #endif
164
165 static void node_buts_curvecol(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
166 {
167         bNode *node= ptr->data;
168         CurveMapping *cumap= node->storage;
169
170         if(_sample_col) {
171                 cumap->flag |= CUMA_DRAW_SAMPLE;
172                 VECCOPY(cumap->sample, _sample_col);
173         }
174         else 
175                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
176
177         uiTemplateCurveMapping(layout, ptr, "mapping", 'c', 0, 0);
178 }
179
180 static void node_normal_cb(bContext *C, void *ntree_v, void *node_v)
181 {
182         Main *bmain = CTX_data_main(C);
183
184         ED_node_generic_update(bmain, ntree_v, node_v);
185         WM_event_add_notifier(C, NC_NODE|NA_EDITED, ntree_v);
186 }
187
188 static void node_buts_normal(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
189 {
190         uiBlock *block= uiLayoutAbsoluteBlock(layout);
191         bNodeTree *ntree= ptr->id.data;
192         bNode *node= ptr->data;
193         rctf *butr= &node->butr;
194         bNodeSocket *sock= node->outputs.first;         /* first socket stores normal */
195         uiBut *bt;
196         
197         bt= uiDefButF(block, BUT_NORMAL, B_NODE_EXEC, "", 
198                           (short)butr->xmin, (short)butr->xmin, butr->xmax-butr->xmin, butr->xmax-butr->xmin, 
199                           sock->ns.vec, 0.0f, 1.0f, 0, 0, "");
200         uiButSetFunc(bt, node_normal_cb, ntree, node);
201 }
202 #if 0 // not used in 2.5x yet
203 static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
204 {
205         Main *bmain= CTX_data_main(C);
206         bNodeTree *ntree= ntree_v;
207         bNode *node= node_v;
208         Tex *tex;
209         
210         if(node->menunr<1) return;
211         
212         if(node->id) {
213                 node->id->us--;
214                 node->id= NULL;
215         }
216         tex= BLI_findlink(&bmain->tex, node->menunr-1);
217
218         node->id= &tex->id;
219         id_us_plus(node->id);
220         BLI_strncpy(node->name, node->id->name+2, sizeof(node->name));
221         
222         nodeSetActive(ntree, node);
223         
224         if( ntree->type == NTREE_TEXTURE )
225                 ntreeTexCheckCyclics( ntree );
226         
227         // allqueue(REDRAWBUTSSHADING, 0);
228         // allqueue(REDRAWNODE, 0);
229         NodeTagChanged(ntree, node); 
230         
231         node->menunr= 0;
232 }
233 #endif
234 static void node_dynamic_update_cb(bContext *C, void *UNUSED(ntree_v), void *node_v)
235 {
236         Main *bmain= CTX_data_main(C);
237         Material *ma;
238         bNode *node= (bNode *)node_v;
239         ID *id= node->id;
240         int error= 0;
241
242         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) error= 1;
243
244         /* Users only have to press the "update" button in one pynode
245          * and we also update all others sharing the same script */
246         for (ma= bmain->mat.first; ma; ma= ma->id.next) {
247                 if (ma->nodetree) {
248                         bNode *nd;
249                         for (nd= ma->nodetree->nodes.first; nd; nd= nd->next) {
250                                 if ((nd->type == NODE_DYNAMIC) && (nd->id == id)) {
251                                         nd->custom1= 0;
252                                         nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_REPARSE);
253                                         nd->menunr= 0;
254                                         if (error)
255                                                 nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_ERROR);
256                                 }
257                         }
258                 }
259         }
260
261         // allqueue(REDRAWBUTSSHADING, 0);
262         // allqueue(REDRAWNODE, 0);
263         // XXX BIF_preview_changed(ID_MA);
264 }
265
266 static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
267 {
268         bNode *node= ptr->data;
269
270         short multi = (
271                 node->id &&
272                 ((Tex*)node->id)->use_nodes &&
273                 (node->type != CMP_NODE_TEXTURE) &&
274                 (node->type != TEX_NODE_TEXTURE)
275         );
276         
277         uiItemR(layout, ptr, "texture", 0, "", ICON_NONE);
278         
279         if(multi) {
280                 /* Number Drawing not optimal here, better have a list*/
281                 uiItemR(layout, ptr, "node_output", 0, "", ICON_NONE);
282         }
283 }
284
285 static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
286
287         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
288 }
289
290 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
291
292 static void node_browse_text_cb(bContext *C, void *ntree_v, void *node_v)
293 {
294         Main *bmain= CTX_data_main(C);
295         bNodeTree *ntree= ntree_v;
296         bNode *node= node_v;
297         ID *oldid;
298         
299         if(node->menunr<1) return;
300         
301         if(node->id) {
302                 node->id->us--;
303         }
304         oldid= node->id;
305         node->id= BLI_findlink(&bmain->text, node->menunr-1);
306         id_us_plus(node->id);
307         BLI_strncpy(node->name, node->id->name+2, sizeof(node->name));
308
309         node->custom1= BSET(node->custom1, NODE_DYNAMIC_NEW);
310         
311         nodeSetActive(ntree, node);
312
313         // allqueue(REDRAWBUTSSHADING, 0);
314         // allqueue(REDRAWNODE, 0);
315
316         node->menunr= 0;
317 }
318
319 static void node_shader_buts_material(uiLayout *layout, bContext *C, PointerRNA *ptr)
320 {
321         bNode *node= ptr->data;
322         uiLayout *col;
323         
324         uiTemplateID(layout, C, ptr, "material", "MATERIAL_OT_new", NULL, NULL);
325         
326         if(!node->id) return;
327         
328         col= uiLayoutColumn(layout, 0);
329         uiItemR(col, ptr, "use_diffuse", 0, NULL, ICON_NONE);
330         uiItemR(col, ptr, "use_specular", 0, NULL, ICON_NONE);
331         uiItemR(col, ptr, "invert_normal", 0, NULL, ICON_NONE);
332 }
333
334 static void node_shader_buts_mapping(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
335 {
336         uiLayout *row;
337         
338         uiItemL(layout, "Translation:", ICON_NONE);
339         row= uiLayoutRow(layout, 1);
340         uiItemR(row, ptr, "location", 0, "", ICON_NONE);
341         
342         uiItemL(layout, "Rotation:", ICON_NONE);
343         row= uiLayoutRow(layout, 1);
344         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
345         
346         uiItemL(layout, "Scale:", ICON_NONE);
347         row= uiLayoutRow(layout, 1);
348         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
349         
350 #if 0
351         row= uiLayoutRow(layout, 1);
352         uiItemR(row, ptr, "use_min", 0, "Min", ICON_NONE);
353         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
354         
355         row= uiLayoutRow(layout, 1);
356         uiItemR(row, ptr, "use_max", 0, "Max", ICON_NONE);
357         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
358 #endif  
359 }
360
361 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
362
363         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
364 }
365
366 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
367 {
368         PointerRNA obptr= CTX_data_pointer_get(C, "active_object");
369         uiLayout *col;
370
371         col= uiLayoutColumn(layout, 0);
372
373         if(obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
374                 PointerRNA dataptr= RNA_pointer_get(&obptr, "data");
375
376                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
377                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
378         }
379         else {
380                 uiItemR(col, ptr, "uv_layer", 0, "UV", ICON_NONE);
381                 uiItemR(col, ptr, "color_layer", 0, "VCol", ICON_NONE);
382         }
383 }
384
385 /*static void node_layout_prop(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname)
386 {
387         if(C && CTX_wm_space_node(C)) {
388                 uiItemR(layout, ptr, propname, 0, NULL, ICON_NONE);
389         }
390         else {
391                 uiLayout *split = uiLayoutSplit(layout, 0.35f, 0);
392                 PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
393
394                 uiItemL(uiLayoutColumn(split, 0), RNA_property_ui_name(prop), ICON_NONE);
395                 uiItemR(uiLayoutColumn(split, 0), ptr, propname, 0, "", ICON_NONE);
396         }
397 }*/
398
399 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
400 {
401         //uiItemR(layout, ptr, "image", 0, "", ICON_NONE);
402         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
403         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
404 }
405
406 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
407 {
408         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
409         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
410 }
411
412 static void node_shader_buts_tex_blend(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
413 {
414         uiItemR(layout, ptr, "progression", 0, "", ICON_NONE);
415         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
416 }
417
418 static void node_shader_buts_tex_clouds(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
419 {
420         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
421         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
422         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
423 }
424
425 static void node_shader_buts_tex_distnoise(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
426 {
427         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
428         uiItemR(layout, ptr, "noise_distortion", 0, "", ICON_NONE);
429 }
430
431 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
432 {
433         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
434 }
435
436 static void node_shader_buts_tex_marble(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
437 {
438         uiItemR(layout, ptr, "marble_type", 0, "", ICON_NONE);
439         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
440         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
441         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
442         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
443 }
444
445 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
446 {
447         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
448         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
449 }
450
451 static void node_shader_buts_tex_stucci(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
452 {
453         uiItemR(layout, ptr, "stucci_type", 0, "", ICON_NONE);
454         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
455         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
456 }
457
458 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
459 {
460         uiItemR(layout, ptr, "distance_metric", 0, "", ICON_NONE);
461         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
462 }
463
464 static void node_shader_buts_tex_wood(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
465 {
466         uiItemR(layout, ptr, "noise_basis", 0, "", ICON_NONE);
467         uiItemR(layout, ptr, "noise_type", 0, "", ICON_NONE);
468         uiItemR(layout, ptr, "wood_type", 0, "", ICON_NONE);
469         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
470 }
471
472 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
473 {
474         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
475 }
476
477 static void node_shader_buts_dynamic(uiLayout *layout, bContext *C, PointerRNA *ptr)
478
479         Main *bmain= CTX_data_main(C);
480         uiBlock *block= uiLayoutAbsoluteBlock(layout);
481         bNode *node= ptr->data;
482         bNodeTree *ntree= ptr->id.data;
483         rctf *butr= &node->butr;
484         uiBut *bt;
485         // XXX SpaceNode *snode= curarea->spacedata.first;
486         short dy= (short)butr->ymin;
487         int xoff=0;
488
489         /* B_NODE_EXEC is handled in butspace.c do_node_buts */
490         if(!node->id) {
491                         const char *strp;
492                         IDnames_to_pupstring(&strp, NULL, "", &(bmain->text), NULL, NULL);
493                         node->menunr= 0;
494                         bt= uiDefButS(block, MENU, B_NODE_EXEC/*+node->nr*/, strp, 
495                                                         butr->xmin, dy, 19, 19, 
496                                                         &node->menunr, 0, 0, 0, 0, "Browses existing choices");
497                         uiButSetFunc(bt, node_browse_text_cb, ntree, node);
498                         xoff=19;
499                         if(strp) MEM_freeN((void *)strp);
500         }
501         else {
502                 bt = uiDefBut(block, BUT, B_NOP, "Update",
503                                 butr->xmin+xoff, butr->ymin+20, 50, 19,
504                                 &node->menunr, 0.0, 19.0, 0, 0, "Refresh this node (and all others that use the same script)");
505                 uiButSetFunc(bt, node_dynamic_update_cb, ntree, node);
506
507                 if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) {
508                         // UI_ThemeColor(TH_REDALERT);
509                         // XXX ui_rasterpos_safe(butr->xmin + xoff, butr->ymin + 5, snode->aspect);
510                         // XXX snode_drawstring(snode, "Error! Check console...", butr->xmax - butr->xmin);
511                         ;
512                 }
513         }
514 }
515
516 /* only once called */
517 static void node_shader_set_butfunc(bNodeType *ntype)
518 {
519         switch(ntype->type) {
520                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
521
522                 case SH_NODE_MATERIAL:
523                 case SH_NODE_MATERIAL_EXT:
524                         ntype->uifunc= node_shader_buts_material;
525                         break;
526                 case SH_NODE_TEXTURE:
527                         ntype->uifunc= node_buts_texture;
528                         break;
529                 case SH_NODE_NORMAL:
530                         ntype->uifunc= node_buts_normal;
531                         break;
532                 case SH_NODE_CURVE_VEC:
533                         ntype->uifunc= node_buts_curvevec;
534                         break;
535                 case SH_NODE_CURVE_RGB:
536                         ntype->uifunc= node_buts_curvecol;
537                         break;
538                 case SH_NODE_MAPPING:
539                         ntype->uifunc= node_shader_buts_mapping;
540                         break;
541                 case SH_NODE_VALUE:
542                         ntype->uifunc= node_buts_value;
543                         break;
544                 case SH_NODE_RGB:
545                         ntype->uifunc= node_buts_rgb;
546                         break;
547                 case SH_NODE_MIX_RGB:
548                         ntype->uifunc= node_buts_mix_rgb;
549                         break;
550                 case SH_NODE_VALTORGB:
551                         ntype->uifunc= node_buts_colorramp;
552                         break;
553                 case SH_NODE_MATH: 
554                         ntype->uifunc= node_buts_math;
555                         break; 
556                 case SH_NODE_VECT_MATH: 
557                         ntype->uifunc= node_shader_buts_vect_math;
558                         break; 
559                 case SH_NODE_GEOM:
560                         ntype->uifunc= node_shader_buts_geometry;
561                         break;
562                 case SH_NODE_TEX_SKY:
563                         ntype->uifunc= node_shader_buts_tex_sky;
564                         break;
565                 case SH_NODE_TEX_IMAGE:
566                         ntype->uifunc= node_shader_buts_tex_image;
567                         break;
568                 case SH_NODE_TEX_ENVIRONMENT:
569                         ntype->uifunc= node_shader_buts_tex_image;
570                         break;
571                 case SH_NODE_TEX_BLEND:
572                         ntype->uifunc= node_shader_buts_tex_blend;
573                         break;
574                 case SH_NODE_TEX_CLOUDS:
575                         ntype->uifunc= node_shader_buts_tex_clouds;
576                         break;
577                 case SH_NODE_TEX_DISTNOISE:
578                         ntype->uifunc= node_shader_buts_tex_distnoise;
579                         break;
580                 case SH_NODE_TEX_MAGIC:
581                         ntype->uifunc= node_shader_buts_tex_magic;
582                         break;
583                 case SH_NODE_TEX_MARBLE:
584                         ntype->uifunc= node_shader_buts_tex_marble;
585                         break;
586                 case SH_NODE_TEX_MUSGRAVE:
587                         ntype->uifunc= node_shader_buts_tex_musgrave;
588                         break;
589                 case SH_NODE_TEX_STUCCI:
590                         ntype->uifunc= node_shader_buts_tex_stucci;
591                         break;
592                 case SH_NODE_TEX_VORONOI:
593                         ntype->uifunc= node_shader_buts_tex_voronoi;
594                         break;
595                 case SH_NODE_TEX_WOOD:
596                         ntype->uifunc= node_shader_buts_tex_wood;
597                         break;
598                 case SH_NODE_BSDF_GLOSSY:
599                 case SH_NODE_BSDF_GLASS:
600                         ntype->uifunc= node_shader_buts_glossy;
601                         break;
602                 case NODE_DYNAMIC:
603                         ntype->uifunc= node_shader_buts_dynamic;
604                         break;
605                 default:
606                         ntype->uifunc= NULL;
607         }
608 }
609
610 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
611
612 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
613 {
614         uiLayout *col;
615         bNode *node= ptr->data;
616         PointerRNA imaptr;
617         PropertyRNA *prop;
618         int source;
619         
620         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
621         
622         if(!node->id) return;
623         
624         prop = RNA_struct_find_property(ptr, "image");
625         if (!prop || RNA_property_type(prop) != PROP_POINTER) return;
626         imaptr= RNA_property_pointer_get(ptr, prop);
627         
628         col= uiLayoutColumn(layout, 0);
629         
630         uiItemR(col, &imaptr, "source", 0, NULL, ICON_NONE);
631         
632         source= RNA_enum_get(&imaptr, "source");
633
634         if(source == IMA_SRC_SEQUENCE) {
635                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
636                 Scene *scene= CTX_data_scene(C);
637                 ImageUser *iuser= node->storage;
638                 char tstr[32];
639                 const int framenr= BKE_image_user_get_frame(iuser, CFRA, 0);
640                 BLI_snprintf(tstr, sizeof(tstr), "Frame: %d", framenr);
641                 uiItemL(layout, tstr, ICON_NONE);
642         }
643
644         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
645                 col= uiLayoutColumn(layout, 1);
646                 uiItemR(col, ptr, "frame_duration", 0, NULL, ICON_NONE);
647                 uiItemR(col, ptr, "frame_start", 0, NULL, ICON_NONE);
648                 uiItemR(col, ptr, "frame_offset", 0, NULL, ICON_NONE);
649                 uiItemR(col, ptr, "use_cyclic", 0, NULL, ICON_NONE);
650                 uiItemR(col, ptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
651         }
652
653         col= uiLayoutColumn(layout, 0);
654         
655         if (RNA_enum_get(&imaptr, "type")== IMA_TYPE_MULTILAYER)
656                 uiItemR(col, ptr, "layer", 0, NULL, ICON_NONE);
657 }
658
659 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
660 {
661         bNode *node= ptr->data;
662         uiLayout *col, *row;
663         PointerRNA op_ptr;
664         PointerRNA scn_ptr;
665         PropertyRNA *prop;
666         const char *layer_name;
667         char scene_name[MAX_ID_NAME-2];
668         
669         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
670         
671         if(!node->id) return;
672
673         col= uiLayoutColumn(layout, 0);
674         row = uiLayoutRow(col, 0);
675         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
676         
677         prop = RNA_struct_find_property(ptr, "layer");
678         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
679                 return;
680         
681         scn_ptr = RNA_pointer_get(ptr, "scene");
682         RNA_string_get(&scn_ptr, "name", scene_name);
683         
684         WM_operator_properties_create(&op_ptr, "RENDER_OT_render");
685         RNA_string_set(&op_ptr, "layer", layer_name);
686         RNA_string_set(&op_ptr, "scene", scene_name);
687         uiItemFullO(row, "RENDER_OT_render", "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
688
689 }
690
691
692 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
693 {
694         uiLayout *col, *row;
695         
696         col= uiLayoutColumn(layout, 0);
697         
698         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
699         if (RNA_enum_get(ptr, "filter_type")!= R_FILTER_FAST_GAUSS) {
700                 uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
701                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
702         }
703         
704         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
705         
706         if (RNA_boolean_get(ptr, "use_relative")) {
707                 uiItemL(col, "Aspect Correction", 0);
708                 row= uiLayoutRow(layout, 1);
709                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, 0);
710                 
711                 col= uiLayoutColumn(layout, 1);
712                 uiItemR(col, ptr, "factor_x", 0, "X", ICON_NONE);
713                 uiItemR(col, ptr, "factor_y", 0, "Y", ICON_NONE);
714         }
715         else {
716                 col= uiLayoutColumn(layout, 1);
717                 uiItemR(col, ptr, "size_x", 0, "X", ICON_NONE);
718                 uiItemR(col, ptr, "size_y", 0, "Y", ICON_NONE);
719         }
720 }
721
722 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
723 {
724         uiLayout *col;
725         
726         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
727         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
728         
729         col= uiLayoutColumn(layout, 1);
730         uiItemL(col, "Center:", ICON_NONE);
731         uiItemR(col, ptr, "center_x", 0, "X", ICON_NONE);
732         uiItemR(col, ptr, "center_y", 0, "Y", ICON_NONE);
733         
734         uiItemS(layout);
735         
736         col= uiLayoutColumn(layout, 1);
737         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
738         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
739         
740         uiItemS(layout);
741         
742         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
743         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
744 }
745
746 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
747 {       
748         uiLayout *col;
749         
750         col= uiLayoutColumn(layout, 1);
751         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
752         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
753         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
754 }
755
756 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
757 {
758         uiLayout *sub, *col;
759         
760         col= uiLayoutColumn(layout, 0);
761         uiItemL(col, "Bokeh Type:", ICON_NONE);
762         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
763         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
764
765         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
766
767         col = uiLayoutColumn(layout, 0);
768         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer")==1);
769         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
770
771         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
772         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
773
774         col = uiLayoutColumn(layout, 0);
775         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
776         sub = uiLayoutColumn(col, 0);
777         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_preview"));
778         uiItemR(sub, ptr, "samples", 0, NULL, ICON_NONE);
779         
780         col = uiLayoutColumn(layout, 0);
781         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
782         sub = uiLayoutColumn(col, 0);
783         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer")==0);
784         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
785 }
786
787 /* qdn: glare node */
788 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
789 {       
790         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
791         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
792
793         if (RNA_enum_get(ptr, "glare_type")!= 1) {
794                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
795         
796                 if (RNA_enum_get(ptr, "glare_type")!= 0) 
797                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
798         }
799         
800         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
801         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
802
803         if (RNA_enum_get(ptr, "glare_type")== 2) {
804                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
805                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
806         }
807         if (RNA_enum_get(ptr, "glare_type")== 0 || RNA_enum_get(ptr, "glare_type")== 2) {
808                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
809                 
810                 if (RNA_enum_get(ptr, "glare_type")== 0) 
811                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
812         }
813         if (RNA_enum_get(ptr, "glare_type")== 1) {
814                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
815         }
816 }
817
818 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
819 {       
820         uiLayout *col;
821
822         col = uiLayoutColumn(layout, 0);
823         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
824         if (RNA_enum_get(ptr, "tonemap_type")== 0) {
825                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
826                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
827                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
828         }
829         else {
830                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
831                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
832                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
833                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
834         }
835 }
836
837 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
838 {
839         uiLayout *col;
840
841         col= uiLayoutColumn(layout, 0);
842         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
843
844         col = uiLayoutColumn(col, 0);
845         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector")==0);
846         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
847         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
848 }
849
850 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
851 {
852         uiLayout *col;
853         
854         col= uiLayoutColumn(layout, 0);
855         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
856         uiItemR(col, ptr, "factor", 0, "Blur", ICON_NONE);
857         
858         col= uiLayoutColumn(layout, 1);
859         uiItemL(col, "Speed:", ICON_NONE);
860         uiItemR(col, ptr, "speed_min", 0, "Min", ICON_NONE);
861         uiItemR(col, ptr, "speed_max", 0, "Max", ICON_NONE);
862
863         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
864 }
865
866 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
867 {
868         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
869 }
870
871 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
872 {
873         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
874 }
875
876 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
877 {
878         uiLayout *col;
879
880         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
881         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
882
883         col= uiLayoutColumn(layout, 1);
884         if (RNA_boolean_get(ptr, "relative")){
885                 uiItemR(col, ptr, "rel_min_x", 0, "Left", ICON_NONE);
886                 uiItemR(col, ptr, "rel_max_x", 0, "Right", ICON_NONE);
887                 uiItemR(col, ptr, "rel_min_y", 0, "Up", ICON_NONE);
888                 uiItemR(col, ptr, "rel_max_y", 0, "Down", ICON_NONE);
889         } else {
890                 uiItemR(col, ptr, "min_x", 0, "Left", ICON_NONE);
891                 uiItemR(col, ptr, "max_x", 0, "Right", ICON_NONE);
892                 uiItemR(col, ptr, "min_y", 0, "Up", ICON_NONE);
893                 uiItemR(col, ptr, "max_y", 0, "Down", ICON_NONE);
894         }
895 }
896
897 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
898 {
899         uiLayout *row, *col;
900         
901         col= uiLayoutColumn(layout, 0);
902         row= uiLayoutRow(col, 0);
903         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
904         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
905 }
906
907 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
908 {
909         uiLayout *sub, *col;
910         
911         col =uiLayoutColumn(layout, 1);
912         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
913         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
914         
915         col =uiLayoutColumn(layout, 1);
916         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
917         sub =uiLayoutColumn(col, 0);
918         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
919         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
920         
921         col =uiLayoutColumn(layout, 1);
922         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
923         sub =uiLayoutColumn(col, 0);
924         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
925         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
926 }
927
928 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
929 {       
930         uiLayout *col;
931         
932         col =uiLayoutColumn(layout, 1);
933         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
934         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
935 }
936
937 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
938 {       
939         uiLayout *col;
940         
941         col =uiLayoutColumn(layout, 1);
942         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
943 }
944
945
946 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
947 {
948         uiLayout *col;
949         
950         col =uiLayoutColumn(layout, 0);
951         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
952         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
953         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
954 }
955
956 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
957 {
958         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
959 }
960
961 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
962 {
963         uiLayout *col;
964         
965         col =uiLayoutColumn(layout, 1);
966         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
967         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
968 }
969
970 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
971 {
972         uiLayout *col;
973         
974         col =uiLayoutColumn(layout, 1);
975         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
976         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
977 }
978
979 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
980 {
981         uiLayout *row, *col;
982         
983         uiItemL(layout, "Despill Channel:", ICON_NONE);
984         row =uiLayoutRow(layout,0);
985         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
986
987         col= uiLayoutColumn(layout, 0);
988         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
989
990         if(RNA_enum_get(ptr, "limit_method")==0) {
991                 uiItemL(col, "Limiting Channel:", ICON_NONE);
992                 row=uiLayoutRow(col,0);
993                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
994         }
995
996         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
997         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
998         if (RNA_boolean_get(ptr, "use_unspill")== 1) {
999                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1000                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1001                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1002         }
1003 }
1004
1005 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1006 {
1007         uiLayout *col;
1008         
1009         col= uiLayoutColumn(layout, 0);
1010         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1011         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1012         
1013         col= uiLayoutColumn(layout, 1);
1014         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1015         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1016         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1017 }
1018
1019 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1020 {
1021         uiLayout *col;
1022         
1023         col= uiLayoutColumn(layout, 1);
1024         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1025         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1026         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1027 }
1028
1029 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1030 {       
1031         uiLayout *col, *row;
1032
1033         uiItemL(layout, "Color Space:", ICON_NONE);
1034         row= uiLayoutRow(layout, 0);
1035         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1036
1037         col=uiLayoutColumn(layout, 0);
1038         uiItemL(col, "Key Channel:", ICON_NONE);
1039         row= uiLayoutRow(col, 0);
1040         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1041
1042         col =uiLayoutColumn(layout, 0);
1043
1044         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1045         if(RNA_enum_get(ptr, "limit_method")==0) {
1046                 uiItemL(col, "Limiting Channel:", ICON_NONE);
1047                 row=uiLayoutRow(col,0);
1048                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1049         }
1050
1051         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1052         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1053 }
1054
1055 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1056 {
1057         uiLayout *col;
1058         
1059         col= uiLayoutColumn(layout, 1);
1060         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1061         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1062 }
1063
1064 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1065 {
1066         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1067 }
1068
1069 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1070 {
1071         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1072 }
1073
1074 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1075 {
1076         uiLayout *col, *row;
1077
1078         col= uiLayoutColumn(layout, 0);
1079         uiItemR(col, ptr, "filepath", 0, "", ICON_NONE);
1080         uiItemR(col, ptr, "image_type", 0, "", ICON_NONE);
1081         
1082         row= uiLayoutRow(layout, 0);
1083         if (RNA_enum_get(ptr, "image_type")== R_OPENEXR) {
1084                 uiItemR(row, ptr, "use_exr_half", 0, NULL, ICON_NONE);
1085                 uiItemR(row, ptr, "exr_codec", 0, "", ICON_NONE);
1086         }
1087         else if (RNA_enum_get(ptr, "image_type")== R_JPEG90) {
1088                 uiItemR(row, ptr, "quality", UI_ITEM_R_SLIDER, "Quality", ICON_NONE);
1089         }
1090         else if (RNA_enum_get(ptr, "image_type")== R_PNG) {
1091                 uiItemR(row, ptr, "quality", UI_ITEM_R_SLIDER, "Compression", ICON_NONE);
1092         }
1093         
1094         row= uiLayoutRow(layout, 1);
1095         uiItemR(row, ptr, "frame_start", 0, "Start", ICON_NONE);
1096         uiItemR(row, ptr, "frame_end", 0, "End", ICON_NONE);
1097 }
1098
1099 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1100 {
1101         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
1102 }
1103
1104 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1105 {
1106         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1107 }
1108
1109 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1110 {
1111         uiLayout *col;
1112         
1113         col= uiLayoutColumn(layout, 0);
1114         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
1115         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
1116 }
1117
1118 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1119 {
1120         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
1121 }
1122
1123 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1124 {
1125         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1126 }
1127
1128 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1129 {
1130         uiLayout *split, *col, *row;
1131         
1132         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
1133         
1134         if (RNA_enum_get(ptr, "correction_method")== 0) {
1135         
1136                 split = uiLayoutSplit(layout, 0, 0);
1137                 col = uiLayoutColumn(split, 0);
1138                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
1139                 row = uiLayoutRow(col, 0);
1140                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
1141                 
1142                 col = uiLayoutColumn(split, 0);
1143                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
1144                 row = uiLayoutRow(col, 0);
1145                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
1146                 
1147                 col = uiLayoutColumn(split, 0);
1148                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
1149                 row = uiLayoutRow(col, 0);
1150                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
1151
1152         } else {
1153                 
1154                 split = uiLayoutSplit(layout, 0, 0);
1155                 col = uiLayoutColumn(split, 0);
1156                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
1157                 row = uiLayoutRow(col, 0);
1158                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
1159                 
1160                 col = uiLayoutColumn(split, 0);
1161                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
1162                 row = uiLayoutRow(col, 0);
1163                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
1164                 
1165                 col = uiLayoutColumn(split, 0);
1166                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
1167                 row = uiLayoutRow(col, 0);
1168                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
1169         }
1170
1171 }
1172
1173 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1174 {
1175         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
1176 }
1177
1178 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1179
1180         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
1181 }
1182
1183 /* only once called */
1184 static void node_composit_set_butfunc(bNodeType *ntype)
1185 {
1186         switch(ntype->type) {
1187                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1188
1189                 case CMP_NODE_IMAGE:
1190                         ntype->uifunc= node_composit_buts_image;
1191                         break;
1192                 case CMP_NODE_R_LAYERS:
1193                         ntype->uifunc= node_composit_buts_renderlayers;
1194                         break;
1195                 case CMP_NODE_NORMAL:
1196                         ntype->uifunc= node_buts_normal;
1197                         break;
1198                 case CMP_NODE_CURVE_VEC:
1199                         ntype->uifunc= node_buts_curvevec;
1200                         break;
1201                 case CMP_NODE_CURVE_RGB:
1202                         ntype->uifunc= node_buts_curvecol;
1203                         break;
1204                 case CMP_NODE_VALUE:
1205                         ntype->uifunc= node_buts_value;
1206                         break;
1207                 case CMP_NODE_RGB:
1208                         ntype->uifunc= node_buts_rgb;
1209                         break;
1210                 case CMP_NODE_FLIP:
1211                         ntype->uifunc= node_composit_buts_flip;
1212                         break;
1213                 case CMP_NODE_SPLITVIEWER:
1214                         ntype->uifunc= node_composit_buts_splitviewer;
1215                         break;
1216                 case CMP_NODE_MIX_RGB:
1217                         ntype->uifunc= node_buts_mix_rgb;
1218                         break;
1219                 case CMP_NODE_VALTORGB:
1220                         ntype->uifunc= node_buts_colorramp;
1221                         break;
1222                 case CMP_NODE_CROP:
1223                         ntype->uifunc= node_composit_buts_crop;
1224                         break;
1225                 case CMP_NODE_BLUR:
1226                         ntype->uifunc= node_composit_buts_blur;
1227                         break;
1228                 case CMP_NODE_DBLUR:
1229                         ntype->uifunc= node_composit_buts_dblur;
1230                         break;
1231                 case CMP_NODE_BILATERALBLUR:
1232                         ntype->uifunc= node_composit_buts_bilateralblur;
1233                         break;
1234                 case CMP_NODE_DEFOCUS:
1235                         ntype->uifunc = node_composit_buts_defocus;
1236                         break;
1237                 case CMP_NODE_GLARE:
1238                         ntype->uifunc = node_composit_buts_glare;
1239                         break;
1240                 case CMP_NODE_TONEMAP:
1241                         ntype->uifunc = node_composit_buts_tonemap;
1242                         break;
1243                 case CMP_NODE_LENSDIST:
1244                         ntype->uifunc = node_composit_buts_lensdist;
1245                         break;
1246                 case CMP_NODE_VECBLUR:
1247                         ntype->uifunc= node_composit_buts_vecblur;
1248                         break;
1249                 case CMP_NODE_FILTER:
1250                         ntype->uifunc= node_composit_buts_filter;
1251                         break;
1252                 case CMP_NODE_MAP_VALUE:
1253                         ntype->uifunc= node_composit_buts_map_value;
1254                         break;
1255                 case CMP_NODE_TIME:
1256                         ntype->uifunc= node_buts_time;
1257                         break;
1258                 case CMP_NODE_ALPHAOVER:
1259                         ntype->uifunc= node_composit_buts_alphaover;
1260                         break;
1261                 case CMP_NODE_HUE_SAT:
1262                         ntype->uifunc= node_composit_buts_hue_sat;
1263                         break;
1264                 case CMP_NODE_TEXTURE:
1265                         ntype->uifunc= node_buts_texture;
1266                         break;
1267                 case CMP_NODE_DILATEERODE:
1268                         ntype->uifunc= node_composit_buts_dilateerode;
1269                         break;
1270                 case CMP_NODE_OUTPUT_FILE:
1271                         ntype->uifunc= node_composit_buts_file_output;
1272                         break;
1273                 case CMP_NODE_DIFF_MATTE:
1274                         ntype->uifunc=node_composit_buts_diff_matte;
1275                         break;
1276                 case CMP_NODE_DIST_MATTE:
1277                         ntype->uifunc=node_composit_buts_distance_matte;
1278                         break;
1279                 case CMP_NODE_COLOR_SPILL:
1280                         ntype->uifunc=node_composit_buts_color_spill;
1281                         break;
1282                 case CMP_NODE_CHROMA_MATTE:
1283                         ntype->uifunc=node_composit_buts_chroma_matte;
1284                         break;
1285                 case CMP_NODE_COLOR_MATTE:
1286                         ntype->uifunc=node_composit_buts_color_matte;
1287                         break;
1288                 case CMP_NODE_SCALE:
1289                         ntype->uifunc= node_composit_buts_scale;
1290                         break;
1291                 case CMP_NODE_ROTATE:
1292                         ntype->uifunc=node_composit_buts_rotate;
1293                         break;
1294                 case CMP_NODE_CHANNEL_MATTE:
1295                         ntype->uifunc= node_composit_buts_channel_matte;
1296                         break;
1297                 case CMP_NODE_LUMA_MATTE:
1298                         ntype->uifunc= node_composit_buts_luma_matte;
1299                         break;
1300                 case CMP_NODE_MAP_UV:
1301                         ntype->uifunc= node_composit_buts_map_uv;
1302                         break;
1303                 case CMP_NODE_ID_MASK:
1304                         ntype->uifunc= node_composit_buts_id_mask;
1305                         break;
1306                 case CMP_NODE_MATH:
1307                         ntype->uifunc= node_buts_math;
1308                         break;
1309                 case CMP_NODE_INVERT:
1310                         ntype->uifunc= node_composit_buts_invert;
1311                         break;
1312                 case CMP_NODE_PREMULKEY:
1313                         ntype->uifunc= node_composit_buts_premulkey;
1314                         break;
1315                 case CMP_NODE_VIEW_LEVELS:
1316                         ntype->uifunc=node_composit_buts_view_levels;
1317                         break;
1318                 case CMP_NODE_COLORBALANCE:
1319                         ntype->uifunc=node_composit_buts_colorbalance;
1320                         break;
1321                 case CMP_NODE_HUECORRECT:
1322                         ntype->uifunc=node_composit_buts_huecorrect;
1323                         break;
1324                 case CMP_NODE_ZCOMBINE:
1325                         ntype->uifunc=node_composit_buts_zcombine;
1326                         break;
1327                 case CMP_NODE_COMBYCCA:
1328                 case CMP_NODE_SEPYCCA:
1329                         ntype->uifunc=node_composit_buts_ycc;
1330                         break;
1331                 default:
1332                         ntype->uifunc= NULL;
1333         }
1334 }
1335
1336 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
1337
1338 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1339 {
1340         uiLayout *col;
1341         
1342         col= uiLayoutColumn(layout, 1);
1343         uiItemR(col, ptr, "offset", 0, "Offset", ICON_NONE);
1344         uiItemR(col, ptr, "offset_frequency", 0, "Frequency", ICON_NONE);
1345         
1346         col= uiLayoutColumn(layout, 1);
1347         uiItemR(col, ptr, "squash", 0, "Squash", ICON_NONE);
1348         uiItemR(col, ptr, "squash_frequency", 0, "Frequency", ICON_NONE);
1349 }
1350
1351 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1352 {
1353         PointerRNA tex_ptr;
1354         bNode *node= ptr->data;
1355         ID *id= ptr->id.data;
1356         Tex *tex = (Tex *)node->storage;
1357         uiLayout *col, *row;
1358         
1359         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
1360
1361         col= uiLayoutColumn(layout, 0);
1362
1363         switch( tex->type ) {
1364                 case TEX_BLEND:
1365                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
1366                         row= uiLayoutRow(col, 0);
1367                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1368                         break;
1369
1370                 case TEX_MARBLE:
1371                         row= uiLayoutRow(col, 0);
1372                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1373                         row= uiLayoutRow(col, 0);
1374                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1375                         row= uiLayoutRow(col, 0);
1376                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
1377                         row= uiLayoutRow(col, 0);
1378                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1379                         break;
1380
1381                 case TEX_MAGIC:
1382                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
1383                         break;
1384
1385                 case TEX_STUCCI:
1386                         row= uiLayoutRow(col, 0);
1387                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1388                         row= uiLayoutRow(col, 0);
1389                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1390                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
1391                         break;
1392
1393                 case TEX_WOOD:
1394                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
1395                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
1396                         row= uiLayoutRow(col, 0);
1397                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1398                         row= uiLayoutRow(col, 0);
1399                         uiLayoutSetActive(row, !(RNA_enum_get(&tex_ptr, "wood_type")==TEX_BAND || RNA_enum_get(&tex_ptr, "wood_type")==TEX_RING)); 
1400                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1401                         break;
1402                         
1403                 case TEX_CLOUDS:
1404                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
1405                         row= uiLayoutRow(col, 0);
1406                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1407                         row= uiLayoutRow(col, 0);
1408                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1409                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, "Depth", ICON_NONE);
1410                         break;
1411                         
1412                 case TEX_DISTNOISE:
1413                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
1414                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
1415                         break;
1416
1417                 case TEX_MUSGRAVE:
1418                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
1419                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
1420                         break;
1421                 case TEX_VORONOI:
1422                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
1423                         if(tex->vn_distm == TEX_MINKOVSKY) {
1424                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
1425                         }
1426                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
1427                         break;
1428         }
1429 }
1430
1431 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1432 {
1433         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1434 }
1435
1436 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1437 {
1438         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
1439 }
1440
1441 /* only once called */
1442 static void node_texture_set_butfunc(bNodeType *ntype)
1443 {
1444         if( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
1445                 ntype->uifunc = node_texture_buts_proc;
1446         }
1447         else switch(ntype->type) {
1448                 
1449                 case TEX_NODE_MATH:
1450                         ntype->uifunc = node_buts_math;
1451                         break;
1452                 
1453                 case TEX_NODE_MIX_RGB:
1454                         ntype->uifunc = node_buts_mix_rgb;
1455                         break;
1456                         
1457                 case TEX_NODE_VALTORGB:
1458                         ntype->uifunc = node_buts_colorramp;
1459                         break;
1460                         
1461                 case TEX_NODE_CURVE_RGB:
1462                         ntype->uifunc= node_buts_curvecol;
1463                         break;
1464                         
1465                 case TEX_NODE_CURVE_TIME:
1466                         ntype->uifunc = node_buts_time;
1467                         break;
1468                         
1469                 case TEX_NODE_TEXTURE:
1470                         ntype->uifunc = node_buts_texture;
1471                         break;
1472                         
1473                 case TEX_NODE_BRICKS:
1474                         ntype->uifunc = node_texture_buts_bricks;
1475                         break;
1476                         
1477                 case TEX_NODE_IMAGE:
1478                         ntype->uifunc = node_texture_buts_image;
1479                         break;
1480                         
1481                 case TEX_NODE_OUTPUT:
1482                         ntype->uifunc = node_texture_buts_output;
1483                         break;
1484                         
1485                 default:
1486                         ntype->uifunc= NULL;
1487         }
1488 }
1489
1490 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
1491
1492 void ED_init_node_butfuncs(void)
1493 {
1494         bNodeType *ntype;
1495         
1496         /* shader nodes */
1497         ntype= node_all_shaders.first;
1498         while(ntype) {
1499                 node_shader_set_butfunc(ntype);
1500                 ntype= ntype->next;
1501         }
1502         /* composit nodes */
1503         ntype= node_all_composit.first;
1504         while(ntype) {
1505                 node_composit_set_butfunc(ntype);
1506                 ntype= ntype->next;
1507         }
1508         ntype = node_all_textures.first;
1509         while(ntype) {
1510                 node_texture_set_butfunc(ntype);
1511                 ntype= ntype->next;
1512         }
1513 }
1514
1515 /* ************** Generic drawing ************** */
1516
1517 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
1518 {
1519         
1520         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
1521                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
1522                 void *lock;
1523                 ImBuf *ibuf= BKE_image_acquire_ibuf(ima, NULL, &lock);
1524                 if(ibuf) {
1525                         float x, y; 
1526                         
1527                         glMatrixMode(GL_PROJECTION);
1528                         glPushMatrix();
1529                         glMatrixMode(GL_MODELVIEW);
1530                         glPushMatrix();
1531
1532                         /* keep this, saves us from a version patch */
1533                         if(snode->zoom==0.0f) snode->zoom= 1.0f;
1534                         
1535                         /* somehow the offset has to be calculated inverse */
1536                         
1537                         glaDefine2DArea(&ar->winrct);
1538                         /* ortho at pixel level curarea */
1539                         wmOrtho2(-0.375, ar->winx-0.375, -0.375, ar->winy-0.375);
1540                         
1541                         x = (ar->winx-snode->zoom*ibuf->x)/2 + snode->xof;
1542                         y = (ar->winy-snode->zoom*ibuf->y)/2 + snode->yof;
1543                         
1544                         if(!ibuf->rect) {
1545                                 if(color_manage)
1546                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
1547                                 else
1548                                         ibuf->profile = IB_PROFILE_NONE;
1549                                 IMB_rect_from_float(ibuf);
1550                         }
1551
1552                         if(ibuf->rect) {
1553                                 if (snode->flag & SNODE_SHOW_ALPHA) {
1554                                         glPixelZoom(snode->zoom, snode->zoom);
1555                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
1556                                         if(ENDIAN_ORDER == B_ENDIAN)
1557                                                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
1558                                         
1559                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
1560                                         
1561                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
1562                                         glPixelZoom(1.0f, 1.0f);
1563                                 } else if (snode->flag & SNODE_USE_ALPHA) {
1564                                         glEnable(GL_BLEND);
1565                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1566                                         glPixelZoom(snode->zoom, snode->zoom);
1567                                         
1568                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1569                                         
1570                                         glPixelZoom(1.0f, 1.0f);
1571                                         glDisable(GL_BLEND);
1572                                 } else {
1573                                         glPixelZoom(snode->zoom, snode->zoom);
1574                                         
1575                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1576                                         
1577                                         glPixelZoom(1.0f, 1.0f);
1578                                 }
1579                         }
1580                         
1581                         glMatrixMode(GL_PROJECTION);
1582                         glPopMatrix();
1583                         glMatrixMode(GL_MODELVIEW);
1584                         glPopMatrix();
1585                 }
1586
1587                 BKE_image_release_ibuf(ima, lock);
1588         }
1589 }
1590
1591 void draw_nodespace_color_info(ARegion *ar, int color_manage, int channels, int x, int y, char *cp, float *fp)
1592 {
1593         char str[256];
1594         float dx= 6;
1595         /* text colors */
1596         /* XXX colored text not allowed in Blender UI */
1597         #if 0
1598         unsigned char red[3] = {255, 50, 50};
1599         unsigned char green[3] = {0, 255, 0};
1600         unsigned char blue[3] = {100, 100, 255};
1601         #else
1602         unsigned char red[3] = {255, 255, 255};
1603         unsigned char green[3] = {255, 255, 255};
1604         unsigned char blue[3] = {255, 255, 255};
1605         #endif
1606         float hue=0, sat=0, val=0, lum=0, u=0, v=0;
1607         float col[4], finalcol[4];
1608
1609         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1610         glEnable(GL_BLEND);
1611
1612         /* noisy, high contrast make impossible to read if lower alpha is used. */
1613         glColor4ub(0, 0, 0, 190);
1614         glRecti(0.0, 0.0, ar->winrct.xmax - ar->winrct.xmin + 1, 20);
1615         glDisable(GL_BLEND);
1616
1617         BLF_size(blf_mono_font, 11, 72);
1618
1619         glColor3ub(255, 255, 255);
1620         sprintf(str, "X:%-4d  Y:%-4d |", x, y);
1621         // UI_DrawString(6, 6, str); // works ok but fixed width is nicer.
1622         BLF_position(blf_mono_font, dx, 6, 0);
1623         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1624         dx += BLF_width(blf_mono_font, str);
1625
1626         #if 0   /* XXX no Z value in compo backdrop atm */
1627         if(zp) {
1628                 glColor3ub(255, 255, 255);
1629                 sprintf(str, " Z:%-.4f |", 0.5f+0.5f*(((float)*zp)/(float)0x7fffffff));
1630                 BLF_position(blf_mono_font, dx, 6, 0);
1631                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1632                 dx += BLF_width(blf_mono_font, str);
1633         }
1634         if(zpf) {
1635                 glColor3ub(255, 255, 255);
1636                 sprintf(str, " Z:%-.3f |", *zpf);
1637                 BLF_position(blf_mono_font, dx, 6, 0);
1638                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1639                 dx += BLF_width(blf_mono_font, str);
1640         }
1641         #endif
1642
1643         if(channels >= 3) {
1644                 glColor3ubv(red);
1645                 if (fp)
1646                         sprintf(str, "  R:%-.4f", fp[0]);
1647                 else if (cp)
1648                         sprintf(str, "  R:%-3d", cp[0]);
1649                 else
1650                         sprintf(str, "  R:-");
1651                 BLF_position(blf_mono_font, dx, 6, 0);
1652                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1653                 dx += BLF_width(blf_mono_font, str);
1654                 
1655                 glColor3ubv(green);
1656                 if (fp)
1657                         sprintf(str, "  G:%-.4f", fp[1]);
1658                 else if (cp)
1659                         sprintf(str, "  G:%-3d", cp[1]);
1660                 else
1661                         sprintf(str, "  G:-");
1662                 BLF_position(blf_mono_font, dx, 6, 0);
1663                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1664                 dx += BLF_width(blf_mono_font, str);
1665                 
1666                 glColor3ubv(blue);
1667                 if (fp)
1668                         sprintf(str, "  B:%-.4f", fp[2]);
1669                 else if (cp)
1670                         sprintf(str, "  B:%-3d", cp[2]);
1671                 else
1672                         sprintf(str, "  B:-");
1673                 BLF_position(blf_mono_font, dx, 6, 0);
1674                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1675                 dx += BLF_width(blf_mono_font, str);
1676                 
1677                 if(channels == 4) {
1678                         glColor3ub(255, 255, 255);
1679                         if (fp)
1680                                 sprintf(str, "  A:%-.4f", fp[3]);
1681                         else if (cp)
1682                                 sprintf(str, "  A:%-3d", cp[3]);
1683                         else
1684                                 sprintf(str, "- ");
1685                         BLF_position(blf_mono_font, dx, 6, 0);
1686                         BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1687                         dx += BLF_width(blf_mono_font, str);
1688                 }
1689         }
1690         
1691         /* color rectangle */
1692         if (channels==1) {
1693                 if (fp)
1694                         col[0] = col[1] = col[2] = fp[0];
1695                 else if (cp)
1696                         col[0] = col[1] = col[2] = (float)cp[0]/255.0f;
1697                 else
1698                         col[0] = col[1] = col[2] = 0.0f;
1699         }
1700         else if (channels==3) {
1701                 if (fp)
1702                         copy_v3_v3(col, fp);
1703                 else if (cp) {
1704                         col[0] = (float)cp[0]/255.0f;
1705                         col[1] = (float)cp[1]/255.0f;
1706                         col[2] = (float)cp[2]/255.0f;
1707                 }
1708                 else
1709                         zero_v3(col);
1710         }
1711         else if (channels==4) {
1712                 if (fp)
1713                         copy_v4_v4(col, fp);
1714                 else if (cp) {
1715                         col[0] = (float)cp[0]/255.0f;
1716                         col[1] = (float)cp[1]/255.0f;
1717                         col[2] = (float)cp[2]/255.0f;
1718                         col[3] = (float)cp[3]/255.0f;
1719                 }
1720                 else
1721                         zero_v4(col);
1722         }
1723         if (color_manage) {
1724                 linearrgb_to_srgb_v3_v3(finalcol, col);
1725                 finalcol[3] = col[3];
1726         }
1727         else {
1728                 copy_v4_v4(finalcol, col);
1729         }
1730         glDisable(GL_BLEND);
1731         glColor3fv(finalcol);
1732         dx += 5;
1733         glBegin(GL_QUADS);
1734         glVertex2f(dx, 3);
1735         glVertex2f(dx, 17);
1736         glVertex2f(dx+30, 17);
1737         glVertex2f(dx+30, 3);
1738         glEnd();
1739         dx += 35;
1740
1741         glColor3ub(255, 255, 255);
1742         if(channels == 1) {
1743                 if (fp) {
1744                         rgb_to_hsv(fp[0], fp[0], fp[0], &hue, &sat, &val);
1745                         rgb_to_yuv(fp[0], fp[0], fp[0], &lum, &u, &v);
1746                 }
1747                 else if (cp) {
1748                         rgb_to_hsv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &hue, &sat, &val);
1749                         rgb_to_yuv((float)cp[0]/255.0f, (float)cp[0]/255.0f, (float)cp[0]/255.0f, &lum, &u, &v);
1750                 }
1751                 
1752                 sprintf(str, "V:%-.4f", val);
1753                 BLF_position(blf_mono_font, dx, 6, 0);
1754                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1755                 dx += BLF_width(blf_mono_font, str);
1756
1757                 sprintf(str, "   L:%-.4f", lum);
1758                 BLF_position(blf_mono_font, dx, 6, 0);
1759                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1760                 dx += BLF_width(blf_mono_font, str);
1761         }
1762         else if(channels >= 3) {
1763                 if (fp) {
1764                         rgb_to_hsv(fp[0], fp[1], fp[2], &hue, &sat, &val);
1765                         rgb_to_yuv(fp[0], fp[1], fp[2], &lum, &u, &v);
1766                 }
1767                 else if (cp) {
1768                         rgb_to_hsv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &hue, &sat, &val);
1769                         rgb_to_yuv((float)cp[0]/255.0f, (float)cp[1]/255.0f, (float)cp[2]/255.0f, &lum, &u, &v);
1770                 }
1771
1772                 sprintf(str, "H:%-.4f", hue);
1773                 BLF_position(blf_mono_font, dx, 6, 0);
1774                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1775                 dx += BLF_width(blf_mono_font, str);
1776
1777                 sprintf(str, "  S:%-.4f", sat);
1778                 BLF_position(blf_mono_font, dx, 6, 0);
1779                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1780                 dx += BLF_width(blf_mono_font, str);
1781
1782                 sprintf(str, "  V:%-.4f", val);
1783                 BLF_position(blf_mono_font, dx, 6, 0);
1784                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1785                 dx += BLF_width(blf_mono_font, str);
1786
1787                 sprintf(str, "   L:%-.4f", lum);
1788                 BLF_position(blf_mono_font, dx, 6, 0);
1789                 BLF_draw_ascii(blf_mono_font, str, sizeof(str));
1790                 dx += BLF_width(blf_mono_font, str);
1791         }
1792
1793         (void)dx;
1794 }
1795
1796 #if 0
1797 /* note: needs to be userpref or opengl profile option */
1798 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
1799 {
1800
1801         draw_nodespace_grid(snode);
1802         
1803         if(snode->flag & SNODE_BACKDRAW) {
1804                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
1805                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
1806                 if(ibuf) {
1807                         int x, y;
1808                         float zoom = 1.0;
1809
1810                         glMatrixMode(GL_PROJECTION);
1811                         glPushMatrix();
1812                         glMatrixMode(GL_MODELVIEW);
1813                         glPushMatrix();
1814                         
1815                         glaDefine2DArea(&sa->winrct);
1816
1817                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
1818                                 float zoomx, zoomy;
1819                                 zoomx= (float)sa->winx/ibuf->x;
1820                                 zoomy= (float)sa->winy/ibuf->y;
1821                                 zoom = MIN2(zoomx, zoomy);
1822                         }
1823                         
1824                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
1825                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
1826
1827                         glPixelZoom(zoom, zoom);
1828
1829                         glColor4f(1.0, 1.0, 1.0, 1.0);
1830                         if(ibuf->rect)
1831                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1832                         else if(ibuf->channels==4)
1833                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
1834
1835                         glPixelZoom(1.0, 1.0);
1836
1837                         glMatrixMode(GL_PROJECTION);
1838                         glPopMatrix();
1839                         glMatrixMode(GL_MODELVIEW);
1840                         glPopMatrix();
1841                 }
1842         }
1843 }
1844 #endif
1845
1846 /* if v2d not NULL, it clips and returns 0 if not visible */
1847 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
1848 {
1849         float dist, vec[4][2];
1850         
1851         /* in v0 and v3 we put begin/end points */
1852         if(link->fromsock) {
1853                 vec[0][0]= link->fromsock->locx;
1854                 vec[0][1]= link->fromsock->locy;
1855         }
1856         else {
1857                 if(snode==NULL) return 0;
1858                 vec[0][0]= snode->mx;
1859                 vec[0][1]= snode->my;
1860         }
1861         if(link->tosock) {
1862                 vec[3][0]= link->tosock->locx;
1863                 vec[3][1]= link->tosock->locy;
1864         }
1865         else {
1866                 if(snode==NULL) return 0;
1867                 vec[3][0]= snode->mx;
1868                 vec[3][1]= snode->my;
1869         }
1870         
1871         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
1872         
1873         /* check direction later, for top sockets */
1874         vec[1][0]= vec[0][0]+dist;
1875         vec[1][1]= vec[0][1];
1876         
1877         vec[2][0]= vec[3][0]-dist;
1878         vec[2][1]= vec[3][1];
1879         
1880         if(v2d && MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax); /* clipped */      
1881         else if (v2d && MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin); /* clipped */
1882         else {
1883                 
1884                 /* always do all three, to prevent data hanging around */
1885                 forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
1886                 forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
1887                 
1888                 return 1;
1889         }
1890         return 0;
1891 }
1892
1893 #define LINK_RESOL      24
1894 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 )
1895 {
1896         float coord_array[LINK_RESOL+1][2];
1897         
1898         if(node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
1899                 float dist, spline_step = 0.0f;
1900                 int i;
1901                 
1902                 /* store current linewidth */
1903                 float linew;
1904                 glGetFloatv(GL_LINE_WIDTH, &linew);
1905                 
1906                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
1907                 dist = 1.0f/(float)LINK_RESOL;
1908                 
1909                 glEnable(GL_LINE_SMOOTH);
1910                 
1911                 if(do_triple) {
1912                         UI_ThemeColorShadeAlpha(th_col3, -80, -120);
1913                         glLineWidth(4.0f);
1914                         
1915                         glBegin(GL_LINE_STRIP);
1916                         for(i=0; i<=LINK_RESOL; i++) {
1917                                 glVertex2fv(coord_array[i]);
1918                         }
1919                         glEnd();
1920                 }
1921                 
1922                 UI_ThemeColor(th_col1);
1923                 glLineWidth(1.5f);
1924                 
1925                 glBegin(GL_LINE_STRIP);
1926                 for(i=0; i<=LINK_RESOL; i++) {
1927                         if(do_shaded) {
1928                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step);
1929                                 spline_step += dist;
1930                         }
1931                         glVertex2fv(coord_array[i]);
1932                 }
1933                 glEnd();
1934                 
1935                 glDisable(GL_LINE_SMOOTH);
1936                 
1937                 /* restore previuos linewidth */
1938                 glLineWidth(linew);
1939         }
1940 }
1941
1942 /* note; this is used for fake links in groups too */
1943 void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link)
1944 {
1945         int do_shaded= 0, th_col1= TH_HEADER, th_col2= TH_HEADER;
1946         int do_triple= 0, th_col3= TH_WIRE;
1947         
1948         if(link->fromsock==NULL && link->tosock==NULL)
1949                 return;
1950         
1951         /* new connection */
1952         if(!link->fromsock || !link->tosock) {
1953                 th_col1 = TH_ACTIVE;
1954                 do_triple = 1;
1955         }
1956         else {
1957                 /* going to give issues once... */
1958                 if(link->tosock->flag & SOCK_UNAVAIL)
1959                         return;
1960                 if(link->fromsock->flag & SOCK_UNAVAIL)
1961                         return;
1962                 
1963                 /* a bit ugly... but thats how we detect the internal group links */
1964                 if(!link->fromnode || !link->tonode) {
1965                         UI_ThemeColorBlend(TH_BACK, TH_WIRE, 0.5f);
1966                         do_shaded= 0;
1967                 }
1968                 else {
1969                         /* check cyclic */
1970                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
1971                                 if(link->fromnode->flag & SELECT)
1972                                         th_col1= TH_EDGE_SELECT;
1973                                 if(link->tonode->flag & SELECT)
1974                                         th_col2= TH_EDGE_SELECT;
1975                                 do_shaded= 1;
1976                                 do_triple= 1;
1977                         }                               
1978                         else {
1979                                 th_col1 = TH_REDALERT;
1980                         }
1981                 }
1982         }
1983         
1984         node_draw_link_bezier(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3);
1985 }
1986
1987