2.5 Nodes:
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36
37 #include "DNA_ID.h"
38 #include "DNA_node_types.h"
39 #include "DNA_image_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_action_types.h"
43 #include "DNA_color_types.h"
44 #include "DNA_customdata_types.h"
45 #include "DNA_gpencil_types.h"
46 #include "DNA_ipo_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_space_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_texture_types.h"
52 #include "DNA_text_types.h"
53 #include "DNA_userdef_types.h"
54
55 #include "BKE_context.h"
56 #include "BKE_curve.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_library.h"
60 #include "BKE_main.h"
61 #include "BKE_material.h"
62 #include "BKE_node.h"
63 #include "BKE_object.h"
64 #include "BKE_texture.h"
65 #include "BKE_text.h"
66 #include "BKE_utildefines.h"
67
68 #include "CMP_node.h"
69 #include "SHD_node.h"
70
71 #include "BIF_gl.h"
72 #include "BIF_glutil.h"
73
74 #include "MEM_guardedalloc.h"
75
76 #include "ED_node.h"
77 #include "ED_space_api.h"
78 #include "ED_screen.h"
79 #include "ED_types.h"
80
81 #include "RNA_access.h"
82 #include "RNA_define.h"
83
84 #include "WM_api.h"
85 #include "WM_types.h"
86
87 #include "UI_view2d.h"
88 #include "UI_interface.h"
89 #include "UI_resources.h"
90
91 #include "RE_pipeline.h"
92 #include "IMB_imbuf_types.h"
93
94 #include "node_intern.h"
95
96
97 /* autocomplete callback for buttons */
98 static void autocomplete_vcol(bContext *C, char *str, void *arg_v)
99 {
100         Mesh *me;
101         CustomDataLayer *layer;
102         AutoComplete *autocpl;
103         int a;
104
105         if(str[0]==0)
106                 return;
107
108         autocpl= autocomplete_begin(str, 32);
109                 
110         /* search if str matches the beginning of name */
111         for(me= G.main->mesh.first; me; me=me->id.next)
112                 for(a=0, layer= me->fdata.layers; a<me->fdata.totlayer; a++, layer++)
113                         if(layer->type == CD_MCOL)
114                                 autocomplete_do_name(autocpl, layer->name);
115         
116         autocomplete_end(autocpl, str);
117 }
118
119 static int verify_valid_vcol_name(char *str)
120 {
121         Mesh *me;
122         CustomDataLayer *layer;
123         int a;
124         
125         if(str[0]==0)
126                 return 1;
127
128         /* search if str matches the name */
129         for(me= G.main->mesh.first; me; me=me->id.next)
130                 for(a=0, layer= me->fdata.layers; a<me->fdata.totlayer; a++, layer++)
131                         if(layer->type == CD_MCOL)
132                                 if(strcmp(layer->name, str)==0)
133                                         return 1;
134         
135         return 0;
136 }
137
138 /* ****************** GENERAL CALLBACKS FOR NODES ***************** */
139
140 static void node_ID_title_cb(bContext *C, void *node_v, void *unused_v)
141 {
142         bNode *node= node_v;
143         
144         if(node->id) {
145                 test_idbutton(node->id->name+2);        /* library.c, verifies unique name */
146                 BLI_strncpy(node->name, node->id->name+2, 21);
147         }
148 }
149
150
151 static void node_but_title_cb(bContext *C, void *node_v, void *but_v)
152 {
153         // bNode *node= node_v;
154         // XXX uiBut *bt= but_v;
155         // XXX BLI_strncpy(node->name, bt->drawstr, NODE_MAXSTR);
156         
157         // allqueue(REDRAWNODE, 0);
158 }
159
160 #if 0
161 /* XXX not used yet, make compiler happy :) */
162 static void node_group_alone_cb(bContext *C, void *node_v, void *unused_v)
163 {
164         bNode *node= node_v;
165         
166         nodeCopyGroup(node);
167
168         // allqueue(REDRAWNODE, 0);
169 }
170
171 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
172
173 static void node_buts_group(uiLayout *layout, PointerRNA *ptr)
174 {
175         uiBlock *block= uiLayoutFreeBlock(layout);
176         bNode *node= ptr->data;
177         rctf *butr= &node->butr;
178
179         if(node->id) {
180                 uiBut *bt;
181                 short width;
182                 
183                 uiBlockBeginAlign(block);
184                 
185                 /* name button */
186                 width= (short)(butr->xmax-butr->xmin - (node->id->us>1?19.0f:0.0f));
187                 bt= uiDefBut(block, TEX, B_NOP, "NT:",
188                                          (short)butr->xmin, (short)butr->ymin, width, 19, 
189                                          node->id->name+2, 0.0, 19.0, 0, 0, "NodeTree name");
190                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
191                 
192                 /* user amount */
193                 if(node->id->us>1) {
194                         char str1[32];
195                         sprintf(str1, "%d", node->id->us);
196                         bt= uiDefBut(block, BUT, B_NOP, str1, 
197                                                  (short)butr->xmax-19, (short)butr->ymin, 19, 19, 
198                                                  NULL, 0, 0, 0, 0, "Displays number of users.");
199                         uiButSetFunc(bt, node_group_alone_cb, node, NULL);
200                 }
201                 
202                 uiBlockEndAlign(block);
203         }       
204 }
205 #endif
206
207 static void node_buts_value(uiLayout *layout, PointerRNA *ptr)
208 {
209         uiBlock *block= uiLayoutFreeBlock(layout);
210         bNode *node= ptr->data;
211         rctf *butr= &node->butr;
212         bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
213         
214         uiDefButF(block, NUM, B_NODE_EXEC, "", 
215                           (short)butr->xmin, (short)butr->ymin, butr->xmax-butr->xmin, 20, 
216                           sock->ns.vec, sock->ns.min, sock->ns.max, 10, 2, "");
217 }
218
219 static void node_buts_rgb(uiLayout *layout, PointerRNA *ptr)
220 {
221         uiBlock *block= uiLayoutFreeBlock(layout);
222         bNode *node= ptr->data;
223         rctf *butr= &node->butr;
224         bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
225
226         if(sock) {
227                 /* enforce square box drawing */
228                 uiBlockSetEmboss(block, UI_EMBOSSP);
229                 
230                 uiDefButF(block, HSVCUBE, B_NODE_EXEC, "", 
231                                   (short)butr->xmin, (short)butr->ymin, butr->xmax-butr->xmin, 12, 
232                                   sock->ns.vec, 0.0f, 1.0f, 3, 0, "");
233                 uiDefButF(block, HSVCUBE, B_NODE_EXEC, "", 
234                                   (short)butr->xmin, (short)butr->ymin+15, butr->xmax-butr->xmin, butr->xmax-butr->xmin -15 -15, 
235                                   sock->ns.vec, 0.0f, 1.0f, 2, 0, "");
236                 uiDefButF(block, COL, B_NOP, "",                
237                                   (short)butr->xmin, (short)butr->ymax-12, butr->xmax-butr->xmin, 12, 
238                                   sock->ns.vec, 0.0, 0.0, -1, 0, "");
239                 /* the -1 above prevents col button to popup a color picker */
240                 
241                 uiBlockSetEmboss(block, UI_EMBOSS);
242         }
243 }
244
245 static void node_buts_mix_rgb(uiLayout *layout, PointerRNA *ptr)
246 {
247         bNodeTree *ntree= (bNodeTree*)ptr->id.data;
248         uiLayout *row;
249
250         row= uiLayoutRow(layout, 1);
251
252         uiItemR(row, "", 0, ptr, "blend_type", 0);
253         if(ntree->type == NTREE_COMPOSIT)
254                 uiItemR(row, "", ICON_IMAGE_RGB_ALPHA, ptr, "alpha", 0);
255 }
256
257 static void node_buts_time(uiLayout *layout, PointerRNA *ptr)
258 {
259         uiLayout *row;
260 #if 0
261         /* XXX no context access here .. */
262         bNode *node= ptr->data;
263         CurveMapping *cumap= node->storage;
264         
265         if(cumap) {
266                 cumap->flag |= CUMA_DRAW_CFRA;
267                 if(node->custom1<node->custom2)
268                         cumap->sample[0]= (float)(CFRA - node->custom1)/(float)(node->custom2-node->custom1);
269         }
270 #endif
271
272         uiTemplateCurveMapping(layout, ptr, "curve", 's', 0);
273
274         row= uiLayoutRow(layout, 1);
275         uiItemR(row, "Sta", 0, ptr, "start", 0);
276         uiItemR(row, "End", 0, ptr, "end", 0);
277 }
278
279 static void node_buts_valtorgb(uiLayout *layout, PointerRNA *ptr)
280 {
281         uiBlock *block= uiLayoutFreeBlock(layout);
282         bNode *node= ptr->data;
283         rctf *butr= &node->butr;
284
285         if(node->storage) {
286                 uiBlockColorbandButtons(block, node->storage, butr, B_NODE_EXEC);
287         }
288 }
289
290 static void node_buts_curvevec(uiLayout *layout, PointerRNA *ptr)
291 {
292         uiTemplateCurveMapping(layout, ptr, "mapping", 'v', 0);
293 }
294
295 static float *_sample_col= NULL;        // bad bad, 2.5 will do better?
296 void node_curvemap_sample(float *col)
297 {
298         _sample_col= col;
299 }
300
301 static void node_buts_curvecol(uiLayout *layout, PointerRNA *ptr)
302 {
303         bNode *node= ptr->data;
304         CurveMapping *cumap= node->storage;
305
306         if(_sample_col) {
307                 cumap->flag |= CUMA_DRAW_SAMPLE;
308                 VECCOPY(cumap->sample, _sample_col);
309         }
310         else 
311                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
312
313         uiTemplateCurveMapping(layout, ptr, "mapping", 'c', 0);
314 }
315
316 static void node_buts_normal(uiLayout *layout, PointerRNA *ptr)
317 {
318         uiBlock *block= uiLayoutFreeBlock(layout);
319         bNode *node= ptr->data;
320         rctf *butr= &node->butr;
321         bNodeSocket *sock= node->outputs.first;         /* first socket stores normal */
322         
323         uiDefButF(block, BUT_NORMAL, B_NODE_EXEC, "", 
324                           (short)butr->xmin, (short)butr->xmin, butr->xmax-butr->xmin, butr->xmax-butr->xmin, 
325                           sock->ns.vec, 0.0f, 1.0f, 0, 0, "");
326 }
327
328 static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
329 {
330         bNodeTree *ntree= ntree_v;
331         bNode *node= node_v;
332         Tex *tex;
333         
334         if(node->menunr<1) return;
335         
336         if(node->id) {
337                 node->id->us--;
338                 node->id= NULL;
339         }
340         tex= BLI_findlink(&G.main->tex, node->menunr-1);
341
342         node->id= &tex->id;
343         id_us_plus(node->id);
344         BLI_strncpy(node->name, node->id->name+2, 21);
345         
346         nodeSetActive(ntree, node);
347         
348         if( ntree->type == NTREE_TEXTURE )
349                 ntreeTexCheckCyclics( ntree );
350         
351         // allqueue(REDRAWBUTSSHADING, 0);
352         // allqueue(REDRAWNODE, 0);
353         NodeTagChanged(ntree, node); 
354         
355         node->menunr= 0;
356 }
357
358 static void node_dynamic_update_cb(bContext *C, void *ntree_v, void *node_v)
359 {
360         Material *ma;
361         bNode *node= (bNode *)node_v;
362         ID *id= node->id;
363         int error= 0;
364
365         if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) error= 1;
366
367         /* Users only have to press the "update" button in one pynode
368          * and we also update all others sharing the same script */
369         for (ma= G.main->mat.first; ma; ma= ma->id.next) {
370                 if (ma->nodetree) {
371                         bNode *nd;
372                         for (nd= ma->nodetree->nodes.first; nd; nd= nd->next) {
373                                 if ((nd->type == NODE_DYNAMIC) && (nd->id == id)) {
374                                         nd->custom1= 0;
375                                         nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_REPARSE);
376                                         nd->menunr= 0;
377                                         if (error)
378                                                 nd->custom1= BSET(nd->custom1, NODE_DYNAMIC_ERROR);
379                                 }
380                         }
381                 }
382         }
383
384         // allqueue(REDRAWBUTSSHADING, 0);
385         // allqueue(REDRAWNODE, 0);
386         // XXX BIF_preview_changed(ID_MA);
387 }
388
389 static void node_buts_texture(uiLayout *layout, PointerRNA *ptr)
390 {
391         uiBlock *block= uiLayoutFreeBlock(layout);
392         bNode *node= ptr->data;
393         bNodeTree *ntree= ptr->id.data;
394         rctf *butr= &node->butr;
395
396         short multi = (
397                 node->id &&
398                 ((Tex*)node->id)->use_nodes &&
399                 (node->type != CMP_NODE_TEXTURE) &&
400                 (node->type != TEX_NODE_TEXTURE)
401         );
402         
403         uiBut *bt;
404         char *strp;
405         short width = (short)(butr->xmax - butr->xmin);
406         
407         /* browse button texture */
408         uiBlockBeginAlign(block);
409         IDnames_to_pupstring(&strp, NULL, "", &(G.main->tex), NULL, NULL);
410         node->menunr= 0;
411         bt= uiDefButS(block, MENU, B_NODE_EXEC, strp, 
412                         butr->xmin, butr->ymin+(multi?30:0), 20, 19, 
413                                   &node->menunr, 0, 0, 0, 0, "Browse texture");
414         uiButSetFunc(bt, node_browse_tex_cb, ntree, node);
415         if(strp) MEM_freeN(strp);
416         
417         if(node->id) {
418                 bt= uiDefBut(block, TEX, B_NOP, "TE:",
419                                 butr->xmin+19, butr->ymin+(multi?30:0), butr->xmax-butr->xmin-19, 19, 
420                                          node->id->name+2, 0.0, 19.0, 0, 0, "Texture name");
421                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
422         }
423         uiBlockEndAlign(block);
424         
425         if(multi) {
426                 char *menustr = ntreeTexOutputMenu(((Tex*)node->id)->nodetree);
427                 uiDefButS(block, MENU, B_MATPRV, menustr, butr->xmin, butr->ymin, width, 19, &node->custom1, 0, 0, 0, 0, "Which output to use, for multi-output textures");
428                 free(menustr);
429         }
430 }
431
432 static void node_buts_math(uiLayout *layout, PointerRNA *ptr)
433
434         uiBlock *block= uiLayoutFreeBlock(layout);
435         bNode *node= ptr->data;
436         rctf *butr= &node->butr;
437         uiBut *bt; 
438
439         bt=uiDefButS(block, MENU, B_NODE_EXEC,  "Add %x0|Subtract %x1|Multiply %x2|Divide %x3|Sine %x4|Cosine %x5|Tangent %x6|Arcsine %x7|Arccosine %x8|Arctangent %x9|Power %x10|Logarithm %x11|Minimum %x12|Maximum %x13|Round %x14|Less Than %x15|Greater Than %x16", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 0, 0, 0, ""); 
440         uiButSetFunc(bt, node_but_title_cb, node, bt); 
441 }
442
443
444 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
445
446 static void node_browse_text_cb(bContext *C, void *ntree_v, void *node_v)
447 {
448         bNodeTree *ntree= ntree_v;
449         bNode *node= node_v;
450         ID *oldid;
451         
452         if(node->menunr<1) return;
453         
454         if(node->id) {
455                 node->id->us--;
456         }
457         oldid= node->id;
458         node->id= BLI_findlink(&G.main->text, node->menunr-1);
459         id_us_plus(node->id);
460         BLI_strncpy(node->name, node->id->name+2, 21); /* huh? why 21? */
461
462         node->custom1= BSET(node->custom1, NODE_DYNAMIC_NEW);
463         
464         nodeSetActive(ntree, node);
465
466         // allqueue(REDRAWBUTSSHADING, 0);
467         // allqueue(REDRAWNODE, 0);
468
469         node->menunr= 0;
470 }
471
472 static void node_mat_alone_cb(bContext *C, void *node_v, void *unused)
473 {
474         bNode *node= node_v;
475         
476         node->id= (ID *)copy_material((Material *)node->id);
477         
478         //BIF_undo_push("Single user material");
479         // allqueue(REDRAWBUTSSHADING, 0);
480         // allqueue(REDRAWNODE, 0);
481         // allqueue(REDRAWOOPS, 0);
482 }
483
484 static void node_browse_mat_cb(bContext *C, void *ntree_v, void *node_v)
485 {
486         bNodeTree *ntree= ntree_v;
487         bNode *node= node_v;
488         
489         if(node->menunr<1) return;
490         
491         if(node->menunr==32767) {       /* code for Add New */
492                 if(node->id) {
493                         /* make copy, but make sure it doesnt have the node tag nor nodes */
494                         Material *ma= (Material *)node->id;
495                         ma->id.us--;
496                         ma= copy_material(ma);
497                         ma->use_nodes= 0;
498                         if(ma->nodetree) {
499                                 ntreeFreeTree(ma->nodetree);
500                                 MEM_freeN(ma->nodetree);
501                         }
502                         ma->nodetree= NULL;
503                         node->id= (ID *)ma;
504                 }
505                 else node->id= (ID *)add_material("MatNode");
506         }
507         else {
508                 if(node->id) node->id->us--;
509                 node->id= BLI_findlink(&G.main->mat, node->menunr-1);
510                 id_us_plus(node->id);
511         }
512         BLI_strncpy(node->name, node->id->name+2, 21);
513         
514         nodeSetActive(ntree, node);
515
516         // allqueue(REDRAWBUTSSHADING, 0);
517         // allqueue(REDRAWNODE, 0);
518         // XXX BIF_preview_changed(ID_MA);
519
520         node->menunr= 0;
521 }
522
523 static void node_new_mat_cb(bContext *C, void *ntree_v, void *node_v)
524 {
525         bNodeTree *ntree= ntree_v;
526         bNode *node= node_v;
527         
528         node->id= (ID *)add_material("MatNode");
529         BLI_strncpy(node->name, node->id->name+2, 21);
530
531         nodeSetActive(ntree, node);
532
533         // allqueue(REDRAWBUTSSHADING, 0);
534         // allqueue(REDRAWNODE, 0);
535         // XXX BIF_preview_changed(ID_MA);
536
537 }
538
539 static void node_texmap_cb(bContext *C, void *texmap_v, void *unused_v)
540 {
541         init_mapping(texmap_v);
542 }
543
544 static void node_shader_buts_material(uiLayout *layout, PointerRNA *ptr)
545 {
546         uiBlock *block= uiLayoutFreeBlock(layout);
547         bNode *node= ptr->data;
548         bNodeTree *ntree= ptr->id.data;
549         rctf *butr= &node->butr;
550         uiBut *bt;
551         short dx= (short)((butr->xmax-butr->xmin)/3.0f), has_us= (node->id && node->id->us>1);
552         short dy= (short)butr->ymin;
553         char *strp;
554         
555         /* WATCH IT: we use this callback in material buttons, but then only want first row */
556         if(butr->ymax-butr->ymin > 21.0f) dy+= 19;
557         
558         uiBlockBeginAlign(block);
559         /* XXX
560         if(node->id==NULL) uiBlockSetCol(block, TH_REDALERT);
561         else if(has_us) uiBlockSetCol(block, TH_BUT_SETTING1);
562         else uiBlockSetCol(block, TH_BUT_SETTING2);
563         */
564         
565         /* browse button */
566         IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->mat), NULL, NULL);
567         node->menunr= 0;
568         bt= uiDefButS(block, MENU, B_NOP, strp, 
569                           butr->xmin, dy, 19, 19, 
570                           &node->menunr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
571         uiButSetFunc(bt, node_browse_mat_cb, ntree, node);
572         if(strp) MEM_freeN(strp);
573         
574         /* Add New button */
575         if(node->id==NULL) {
576                 bt= uiDefBut(block, BUT, B_NOP, "Add New",
577                                          butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
578                                          NULL, 0.0, 0.0, 0, 0, "Add new Material");
579                 uiButSetFunc(bt, node_new_mat_cb, ntree, node);
580         }
581         else {
582                 /* name button */
583                 short width= (short)(butr->xmax-butr->xmin-19.0f - (has_us?19.0f:0.0f));
584                 bt= uiDefBut(block, TEX, B_NOP, "MA:",
585                                           butr->xmin+19, dy, width, 19, 
586                                           node->id->name+2, 0.0, 19.0, 0, 0, "Material name");
587                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
588                 
589                 /* user amount */
590                 if(has_us) {
591                         char str1[32];
592                         sprintf(str1, "%d", node->id->us);
593                         bt= uiDefBut(block, BUT, B_NOP, str1, 
594                                                   butr->xmax-19, dy, 19, 19, 
595                                                   NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
596                         uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
597                 }
598                 
599                 /* WATCH IT: we use this callback in material buttons, but then only want first row */
600                 if(butr->ymax-butr->ymin > 21.0f) {
601                         /* node options */
602                         uiDefButBitS(block, TOG, SH_NODE_MAT_DIFF, B_NODE_EXEC, "Diff",
603                                                  butr->xmin, butr->ymin, dx, 19, 
604                                                  &node->custom1, 0, 0, 0, 0, "Material Node outputs Diffuse");
605                         uiDefButBitS(block, TOG, SH_NODE_MAT_SPEC, B_NODE_EXEC, "Spec",
606                                                  butr->xmin+dx, butr->ymin, dx, 19, 
607                                                  &node->custom1, 0, 0, 0, 0, "Material Node outputs Specular");
608                         uiDefButBitS(block, TOG, SH_NODE_MAT_NEG, B_NODE_EXEC, "Neg Normal",
609                                                  butr->xmax-dx, butr->ymin, dx, 19,
610                                                  &node->custom1, 0, 0, 0, 0, "Material Node uses inverted Normal");
611                 }
612         }
613         uiBlockEndAlign(block);
614 }
615
616 static void node_shader_buts_mapping(uiLayout *layout, PointerRNA *ptr)
617 {
618         uiBlock *block= uiLayoutFreeBlock(layout);
619         bNode *node= ptr->data;
620         rctf *butr= &node->butr;
621         TexMapping *texmap= node->storage;
622         short dx= (short)((butr->xmax-butr->xmin)/7.0f);
623         short dy= (short)(butr->ymax-19);
624         
625         uiBlockSetFunc(block, node_texmap_cb, texmap, NULL);    /* all buttons get this */
626         
627         uiBlockBeginAlign(block);
628         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+dx, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
629         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->loc+1, -1000.0f, 1000.0f, 10, 2, "");
630         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->loc+2, -1000.0f, 1000.0f, 10, 2, "");
631         dy-= 19;
632         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+dx, dy, 2*dx, 19, texmap->rot, -1000.0f, 1000.0f, 1000, 1, "");
633         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->rot+1, -1000.0f, 1000.0f, 1000, 1, "");
634         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->rot+2, -1000.0f, 1000.0f, 1000, 1, "");
635         dy-= 19;
636         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+dx, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 2, "");
637         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->size+1, -1000.0f, 1000.0f, 10, 2, "");
638         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->size+2, -1000.0f, 1000.0f, 10, 2, "");
639         dy-= 25;
640         uiBlockBeginAlign(block);
641         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+dx, dy, 2*dx, 19, texmap->min, -10.0f, 10.0f, 100, 2, "");
642         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->min+1, -10.0f, 10.0f, 100, 2, "");
643         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->min+2, -10.0f, 10.0f, 100, 2, "");
644         dy-= 19;
645         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+dx, dy, 2*dx, 19, texmap->max, -10.0f, 10.0f, 10, 2, "");
646         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->max+1, -10.0f, 10.0f, 10, 2, "");
647         uiDefButF(block, NUM, B_NODE_EXEC, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->max+2, -10.0f, 10.0f, 10, 2, "");
648         uiBlockEndAlign(block);
649         
650         /* labels/options */
651         
652         dy= (short)(butr->ymax-19);
653         uiDefBut(block, LABEL, B_NOP, "Loc", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
654         dy-= 19;
655         uiDefBut(block, LABEL, B_NOP, "Rot", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
656         dy-= 19;
657         uiDefBut(block, LABEL, B_NOP, "Size", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
658         dy-= 25;
659         uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC, "Min", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
660         dy-= 19;
661         uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC, "Max", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
662 }
663
664 static void node_shader_buts_vect_math(uiLayout *layout, PointerRNA *ptr)
665
666         uiBlock *block= uiLayoutFreeBlock(layout);
667         bNode *node= ptr->data;
668         rctf *butr= &node->butr;
669         uiBut *bt; 
670
671         bt=uiDefButS(block, MENU, B_NODE_EXEC,  "Add %x0|Subtract %x1|Average %x2|Dot Product %x3 |Cross Product %x4|Normalize %x5", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, &node->custom1, 0, 0, 0, 0, ""); 
672         uiButSetFunc(bt, node_but_title_cb, node, bt); 
673 }
674
675 static void node_shader_buts_geometry(uiLayout *layout, PointerRNA *ptr)
676 {
677         uiBlock *block= uiLayoutFreeBlock(layout);
678         bNode *node= ptr->data;
679         rctf *butr= &node->butr;
680         uiBut *but;
681         NodeGeometry *ngeo= (NodeGeometry*)node->storage;
682
683         // XXX if(!verify_valid_uv_name(ngeo->uvname))
684         // XXX  uiBlockSetCol(block, TH_REDALERT);
685         but= uiDefBut(block, TEX, B_NODE_EXEC, "UV:", butr->xmin, butr->ymin+20, butr->xmax-butr->xmin, 20, ngeo->uvname, 0, 31, 0, 0, "Set name of UV layer to use, default is active UV layer");
686         // XXX uiButSetCompleteFunc(but, autocomplete_uv, NULL);
687
688         if(!verify_valid_vcol_name(ngeo->colname));
689 //                      uiBlockSetCol(block, TH_REDALERT);
690         but= uiDefBut(block, TEX, B_NODE_EXEC, "Col:", butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, ngeo->colname, 0, 31, 0, 0, "Set name of vertex color layer to use, default is active vertex color layer");
691         uiButSetCompleteFunc(but, autocomplete_vcol, NULL);
692 }
693
694 static void node_shader_buts_dynamic(uiLayout *layout, PointerRNA *ptr)
695
696         uiBlock *block= uiLayoutFreeBlock(layout);
697         bNode *node= ptr->data;
698         bNodeTree *ntree= ptr->id.data;
699         rctf *butr= &node->butr;
700         uiBut *bt;
701         // XXX SpaceNode *snode= curarea->spacedata.first;
702         short dy= (short)butr->ymin;
703         int xoff=0;
704
705         /* B_NODE_EXEC is handled in butspace.c do_node_buts */
706         if(!node->id) {
707                         char *strp;
708                         IDnames_to_pupstring(&strp, NULL, "", &(G.main->text), NULL, NULL);
709                         node->menunr= 0;
710                         bt= uiDefButS(block, MENU, B_NODE_EXEC/*+node->nr*/, strp, 
711                                                         butr->xmin, dy, 19, 19, 
712                                                         &node->menunr, 0, 0, 0, 0, "Browses existing choices");
713                         uiButSetFunc(bt, node_browse_text_cb, ntree, node);
714                         xoff=19;
715                         if(strp) MEM_freeN(strp);       
716         }
717         else {
718                 bt = uiDefBut(block, BUT, B_NOP, "Update",
719                                 butr->xmin+xoff, butr->ymin+20, 50, 19,
720                                 &node->menunr, 0.0, 19.0, 0, 0, "Refresh this node (and all others that use the same script)");
721                 uiButSetFunc(bt, node_dynamic_update_cb, ntree, node);
722
723                 if (BTST(node->custom1, NODE_DYNAMIC_ERROR)) {
724                         // UI_ThemeColor(TH_REDALERT);
725                         // XXX ui_rasterpos_safe(butr->xmin + xoff, butr->ymin + 5, snode->aspect);
726                         // XXX snode_drawstring(snode, "Error! Check console...", butr->xmax - butr->xmin);
727                         ;
728                 }
729         }
730 }
731
732 /* only once called */
733 static void node_shader_set_butfunc(bNodeType *ntype)
734 {
735         switch(ntype->type) {
736                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
737
738                 case SH_NODE_MATERIAL:
739                 case SH_NODE_MATERIAL_EXT:
740                         ntype->uifunc= node_shader_buts_material;
741                         break;
742                 case SH_NODE_TEXTURE:
743                         ntype->uifunc= node_buts_texture;
744                         break;
745                 case SH_NODE_NORMAL:
746                         ntype->uifunc= node_buts_normal;
747                         break;
748                 case SH_NODE_CURVE_VEC:
749                         ntype->uifunc= node_buts_curvevec;
750                         break;
751                 case SH_NODE_CURVE_RGB:
752                         ntype->uifunc= node_buts_curvecol;
753                         break;
754                 case SH_NODE_MAPPING:
755                         ntype->uifunc= node_shader_buts_mapping;
756                         break;
757                 case SH_NODE_VALUE:
758                         ntype->uifunc= node_buts_value;
759                         break;
760                 case SH_NODE_RGB:
761                         ntype->uifunc= node_buts_rgb;
762                         break;
763                 case SH_NODE_MIX_RGB:
764                         ntype->uifunc= node_buts_mix_rgb;
765                         break;
766                 case SH_NODE_VALTORGB:
767                         ntype->uifunc= node_buts_valtorgb;
768                         break;
769                 case SH_NODE_MATH: 
770                         ntype->uifunc= node_buts_math;
771                         break; 
772                 case SH_NODE_VECT_MATH: 
773                         ntype->uifunc= node_shader_buts_vect_math;
774                         break; 
775                 case SH_NODE_GEOMETRY:
776                         ntype->uifunc= node_shader_buts_geometry;
777                         break;
778                 case NODE_DYNAMIC:
779                         ntype->uifunc= node_shader_buts_dynamic;
780                         break;
781                 default:
782                         ntype->uifunc= NULL;
783         }
784 }
785
786 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
787
788
789
790 static void node_browse_image_cb(bContext *C, void *ntree_v, void *node_v)
791 {
792         bNodeTree *ntree= ntree_v;
793         bNode *node= node_v;
794         
795         nodeSetActive(ntree, node);
796         
797         if(node->menunr<1) return;
798         if(node->menunr==32767) {       /* code for Load New */
799                 /// addqueue(curarea->win, UI_BUT_EVENT, B_NODE_LOADIMAGE); XXX
800         }
801         else {
802                 if(node->id) node->id->us--;
803                 node->id= BLI_findlink(&G.main->image, node->menunr-1);
804                 id_us_plus(node->id);
805
806                 BLI_strncpy(node->name, node->id->name+2, 21);
807
808                 NodeTagChanged(ntree, node); 
809                 BKE_image_signal((Image *)node->id, node->storage, IMA_SIGNAL_USER_NEW_IMAGE);
810                 // addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC); XXX
811         }
812         node->menunr= 0;
813 }
814
815 static void node_active_cb(bContext *C, void *ntree_v, void *node_v)
816 {
817         nodeSetActive(ntree_v, node_v);
818 }
819 static void node_image_type_cb(bContext *C, void *node_v, void *unused)
820 {
821         
822         // allqueue(REDRAWNODE, 1);
823 }
824
825 static char *node_image_type_pup(void)
826 {
827         char *str= MEM_mallocN(256, "image type pup");
828         int a;
829         
830         str[0]= 0;
831         
832         a= sprintf(str, "Image Type %%t|");
833         a+= sprintf(str+a, "  Image %%x%d %%i%d|", IMA_SRC_FILE, ICON_IMAGE_DATA);
834         a+= sprintf(str+a, "  Movie %%x%d %%i%d|", IMA_SRC_MOVIE, ICON_SEQUENCE);
835         a+= sprintf(str+a, "  Sequence %%x%d %%i%d|", IMA_SRC_SEQUENCE, ICON_IMAGE_COL);
836         a+= sprintf(str+a, "  Generated %%x%d %%i%d", IMA_SRC_GENERATED, ICON_BLANK1);
837         
838         return str;
839 }
840
841 /* copy from buttons_shading.c */
842 static char *layer_menu(RenderResult *rr)
843 {
844         RenderLayer *rl;
845         int len= 40 + 40*BLI_countlist(&rr->layers);
846         short a, nr;
847         char *str= MEM_callocN(len, "menu layers");
848         
849         strcpy(str, "Layer %t");
850         a= strlen(str);
851         for(nr=0, rl= rr->layers.first; rl; rl= rl->next, nr++) {
852                 a+= sprintf(str+a, "|%s %%x%d", rl->name, nr);
853         }
854         
855         return str;
856 }
857
858 static void image_layer_cb(bContext *C, void *ima_v, void *iuser_v)
859 {
860         Scene *scene= CTX_data_scene(C);
861         
862         ntreeCompositForceHidden(scene->nodetree, scene);
863         BKE_image_multilayer_index(ima_v, iuser_v);
864         // allqueue(REDRAWNODE, 0);
865 }
866
867 static void node_composit_buts_image(uiLayout *layout, PointerRNA *ptr)
868 {
869         uiBlock *block= uiLayoutFreeBlock(layout);
870         bNode *node= ptr->data;
871         bNodeTree *ntree= ptr->id.data;
872         rctf *butr= &node->butr;
873         ImageUser *iuser= node->storage;
874         uiBut *bt;
875         short dy= (short)butr->ymax-19;
876         char *strp;
877         
878         uiBlockBeginAlign(block);
879         
880         /* browse button */
881         IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
882         node->menunr= 0;
883         bt= uiDefButS(block, MENU, B_NOP, strp, 
884                                   butr->xmin, dy, 19, 19, 
885                                   &node->menunr, 0, 0, 0, 0, "Browses existing choices");
886         uiButSetFunc(bt, node_browse_image_cb, ntree, node);
887         if(strp) MEM_freeN(strp);
888         
889         /* Add New button */
890         if(node->id==NULL) {
891                 bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
892                                          butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
893                                          NULL, 0.0, 0.0, 0, 0, "Add new Image");
894                 uiButSetFunc(bt, node_active_cb, ntree, node);
895         }
896         else {
897                 /* name button + type */
898                 Image *ima= (Image *)node->id;
899                 short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
900                 short width= xmax - xmin - 45;
901                 short icon= ICON_IMAGE_DATA;
902                 
903                 if(ima->source==IMA_SRC_MOVIE) icon= ICON_SEQUENCE;
904                 else if(ima->source==IMA_SRC_SEQUENCE) icon= ICON_IMAGE_COL;
905                 else if(ima->source==IMA_SRC_GENERATED) icon= ICON_BLANK1;
906                 
907                 bt= uiDefBut(block, TEX, B_NOP, "IM:",
908                                          xmin+19, dy, width, 19, 
909                                          node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
910                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
911                 
912                 /* buffer type option */
913                 strp= node_image_type_pup();
914                 bt= uiDefIconTextButS(block, MENU, B_NOP, icon, strp,
915                                          xmax-26, dy, 26, 19, 
916                                          &ima->source, 0.0, 19.0, 0, 0, "Image type");
917                 uiButSetFunc(bt, node_image_type_cb, node, ima);
918                 MEM_freeN(strp);
919                 
920                 if( ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE) ) {
921                         width= (xmax-xmin)/2;
922                         
923                         dy-= 19;
924                         uiDefButI(block, NUM, B_NODE_EXEC, "Frs:",
925                                           xmin, dy, width, 19, 
926                                           &iuser->frames, 1.0, MAXFRAMEF, 0, 0, "Amount of images used in animation");
927                         uiDefButI(block, NUM, B_NODE_EXEC, "SFra:",
928                                           xmin+width, dy, width, 19, 
929                                           &iuser->sfra, 1.0, MAXFRAMEF, 0, 0, "Start frame of animation");
930                         dy-= 19;
931                         uiDefButI(block, NUM, B_NODE_EXEC, "Offs:",
932                                           xmin, dy, width, 19, 
933                                           &iuser->offset, -MAXFRAMEF, MAXFRAMEF, 0, 0, "Offsets the number of the frame to use in the animation");
934                         uiDefButS(block, TOG, B_NODE_EXEC, "Cycl",
935                                           xmin+width, dy, width-20, 19, 
936                                           &iuser->cycl, 0.0, 0.0, 0, 0, "Make animation go cyclic");
937                         uiDefIconButBitS(block, TOG, IMA_ANIM_ALWAYS, B_NODE_EXEC, ICON_AUTO,
938                                           xmax-20, dy, 20, 19, 
939                                           &iuser->flag, 0.0, 0.0, 0, 0, "Always refresh Image on frame changes");
940                 }
941                 if( ima->type==IMA_TYPE_MULTILAYER && ima->rr) {
942                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
943                         if(rl) {
944                                 width= (xmax-xmin);
945                                 dy-= 19;
946                                 strp= layer_menu(ima->rr);
947                                 bt= uiDefButS(block, MENU, B_NODE_EXEC, strp,
948                                                   xmin, dy, width, 19, 
949                                                   &iuser->layer, 0.0, 10000.0, 0, 0, "Layer");
950                                 uiButSetFunc(bt, image_layer_cb, ima->rr, node->storage);
951                                 MEM_freeN(strp);
952                         }
953                 }
954         }
955                 
956         if(node->id) {
957                 /* for each draw we test for anim refresh event */
958                 if(iuser->flag & IMA_ANIM_REFRESHED) {
959                         iuser->flag &= ~IMA_ANIM_REFRESHED;
960                         // addqueue(curarea->win, UI_BUT_EVENT, B_NODE_EXEC); XXX
961                 }
962         }
963 }
964
965 /* if we use render layers from other scene, we make a nice title */
966 static void set_render_layers_title(bContext *C, void *node_v, void *unused)
967 {
968         bNode *node= node_v;
969         Scene *sce;
970         SceneRenderLayer *srl;
971         char str[64];
972         
973         if(node->id) {
974                 BLI_strncpy(str, node->id->name+2, 21);
975                 strcat(str, "|");
976                 sce= (Scene *)node->id;
977         }
978         else {
979                 str[0]= 0;
980                 sce= CTX_data_scene(C);
981         }
982         srl= BLI_findlink(&sce->r.layers, node->custom1);
983         if(srl==NULL) {
984                 node->custom1= 0;
985                 srl= sce->r.layers.first;
986         }
987         
988         strcat(str, srl->name);
989         BLI_strncpy(node->name, str, 32);
990 }
991
992 static char *scene_layer_menu(Scene *sce)
993 {
994         SceneRenderLayer *srl;
995         int len= 40 + 40*BLI_countlist(&sce->r.layers);
996         short a, nr;
997         char *str= MEM_callocN(len, "menu layers");
998         
999         strcpy(str, "Active Layer %t");
1000         a= strlen(str);
1001         for(nr=0, srl= sce->r.layers.first; srl; srl= srl->next, nr++) {
1002                 a+= sprintf(str+a, "|%s %%x%d", srl->name, nr);
1003         }
1004         
1005         return str;
1006 }
1007
1008 static void node_browse_scene_cb(bContext *C, void *ntree_v, void *node_v)
1009 {
1010         bNodeTree *ntree= ntree_v;
1011         bNode *node= node_v;
1012         Scene *sce;
1013         
1014         if(node->menunr<1) return;
1015         
1016         if(node->id) {
1017                 node->id->us--;
1018                 node->id= NULL;
1019         }
1020         sce= BLI_findlink(&G.main->scene, node->menunr-1);
1021         node->id= &sce->id;
1022         id_us_plus(node->id);
1023         
1024         set_render_layers_title(C, node, NULL);
1025         nodeSetActive(ntree, node);
1026
1027         // allqueue(REDRAWBUTSSHADING, 0);
1028         // allqueue(REDRAWNODE, 0);
1029         NodeTagChanged(ntree, node); 
1030
1031         node->menunr= 0;
1032 }
1033
1034
1035 static void node_composit_buts_renderlayers(uiLayout *layout, PointerRNA *ptr)
1036 {
1037         uiBlock *block= uiLayoutFreeBlock(layout);
1038         bNode *node= ptr->data;
1039         bNodeTree *ntree= ptr->id.data;
1040         rctf *butr= &node->butr;
1041
1042         if(node->id) {
1043                 Scene *scene= (Scene *)node->id;
1044                 uiBut *bt;
1045                 char *strp;
1046                 
1047                 /* browse button scene */
1048                 uiBlockBeginAlign(block);
1049                 IDnames_to_pupstring(&strp, NULL, "", &(G.main->scene), NULL, NULL);
1050                 node->menunr= 0;
1051                 bt= uiDefButS(block, MENU, B_NOP, strp, 
1052                                           butr->xmin, butr->ymin, 20, 19, 
1053                                           &node->menunr, 0, 0, 0, 0, "Browse Scene to use RenderLayer from");
1054                 uiButSetFunc(bt, node_browse_scene_cb, ntree, node);
1055                 if(strp) MEM_freeN(strp);
1056                 
1057                 /* browse button layer */
1058                 strp= scene_layer_menu(node->id?(Scene *)node->id:scene);
1059                 if(node->id)
1060                         bt= uiDefIconTextButS(block, MENU, B_NODE_EXEC, ICON_RENDERLAYERS, strp, 
1061                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1062                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1063                 else
1064                         bt= uiDefButS(block, MENU, B_NODE_EXEC, strp, 
1065                                   butr->xmin+20, butr->ymin, (butr->xmax-butr->xmin)-40, 19, 
1066                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
1067                 uiButSetFunc(bt, set_render_layers_title, node, NULL);
1068                 MEM_freeN(strp);
1069                 
1070                 /* re-render */
1071                 /* uses custom2, not the best implementation of the world... but we need it to work now :) */
1072                 bt= uiDefIconButS(block, TOG, B_NODE_EXEC, ICON_SCENE, 
1073                                   butr->xmax-20, butr->ymin, 20, 19, 
1074                                   &node->custom2, 0, 0, 0, 0, "Re-render this Layer");
1075                 
1076         }
1077 }
1078
1079 static void node_blur_relative_cb(bContext *C, void *node, void *poin2)
1080 {
1081         bNode *nodev= node;
1082         NodeBlurData *nbd= nodev->storage;
1083         if(nbd->image_in_width != 0){
1084                 if(nbd->relative){ /* convert absolute values to relative */
1085                         nbd->percentx= (float)(nbd->sizex)/nbd->image_in_width;
1086                         nbd->percenty= (float)(nbd->sizey)/nbd->image_in_height;
1087                 }else{ /* convert relative values to absolute */
1088                         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1089                         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1090                 }
1091         }
1092         // allqueue(REDRAWNODE, 0);
1093 }
1094 static void node_blur_update_sizex_cb(bContext *C, void *node, void *poin2)
1095 {
1096         bNode *nodev= node;
1097         NodeBlurData *nbd= nodev->storage;
1098
1099         nbd->sizex= (int)(nbd->percentx*nbd->image_in_width);
1100 }
1101 static void node_blur_update_sizey_cb(bContext *C, void *node, void *poin2)
1102 {
1103         bNode *nodev= node;
1104         NodeBlurData *nbd= nodev->storage;
1105
1106         nbd->sizey= (int)(nbd->percenty*nbd->image_in_height);
1107 }
1108 static void node_composit_buts_blur(uiLayout *layout, PointerRNA *ptr)
1109 {
1110         uiLayout *row, *col;
1111         
1112         col= uiLayoutColumn(layout, 0);
1113         
1114         uiItemR(col, "", 0, ptr, "filter_type", 0);
1115         /* Only for "Fast Gaussian" */
1116         if (RNA_enum_get(ptr, "filter_type")!= 7) {
1117                 uiItemR(col, NULL, 0, ptr, "bokeh", 0);
1118                 uiItemR(col, NULL, 0, ptr, "gamma", 0);
1119         }
1120         
1121         uiItemR(col, NULL, 0, ptr, "relative", 0);
1122         row= uiLayoutRow(col, 1);
1123         if (RNA_boolean_get(ptr, "relative")== 1) {
1124                 uiItemR(row, "X", 0, ptr, "factor_x", 0);
1125                 uiItemR(row, "Y", 0, ptr, "factor_y", 0);
1126         }
1127         
1128         else {
1129                 uiItemR(row, "X", 0, ptr, "sizex", 0);
1130                 uiItemR(row, "Y", 0, ptr, "sizey", 0);
1131         }
1132 }
1133
1134 static void node_composit_buts_dblur(uiLayout *layout, PointerRNA *ptr)
1135 {
1136         uiLayout *row, *col;
1137         
1138         uiItemR(layout, NULL, 0, ptr, "iterations", 0);
1139         uiItemR(layout, NULL, 0, ptr, "wrap", 0);
1140         
1141         col= uiLayoutColumn(layout, 1);
1142         uiItemL(col, "Center:", 0);
1143         
1144         row= uiLayoutRow(col, 1);
1145         uiItemR(row, "X:", 0, ptr, "center_x", 0);
1146         uiItemR(row, "Y", 0, ptr, "center_y", 0);
1147         
1148         uiItemS(layout);
1149         
1150         col= uiLayoutColumn(layout, 1);
1151         uiItemR(col, NULL, 0, ptr, "distance", 0);
1152         uiItemR(col, NULL, 0, ptr, "angle", 0);
1153         
1154         uiItemS(layout);
1155         
1156         uiItemR(layout, NULL, 0, ptr, "spin", 0);
1157         uiItemR(layout, NULL, 0, ptr, "zoom", 0);
1158 }
1159
1160 static void node_composit_buts_bilateralblur(uiLayout *layout, PointerRNA *ptr)
1161 {       
1162         uiLayout *col;
1163         
1164         col= uiLayoutColumn(layout, 1);
1165         uiItemR(col, NULL, 0, ptr, "iterations", 0);
1166         uiItemR(col, NULL, 0, ptr, "sigma_color", 0);
1167         uiItemR(col, NULL, 0, ptr, "sigma_space", 0);
1168 }
1169
1170 /* qdn: defocus node */
1171 static void node_composit_buts_defocus(uiLayout *layout, PointerRNA *ptr)
1172 {
1173         uiLayout *sub, *col;
1174         
1175         col= uiLayoutColumn(layout, 1);
1176         uiItemL(col, "Bokeh Type:", 0);
1177         uiItemR(col, "", 0, ptr, "bokeh", 0);
1178         uiItemR(col, NULL, 0, ptr, "angle", 0);
1179
1180         uiItemR(layout, NULL, 0, ptr, "gamma_correction", 0);
1181
1182         col = uiLayoutColumn(layout, 0);
1183         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer")==0);
1184         uiItemR(col, NULL, 0, ptr, "f_stop", 0);
1185
1186         uiItemR(layout, NULL, 0, ptr, "max_blur", 0);
1187         uiItemR(layout, NULL, 0, ptr, "threshold", 0);
1188         
1189         // Preview
1190         col = uiLayoutColumn(layout, 0);
1191         uiItemR(col, NULL, 0, ptr, "preview", 0);
1192         sub = uiLayoutColumn(col, 0);
1193         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "preview"));
1194         uiItemR(sub, NULL, 0, ptr, "samples", 0);
1195         
1196         // Z-Buffer
1197         col = uiLayoutColumn(layout, 0);
1198         uiItemR(col, NULL, 0, ptr, "use_zbuffer", 0);
1199         sub = uiLayoutColumn(col, 0);
1200         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer"));
1201         uiItemR(sub, NULL, 0, ptr, "z_scale", 0);
1202 }
1203
1204 /* qdn: glare node */
1205 static void node_composit_buts_glare(uiLayout *layout, PointerRNA *ptr)
1206 {       
1207         uiItemR(layout, "", 0, ptr, "glare_type", 0);
1208         uiItemR(layout, "", 0, ptr, "quality", 0);
1209
1210         if (RNA_enum_get(ptr, "glare_type")!= 1) {
1211                 uiItemR(layout, NULL, 0, ptr, "iterations", 0);
1212         
1213                 if (RNA_enum_get(ptr, "glare_type")!= 0) 
1214                         uiItemR(layout, NULL, 0, ptr, "color_modulation", 0);
1215         }
1216         
1217         uiItemR(layout, NULL, 0, ptr, "mix", 0);                
1218         uiItemR(layout, NULL, 0, ptr, "threshold", 0);
1219
1220         if (RNA_enum_get(ptr, "glare_type")== 2) {
1221                 uiItemR(layout, NULL, 0, ptr, "streaks", 0);            
1222                 uiItemR(layout, NULL, 0, ptr, "angle_offset", 0);
1223         }
1224         if (RNA_enum_get(ptr, "glare_type")== 0 || RNA_enum_get(ptr, "glare_type")== 2) {
1225                 uiItemR(layout, NULL, 0, ptr, "fade", 0);
1226                 
1227                 if (RNA_enum_get(ptr, "glare_type")== 0) 
1228                         uiItemR(layout, NULL, 0, ptr, "rotate_45", 0);
1229         }
1230         if (RNA_enum_get(ptr, "glare_type")== 1) {
1231                 uiItemR(layout, NULL, 0, ptr, "size", 0);
1232         }
1233 }
1234
1235 /* qdn: tonemap node */
1236 static void node_composit_buts_tonemap(uiLayout *layout, PointerRNA *ptr)
1237 {       
1238         uiLayout *col;
1239
1240         col = uiLayoutColumn(layout, 1);
1241         uiItemR(col, "", 0, ptr, "tonemap_type", 0);
1242         if (RNA_enum_get(ptr, "tonemap_type")== 0) {
1243                 uiItemR(col, NULL, 0, ptr, "key", 0);
1244                 uiItemR(col, NULL, 0, ptr, "offset", 0);
1245                 uiItemR(col, NULL, 0, ptr, "gamma", 0);
1246         }
1247         else {
1248                 uiItemR(col, NULL, 0, ptr, "intensity", 0);
1249                 uiItemR(col, NULL, 0, ptr, "contrast", 0);
1250                 uiItemR(col, NULL, 0, ptr, "adaptation", 0);
1251                 uiItemR(col, NULL, 0, ptr, "correction", 0);
1252         }
1253 }
1254
1255 /* qdn: lens distortion node */
1256 static void node_composit_buts_lensdist(uiLayout *layout, PointerRNA *ptr)
1257 {
1258         uiLayout *col;
1259
1260         col= uiLayoutColumn(layout, 0);
1261         uiItemR(col, NULL, 0, ptr, "projector", 0);
1262
1263         col = uiLayoutColumn(col, 0);
1264         uiLayoutSetActive(col, RNA_boolean_get(ptr, "projector")==0);
1265         uiItemR(col, NULL, 0, ptr, "jitter", 0);
1266         uiItemR(col, NULL, 0, ptr, "fit", 0);
1267 }
1268
1269 static void node_composit_buts_vecblur(uiLayout *layout, PointerRNA *ptr)
1270 {
1271         uiLayout *col;
1272         
1273         col= uiLayoutColumn(layout, 1);
1274         uiItemR(col, NULL, 0, ptr, "samples", 0);
1275         uiItemR(col, "Blur", 0, ptr, "factor", 0);
1276         
1277         col= uiLayoutColumn(layout, 1);
1278         uiItemL(col, "Speed:", 0);
1279         uiItemR(col, "Min", 0, ptr, "min_speed", 0);
1280         uiItemR(col, "Max", 0, ptr, "max_speed", 0);
1281
1282         uiItemR(layout, NULL, 0, ptr, "curved", 0);
1283 }
1284
1285 static void node_composit_buts_filter(uiLayout *layout, PointerRNA *ptr)
1286 {
1287         uiItemR(layout, "", 0, ptr, "filter_type", 0);
1288 }
1289
1290 static void node_composit_buts_flip(uiLayout *layout, PointerRNA *ptr)
1291 {
1292         uiItemR(layout, "", 0, ptr, "axis", 0);
1293 }
1294
1295 static void node_composit_buts_crop(uiLayout *layout, PointerRNA *ptr)
1296 {
1297         uiLayout *col;
1298         
1299         uiItemR(layout, NULL, 0, ptr, "crop_size", 0);
1300         
1301         col= uiLayoutColumn(layout, 1);
1302         uiItemR(col, "Left", 0, ptr, "x1", 0);
1303         uiItemR(col, "Right", 0, ptr, "x2", 0);
1304         uiItemR(col, "Up", 0, ptr, "y1", 0);
1305         uiItemR(col, "Down", 0, ptr, "y2", 0);
1306 }
1307
1308 static void node_composit_buts_splitviewer(uiLayout *layout, PointerRNA *ptr)
1309 {
1310         uiLayout *row, *col;
1311         
1312         col= uiLayoutColumn(layout, 1);
1313         row= uiLayoutRow(col, 0);
1314         uiItemR(row, NULL, 0, ptr, "axis", UI_ITEM_R_EXPAND);
1315         uiItemR(col, NULL, 0, ptr, "factor", 0);
1316 }
1317
1318 static void node_composit_buts_map_value(uiLayout *layout, PointerRNA *ptr)
1319 {
1320         uiLayout *sub, *col;
1321         
1322         col =uiLayoutColumn(layout, 1);
1323         uiItemR(col, NULL, 0, ptr, "offset", 0);
1324         uiItemR(col, NULL, 0, ptr, "size", 0);
1325         
1326         col =uiLayoutColumn(layout, 1);
1327         uiItemR(col, NULL, 0, ptr, "use_min", 0);
1328         sub =uiLayoutColumn(col, 0);
1329         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1330         uiItemR(sub, "", 0, ptr, "min", 0);
1331         
1332         col =uiLayoutColumn(layout, 1);
1333         uiItemR(col, NULL, 0, ptr, "use_max", 0);
1334         sub =uiLayoutColumn(col, 0);
1335         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1336         uiItemR(sub, "", 0, ptr, "max", 0);
1337 }
1338
1339 static void node_composit_buts_alphaover(uiLayout *layout, PointerRNA *ptr)
1340 {       
1341         uiLayout *col;
1342         
1343         col =uiLayoutColumn(layout, 1);
1344         /* alpha type */
1345         uiItemR(col, NULL, 0, ptr, "convert_premul", 0);
1346         /* mix factor */
1347         uiItemR(col, NULL, 0, ptr, "premul", 0);
1348 }
1349
1350 static void node_composit_buts_hue_sat(uiLayout *layout, PointerRNA *ptr)
1351 {
1352         uiLayout *col;
1353         
1354         col =uiLayoutColumn(layout, 1);
1355         uiItemR(col, NULL, 0, ptr, "hue", UI_ITEM_R_SLIDER);
1356         uiItemR(col, NULL, 0, ptr, "sat", UI_ITEM_R_SLIDER);
1357         uiItemR(col, NULL, 0, ptr, "val", UI_ITEM_R_SLIDER);
1358 }
1359
1360 static void node_composit_buts_dilateerode(uiLayout *layout, PointerRNA *ptr)
1361 {
1362         uiItemR(layout, NULL, 0, ptr, "distance", 0);
1363 }
1364
1365 static void node_composit_buts_diff_matte(uiLayout *layout, PointerRNA *ptr)
1366 {
1367         uiLayout *col;
1368         
1369         col =uiLayoutColumn(layout, 1);
1370         uiItemR(col, NULL, 0, ptr, "tolerance", UI_ITEM_R_SLIDER);
1371         uiItemR(col, NULL, 0, ptr, "falloff", UI_ITEM_R_SLIDER);
1372 }
1373
1374 static void node_composit_buts_distance_matte(uiLayout *layout, PointerRNA *ptr)
1375 {
1376         uiLayout *col;
1377         
1378         col =uiLayoutColumn(layout, 1);
1379         uiItemR(col, NULL, 0, ptr, "tolerance", UI_ITEM_R_SLIDER);
1380         uiItemR(col, NULL, 0, ptr, "falloff", UI_ITEM_R_SLIDER);
1381 }
1382
1383 static void node_composit_buts_color_spill(uiLayout *layout, PointerRNA *ptr)
1384 {
1385         uiLayout *row, *col;
1386         
1387         col =uiLayoutColumn(layout, 1);
1388         uiItemR(col, NULL, 0, ptr, "factor", 0);
1389         row= uiLayoutRow(col, 0);
1390         uiItemR(row, NULL, 0, ptr, "channel", UI_ITEM_R_EXPAND);
1391 }
1392
1393 static void node_composit_buts_chroma_matte(uiLayout *layout, PointerRNA *ptr)
1394 {
1395         uiBlock *block= uiLayoutFreeBlock(layout);
1396         bNode *node= ptr->data;
1397         rctf *butr= &node->butr;
1398         short dx=(butr->xmax-butr->xmin)/2;
1399         NodeChroma *c= node->storage;
1400
1401         uiBlockBeginAlign(block);
1402
1403         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Acceptance ",
1404                 butr->xmin, butr->ymin+60, butr->xmax-butr->xmin, 20,
1405                 &c->t1, 1.0f, 80.0f, 100, 0, "Tolerance for colors to be considered a keying color");
1406         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Cutoff ",
1407                 butr->xmin, butr->ymin+40, butr->xmax-butr->xmin, 20,
1408                 &c->t2, 0.0f, 30.0f, 100, 0, "Colors below this will be considered as exact matches for keying color");
1409
1410         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Lift ",
1411                 butr->xmin, butr->ymin+20, dx, 20,
1412                 &c->fsize, 0.0f, 1.0f, 100, 0, "Alpha Lift");
1413         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Gain ",
1414                 butr->xmin+dx, butr->ymin+20, dx, 20,
1415                 &c->fstrength, 0.0f, 1.0f, 100, 0, "Alpha Gain");
1416
1417         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Shadow Adjust ",
1418                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1419                 &c->t3, 0.0f, 1.0f, 100, 0, "Adjusts the brightness of any shadows captured");
1420         uiBlockEndAlign(block);
1421
1422         if(c->t2 > c->t1)
1423                 c->t2=c->t1;
1424 }
1425
1426 static void node_composit_buts_color_matte(uiLayout *layout, PointerRNA *ptr)
1427 {
1428         uiLayout *col;
1429         
1430         col= uiLayoutColumn(layout, 1);
1431         uiItemR(col, NULL, 0, ptr, "h", UI_ITEM_R_SLIDER);
1432         uiItemR(col, NULL, 0, ptr, "s", UI_ITEM_R_SLIDER);
1433         uiItemR(col, NULL, 0, ptr, "v", UI_ITEM_R_SLIDER);
1434 }
1435
1436 static void node_composit_buts_channel_matte(uiLayout *layout, PointerRNA *ptr)
1437 {
1438         uiBlock *block= uiLayoutFreeBlock(layout);
1439         bNode *node= ptr->data;
1440         rctf *butr= &node->butr;
1441         short sx= (butr->xmax-butr->xmin)/4;
1442         short cx= (butr->xmax-butr->xmin)/3;
1443         NodeChroma *c=node->storage;
1444         char *c1, *c2, *c3;
1445
1446         /*color space selectors*/
1447         uiBlockBeginAlign(block);
1448         uiDefButS(block, ROW,B_NODE_EXEC,"RGB",
1449                 butr->xmin,butr->ymin+60,sx,20,&node->custom1,1,1, 0, 0, "RGB Color Space");
1450         uiDefButS(block, ROW,B_NODE_EXEC,"HSV",
1451                 butr->xmin+sx,butr->ymin+60,sx,20,&node->custom1,1,2, 0, 0, "HSV Color Space");
1452         uiDefButS(block, ROW,B_NODE_EXEC,"YUV",
1453                 butr->xmin+2*sx,butr->ymin+60,sx,20,&node->custom1,1,3, 0, 0, "YUV Color Space");
1454         uiDefButS(block, ROW,B_NODE_EXEC,"YCC",
1455                 butr->xmin+3*sx,butr->ymin+60,sx,20,&node->custom1,1,4, 0, 0, "YCbCr Color Space");
1456
1457         if (node->custom1==1) {
1458                 c1="R"; c2="G"; c3="B";
1459         }
1460         else if(node->custom1==2){
1461                 c1="H"; c2="S"; c3="V";
1462         }
1463         else if(node->custom1==3){
1464                 c1="Y"; c2="U"; c3="V";
1465         }
1466         else { // if(node->custom1==4){
1467                 c1="Y"; c2="Cb"; c3="Cr";
1468         }
1469
1470         /*channel selector */
1471         uiDefButS(block, ROW, B_NODE_EXEC, c1,
1472                 butr->xmin,butr->ymin+40,cx,20,&node->custom2,1, 1, 0, 0, "Channel 1");
1473         uiDefButS(block, ROW, B_NODE_EXEC, c2,
1474                 butr->xmin+cx,butr->ymin+40,cx,20,&node->custom2,1, 2, 0, 0, "Channel 2");
1475         uiDefButS(block, ROW, B_NODE_EXEC, c3,
1476                 butr->xmin+cx+cx,butr->ymin+40,cx,20,&node->custom2, 1, 3, 0, 0, "Channel 3");
1477
1478         /*tolerance sliders */
1479         uiDefButF(block, NUMSLI, B_NODE_EXEC, "High ", 
1480                 butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1481                 &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1482         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Low ", 
1483                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1484                 &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1485         uiBlockEndAlign(block);
1486
1487         /*keep t2 (low) less than t1 (high) */
1488         if(c->t2 > c->t1) {
1489                 c->t2=c->t1;
1490         }
1491 }
1492
1493 static void node_composit_buts_luma_matte(uiLayout *layout, PointerRNA *ptr)
1494 {
1495         uiBlock *block= uiLayoutFreeBlock(layout);
1496         bNode *node= ptr->data;
1497         rctf *butr= &node->butr;
1498         NodeChroma *c=node->storage;
1499
1500         /*tolerance sliders */
1501         uiDefButF(block, NUMSLI, B_NODE_EXEC, "High ", 
1502                 butr->xmin, butr->ymin+20.0, butr->xmax-butr->xmin, 20,
1503                 &c->t1, 0.0f, 1.0f, 100, 0, "Values higher than this setting are 100% opaque");
1504         uiDefButF(block, NUMSLI, B_NODE_EXEC, "Low ", 
1505                 butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20,
1506                 &c->t2, 0.0f, 1.0f, 100, 0, "Values lower than this setting are 100% keyed");
1507         uiBlockEndAlign(block);
1508
1509         /*keep t2 (low) less than t1 (high) */
1510         if(c->t2 > c->t1) {
1511                 c->t2=c->t1;
1512         }
1513 }
1514
1515 static void node_composit_buts_map_uv(uiLayout *layout, PointerRNA *ptr)
1516 {
1517         uiItemR(layout, NULL, 0, ptr, "alpha", 0);
1518 }
1519
1520 static void node_composit_buts_id_mask(uiLayout *layout, PointerRNA *ptr)
1521 {
1522         uiBlock *block= uiLayoutFreeBlock(layout);
1523         bNode *node= ptr->data;
1524         rctf *butr= &node->butr;
1525
1526         uiDefButS(block, NUM, B_NODE_EXEC, "ID:",
1527                           butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1528                           &node->custom1, 0, 10000, 0, 0, "Pass Index number to convert to Alpha");
1529 }
1530
1531 /* allocate sufficient! */
1532 static void node_imagetype_string(char *str)
1533 {
1534         str += sprintf(str, "Save Image as: %%t|");
1535         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1536         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1537         str += sprintf(str, "PNG %%x%d|", R_PNG);
1538         str += sprintf(str, "BMP %%x%d|", R_BMP);
1539         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1540         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1541         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1542         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1543         str += sprintf(str, "DPX %%x%d|", R_DPX);
1544         str += sprintf(str, "OpenEXR %%x%d", R_OPENEXR);
1545 }
1546
1547 static void node_set_image_cb(bContext *C, void *ntree_v, void *node_v)
1548 {
1549         bNodeTree *ntree= ntree_v;
1550         bNode *node= node_v;
1551         
1552         nodeSetActive(ntree, node);
1553 }
1554
1555 static void node_composit_buts_file_output(uiLayout *layout, PointerRNA *ptr)
1556 {
1557         uiBlock *block= uiLayoutFreeBlock(layout);
1558         bNode *node= ptr->data;
1559         bNodeTree *ntree= ptr->id.data;
1560         rctf *butr= &node->butr;
1561         NodeImageFile *nif= node->storage;
1562         uiBut *bt;
1563         short x= (short)butr->xmin;
1564         short y= (short)butr->ymin;
1565         short w= (short)butr->xmax-butr->xmin;
1566         char str[320];
1567         
1568         node_imagetype_string(str);
1569         
1570         uiBlockBeginAlign(block);
1571         
1572         bt = uiDefIconBut(block, BUT, B_NODE_SETIMAGE, ICON_FILESEL,
1573                           x, y+60, 20, 20,
1574                           0, 0, 0, 0, 0, "Open Fileselect to get Backbuf image");
1575         uiButSetFunc(bt, node_set_image_cb, ntree, node);
1576         
1577         uiDefBut(block, TEX, B_NOP, "",
1578                           20+x, y+60, w-20, 20, 
1579                           nif->name, 0.0f, 240.0f, 0, 0, "");
1580         
1581         uiDefButS(block, MENU, B_NOP, str,
1582                           x, y+40, w, 20, 
1583                           &nif->imtype, 0.0f, 1.0f, 0, 0, "");
1584         
1585         if(nif->imtype==R_OPENEXR) {
1586                 uiDefButBitS(block, TOG, R_OPENEXR_HALF, B_REDR, "Half",        
1587                                         x, y+20, w/2, 20, 
1588                                         &nif->subimtype, 0, 0, 0, 0, "");
1589
1590                 uiDefButS(block, MENU,B_NOP, "Codec %t|None %x0|Pxr24 (lossy) %x1|ZIP (lossless) %x2|PIZ (lossless) %x3|RLE (lossless) %x4",  
1591                                         x+w/2, y+20, w/2, 20, 
1592                                         &nif->codec, 0, 0, 0, 0, "");
1593         }
1594         else {
1595                 uiDefButS(block, NUM, B_NOP, "Quality: ",
1596                           x, y+20, w, 20, 
1597                           &nif->quality, 10.0f, 100.0f, 10, 0, "");
1598         }
1599         
1600         /* start frame, end frame */
1601         uiDefButI(block, NUM, B_NODE_EXEC, "SFra: ", 
1602                           x, y, w/2, 20, 
1603                           &nif->sfra, 1, MAXFRAMEF, 10, 0, "");
1604         uiDefButI(block, NUM, B_NODE_EXEC, "EFra: ", 
1605                           x+w/2, y, w/2, 20, 
1606                           &nif->efra, 1, MAXFRAMEF, 10, 0, "");
1607 }
1608
1609 static void node_scale_cb(bContext *C, void *node_v, void *unused_v)
1610 {
1611         bNode *node= node_v;
1612         bNodeSocket *nsock;
1613
1614         /* check the 2 inputs, and set them to reasonable values */
1615         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
1616                 if(ELEM(node->custom1, CMP_SCALE_RELATIVE, CMP_SCALE_SCENEPERCENT))
1617                         nsock->ns.vec[0]= 1.0;
1618                 else {
1619                         if(nsock->next==NULL)
1620                                 nsock->ns.vec[0]= (float)CTX_data_scene(C)->r.ysch;
1621                         else
1622                                 nsock->ns.vec[0]= (float)CTX_data_scene(C)->r.xsch;
1623                 }
1624         }       
1625 }
1626
1627 static void node_composit_buts_scale(uiLayout *layout, PointerRNA *ptr)
1628 {
1629         uiBlock *block= uiLayoutFreeBlock(layout);
1630         bNode *node= ptr->data;
1631         rctf *butr= &node->butr;
1632         uiBut *bt= uiDefButS(block, MENU, B_NODE_EXEC, "Relative %x0|Absolute %x1|Scene Size % %x2|",
1633                           butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
1634                           &node->custom1, 0, 0, 0, 0, "Scale new image to absolute pixel size, size relative to the incoming image, or using the 'percent' size of the scene");
1635         uiButSetFunc(bt, node_scale_cb, node, NULL);
1636 }
1637
1638 static void node_composit_buts_invert(uiLayout *layout, PointerRNA *ptr)
1639 {
1640         uiLayout *col;
1641         
1642         col= uiLayoutColumn(layout, 0);
1643         uiItemR(col, NULL, 0, ptr, "rgb", 0);
1644         uiItemR(col, NULL, 0, ptr, "alpha", 0);
1645 }
1646
1647 static void node_composit_buts_premulkey(uiLayout *layout, PointerRNA *ptr)
1648 {
1649         uiItemR(layout, "", 0, ptr, "mapping", 0);
1650 }
1651
1652 static void node_composit_buts_view_levels(uiLayout *layout, PointerRNA *ptr)
1653 {
1654         uiItemR(layout, NULL, 0, ptr, "color_space", UI_ITEM_R_EXPAND);
1655 }
1656
1657 /* only once called */
1658 static void node_composit_set_butfunc(bNodeType *ntype)
1659 {
1660         switch(ntype->type) {
1661                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1662
1663                 case CMP_NODE_IMAGE:
1664                         ntype->uifunc= node_composit_buts_image;
1665                         break;
1666                 case CMP_NODE_R_LAYERS:
1667                         ntype->uifunc= node_composit_buts_renderlayers;
1668                         break;
1669                 case CMP_NODE_NORMAL:
1670                         ntype->uifunc= node_buts_normal;
1671                         break;
1672                 case CMP_NODE_CURVE_VEC:
1673                         ntype->uifunc= node_buts_curvevec;
1674                         break;
1675                 case CMP_NODE_CURVE_RGB:
1676                         ntype->uifunc= node_buts_curvecol;
1677                         break;
1678                 case CMP_NODE_VALUE:
1679                         ntype->uifunc= node_buts_value;
1680                         break;
1681                 case CMP_NODE_RGB:
1682                         ntype->uifunc= node_buts_rgb;
1683                         break;
1684                 case CMP_NODE_FLIP:
1685                         ntype->uifunc= node_composit_buts_flip;
1686                         break;
1687                 case CMP_NODE_SPLITVIEWER:
1688                         ntype->uifunc= node_composit_buts_splitviewer;
1689                         break;
1690                 case CMP_NODE_MIX_RGB:
1691                         ntype->uifunc= node_buts_mix_rgb;
1692                         break;
1693                 case CMP_NODE_VALTORGB:
1694                         ntype->uifunc= node_buts_valtorgb;
1695                         break;
1696                 case CMP_NODE_CROP:
1697                         ntype->uifunc= node_composit_buts_crop;
1698                         break;
1699                 case CMP_NODE_BLUR:
1700                         ntype->uifunc= node_composit_buts_blur;
1701                         break;
1702                 case CMP_NODE_DBLUR:
1703                         ntype->uifunc= node_composit_buts_dblur;
1704                         break;
1705                 case CMP_NODE_BILATERALBLUR:
1706                         ntype->uifunc= node_composit_buts_bilateralblur;
1707                         break;
1708                 /* qdn: defocus node */
1709                 case CMP_NODE_DEFOCUS:
1710                         ntype->uifunc = node_composit_buts_defocus;
1711                         break;
1712                 /* qdn: glare node */
1713                 case CMP_NODE_GLARE:
1714                         ntype->uifunc = node_composit_buts_glare;
1715                         break;
1716                 /* qdn: tonemap node */
1717                 case CMP_NODE_TONEMAP:
1718                         ntype->uifunc = node_composit_buts_tonemap;
1719                         break;
1720                 /* qdn: lens distortion node */
1721                 case CMP_NODE_LENSDIST:
1722                         ntype->uifunc = node_composit_buts_lensdist;
1723                         break;
1724                 case CMP_NODE_VECBLUR:
1725                         ntype->uifunc= node_composit_buts_vecblur;
1726                         break;
1727                 case CMP_NODE_FILTER:
1728                         ntype->uifunc= node_composit_buts_filter;
1729                         break;
1730                 case CMP_NODE_MAP_VALUE:
1731                         ntype->uifunc= node_composit_buts_map_value;
1732                         break;
1733                 case CMP_NODE_TIME:
1734                         ntype->uifunc= node_buts_time;
1735                         break;
1736                 case CMP_NODE_ALPHAOVER:
1737                         ntype->uifunc= node_composit_buts_alphaover;
1738                         break;
1739                 case CMP_NODE_HUE_SAT:
1740                         ntype->uifunc= node_composit_buts_hue_sat;
1741                         break;
1742                 case CMP_NODE_TEXTURE:
1743                         ntype->uifunc= node_buts_texture;
1744                         break;
1745                 case CMP_NODE_DILATEERODE:
1746                         ntype->uifunc= node_composit_buts_dilateerode;
1747                         break;
1748                 case CMP_NODE_OUTPUT_FILE:
1749                         ntype->uifunc= node_composit_buts_file_output;
1750                         break;  
1751                 case CMP_NODE_DIFF_MATTE:
1752                         ntype->uifunc=node_composit_buts_diff_matte;
1753                         break;
1754                 case CMP_NODE_DIST_MATTE:
1755                         ntype->uifunc=node_composit_buts_distance_matte;
1756                         break;
1757                 case CMP_NODE_COLOR_SPILL:
1758                         ntype->uifunc=node_composit_buts_color_spill;
1759                         break;
1760                 case CMP_NODE_CHROMA_MATTE:
1761                         ntype->uifunc=node_composit_buts_chroma_matte;
1762                         break;
1763                 case CMP_NODE_COLOR_MATTE:
1764                         ntype->uifunc=node_composit_buts_color_matte;
1765                         break;
1766                 case CMP_NODE_SCALE:
1767                         ntype->uifunc= node_composit_buts_scale;
1768                         break;
1769                 case CMP_NODE_CHANNEL_MATTE:
1770                         ntype->uifunc= node_composit_buts_channel_matte;
1771                         break;
1772                 case CMP_NODE_LUMA_MATTE:
1773                         ntype->uifunc= node_composit_buts_luma_matte;
1774                         break;
1775                 case CMP_NODE_MAP_UV:
1776                         ntype->uifunc= node_composit_buts_map_uv;
1777                         break;
1778                 case CMP_NODE_ID_MASK:
1779                         ntype->uifunc= node_composit_buts_id_mask;
1780                         break;
1781                 case CMP_NODE_MATH:
1782                         ntype->uifunc= node_buts_math;
1783                         break;
1784                 case CMP_NODE_INVERT:
1785                         ntype->uifunc= node_composit_buts_invert;
1786                         break;
1787                 case CMP_NODE_PREMULKEY:
1788                         ntype->uifunc= node_composit_buts_premulkey;
1789                         break;
1790       case CMP_NODE_VIEW_LEVELS:
1791                         ntype->uifunc=node_composit_buts_view_levels;
1792                         break;
1793                 default:
1794                         ntype->uifunc= NULL;
1795         }
1796 }
1797
1798 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
1799
1800 static void node_texture_buts_bricks(uiLayout *layout, PointerRNA *ptr)
1801 {
1802         uiBlock *block= uiLayoutFreeBlock(layout);
1803         bNode *node= ptr->data;
1804         rctf *butr= &node->butr;
1805         short w = butr->xmax-butr->xmin;
1806         short ofw = 32;
1807         
1808         uiBlockBeginAlign(block);
1809         
1810         /* Offset */
1811         uiDefButF(
1812                 block, NUM, B_NODE_EXEC, "Offset",
1813                 butr->xmin, butr->ymin+20, w-ofw, 20,
1814                 &node->custom3,
1815                 0, 1, 0.25, 2,
1816                 "Offset amount" );
1817         uiDefButS(
1818                 block, NUM, B_NODE_EXEC, "",
1819                 butr->xmin+w-ofw, butr->ymin+20, ofw, 20,
1820                 &node->custom1,
1821                 2, 99, 0, 0,
1822                 "Offset every N rows" );
1823         
1824         /* Squash */
1825         uiDefButF(
1826                 block, NUM, B_NODE_EXEC, "Squash",
1827                 butr->xmin, butr->ymin+0, w-ofw, 20,
1828                 &node->custom4,
1829                 0, 99, 0.25, 2,
1830                 "Stretch amount" );
1831         uiDefButS(
1832                 block, NUM, B_NODE_EXEC, "",
1833                 butr->xmin+w-ofw, butr->ymin+0, ofw, 20,
1834                 &node->custom2,
1835                 2, 99, 0, 0,
1836                 "Stretch every N rows" );
1837         
1838         uiBlockEndAlign(block);
1839 }
1840
1841 /* Copied from buttons_shading.c -- needs unifying */
1842 static char* noisebasis_menu()
1843 {
1844         static char nbmenu[256];
1845         sprintf(nbmenu, "Noise Basis %%t|Blender Original %%x%d|Original Perlin %%x%d|Improved Perlin %%x%d|Voronoi F1 %%x%d|Voronoi F2 %%x%d|Voronoi F3 %%x%d|Voronoi F4 %%x%d|Voronoi F2-F1 %%x%d|Voronoi Crackle %%x%d|CellNoise %%x%d", TEX_BLENDER, TEX_STDPERLIN, TEX_NEWPERLIN, TEX_VORONOI_F1, TEX_VORONOI_F2, TEX_VORONOI_F3, TEX_VORONOI_F4, TEX_VORONOI_F2F1, TEX_VORONOI_CRACKLE, TEX_CELLNOISE);
1846         return nbmenu;
1847 }
1848
1849 static void node_texture_buts_proc(uiLayout *layout, PointerRNA *ptr)
1850 {
1851         uiBlock *block= uiLayoutFreeBlock(layout);
1852         bNode *node= ptr->data;
1853         rctf *butr= &node->butr;
1854         Tex *tex = (Tex *)node->storage;
1855         short x,y,w,h;
1856         
1857         x = butr->xmin;
1858         y = butr->ymin;
1859         w = butr->xmax - x;
1860         h = butr->ymax - y;
1861         
1862         switch( tex->type ) {
1863                 case TEX_BLEND:
1864                         uiBlockBeginAlign( block );
1865                         uiDefButS( block, MENU, B_NODE_EXEC,
1866                                 "Linear %x0|Quad %x1|Ease %x2|Diag %x3|Sphere %x4|Halo %x5|Radial %x6",
1867                                 x, y+20, w, 20, &tex->stype, 0, 1, 0, 0, "Blend Type" );
1868                         uiDefButBitS(block, TOG, TEX_FLIPBLEND, B_NODE_EXEC, "Flip XY", x, y, w, 20,
1869                                 &tex->flag, 0, 0, 0, 0, "Flips the direction of the progression 90 degrees");
1870                         uiBlockEndAlign( block );
1871                         break;
1872                         
1873                 case TEX_MARBLE:
1874                         uiBlockBeginAlign(block);
1875                 
1876                         uiDefButS(block, ROW, B_NODE_EXEC, "Soft",       0*w/3+x, 40+y, w/3, 18, &tex->stype, 2.0, (float)TEX_SOFT, 0, 0, "Uses soft marble"); 
1877                         uiDefButS(block, ROW, B_NODE_EXEC, "Sharp",      1*w/3+x, 40+y, w/3, 18, &tex->stype, 2.0, (float)TEX_SHARP, 0, 0, "Uses more clearly defined marble"); 
1878                         uiDefButS(block, ROW, B_NODE_EXEC, "Sharper",    2*w/3+x, 40+y, w/3, 18, &tex->stype, 2.0, (float)TEX_SHARPER, 0, 0, "Uses very clearly defined marble"); 
1879                         
1880                         uiDefButS(block, ROW, B_NODE_EXEC, "Soft noise", 0*w/2+x, 20+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
1881                         uiDefButS(block, ROW, B_NODE_EXEC, "Hard noise", 1*w/2+x, 20+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISEPERL, 0, 0, "Generates hard noise");
1882                         
1883                         uiDefButS(block, ROW, B_NODE_EXEC, "Sin",        0*w/3+x,  0+y, w/3, 18, &tex->noisebasis2, 8.0, 0.0, 0, 0, "Uses a sine wave to produce bands."); 
1884                         uiDefButS(block, ROW, B_NODE_EXEC, "Saw",        1*w/3+x,  0+y, w/3, 18, &tex->noisebasis2, 8.0, 1.0, 0, 0, "Uses a saw wave to produce bands"); 
1885                         uiDefButS(block, ROW, B_NODE_EXEC, "Tri",        2*w/3+x,  0+y, w/3, 18, &tex->noisebasis2, 8.0, 2.0, 0, 0, "Uses a triangle wave to produce bands"); 
1886                 
1887                         uiBlockEndAlign(block);
1888                         break;
1889                         
1890                 case TEX_WOOD:
1891                         uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+64, w, 18, &tex->noisebasis, 0,0,0,0, "Sets the noise basis used for turbulence");
1892                         
1893                         uiBlockBeginAlign(block);
1894                         uiDefButS(block, ROW, B_TEXPRV,             "Bands",     x,  40+y, w/2, 18, &tex->stype, 2.0, (float)TEX_BANDNOISE, 0, 0, "Uses standard noise"); 
1895                         uiDefButS(block, ROW, B_TEXPRV,             "Rings", w/2+x,  40+y, w/2, 18, &tex->stype, 2.0, (float)TEX_RINGNOISE, 0, 0, "Lets Noise return RGB value"); 
1896                         
1897                         uiDefButS(block, ROW, B_NODE_EXEC, "Sin", 0*w/3+x,  20+y, w/3, 18, &tex->noisebasis2, 8.0, (float)TEX_SIN, 0, 0, "Uses a sine wave to produce bands."); 
1898                         uiDefButS(block, ROW, B_NODE_EXEC, "Saw", 1*w/3+x,  20+y, w/3, 18, &tex->noisebasis2, 8.0, (float)TEX_SAW, 0, 0, "Uses a saw wave to produce bands"); 
1899                         uiDefButS(block, ROW, B_NODE_EXEC, "Tri", 2*w/3+x,  20+y, w/3, 18, &tex->noisebasis2, 8.0, (float)TEX_TRI, 0, 0, "Uses a triangle wave to produce bands");
1900                         
1901                         uiDefButS(block, ROW, B_NODE_EXEC, "Soft noise", 0*w/2+x, 0+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
1902                         uiDefButS(block, ROW, B_NODE_EXEC, "Hard noise", 1*w/2+x, 0+y, w/2, 19, &tex->noisetype, 12.0, (float)TEX_NOISEPERL, 0, 0, "Generates hard noise");
1903                         uiBlockEndAlign(block);
1904                         break;
1905                         
1906                 case TEX_CLOUDS:
1907                         uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+60, w, 18, &tex->noisebasis, 0,0,0,0, "Sets the noise basis used for turbulence");
1908                         
1909                         uiBlockBeginAlign(block);
1910                         uiDefButS(block, ROW, B_TEXPRV, "B/W",       x, y+38, w/2, 18, &tex->stype, 2.0, (float)TEX_DEFAULT, 0, 0, "Uses standard noise"); 
1911                         uiDefButS(block, ROW, B_TEXPRV, "Color", w/2+x, y+38, w/2, 18, &tex->stype, 2.0, (float)TEX_COLOR, 0, 0, "Lets Noise return RGB value"); 
1912                         uiDefButS(block, ROW, B_TEXPRV, "Soft",      x, y+20, w/2, 18, &tex->noisetype, 12.0, (float)TEX_NOISESOFT, 0, 0, "Generates soft noise");
1913                         uiDefButS(block, ROW, B_TEXPRV, "Hard",  w/2+x, y+20, w/2, 18, &tex->noisetype, 12.0, (float)TEX_NOISEPERL, 0, 0, "Generates hard noise");
1914                         uiBlockEndAlign(block);
1915                         
1916                         uiDefButS(block, NUM, B_TEXPRV, "Depth:", x, y, w, 18, &tex->noisedepth, 0.0, 6.0, 0, 0, "Sets the depth of the cloud calculation");
1917                         break;
1918                         
1919                 case TEX_DISTNOISE:
1920                         uiBlockBeginAlign(block);
1921                         uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y+18, w, 18, &tex->noisebasis2, 0,0,0,0, "Sets the noise basis to distort");
1922                         uiDefButS(block, MENU, B_TEXPRV, noisebasis_menu(), x, y,    w, 18, &tex->noisebasis,  0,0,0,0, "Sets the noise basis which does the distortion");
1923                         uiBlockEndAlign(block);
1924                         break;
1925         }
1926 }
1927
1928 static void node_texture_buts_image(uiLayout *layout, PointerRNA *ptr)
1929 {
1930         uiBlock *block= uiLayoutFreeBlock(layout);
1931         bNode *node= ptr->data;
1932         bNodeTree *ntree= ptr->id.data;
1933         rctf *butr= &node->butr;
1934         char *strp;
1935         uiBut *bt;
1936
1937         uiBlockBeginAlign(block);
1938         
1939         /* browse button */
1940         IMAnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
1941         node->menunr= 0;
1942         bt= uiDefButS(block, MENU, B_NOP, strp, 
1943                                   butr->xmin, butr->ymin, 19, 19, 
1944                                   &node->menunr, 0, 0, 0, 0, "Browses existing choices");
1945         uiButSetFunc(bt, node_browse_image_cb, ntree, node);
1946         if(strp) MEM_freeN(strp);
1947         
1948         /* Add New button */
1949         if(node->id==NULL) {
1950                 bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
1951                                          butr->xmin+19, butr->ymin, (short)(butr->xmax-butr->xmin-19.0f), 19, 
1952                                          NULL, 0.0, 0.0, 0, 0, "Add new Image");
1953                 uiButSetFunc(bt, node_active_cb, ntree, node);
1954         }
1955         else {
1956                 /* name button */
1957                 short xmin= (short)butr->xmin, xmax= (short)butr->xmax;
1958                 short width= xmax - xmin - 19;
1959                 
1960                 bt= uiDefBut(block, TEX, B_NOP, "IM:",
1961                                          xmin+19, butr->ymin, width, 19, 
1962                                          node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
1963                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
1964         }
1965 }
1966
1967 static void node_texture_buts_output(uiLayout *layout, PointerRNA *ptr)
1968 {
1969         uiBlock *block= uiLayoutFreeBlock(layout);
1970         bNode *node= ptr->data;
1971         rctf *butr= &node->butr;
1972         uiBut *bt;
1973         short width;
1974         char *name = ((TexNodeOutput*)node->storage)->name;
1975         
1976         uiBlockBeginAlign(block);
1977         
1978         width = (short)(butr->xmax - butr->xmin);
1979         
1980         bt = uiDefBut(
1981                 block, TEX, B_NOP,
1982                 "Name:",
1983                 butr->xmin, butr->ymin,
1984                 width, 19, 
1985                 name, 0, 31,
1986                 0, 0, 
1987                 "Name this output"
1988         );
1989         
1990         uiBlockEndAlign(block);
1991 }
1992
1993 /* only once called */
1994 static void node_texture_set_butfunc(bNodeType *ntype)
1995 {
1996         if( ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX ) {
1997                 ntype->uifunc = node_texture_buts_proc;
1998         }
1999         else switch(ntype->type) {
2000                 
2001                 case TEX_NODE_MATH:
2002                         ntype->uifunc = node_buts_math;
2003                         break;
2004                 
2005                 case TEX_NODE_MIX_RGB:
2006                         ntype->uifunc = node_buts_mix_rgb;
2007                         break;
2008                         
2009                 case TEX_NODE_VALTORGB:
2010                         ntype->uifunc = node_buts_valtorgb;
2011                         break;
2012                         
2013                 case TEX_NODE_CURVE_RGB:
2014                         ntype->uifunc= node_buts_curvecol;
2015                         break;
2016                         
2017                 case TEX_NODE_CURVE_TIME:
2018                         ntype->uifunc = node_buts_time;
2019                         break;
2020                         
2021                 case TEX_NODE_TEXTURE:
2022                         ntype->uifunc = node_buts_texture;
2023                         break;
2024                         
2025                 case TEX_NODE_BRICKS:
2026                         ntype->uifunc = node_texture_buts_bricks;
2027                         break;
2028                         
2029                 case TEX_NODE_IMAGE:
2030                         ntype->uifunc = node_texture_buts_image;
2031                         break;
2032                         
2033                 case TEX_NODE_OUTPUT:
2034                         ntype->uifunc = node_texture_buts_output;
2035                         break;
2036                         
2037                 default:
2038                         ntype->uifunc= NULL;
2039         }
2040 }
2041
2042 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2043
2044 void ED_init_node_butfuncs(void)
2045 {
2046         bNodeType *ntype;
2047         
2048         /* shader nodes */
2049         ntype= node_all_shaders.first;
2050         while(ntype) {
2051                 node_shader_set_butfunc(ntype);
2052                 ntype= ntype->next;
2053         }
2054         /* composit nodes */
2055         ntype= node_all_composit.first;
2056         while(ntype) {
2057                 node_composit_set_butfunc(ntype);
2058                 ntype= ntype->next;
2059         }
2060         ntype = node_all_textures.first;
2061         while(ntype) {
2062                 node_texture_set_butfunc(ntype);
2063                 ntype= ntype->next;
2064         }
2065 }
2066
2067 /* ************** Generic drawing ************** */
2068
2069 #if 0
2070 void node_rename_but(char *s)
2071 {
2072         uiBlock *block;
2073         ListBase listb={0, 0};
2074         int dy, x1, y1, sizex=80, sizey=30;
2075         short pivot[2], mval[2], ret=0;
2076         
2077         getmouseco_sc(mval);
2078
2079         pivot[0]= CLAMPIS(mval[0], (sizex+10), G.curscreen->sizex-30);
2080         pivot[1]= CLAMPIS(mval[1], (sizey/2)+10, G.curscreen->sizey-(sizey/2)-10);
2081         
2082         if (pivot[0]!=mval[0] || pivot[1]!=mval[1])
2083                 warp_pointer(pivot[0], pivot[1]);
2084
2085         mywinset(G.curscreen->mainwin);
2086         
2087         x1= pivot[0]-sizex+10;
2088         y1= pivot[1]-sizey/2;
2089         dy= sizey/2;
2090         
2091         block= uiNewBlock(&listb, "button", UI_EMBOSS, UI_HELV, G.curscreen->mainwin);
2092         uiBlockSetFlag(block, UI_BLOCK_LOOP|UI_BLOCK_REDRAW|UI_BLOCK_NUMSELECT|UI_BLOCK_ENTER_OK);
2093         
2094         /* buttons have 0 as return event, to prevent menu to close on hotkeys */
2095         uiBlockBeginAlign(block);
2096         
2097         uiDefBut(block, TEX, B_NOP, "Name: ", (short)(x1),(short)(y1+dy), 150, 19, s, 0.0, 19.0, 0, 0, "Node user name");
2098         
2099         uiBlockEndAlign(block);
2100
2101         uiDefBut(block, BUT, 32767, "OK", (short)(x1+150), (short)(y1+dy), 29, 19, NULL, 0, 0, 0, 0, "");
2102
2103         uiBoundsBlock(block, 2);
2104
2105         ret= uiDoBlocks(&listb, 0, 0);
2106 }
2107
2108 #endif
2109
2110 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode)
2111 {
2112         
2113         if((snode->flag & SNODE_BACKDRAW) && snode->treetype==NTREE_COMPOSIT) {
2114                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2115                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2116                 if(ibuf) {
2117                         float x, y; 
2118                         
2119                         wmPushMatrix();
2120                         
2121                         /* somehow the offset has to be calculated inverse */
2122                         
2123                         glaDefine2DArea(&ar->winrct);
2124                         /* ortho at pixel level curarea */
2125                         wmOrtho2(-0.375, ar->winx-0.375, -0.375, ar->winy-0.375);
2126                         
2127                         x = (ar->winx-ibuf->x)/2 + snode->xof;
2128                         y = (ar->winy-ibuf->y)/2 + snode->yof;
2129                         
2130                         if(ibuf->rect)
2131                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2132                         else if(ibuf->channels==4)
2133                                 glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_FLOAT, ibuf->rect_float);
2134                         
2135                         wmPopMatrix();
2136                 }
2137         }
2138 }
2139
2140 #if 0
2141 /* note: needs to be userpref or opengl profile option */
2142 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
2143 {
2144
2145         draw_nodespace_grid(snode);
2146         
2147         if(snode->flag & SNODE_BACKDRAW) {
2148                 Image *ima= BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2149                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
2150                 if(ibuf) {
2151                         int x, y;
2152                         float zoom = 1.0;
2153
2154                         glMatrixMode(GL_PROJECTION);
2155                         glPushMatrix();
2156                         glMatrixMode(GL_MODELVIEW);
2157                         glPushMatrix();
2158                         
2159                         glaDefine2DArea(&sa->winrct);
2160
2161                         if(ibuf->x > sa->winx || ibuf->y > sa->winy) {
2162                                 float zoomx, zoomy;
2163                                 zoomx= (float)sa->winx/ibuf->x;
2164                                 zoomy= (float)sa->winy/ibuf->y;
2165                                 zoom = MIN2(zoomx, zoomy);
2166                         }
2167                         
2168                         x = (sa->winx-zoom*ibuf->x)/2 + snode->xof;
2169                         y = (sa->winy-zoom*ibuf->y)/2 + snode->yof;
2170
2171                         glPixelZoom(zoom, zoom);
2172
2173                         glColor4f(1.0, 1.0, 1.0, 1.0);
2174                         if(ibuf->rect)
2175                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
2176                         else if(ibuf->channels==4)
2177                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
2178
2179                         glPixelZoom(1.0, 1.0);
2180
2181                         glMatrixMode(GL_PROJECTION);
2182                         glPopMatrix();
2183                         glMatrixMode(GL_MODELVIEW);
2184                         glPopMatrix();
2185                 }
2186         }
2187 }
2188 #endif
2189
2190 /* if v2d not NULL, it clips and returns 0 if not visible */
2191 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
2192 {
2193         float dist, vec[4][2];
2194         
2195         /* in v0 and v3 we put begin/end points */
2196         if(link->fromsock) {
2197                 vec[0][0]= link->fromsock->locx;
2198                 vec[0][1]= link->fromsock->locy;
2199         }
2200         else {
2201                 if(snode==NULL) return 0;
2202                 vec[0][0]= snode->mx;
2203                 vec[0][1]= snode->my;
2204         }
2205         if(link->tosock) {
2206                 vec[3][0]= link->tosock->locx;
2207                 vec[3][1]= link->tosock->locy;
2208         }
2209         else {
2210                 if(snode==NULL) return 0;
2211                 vec[3][0]= snode->mx;
2212                 vec[3][1]= snode->my;
2213         }
2214         
2215         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
2216         
2217         /* check direction later, for top sockets */
2218         vec[1][0]= vec[0][0]+dist;
2219         vec[1][1]= vec[0][1];
2220         
2221         vec[2][0]= vec[3][0]-dist;
2222         vec[2][1]= vec[3][1];
2223         
2224         if(v2d && MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > v2d->cur.xmax); /* clipped */      
2225         else if (v2d && MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < v2d->cur.xmin); /* clipped */
2226         else {
2227                 
2228                 /* always do all three, to prevent data hanging around */
2229                 forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, sizeof(float)*2);
2230                 forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, sizeof(float)*2);
2231                 
2232                 return 1;
2233         }
2234         return 0;
2235 }
2236
2237 #define LINK_RESOL      24
2238 void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, int th_col1, int th_col2, int do_shaded)
2239 {
2240         float coord_array[LINK_RESOL+1][2];
2241         
2242         if(node_link_bezier_points(v2d, snode, link, coord_array, LINK_RESOL)) {
2243                 float dist, spline_step = 0.0f;
2244                 int i;
2245                 
2246                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
2247                 dist = 1.0f/(float)LINK_RESOL;
2248                 
2249                 glBegin(GL_LINE_STRIP);
2250                 for(i=0; i<=LINK_RESOL; i++) {
2251                         if(do_shaded) {
2252                                 UI_ThemeColorBlend(th_col1, th_col2, spline_step);
2253                                 spline_step += dist;
2254                         }                               
2255                         glVertex2fv(coord_array[i]);
2256                 }
2257                 glEnd();
2258         }
2259 }
2260
2261 /* note; this is used for fake links in groups too */
2262 void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link)
2263 {
2264         int do_shaded= 1, th_col1= TH_WIRE, th_col2= TH_WIRE;
2265         
2266         if(link->fromnode==NULL && link->tonode==NULL)
2267                 return;
2268         
2269         if(link->fromnode==NULL || link->tonode==NULL) {
2270                 UI_ThemeColor(TH_WIRE);
2271                 do_shaded= 0;
2272         }
2273         else {
2274                 /* going to give issues once... */
2275                 if(link->tosock->flag & SOCK_UNAVAIL)
2276                         return;
2277                 if(link->fromsock->flag & SOCK_UNAVAIL)
2278                         return;
2279                 
2280                 /* a bit ugly... but thats how we detect the internal group links */
2281                 if(link->fromnode==link->tonode) {
2282                         UI_ThemeColorBlend(TH_BACK, TH_WIRE, 0.25f);
2283                         do_shaded= 0;
2284                 }
2285                 else {
2286                         /* check cyclic */
2287                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2288                                 if(link->fromnode->flag & SELECT)
2289                                         th_col1= TH_EDGE_SELECT;
2290                                 if(link->tonode->flag & SELECT)
2291                                         th_col2= TH_EDGE_SELECT;
2292                         }                               
2293                         else {
2294                                 UI_ThemeColor(TH_REDALERT);
2295                                 do_shaded= 0;
2296                         }
2297                 }
2298         }
2299         
2300         node_draw_link_bezier(v2d, snode, link, th_col1, th_col2, do_shaded);
2301 }
2302
2303