ed8f6d81ba9ea15f8db33a764b850109b37956bb
[blender-staging.git] / source / blender / src / 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): none yet.
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_action_types.h"
38 #include "DNA_ipo_types.h"
39 #include "DNA_ID.h"
40 #include "DNA_material_types.h"
41 #include "DNA_node_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_texture_types.h"
47 #include "DNA_userdef_types.h"
48
49 #include "BKE_global.h"
50 #include "BKE_image.h"
51 #include "BKE_library.h"
52 #include "BKE_main.h"
53 #include "BKE_material.h"
54 #include "BKE_node.h"
55 #include "BKE_object.h"
56 #include "BKE_texture.h"
57 #include "BKE_utildefines.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61 #include "BIF_interface.h"
62 #include "BIF_interface_icons.h"
63 #include "BIF_language.h"
64 #include "BIF_mywindow.h"
65 #include "BIF_previewrender.h"
66 #include "BIF_resources.h"
67 #include "BIF_screen.h"
68 #include "BIF_space.h"
69
70 #include "BSE_drawipo.h"
71 #include "BSE_node.h"
72 #include "BSE_view.h"
73
74 #include "BMF_Api.h"
75
76 #include "MEM_guardedalloc.h"
77
78 #include "RE_pipeline.h"
79
80 #include "blendef.h"
81 #include "butspace.h"
82 #include "interface.h"  /* urm...  for rasterpos_safe, roundbox */
83 #include "mydevice.h"
84
85
86
87 static void snode_drawstring(SpaceNode *snode, char *str, int okwidth)
88 {
89         char drawstr[NODE_MAXSTR];
90         int width;
91         
92         if(str[0]==0 || okwidth<4) return;
93         
94         BLI_strncpy(drawstr, str, NODE_MAXSTR);
95         width= snode->aspect*BIF_GetStringWidth(snode->curfont, drawstr, 0);
96
97         if(width > okwidth) {
98                 int len= strlen(drawstr)-1;
99                 
100                 while(width > okwidth && len>=0) {
101                         drawstr[len]= 0;
102                         
103                         width= snode->aspect*BIF_GetStringWidth(snode->curfont, drawstr, 0);
104                         len--;
105                 }
106                 if(len==0) return;
107         }
108         BIF_DrawString(snode->curfont, drawstr, 0);
109
110 }
111
112 /* **************  Socket callbacks *********** */
113
114 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
115 static uiBlock *socket_vector_menu(void *butpoin_v)
116 {
117         float *butpoin= butpoin_v;
118         uiBlock *block;
119         
120         block= uiNewBlock(&curarea->uiblocks, "socket menu", UI_EMBOSS, UI_HELV, curarea->win);
121         
122         /* use this for a fake extra empy space around the buttons */
123         uiDefBut(block, LABEL, 0, "",                   -4, -4, 188, 68, NULL, 0, 0, 0, 0, "");
124         
125         uiBlockBeginAlign(block);
126         uiDefButF(block, NUMSLI, 0, "X ",        0,40,180,20, butpoin, -1.0, 1.0, 10, 0, "");
127         uiDefButF(block, NUMSLI, 0, "Y ",        0,20,180,20, butpoin+1, -1.0, 1.0, 10, 0, "");
128         uiDefButF(block, NUMSLI, 0, "Z ",        0,0,180,20, butpoin+2, -1.0, 1.0, 10, 0, "");
129         
130         uiBlockSetDirection(block, UI_TOP);
131         
132         allqueue(REDRAWNODE, 0);
133         
134         return block;
135 }
136
137 static void node_sync_cb(void *snode_v, void *node_v)
138 {
139         SpaceNode *snode= snode_v;
140         
141         if(snode->treetype==NTREE_SHADER) {
142                 nodeShaderSynchronizeID(node_v, 1);
143                 allqueue(REDRAWBUTSSHADING, 0);
144         }
145 }
146
147 /* ****************** GENERAL CALLBACKS FOR NODES ***************** */
148
149 static void node_ID_title_cb(void *node_v, void *unused_v)
150 {
151         bNode *node= node_v;
152         
153         if(node->id) {
154                 test_idbutton(node->id->name+2);        /* library.c, verifies unique name */
155                 BLI_strncpy(node->name, node->id->name+2, 21);
156                 
157                 allqueue(REDRAWBUTSSHADING, 0);
158                 allqueue(REDRAWNODE, 0);
159                 allqueue(REDRAWOOPS, 0);
160         }
161 }
162
163
164 static void node_but_title_cb(void *node_v, void *but_v)
165 {
166         bNode *node= node_v;
167         uiBut *bt= but_v;
168         BLI_strncpy(node->name, bt->drawstr, NODE_MAXSTR);
169         
170         allqueue(REDRAWNODE, 0);
171 }
172
173
174 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
175
176 static int node_buts_group(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
177 {
178         if(block && node->id) {
179                 uiBut *bt;
180                 short width;
181                 
182                 uiBlockBeginAlign(block);
183                 
184                 /* name button */
185                 width= (short)(butr->xmax-butr->xmin - (node->id->us>1?19.0f:0.0f));
186                 bt= uiDefBut(block, TEX, B_NOP, "NT:",
187                                          butr->xmin, butr->ymin, width, 19, 
188                                          node->id->name+2, 0.0, 19.0, 0, 0, "NodeTree name");
189                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
190                 
191                 /* user amount */
192                 if(node->id->us>1) {
193                         char str1[32];
194                         sprintf(str1, "%d", node->id->us);
195                         bt= uiDefBut(block, BUT, B_NOP, str1, 
196                                                  butr->xmax-19, butr->ymin, 19, 19, 
197                                                  NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
198                         //uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
199                 }
200                 
201                 uiBlockEndAlign(block);
202         }       
203         return 19;
204 }
205
206 static int node_buts_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
207 {
208         if(block) {
209                 bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
210                 
211                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", 
212                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
213                                   sock->ns.vec, 0.0f, 1.0f, 10, 2, "");
214                 
215         }
216         return 20;
217 }
218
219 static int node_buts_rgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
220 {
221         if(block) {
222                 bNodeSocket *sock= node->outputs.first;         /* first socket stores value */
223                 if(sock) {
224                         /* enforce square box drawing */
225                         uiBlockSetEmboss(block, UI_EMBOSSP);
226                         
227                         uiDefButF(block, HSVCUBE, B_NODE_EXEC+node->nr, "", 
228                                           butr->xmin, butr->ymin, butr->xmax-butr->xmin, 12, 
229                                           sock->ns.vec, 0.0f, 1.0f, 3, 0, "");
230                         uiDefButF(block, HSVCUBE, B_NODE_EXEC+node->nr, "", 
231                                           butr->xmin, butr->ymin+15, butr->xmax-butr->xmin, butr->ymax-butr->ymin -15 -15, 
232                                           sock->ns.vec, 0.0f, 1.0f, 2, 0, "");
233                         uiDefButF(block, COL, B_NOP, "",                
234                                           butr->xmin, butr->ymax-12, butr->xmax-butr->xmin, 12, 
235                                           sock->ns.vec, 0.0, 0.0, -1, 0, "");
236                         /* the -1 above prevents col button to popup a color picker */
237                         
238                         uiBlockSetEmboss(block, UI_EMBOSS);
239                 }
240         }
241         return 30 + (int)(node->width-NODE_DY);
242 }
243
244 static int node_buts_mix_rgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
245 {
246         if(block) {
247                 uiBut *bt;
248                 
249                 /* blend type */
250                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Mix %x0|Add %x1|Subtract %x3|Multiply %x2|Screen %x4|Divide %x5|Difference %x6|Darken %x7|Lighten %x8",
251                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
252                                          &node->custom1, 0, 0, 0, 0, "");
253                 uiButSetFunc(bt, node_but_title_cb, node, bt);
254         }
255         return 20;
256 }
257
258 static int node_buts_valtorgb(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
259 {
260         if(block) {
261                 if(node->storage) {
262                         draw_colorband_buts_small(block, node->storage, butr, B_NODE_EXEC+node->nr);
263                 }
264         }
265         return 40;
266 }
267
268 static int node_buts_curvevec(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
269 {
270         if(block) {
271                 curvemap_buttons(block, node->storage, 'v', B_NODE_EXEC+node->nr, B_REDR, butr);
272         }       
273         return (int)(node->width-NODE_DY);
274 }
275
276 static int node_buts_curvecol(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
277 {
278         if(block) {
279                 curvemap_buttons(block, node->storage, 'c', B_NODE_EXEC+node->nr, B_REDR, butr);
280         }       
281         return (int)(node->width-NODE_DY);
282 }
283
284 static int node_buts_normal(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
285 {
286         if(block) {
287                 bNodeSocket *sock= node->outputs.first;         /* first socket stores normal */
288                 
289                 uiDefButF(block, BUT_NORMAL, B_NODE_EXEC+node->nr, "", 
290                                   butr->xmin, butr->ymin, butr->xmax-butr->xmin, butr->ymax-butr->ymin, 
291                                   sock->ns.vec, 0.0f, 1.0f, 0, 0, "");
292                 
293         }       
294         return (int)(node->width-NODE_DY);
295 }
296
297
298
299 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
300
301
302 static void node_mat_alone_cb(void *node_v, void *unused)
303 {
304         bNode *node= node_v;
305         
306         node->id= (ID *)copy_material((Material *)node->id);
307         
308         BIF_undo_push("Single user material");
309         allqueue(REDRAWBUTSSHADING, 0);
310         allqueue(REDRAWNODE, 0);
311         allqueue(REDRAWOOPS, 0);
312 }
313
314 static void node_browse_mat_cb(void *ntree_v, void *node_v)
315 {
316         bNodeTree *ntree= ntree_v;
317         bNode *node= node_v;
318         
319         if(node->menunr<1) return;
320         
321         if(node->menunr==32767) {       /* code for Add New */
322                 if(node->id) {
323                         /* make copy, but make sure it doesnt have the node tag nor nodes */
324                         Material *ma= (Material *)node->id;
325                         ma->id.us--;
326                         ma= copy_material(ma);
327                         ma->use_nodes= 0;
328                         if(ma->nodetree) {
329                                 ntreeFreeTree(ma->nodetree);
330                                 MEM_freeN(ma->nodetree);
331                         }
332                         ma->nodetree= NULL;
333                         node->id= (ID *)ma;
334                 }
335                 else node->id= (ID *)add_material("MatNode");
336         }
337         else {
338                 if(node->id) node->id->us--;
339                 node->id= BLI_findlink(&G.main->mat, node->menunr-1);
340                 id_us_plus(node->id);
341         }
342         BLI_strncpy(node->name, node->id->name+2, 21);
343         
344         nodeSetActive(ntree, node);
345
346         allqueue(REDRAWBUTSSHADING, 0);
347         allqueue(REDRAWNODE, 0);
348         BIF_preview_changed(ID_MA);
349
350         node->menunr= 0;
351 }
352
353 static void node_new_mat_cb(void *ntree_v, void *node_v)
354 {
355         bNodeTree *ntree= ntree_v;
356         bNode *node= node_v;
357         
358         node->id= (ID *)add_material("MatNode");
359         BLI_strncpy(node->name, node->id->name+2, 21);
360
361         nodeSetActive(ntree, node);
362
363         allqueue(REDRAWBUTSSHADING, 0);
364         allqueue(REDRAWNODE, 0);
365         BIF_preview_changed(ID_MA);
366
367 }
368
369 static void node_texmap_cb(void *texmap_v, void *unused_v)
370 {
371         init_mapping(texmap_v);
372 }
373
374 static int node_shader_buts_material(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
375 {
376         if(block) {
377                 uiBut *bt;
378                 short dx= (short)((butr->xmax-butr->xmin)/3.0f), has_us= (node->id && node->id->us>1);
379                 short dy= (short)butr->ymin;
380                 char *strp;
381                 
382                 /* WATCH IT: we use this callback in material buttons, but then only want first row */
383                 if(butr->ymax-butr->ymin > 21.0f) dy+= 19;
384                 
385                 uiBlockBeginAlign(block);
386                 if(node->id==NULL) uiBlockSetCol(block, TH_REDALERT);
387                 else if(has_us) uiBlockSetCol(block, TH_BUT_SETTING1);
388                 else uiBlockSetCol(block, TH_BUT_SETTING2);
389                 
390                 /* browse button */
391                 IDnames_to_pupstring(&strp, NULL, "ADD NEW %x32767", &(G.main->mat), NULL, NULL);
392                 node->menunr= 0;
393                 bt= uiDefButS(block, MENU, B_NOP, strp, 
394                                   butr->xmin, dy, 19, 19, 
395                                   &node->menunr, 0, 0, 0, 0, "Browses existing choices or adds NEW");
396                 uiButSetFunc(bt, node_browse_mat_cb, ntree, node);
397                 if(strp) MEM_freeN(strp);
398                 
399                 /* Add New button */
400                 if(node->id==NULL) {
401                         bt= uiDefBut(block, BUT, B_NOP, "Add New",
402                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
403                                                  NULL, 0.0, 0.0, 0, 0, "Add new Material");
404                         uiButSetFunc(bt, node_new_mat_cb, ntree, node);
405                         uiBlockSetCol(block, TH_AUTO);
406                 }
407                 else {
408                         /* name button */
409                         short width= (short)(butr->xmax-butr->xmin-19.0f - (has_us?19.0f:0.0f));
410                         bt= uiDefBut(block, TEX, B_NOP, "MA:",
411                                                   butr->xmin+19, dy, width, 19, 
412                                                   node->id->name+2, 0.0, 19.0, 0, 0, "Material name");
413                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
414                         
415                         /* user amount */
416                         if(has_us) {
417                                 char str1[32];
418                                 sprintf(str1, "%d", node->id->us);
419                                 bt= uiDefBut(block, BUT, B_NOP, str1, 
420                                                           butr->xmax-19, dy, 19, 19, 
421                                                           NULL, 0, 0, 0, 0, "Displays number of users. Click to make a single-user copy.");
422                                 uiButSetFunc(bt, node_mat_alone_cb, node, NULL);
423                         }
424                         
425                         /* WATCH IT: we use this callback in material buttons, but then only want first row */
426                         if(butr->ymax-butr->ymin > 21.0f) {
427                                 /* node options */
428                                 uiBlockSetCol(block, TH_AUTO);
429                                 uiDefButBitS(block, TOG, SH_NODE_MAT_DIFF, B_NODE_EXEC+node->nr, "Diff",
430                                                          butr->xmin, butr->ymin, dx, 19, 
431                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Diffuse");
432                                 uiDefButBitS(block, TOG, SH_NODE_MAT_SPEC, B_NODE_EXEC+node->nr, "Spec",
433                                                          butr->xmin+dx, butr->ymin, dx, 19, 
434                                                          &node->custom1, 0, 0, 0, 0, "Material Node outputs Specular");
435                                 uiDefButBitS(block, TOG, SH_NODE_MAT_NEG, B_NODE_EXEC+node->nr, "Neg Normal",
436                                                          butr->xmax-dx, butr->ymin, dx, 19,
437                                                          &node->custom1, 0, 0, 0, 0, "Material Node uses inverted Normal");
438                         }
439                 }
440                 uiBlockEndAlign(block);
441         }       
442         return 38;
443 }
444
445 static int node_shader_buts_texture(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
446 {
447         if(block) {
448                 uiBut *bt;
449                 
450                 bt= uiDefIDPoinBut(block, test_texpoin_but, ID_TE, B_NODE_EXEC+node->nr, "",
451                                                    butr->xmin, butr->ymin, butr->xmax-butr->xmin, 19, 
452                                                    &node->id,  ""); 
453                 uiButSetFunc(bt, node_ID_title_cb, node, NULL);
454                 
455         }       
456         return 19;
457 }
458
459
460 static int node_shader_buts_mapping(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
461 {
462         if(block) {
463                 TexMapping *texmap= node->storage;
464                 short dx= (short)((butr->xmax-butr->xmin)/7.0f);
465                 short dy= (short)(butr->ymax-19);
466                 
467                 uiBlockSetFunc(block, node_texmap_cb, texmap, NULL);    /* all buttons get this */
468                 
469                 uiBlockBeginAlign(block);
470                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
471                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->loc+1, -1000.0f, 1000.0f, 10, 2, "");
472                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->loc+2, -1000.0f, 1000.0f, 10, 2, "");
473                 dy-= 19;
474                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->rot, -1000.0f, 1000.0f, 1000, 1, "");
475                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->rot+1, -1000.0f, 1000.0f, 1000, 1, "");
476                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->rot+2, -1000.0f, 1000.0f, 1000, 1, "");
477                 dy-= 19;
478                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 2, "");
479                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->size+1, -1000.0f, 1000.0f, 10, 2, "");
480                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->size+2, -1000.0f, 1000.0f, 10, 2, "");
481                 dy-= 25;
482                 uiBlockBeginAlign(block);
483                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->min, -10.0f, 10.0f, 100, 2, "");
484                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->min+1, -10.0f, 10.0f, 100, 2, "");
485                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->min+2, -10.0f, 10.0f, 100, 2, "");
486                 dy-= 19;
487                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+dx, dy, 2*dx, 19, texmap->max, -10.0f, 10.0f, 10, 2, "");
488                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+3*dx, dy, 2*dx, 19, texmap->max+1, -10.0f, 10.0f, 10, 2, "");
489                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", butr->xmin+5*dx, dy, 2*dx, 19, texmap->max+2, -10.0f, 10.0f, 10, 2, "");
490                 uiBlockEndAlign(block);
491                 
492                 /* labels/options */
493                 
494                 dy= (short)(butr->ymax-19);
495                 uiDefBut(block, LABEL, B_NOP, "Loc", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
496                 dy-= 19;
497                 uiDefBut(block, LABEL, B_NOP, "Rot", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
498                 dy-= 19;
499                 uiDefBut(block, LABEL, B_NOP, "Size", butr->xmin, dy, dx, 19, NULL, 0.0f, 0.0f, 0, 0, "");
500                 dy-= 25;
501                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
502                 dy-= 19;
503                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", butr->xmin, dy, dx-4, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
504                 
505         }       
506         return 5*19 + 6;
507 }
508
509
510 /* only once called */
511 static void node_shader_set_butfunc(bNodeType *ntype)
512 {
513         switch(ntype->type) {
514                 case NODE_GROUP:        /* note, generic type, but put here because we call this function anyway */
515                         ntype->butfunc= node_buts_group;
516                         break;
517                 case SH_NODE_MATERIAL:
518                         ntype->butfunc= node_shader_buts_material;
519                         break;
520                 case SH_NODE_TEXTURE:
521                         ntype->butfunc= node_shader_buts_texture;
522                         break;
523                 case SH_NODE_NORMAL:
524                         ntype->butfunc= node_buts_normal;
525                         break;
526                 case SH_NODE_CURVE_VEC:
527                         ntype->butfunc= node_buts_curvevec;
528                         break;
529                 case SH_NODE_CURVE_RGB:
530                         ntype->butfunc= node_buts_curvecol;
531                         break;
532                 case SH_NODE_MAPPING:
533                         ntype->butfunc= node_shader_buts_mapping;
534                         break;
535                 case SH_NODE_VALUE:
536                         ntype->butfunc= node_buts_value;
537                         break;
538                 case SH_NODE_RGB:
539                         ntype->butfunc= node_buts_rgb;
540                         break;
541                 case SH_NODE_MIX_RGB:
542                         ntype->butfunc= node_buts_mix_rgb;
543                         break;
544                 case SH_NODE_VALTORGB:
545                         ntype->butfunc= node_buts_valtorgb;
546                         break;
547                 default:
548                         ntype->butfunc= NULL;
549         }
550 }
551
552 /* ****************** BUTTON CALLBACKS FOR COMPOSIT NODES ***************** */
553
554
555
556 static void node_browse_image_cb(void *ntree_v, void *node_v)
557 {
558         bNodeTree *ntree= ntree_v;
559         bNode *node= node_v;
560         
561         nodeSetActive(ntree, node);
562         
563         if(node->menunr<1) return;
564         if(node->menunr==32767) {       /* code for Load New */
565                 addqueue(curarea->win, UI_BUT_EVENT, B_NODE_LOADIMAGE);
566         }
567         else {
568                 if(node->id) node->id->us--;
569                 node->id= BLI_findlink(&G.main->image, node->menunr-1);
570                 id_us_plus(node->id);
571
572                 BLI_strncpy(node->name, node->id->name+2, 21);
573
574                 NodeTagChanged(ntree, node); 
575         }
576         node->menunr= 0;
577 }
578
579 static void node_active_cb(void *ntree_v, void *node_v)
580 {
581         nodeSetActive(ntree_v, node_v);
582 }
583 static void node_image_anim_cb(void *node_v, void *unused)
584 {
585         bNode *node= node_v;
586         NodeImageAnim *nia;
587         
588         if(node->storage) {
589                 MEM_freeN(node->storage);
590                 node->storage= NULL;
591         }
592         else {
593                 nia= node->storage= MEM_callocN(sizeof(NodeImageAnim), "node image anim");
594                 nia->sfra= nia->nr= 1;
595         }
596         allqueue(REDRAWNODE, 1);
597 }
598
599 static int node_composit_buts_image(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
600 {
601         if(block) {
602                 uiBut *bt;
603                 short dy= (short)butr->ymax-19;
604                 char *strp;
605                 
606                 uiBlockBeginAlign(block);
607                 uiBlockSetCol(block, TH_BUT_SETTING2);
608                 
609                 /* browse button */
610                 IDnames_to_pupstring(&strp, NULL, "LOAD NEW %x32767", &(G.main->image), NULL, NULL);
611                 node->menunr= 0;
612                 bt= uiDefButS(block, MENU, B_NOP, strp, 
613                                           butr->xmin, dy, 19, 19, 
614                                           &node->menunr, 0, 0, 0, 0, "Browses existing choices");
615                 uiButSetFunc(bt, node_browse_image_cb, ntree, node);
616                 if(strp) MEM_freeN(strp);
617                 
618                 /* Add New button */
619                 if(node->id==NULL) {
620                         bt= uiDefBut(block, BUT, B_NODE_LOADIMAGE, "Load New",
621                                                  butr->xmin+19, dy, (short)(butr->xmax-butr->xmin-19.0f), 19, 
622                                                  NULL, 0.0, 0.0, 0, 0, "Add new Image");
623                         uiButSetFunc(bt, node_active_cb, ntree, node);
624                         uiBlockSetCol(block, TH_AUTO);
625                 }
626                 else {
627                         /* name button */
628                         short width= (short)(butr->xmax-butr->xmin-38.0f);
629                         bt= uiDefBut(block, TEX, B_NOP, "IMA:",
630                                                  butr->xmin+19, dy, width, 19, 
631                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Image name");
632                         uiButSetFunc(bt, node_ID_title_cb, node, NULL);
633                         bt= uiDefIconBut(block, BUT, B_NOP, ICON_SEQUENCE,
634                                                  butr->xmax-19, dy, 19, 19, 
635                                                  node->id->name+2, 0.0, 19.0, 0, 0, "Enable/Disable Image animation");
636                         uiButSetFunc(bt, node_image_anim_cb, node, NULL);
637                 }
638                 if(node->storage) {
639                         NodeImageAnim *nia= node->storage;
640                         short width= (short)(butr->xmax-butr->xmin)/2;
641                         
642                         dy-= 19;
643                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Frs:",
644                                           butr->xmin, dy, width, 19, 
645                                           &nia->frames, 0.0, 10000.0, 0, 0, "Amount of images used in animation");
646                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "SFra:",
647                                           butr->xmin+width, dy, width, 19, 
648                                           &nia->sfra, 1.0, 1000.0, 0, 0, "Start frame of animation");
649                         dy-= 19;
650                         uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "First:",
651                                           butr->xmin, dy, width, 19, 
652                                           &nia->nr, 0.0, 10000.0, 0, 0, "Number in image name, used as first in animation");
653                         uiDefButC(block, TOG, B_NODE_EXEC+node->nr, "Cycl",
654                                           butr->xmin+width, dy, width, 19, 
655                                           &nia->cyclic, 0.0, 0.0, 0, 0, "Make animation go cyclic");
656                         
657                 }
658                 
659         }       
660         if(node->storage) 
661                 return 57;
662         else
663                 return 19;
664 }
665
666 static char *scene_layer_menu(void)
667 {
668         SceneRenderLayer *srl;
669         int len= 32 + 32*BLI_countlist(&G.scene->r.layers);
670         short a, nr;
671         char *str= MEM_callocN(len, "menu layers");
672         
673         strcpy(str, "Active Layer %t");
674         a= strlen(str);
675         for(nr=0, srl= G.scene->r.layers.first; srl; srl= srl->next, nr++) {
676                 a+= sprintf(str+a, "|%s %%x%d", srl->name, nr);
677         }
678         
679         return str;
680 }
681
682
683 static int node_composit_buts_renderresult(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
684 {
685         if(block) {
686                 uiBut *bt;
687                 char *strp;
688                 
689                 strp= scene_layer_menu();
690                 bt= uiDefButS(block, MENU, B_NODE_EXEC+node->nr, strp, 
691                                   butr->xmin, butr->ymin, (butr->xmax-butr->xmin), 19, 
692                                   &node->custom1, 0, 0, 0, 0, "Choose Render Layer");
693                 uiButSetFunc(bt, node_but_title_cb, node, bt);
694                 MEM_freeN(strp);
695         }
696         return 19;
697 }
698
699 static int node_composit_buts_blur(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
700 {
701         if(block) {
702                 NodeBlurData *nbd= node->storage;
703                 uiBut *bt;
704                 short dy= butr->ymin+19;
705                 short dx= (butr->xmax-butr->xmin)/2;
706                 short dx3=(butr->xmax-butr->xmin)/3;
707                 char str[256];
708                 
709                 uiBlockBeginAlign(block);
710                 sprintf(str, "Filter Type%%t|Flat %%x%d|Tent %%x%d|Quad %%x%d|Cubic %%x%d|Gauss %%x%d|CatRom %%x%d|Mitch %%x%d", R_FILTER_BOX, R_FILTER_TENT, R_FILTER_QUAD, R_FILTER_CUBIC, R_FILTER_GAUSS, R_FILTER_CATROM, R_FILTER_MITCH);
711                 uiDefButS(block, MENU, B_NODE_EXEC,str,         
712                                   butr->xmin, dy, dx3, 19, 
713                                   &nbd->filtertype, 0, 0, 0, 0, "Set sampling filter for blur");
714                 uiDefButC(block, TOG, B_NODE_EXEC, "Bokeh",             
715                                   butr->xmin+dx3, dy, dx3, 19, 
716                                   &nbd->bokeh, 0, 0, 0, 0, "Uses circular filter, warning it's slow!");
717                 uiDefButC(block, TOG, B_NODE_EXEC, "Gamma",             
718                                   butr->xmin+2*dx3, dy, dx3, 19, 
719                                   &nbd->gamma, 0, 0, 0, 0, "Applies filter on gamma corrected values");
720                 
721                 dy-=19;
722                 bt=uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "X:",
723                                          butr->xmin, dy, dx, 19, 
724                                          &nbd->sizex, 0, 256, 0, 0, "");
725                 bt=uiDefButS(block, NUM, B_NODE_EXEC+node->nr, "Y:",
726                                          butr->xmin+dx, dy, dx, 19, 
727                                          &nbd->sizey, 0, 256, 0, 0, "");
728         }
729         return 38;
730 }
731
732 static int node_composit_buts_filter(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
733 {
734         if(block) {
735                 uiBut *bt;
736                 
737                 /* blend type */
738                 bt=uiDefButS(block, MENU, B_NODE_EXEC+node->nr, "Soften %x0|Sharpen %x1|Laplace %x2|Sobel %x3|Prewitt %x4|Kirsch %x5|Shadow %x6",
739                                          butr->xmin, butr->ymin, butr->xmax-butr->xmin, 20, 
740                                          &node->custom1, 0, 0, 0, 0, "");
741                 uiButSetFunc(bt, node_but_title_cb, node, bt);
742         }
743         return 20;
744 }
745
746 static int node_composit_buts_map_value(uiBlock *block, bNodeTree *ntree, bNode *node, rctf *butr)
747 {
748         if(block) {
749                 TexMapping *texmap= node->storage;
750                 short xstart= (short)butr->xmin;
751                 short dy= (short)(butr->ymax-19.0f);
752                 short dx= (short)(butr->xmax-butr->xmin)/2;
753                 
754                 uiBlockBeginAlign(block);
755                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Offs:", xstart, dy, 2*dx, 19, texmap->loc, -1000.0f, 1000.0f, 10, 2, "");
756                 dy-= 19;
757                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "Size:", xstart, dy, 2*dx, 19, texmap->size, -1000.0f, 1000.0f, 10, 3, "");
758                 dy-= 23;
759                 uiBlockBeginAlign(block);
760                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MIN, B_NODE_EXEC+node->nr, "Min", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
761                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->min, -1000.0f, 1000.0f, 10, 2, "");
762                 dy-= 19;
763                 uiDefButBitI(block, TOG, TEXMAP_CLIP_MAX, B_NODE_EXEC+node->nr, "Max", xstart, dy, dx, 19, &texmap->flag, 0.0f, 0.0f, 0, 0, "");
764                 uiDefButF(block, NUM, B_NODE_EXEC+node->nr, "", xstart+dx, dy, dx, 19, texmap->max, -1000.0f, 1000.0f, 10, 2, "");
765         }
766         return 80;
767 }
768
769
770 /* only once called */
771 static void node_composit_set_butfunc(bNodeType *ntype)
772 {
773         switch(ntype->type) {
774                 case NODE_GROUP:        /* note, generic type, but put here because we call this function anyway */
775                         ntype->butfunc= node_buts_group;
776                         break;
777                 case CMP_NODE_IMAGE:
778                         ntype->butfunc= node_composit_buts_image;
779                         break;
780                 case CMP_NODE_R_RESULT:
781                         ntype->butfunc= node_composit_buts_renderresult;
782                         break;
783                 case CMP_NODE_NORMAL:
784                         ntype->butfunc= node_buts_normal;
785                         break;
786                 case CMP_NODE_CURVE_VEC:
787                         ntype->butfunc= node_buts_curvevec;
788                         break;
789                 case CMP_NODE_CURVE_RGB:
790                         ntype->butfunc= node_buts_curvecol;
791                         break;
792                 case CMP_NODE_VALUE:
793                         ntype->butfunc= node_buts_value;
794                         break;
795                 case CMP_NODE_RGB:
796                         ntype->butfunc= node_buts_rgb;
797                         break;
798                 case CMP_NODE_MIX_RGB:
799                         ntype->butfunc= node_buts_mix_rgb;
800                         break;
801                 case CMP_NODE_VALTORGB:
802                         ntype->butfunc= node_buts_valtorgb;
803                         break;
804                 case CMP_NODE_BLUR:
805                         ntype->butfunc= node_composit_buts_blur;
806                         break;
807                 case CMP_NODE_FILTER:
808                         ntype->butfunc= node_composit_buts_filter;
809                         break;
810                 case CMP_NODE_MAP_VALUE:
811                         ntype->butfunc= node_composit_buts_map_value;
812                         break;
813                 default:
814                         ntype->butfunc= NULL;
815         }
816 }
817
818
819 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
820
821 void init_node_butfuncs(void)
822 {
823         bNodeType **typedefs;
824         
825         /* shader nodes */
826         typedefs= node_all_shaders;             /* BKE_node.h */
827         while( *typedefs) {
828                 node_shader_set_butfunc(*typedefs);
829                 typedefs++;
830         }
831         /* composit nodes */
832         typedefs= node_all_composit;            /* BKE_node.h */
833         while( *typedefs) {
834                 node_composit_set_butfunc(*typedefs);
835                 typedefs++;
836         }
837 }
838
839 /* ************** Generic drawing ************** */
840
841 static void draw_nodespace_grid(SpaceNode *snode)
842 {
843         float start, step= 25.0f;
844
845         BIF_ThemeColorShade(TH_BACK, -10);
846         
847         start= snode->v2d.cur.xmin -fmod(snode->v2d.cur.xmin, step);
848         
849         glBegin(GL_LINES);
850         for(; start<snode->v2d.cur.xmax; start+=step) {
851                 glVertex2f(start, snode->v2d.cur.ymin);
852                 glVertex2f(start, snode->v2d.cur.ymax);
853         }
854
855         start= snode->v2d.cur.ymin -fmod(snode->v2d.cur.ymin, step);
856         for(; start<snode->v2d.cur.ymax; start+=step) {
857                 glVertex2f(snode->v2d.cur.xmin, start);
858                 glVertex2f(snode->v2d.cur.xmax, start);
859         }
860         
861         /* X and Y axis */
862         BIF_ThemeColorShade(TH_BACK, -18);
863         glVertex2f(0.0f, snode->v2d.cur.ymin);
864         glVertex2f(0.0f, snode->v2d.cur.ymax);
865         glVertex2f(snode->v2d.cur.xmin, 0.0f);
866         glVertex2f(snode->v2d.cur.xmax, 0.0f);
867         
868         glEnd();
869 }
870
871
872 static void nodeshadow(rctf *rct, float radius, float aspect, int select)
873 {
874         float rad;
875         float a;
876         char alpha= 2;
877         
878         glEnable(GL_BLEND);
879         
880         if(radius > (rct->ymax-rct->ymin-10.0f)/2.0f)
881                 rad= (rct->ymax-rct->ymin-10.0f)/2.0f;
882         else
883                 rad= radius;
884         
885         if(select) a= 10.0f*aspect; else a= 7.0f*aspect;
886         for(; a>0.0f; a-=aspect) {
887                 /* alpha ranges from 2 to 20 or so */
888                 glColor4ub(0, 0, 0, alpha);
889                 alpha+= 2;
890                 
891                 gl_round_box(GL_POLYGON, rct->xmin - a, rct->ymin - a, rct->xmax + a, rct->ymax-10.0f + a, rad+a);
892         }
893         
894         /* outline emphasis */
895         glEnable( GL_LINE_SMOOTH );
896         glColor4ub(0, 0, 0, 100);
897         gl_round_box(GL_LINE_LOOP, rct->xmin-0.5f, rct->ymin-0.5f, rct->xmax+0.5f, rct->ymax+0.5f, radius);
898         glDisable( GL_LINE_SMOOTH );
899         
900         glDisable(GL_BLEND);
901 }
902
903 /* nice AA filled circle */
904 static void socket_circle_draw(float x, float y, float size, int type, int select)
905 {
906         /* 16 values of sin function */
907         static float si[16] = {
908                 0.00000000, 0.39435585,0.72479278,0.93775213,
909                 0.99871650,0.89780453,0.65137248,0.29936312,
910                 -0.10116832,-0.48530196,-0.79077573,-0.96807711,
911                 -0.98846832,-0.84864425,-0.57126821,-0.20129852
912         };
913         /* 16 values of cos function */
914         static float co[16] ={
915                 1.00000000,0.91895781,0.68896691,0.34730525,
916                 -0.05064916,-0.44039415,-0.75875812,-0.95413925,
917                 -0.99486932,-0.87434661,-0.61210598,-0.25065253,
918                 0.15142777,0.52896401,0.82076344,0.97952994,
919         };
920         int a;
921         
922         if(select==0) {
923                 if(type==-1)
924                         glColor3ub(0, 0, 0);
925                 else if(type==SOCK_VALUE)
926                         glColor3ub(160, 160, 160);
927                 else if(type==SOCK_VECTOR)
928                         glColor3ub(100, 100, 200);
929                 else if(type==SOCK_RGBA)
930                         glColor3ub(200, 200, 40);
931                 else 
932                         glColor3ub(100, 200, 100);
933         }
934         else {
935                 if(type==SOCK_VALUE)
936                         glColor3ub(200, 200, 200);
937                 else if(type==SOCK_VECTOR)
938                         glColor3ub(140, 140, 240);
939                 else if(type==SOCK_RGBA)
940                         glColor3ub(240, 240, 100);
941                 else 
942                         glColor3ub(140, 240, 140);
943         }
944         
945         glBegin(GL_POLYGON);
946         for(a=0; a<16; a++)
947                 glVertex2f(x+size*si[a], y+size*co[a]);
948         glEnd();
949         
950         glColor4ub(0, 0, 0, 150);
951         glEnable(GL_BLEND);
952         glEnable( GL_LINE_SMOOTH );
953         glBegin(GL_LINE_LOOP);
954         for(a=0; a<16; a++)
955                 glVertex2f(x+size*si[a], y+size*co[a]);
956         glEnd();
957         glDisable( GL_LINE_SMOOTH );
958         glDisable(GL_BLEND);
959 }
960
961 /* not a callback */
962 static void node_draw_preview(bNodePreview *preview, rctf *prv)
963 {
964         float scale= (prv->xmax-prv->xmin)/((float)preview->xsize);
965         float centx= prv->xmin + 0.5*(prv->xmax-prv->xmin);
966         float centy= prv->ymin + 0.5*(prv->ymax-prv->ymin);
967         
968         /* draw standard bacdrop to show alpha */
969         glBegin(GL_TRIANGLES);
970         glColor3f(0.625f, 0.625f, 0.625f);
971         glVertex2f(prv->xmin, prv->ymin);
972         glVertex2f(prv->xmax, prv->ymin);
973         glVertex2f(centx, centy);
974         glVertex2f(prv->xmin, prv->ymax);
975         glVertex2f(prv->xmax, prv->ymax);
976         glVertex2f(centx, centy);
977         
978         glColor3f(0.25f, 0.25f, 0.25f);
979         glVertex2f(prv->xmin, prv->ymin);
980         glVertex2f(prv->xmin, prv->ymax);
981         glVertex2f(centx, centy);
982         glVertex2f(prv->xmax, prv->ymin);
983         glVertex2f(prv->xmax, prv->ymax);
984         glVertex2f(centx, centy);
985         glEnd();
986         
987         glPixelZoom(scale, scale);
988         glEnable(GL_BLEND);
989         
990         glaDrawPixelsTex(prv->xmin, prv->ymin, preview->xsize, preview->ysize, GL_FLOAT, preview->rect);
991         
992         glDisable(GL_BLEND);
993         glPixelZoom(1.0f, 1.0f);
994         
995 }
996
997 /* based on settings in node, sets drawing rect info. each redraw! */
998 static void node_update_hidden(bNode *node)
999 {
1000         bNodeSocket *nsock;
1001         float rad, drad, hiddenrad= HIDDEN_RAD;
1002         int totin=0, totout=0, tot;
1003         
1004         /* calculate minimal radius */
1005         for(nsock= node->inputs.first; nsock; nsock= nsock->next)
1006                 if(!(nsock->flag & SOCK_HIDDEN))
1007                         totin++;
1008         for(nsock= node->outputs.first; nsock; nsock= nsock->next)
1009                 if(!(nsock->flag & SOCK_HIDDEN))
1010                         totout++;
1011         
1012         tot= MAX2(totin, totout);
1013         if(tot>4) {
1014                 hiddenrad += 5.0*(float)(tot-4);
1015         }
1016         
1017         node->totr.xmin= node->locx;
1018         node->totr.xmax= node->locx + 3*hiddenrad + node->miniwidth;
1019         node->totr.ymax= node->locy + (hiddenrad - 0.5f*NODE_DY);
1020         node->totr.ymin= node->totr.ymax - 2*hiddenrad;
1021         
1022         /* output sockets */
1023         rad=drad= M_PI/(1.0f + (float)totout);
1024         
1025         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
1026                 if(!(nsock->flag & SOCK_HIDDEN)) {
1027                         nsock->locx= node->totr.xmax - hiddenrad + sin(rad)*hiddenrad;
1028                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
1029                         rad+= drad;
1030                 }
1031         }
1032         
1033         /* input sockets */
1034         rad=drad= - M_PI/(1.0f + (float)totin);
1035         
1036         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
1037                 if(!(nsock->flag & SOCK_HIDDEN)) {
1038                         nsock->locx= node->totr.xmin + hiddenrad + sin(rad)*hiddenrad;
1039                         nsock->locy= node->totr.ymin + hiddenrad + cos(rad)*hiddenrad;
1040                         rad+= drad;
1041                 }
1042         }
1043 }
1044
1045 /* based on settings in node, sets drawing rect info. each redraw! */
1046 static void node_update(bNode *node)
1047 {
1048         bNodeSocket *nsock;
1049         float dy= node->locy;
1050         
1051         /* header */
1052         dy-= NODE_DY;
1053         
1054         /* little bit space in top */
1055         if(node->outputs.first)
1056                 dy-= NODE_DYS/2;
1057         
1058         /* output sockets */
1059         for(nsock= node->outputs.first; nsock; nsock= nsock->next) {
1060                 if(!(nsock->flag & SOCK_HIDDEN)) {
1061                         nsock->locx= node->locx + node->width;
1062                         nsock->locy= dy - NODE_DYS;
1063                         dy-= NODE_DY;
1064                 }
1065         }
1066         
1067         node->prvr.xmin= node->butr.xmin= node->locx + NODE_DYS;
1068         node->prvr.xmax= node->butr.xmax= node->locx + node->width- NODE_DYS;
1069         
1070         /* preview rect? */
1071         if(node->flag & NODE_PREVIEW) {
1072                 /* only recalculate size when there's a preview actually, otherwise we use stored result */
1073                 if(node->preview && node->preview->rect) {
1074                         float aspect= 1.0f;
1075                         
1076                         if(node->preview && node->preview->xsize && node->preview->ysize) 
1077                                 aspect= (float)node->preview->ysize/(float)node->preview->xsize;
1078                         
1079                         dy-= NODE_DYS/2;
1080                         node->prvr.ymax= dy;
1081                         node->prvr.ymin= dy - aspect*(node->width-NODE_DY);
1082                         dy= node->prvr.ymin - NODE_DYS/2;
1083                 }
1084                 else {
1085                         float oldh= node->prvr.ymax - node->prvr.ymin;
1086                         if(oldh==0.0f)
1087                                 oldh= node->width-NODE_DY;
1088                         dy-= NODE_DYS/2;
1089                         node->prvr.ymax= dy;
1090                         node->prvr.ymin= dy - oldh;
1091                         dy= node->prvr.ymin - NODE_DYS/2;
1092                 }
1093         }
1094         
1095         /* buttons rect? */
1096         if((node->flag & NODE_OPTIONS) && node->typeinfo->butfunc) {
1097                 dy-= NODE_DYS/2;
1098                 node->butr.ymax= dy;
1099                 node->butr.ymin= dy - (float)node->typeinfo->butfunc(NULL, NULL, node, NULL);
1100                 dy= node->butr.ymin - NODE_DYS/2;
1101         }
1102         
1103         /* input sockets */
1104         for(nsock= node->inputs.first; nsock; nsock= nsock->next) {
1105                 if(!(nsock->flag & SOCK_HIDDEN)) {
1106                         nsock->locx= node->locx;
1107                         nsock->locy= dy - NODE_DYS;
1108                         dy-= NODE_DY;
1109                 }
1110         }
1111         
1112         /* little bit space in end */
1113         if(node->inputs.first || (node->flag & (NODE_OPTIONS|NODE_PREVIEW))==0 )
1114                 dy-= NODE_DYS/2;
1115         
1116         node->totr.xmin= node->locx;
1117         node->totr.xmax= node->locx + node->width;
1118         node->totr.ymax= node->locy;
1119         node->totr.ymin= dy;
1120 }
1121
1122 /* based on settings in node, sets drawing rect info. each redraw! */
1123 /* note: this assumes only 1 group at a time is drawn (linked data) */
1124 /* in node->totr the entire boundbox for the group is stored */
1125 static void node_update_group(bNode *gnode)
1126 {
1127         bNodeTree *ngroup= (bNodeTree *)gnode->id;
1128         bNode *node;
1129         bNodeSocket *nsock;
1130         rctf *rect= &gnode->totr;
1131         int counter;
1132         
1133         /* center them, is a bit of abuse of locx and locy though */
1134         for(node= ngroup->nodes.first; node; node= node->next) {
1135                 node->locx+= gnode->locx;
1136                 node->locy+= gnode->locy;
1137                 if(node->flag & NODE_HIDDEN)
1138                         node_update_hidden(node);
1139                 else
1140                         node_update(node);
1141                 node->locx-= gnode->locx;
1142                 node->locy-= gnode->locy;
1143         }
1144         counter= 1;
1145         for(node= ngroup->nodes.first; node; node= node->next) {
1146                 if(counter) {
1147                         *rect= node->totr;
1148                         counter= 0;
1149                 }
1150                 else
1151                         BLI_union_rctf(rect, &node->totr);
1152         }
1153         if(counter==1) return;  /* should be prevented? */
1154         
1155         rect->xmin-= NODE_DY;
1156         rect->ymin-= NODE_DY;
1157         rect->xmax+= NODE_DY;
1158         rect->ymax+= NODE_DY;
1159         
1160         /* output sockets */
1161         for(nsock= gnode->outputs.first; nsock; nsock= nsock->next) {
1162                 nsock->locx= rect->xmax;
1163                 nsock->locy= nsock->tosock->locy;
1164         }
1165         
1166         /* input sockets */
1167         for(nsock= gnode->inputs.first; nsock; nsock= nsock->next) {
1168                 nsock->locx= rect->xmin;
1169                 nsock->locy= nsock->tosock->locy;
1170         }
1171 }
1172
1173 static void node_scaling_widget(int color_id, float aspect, float xmin, float ymin, float xmax, float ymax)
1174 {
1175         float dx;
1176         float dy;
1177         
1178         dx= 0.5f*(xmax-xmin);
1179         dy= 0.5f*(ymax-ymin);
1180         
1181         BIF_ThemeColorShade(color_id, +30);     
1182         fdrawline(xmin, ymin, xmax, ymax);
1183         fdrawline(xmin+dx, ymin, xmax, ymax-dy);
1184         
1185         BIF_ThemeColorShade(color_id, -10);
1186         fdrawline(xmin, ymin+aspect, xmax, ymax+aspect);
1187         fdrawline(xmin+dx, ymin+aspect, xmax, ymax-dy+aspect);
1188 }
1189
1190 static int node_get_colorid(bNode *node)
1191 {
1192         if(node->typeinfo->nclass==NODE_CLASS_INPUT)
1193                 return TH_NODE_IN_OUT;
1194         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1195                 if(node->flag & NODE_DO_OUTPUT)
1196                         return TH_NODE_IN_OUT;
1197                 else
1198                         return TH_NODE;
1199         }
1200         if(node->typeinfo->nclass==NODE_CLASS_GENERATOR)
1201                 return TH_NODE_GENERATOR;
1202         if(node->typeinfo->nclass==NODE_CLASS_OPERATOR)
1203                 return TH_NODE_OPERATOR;
1204         if(node->typeinfo->nclass==NODE_CLASS_GROUP)
1205                 return TH_NODE_GROUP;
1206         return TH_NODE;
1207 }
1208
1209 static void node_draw_basis(ScrArea *sa, SpaceNode *snode, bNode *node)
1210 {
1211         bNodeSocket *sock;
1212         uiBut *bt;
1213         rctf *rct= &node->totr;
1214         float slen, iconofs;
1215         int ofs, color_id= node_get_colorid(node);
1216         
1217         uiSetRoundBox(15-4);
1218         nodeshadow(rct, BASIS_RAD, snode->aspect, node->flag & SELECT);
1219         
1220         /* header */
1221         if(color_id==TH_NODE)
1222                 BIF_ThemeColorShade(color_id, -20);
1223         else
1224                 BIF_ThemeColor(color_id);
1225                 
1226         uiSetRoundBox(3);
1227         uiRoundBox(rct->xmin, rct->ymax-NODE_DY, rct->xmax, rct->ymax, BASIS_RAD);
1228         
1229         /* show/hide icons, note this sequence is copied in editnode.c */
1230         iconofs= rct->xmax;
1231         
1232         if(node->typeinfo->flag & NODE_PREVIEW) {
1233                 int icon_id;
1234                 
1235                 if(node->flag & (NODE_ACTIVE_ID|NODE_DO_OUTPUT))
1236                         icon_id= ICON_MATERIAL;
1237                 else
1238                         icon_id= ICON_MATERIAL_DEHLT;
1239                 iconofs-= 18.0f;
1240                 glEnable(GL_BLEND);
1241                 BIF_icon_set_aspect(icon_id, snode->aspect);
1242                 BIF_icon_draw_blended(iconofs, rct->ymax-NODE_DY+2, icon_id, 0, -60);
1243                 glDisable(GL_BLEND);
1244         }
1245         if(node->type == NODE_GROUP) {
1246                 iconofs-= 18.0f;
1247                 glEnable(GL_BLEND);
1248                 BIF_icon_set_aspect(ICON_NODE, snode->aspect);
1249                 BIF_icon_draw_blended(iconofs, rct->ymax-NODE_DY+2, ICON_NODE, 0, -60);
1250                 glDisable(GL_BLEND);
1251         }
1252         if(node->typeinfo->flag & NODE_OPTIONS) {
1253                 iconofs-= 18.0f;
1254                 glEnable(GL_BLEND);
1255                 BIF_icon_set_aspect(ICON_BUTS, snode->aspect);
1256                 BIF_icon_draw_blended(iconofs, rct->ymax-NODE_DY+2, ICON_BUTS, 0, -60);
1257                 glDisable(GL_BLEND);
1258         }
1259         if(node->outputs.first) {
1260                 int shade;
1261                 /* socket selector */
1262                 iconofs-= 18.0f;
1263                 if(node_has_hidden_sockets(node))
1264                         shade= -40;
1265                 else
1266                         shade= -90;
1267                 glEnable(GL_BLEND);
1268                 BIF_icon_set_aspect(ICON_PLUS, snode->aspect);
1269                 BIF_icon_draw_blended(iconofs, rct->ymax-NODE_DY+2, ICON_PLUS, 0, shade);
1270                 glDisable(GL_BLEND);
1271         }
1272         
1273         /* title */
1274         if(node->flag & SELECT) 
1275                 BIF_ThemeColor(TH_TEXT_HI);
1276         else
1277                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
1278         
1279         /* open/close entirely? */
1280         ui_draw_tria_icon(rct->xmin+8.0f, rct->ymax-NODE_DY+4.0f, snode->aspect, 'v');
1281
1282         if(node->flag & SELECT) 
1283                 BIF_ThemeColor(TH_TEXT_HI);
1284         else
1285                 BIF_ThemeColor(TH_TEXT);
1286         
1287         ui_rasterpos_safe(rct->xmin+19.0f, rct->ymax-NODE_DY+5.0f, snode->aspect);
1288         snode_drawstring(snode, node->name, (int)(iconofs - rct->xmin-18.0f));
1289                                          
1290         /* body */
1291         BIF_ThemeColor(TH_NODE);        
1292         uiSetRoundBox(8);
1293         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax-NODE_DY, BASIS_RAD);
1294         
1295         /* scaling indicator */
1296         node_scaling_widget(TH_NODE, snode->aspect, rct->xmax-BASIS_RAD*snode->aspect, rct->ymin, rct->xmax, rct->ymin+BASIS_RAD*snode->aspect);
1297
1298         /* outline active emphasis */
1299         if(node->flag & NODE_ACTIVE) {
1300                 glEnable(GL_BLEND);
1301                 glColor4ub(200, 200, 200, 140);
1302                 uiSetRoundBox(15-4);
1303                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
1304                 glDisable(GL_BLEND);
1305         }
1306         
1307         /* we make buttons for input sockets, if... */
1308         if(node->flag & NODE_OPTIONS) {
1309                 if(node->inputs.first || node->typeinfo->butfunc) {
1310                         uiBlock *block= uiNewBlock(NULL, "node buttons", UI_EMBOSS, UI_HELV, sa->win);
1311                         BLI_addtail(&sa->uiblocks, block);
1312                         uiBlockSetFlag(block, UI_BLOCK_NO_HILITE);
1313                         if(snode->id)
1314                                 uiSetButLock(snode->id->lib!=NULL, "Can't edit library data");
1315                         node->block= block;
1316                 }
1317         }
1318         
1319         /* hurmf... another candidate for callback, have to see how this works first */
1320         if(node->id && node->block && snode->treetype==NTREE_SHADER)
1321                 nodeShaderSynchronizeID(node, 0);
1322         
1323         /* socket inputs, buttons */
1324         for(sock= node->inputs.first; sock; sock= sock->next) {
1325                 if(!(sock->flag & SOCK_HIDDEN)) {
1326                         socket_circle_draw(sock->locx, sock->locy, NODE_SOCKSIZE, sock->type, sock->flag & SELECT);
1327                         
1328                         if(node->block && sock->link==NULL) {
1329                                 float *butpoin= sock->ns.vec;
1330                                 
1331                                 if(node->type==NODE_GROUP && sock->tosock)
1332                                         butpoin= sock->tosock->ns.vec;
1333                                 
1334                                 if(sock->type==SOCK_VALUE) {
1335                                         bt= uiDefButF(node->block, NUM, B_NODE_EXEC+node->nr, sock->name, 
1336                                                   (short)sock->locx+NODE_DYS, (short)(sock->locy)-9, (short)node->width-NODE_DY, 17, 
1337                                                   butpoin, 0.0f, 1.0f, 10, 2, "");
1338                                         uiButSetFunc(bt, node_sync_cb, snode, node);
1339                                 }
1340                                 else if(sock->type==SOCK_VECTOR) {
1341                                         uiDefBlockBut(node->block, socket_vector_menu, butpoin, sock->name, 
1342                                                   (short)sock->locx+NODE_DYS, (short)sock->locy-9, (short)node->width-NODE_DY, 17, 
1343                                                   "");
1344                                 }
1345                                 else if(node->block && sock->type==SOCK_RGBA) {
1346                                         short labelw= node->width-NODE_DY-40, width;
1347                                         
1348                                         if(labelw>0) width= 40; else width= node->width-NODE_DY;
1349                                         
1350                                         bt= uiDefButF(node->block, COL, B_NODE_EXEC+node->nr, "", 
1351                                                 (short)(sock->locx+NODE_DYS), (short)sock->locy-8, width, 15, 
1352                                                    butpoin, 0, 0, 0, 0, "");
1353                                         uiButSetFunc(bt, node_sync_cb, snode, node);
1354                                         
1355                                         if(labelw>0) uiDefBut(node->block, LABEL, 0, sock->name, 
1356                                                                                    (short)(sock->locx+NODE_DYS) + 40, (short)sock->locy-8, labelw, 15, 
1357                                                                                    NULL, 0, 0, 0, 0, "");
1358                                 }
1359                         }
1360                         else {
1361                                 BIF_ThemeColor(TH_TEXT);
1362                                 ui_rasterpos_safe(sock->locx+8.0f, sock->locy-5.0f, snode->aspect);
1363                                 BIF_DrawString(snode->curfont, sock->name, 0);
1364                         }
1365                 }
1366         }
1367         
1368         /* socket outputs */
1369         for(sock= node->outputs.first; sock; sock= sock->next) {
1370                 if(!(sock->flag & SOCK_HIDDEN)) {
1371                         socket_circle_draw(sock->locx, sock->locy, NODE_SOCKSIZE, sock->type, sock->flag & SELECT);
1372                         
1373                         BIF_ThemeColor(TH_TEXT);
1374                         ofs= 0;
1375                         slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name, 0);
1376                         while(slen > node->width) {
1377                                 ofs++;
1378                                 slen= snode->aspect*BIF_GetStringWidth(snode->curfont, sock->name+ofs, 0);
1379                         }
1380                         ui_rasterpos_safe(sock->locx-8.0f-slen, sock->locy-5.0f, snode->aspect);
1381                         BIF_DrawString(snode->curfont, sock->name+ofs, 0);
1382                 }
1383         }
1384         
1385         /* preview */
1386         if(node->flag & NODE_PREVIEW)
1387                 if(node->preview && node->preview->rect)
1388                         node_draw_preview(node->preview, &node->prvr);
1389                 
1390         /* buttons */
1391         if(node->flag & NODE_OPTIONS) {
1392                 if(node->typeinfo->butfunc) {
1393                         node->typeinfo->butfunc(node->block, snode->nodetree, node, &node->butr);
1394                 }
1395                 uiDrawBlock(node->block);
1396         }
1397
1398 }
1399
1400 void node_draw_hidden(SpaceNode *snode, bNode *node)
1401 {
1402         bNodeSocket *sock;
1403         rctf *rct= &node->totr;
1404         float dx, centy= 0.5f*(rct->ymax+rct->ymin);
1405         float hiddenrad= 0.5f*(rct->ymax-rct->ymin);
1406         int color_id= node_get_colorid(node);
1407         
1408         /* shadow */
1409         uiSetRoundBox(15);
1410         nodeshadow(rct, hiddenrad, snode->aspect, node->flag & SELECT);
1411
1412         /* body */
1413         BIF_ThemeColor(color_id);       
1414         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad);
1415         
1416         /* outline active emphasis */
1417         if(node->flag & NODE_ACTIVE) {
1418                 glEnable(GL_BLEND);
1419                 glColor4ub(200, 200, 200, 140);
1420                 gl_round_box(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad);
1421                 glDisable(GL_BLEND);
1422         }
1423         
1424         /* title */
1425         if(node->flag & SELECT) 
1426                 BIF_ThemeColor(TH_TEXT_HI);
1427         else
1428                 BIF_ThemeColorBlendShade(TH_TEXT, color_id, 0.4, 10);
1429         
1430         /* open entirely icon */
1431         ui_draw_tria_icon(rct->xmin+9.0f, centy-6.0f, snode->aspect, 'h');      
1432         
1433         if(node->flag & SELECT) 
1434                 BIF_ThemeColor(TH_TEXT_HI);
1435         else
1436                 BIF_ThemeColor(TH_TEXT);
1437         
1438         if(node->miniwidth>0.0f) {
1439                 ui_rasterpos_safe(rct->xmin+21.0f, centy-4.0f, snode->aspect);
1440                 snode_drawstring(snode, node->name, (int)(rct->xmax - rct->xmin-18.0f -12.0f));
1441         }       
1442
1443         /* scale widget thing */
1444         BIF_ThemeColorShade(color_id, -10);     
1445         dx= 10.0f;
1446         fdrawline(rct->xmax-dx, centy-4.0f, rct->xmax-dx, centy+4.0f);
1447         fdrawline(rct->xmax-dx-3.0f*snode->aspect, centy-4.0f, rct->xmax-dx-3.0f*snode->aspect, centy+4.0f);
1448         
1449         BIF_ThemeColorShade(color_id, +30);
1450         dx-= snode->aspect;
1451         fdrawline(rct->xmax-dx, centy-4.0f, rct->xmax-dx, centy+4.0f);
1452         fdrawline(rct->xmax-dx-3.0f*snode->aspect, centy-4.0f, rct->xmax-dx-3.0f*snode->aspect, centy+4.0f);
1453         
1454         /* sockets */
1455         for(sock= node->inputs.first; sock; sock= sock->next) {
1456                 if(!(sock->flag & SOCK_HIDDEN))
1457                         socket_circle_draw(sock->locx, sock->locy, NODE_SOCKSIZE, sock->type, sock->flag & SELECT);
1458         }
1459         
1460         for(sock= node->outputs.first; sock; sock= sock->next) {
1461                 if(!(sock->flag & SOCK_HIDDEN))
1462                         socket_circle_draw(sock->locx, sock->locy, NODE_SOCKSIZE, sock->type, sock->flag & SELECT);
1463         }
1464 }
1465
1466 /* note; this is used for fake links in groups too */
1467 void node_draw_link(SpaceNode *snode, bNodeLink *link)
1468 {
1469         float vec[4][3];
1470         float dist, spline_step, mx=0.0f, my=0.0f;
1471         int curve_res, do_shaded= 1, th_col1= TH_WIRE, th_col2= TH_WIRE;
1472         
1473         if(link->fromnode==NULL && link->tonode==NULL)
1474                 return;
1475         
1476         /* this is dragging link */
1477         if(link->fromnode==NULL || link->tonode==NULL) {
1478                 short mval[2];
1479                 getmouseco_areawin(mval);
1480                 areamouseco_to_ipoco(G.v2d, mval, &mx, &my);
1481                 BIF_ThemeColor(TH_WIRE);
1482                 do_shaded= 0;
1483         }
1484         else {
1485                 /* a bit ugly... but thats how we detect the internal group links */
1486                 if(link->fromnode==link->tonode) {
1487                         BIF_ThemeColorBlend(TH_BACK, TH_WIRE, 0.25f);
1488                         do_shaded= 0;
1489                 }
1490                 else {
1491                         /* check cyclic */
1492                         if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
1493                                 if(link->fromnode->flag & SELECT)
1494                                         th_col1= TH_EDGE_SELECT;
1495                                 if(link->tonode->flag & SELECT)
1496                                         th_col2= TH_EDGE_SELECT;
1497                         }                               
1498                         else {
1499                                 BIF_ThemeColor(TH_REDALERT);
1500                                 do_shaded= 0;
1501                         }
1502                 }
1503         }
1504         
1505         vec[0][2]= vec[1][2]= vec[2][2]= vec[3][2]= 0.0; /* only 2d spline, set the Z to 0*/
1506         
1507         /* in v0 and v3 we put begin/end points */
1508         if(link->fromnode) {
1509                 vec[0][0]= link->fromsock->locx;
1510                 vec[0][1]= link->fromsock->locy;
1511         }
1512         else {
1513                 vec[0][0]= mx;
1514                 vec[0][1]= my;
1515         }
1516         if(link->tonode) {
1517                 vec[3][0]= link->tosock->locx;
1518                 vec[3][1]= link->tosock->locy;
1519         }
1520         else {
1521                 vec[3][0]= mx;
1522                 vec[3][1]= my;
1523         }
1524         
1525         dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
1526         
1527         /* check direction later, for top sockets */
1528         vec[1][0]= vec[0][0]+dist;
1529         vec[1][1]= vec[0][1];
1530         
1531         vec[2][0]= vec[3][0]-dist;
1532         vec[2][1]= vec[3][1];
1533         
1534         if( MIN4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) > G.v2d->cur.xmax); /* clipped */  
1535         else if ( MAX4(vec[0][0], vec[1][0], vec[2][0], vec[3][0]) < G.v2d->cur.xmin); /* clipped */
1536         else {
1537                 curve_res = 24;
1538                 
1539                 /* we can reuse the dist variable here to increment the GL curve eval amount*/
1540                 dist = 1.0f/curve_res;
1541                 spline_step = 0.0f;
1542                 
1543                 glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, vec[0]);
1544                 glBegin(GL_LINE_STRIP);
1545                 while (spline_step < 1.000001f) {
1546                         if(do_shaded)
1547                                 BIF_ThemeColorBlend(th_col1, th_col2, spline_step);
1548                         glEvalCoord1f(spline_step);
1549                         spline_step += dist;
1550                 }
1551                 glEnd();
1552         }
1553 }
1554
1555 static void node_draw_nodetree(ScrArea *sa, SpaceNode *snode, bNodeTree *ntree)
1556 {
1557         bNode *node;
1558         bNodeLink *link;
1559         int a;
1560         
1561         if(ntree==NULL) return;         /* groups... */
1562         
1563         /* node lines */
1564         glEnable(GL_BLEND);
1565         glEnable( GL_LINE_SMOOTH );
1566         for(link= ntree->links.first; link; link= link->next)
1567                 node_draw_link(snode, link);
1568         glDisable(GL_BLEND);
1569         glDisable( GL_LINE_SMOOTH );
1570         
1571         /* not selected first */
1572         for(a=0, node= ntree->nodes.first; node; node= node->next, a++) {
1573                 node->block= NULL;      /* were freed */
1574                 node->nr= a;            /* index of node in list, used for exec event code */
1575                 if(!(node->flag & SELECT)) {
1576                         if(node->flag & NODE_GROUP_EDIT);
1577                         else if(node->flag & NODE_HIDDEN)
1578                                 node_draw_hidden(snode, node);
1579                         else
1580                                 node_draw_basis(sa, snode, node);
1581                 }
1582         }
1583         
1584         /* selected */
1585         for(node= ntree->nodes.first; node; node= node->next) {
1586                 if(node->flag & SELECT) {
1587                         if(node->flag & NODE_GROUP_EDIT);
1588                         else if(node->flag & NODE_HIDDEN)
1589                                 node_draw_hidden(snode, node);
1590                         else
1591                                 node_draw_basis(sa, snode, node);
1592                 }
1593         }       
1594 }
1595
1596 /* fake links from groupnode to internal nodes */
1597 static void node_draw_group_links(SpaceNode *snode, bNode *gnode)
1598 {
1599         bNodeLink fakelink;
1600         bNodeSocket *sock;
1601         
1602         glEnable(GL_BLEND);
1603         glEnable( GL_LINE_SMOOTH );
1604         
1605         fakelink.tonode= fakelink.fromnode= gnode;
1606         
1607         for(sock= gnode->inputs.first; sock; sock= sock->next) {
1608                 if(!(sock->flag & SOCK_HIDDEN)) {
1609                         if(sock->tosock) {
1610                                 fakelink.fromsock= sock;
1611                                 fakelink.tosock= sock->tosock;
1612                                 node_draw_link(snode, &fakelink);
1613                         }
1614                 }
1615         }
1616         
1617         for(sock= gnode->outputs.first; sock; sock= sock->next) {
1618                 if(!(sock->flag & SOCK_HIDDEN)) {
1619                         if(sock->tosock) {
1620                                 fakelink.tosock= sock;
1621                                 fakelink.fromsock= sock->tosock;
1622                                 node_draw_link(snode, &fakelink);
1623                         }
1624                 }
1625         }
1626         
1627         glDisable(GL_BLEND);
1628         glDisable( GL_LINE_SMOOTH );
1629 }
1630
1631 /* groups are, on creation, centered around 0,0 */
1632 static void node_draw_group(ScrArea *sa, SpaceNode *snode, bNode *gnode)
1633 {
1634         bNodeTree *ngroup= (bNodeTree *)gnode->id;
1635         bNodeSocket *sock;
1636         rctf rect= gnode->totr;
1637         
1638         /* backdrop header */
1639         glEnable(GL_BLEND);
1640         uiSetRoundBox(3);
1641         BIF_ThemeColorShadeAlpha(TH_NODE_GROUP, 0, -70);
1642         gl_round_box(GL_POLYGON, rect.xmin, rect.ymax, rect.xmax, rect.ymax+NODE_DY, BASIS_RAD);
1643         
1644         /* backdrop body */
1645         BIF_ThemeColorShadeAlpha(TH_BACK, -8, -70);
1646         uiSetRoundBox(12);
1647         gl_round_box(GL_POLYGON, rect.xmin, rect.ymin, rect.xmax, rect.ymax, BASIS_RAD);
1648         
1649         /* selection outline */
1650         uiSetRoundBox(15);
1651         glColor4ub(200, 200, 200, 140);
1652         glEnable( GL_LINE_SMOOTH );
1653         gl_round_box(GL_LINE_LOOP, rect.xmin, rect.ymin, rect.xmax, rect.ymax+NODE_DY, BASIS_RAD);
1654         glDisable( GL_LINE_SMOOTH );
1655         glDisable(GL_BLEND);
1656         
1657         /* backdrop title */
1658         BIF_ThemeColor(TH_TEXT_HI);
1659         ui_rasterpos_safe(rect.xmin+8.0f, rect.ymax+5.0f, snode->aspect);
1660         BIF_DrawString(snode->curfont, ngroup->id.name+2, 0);
1661         
1662         /* links from groupsockets to the internal nodes */
1663         node_draw_group_links(snode, gnode);
1664         
1665         /* group sockets */
1666         for(sock= gnode->inputs.first; sock; sock= sock->next)
1667                 if(!(sock->flag & SOCK_HIDDEN))
1668                         socket_circle_draw(sock->locx, sock->locy, NODE_SOCKSIZE, sock->type, sock->flag & SELECT);
1669         for(sock= gnode->outputs.first; sock; sock= sock->next)
1670                 if(!(sock->flag & SOCK_HIDDEN))
1671                         socket_circle_draw(sock->locx, sock->locy, NODE_SOCKSIZE, sock->type, sock->flag & SELECT);
1672
1673         /* and finally the whole tree */
1674         node_draw_nodetree(sa, snode, ngroup);
1675 }
1676
1677
1678 void drawnodespace(ScrArea *sa, void *spacedata)
1679 {
1680         SpaceNode *snode= sa->spacedata.first;
1681         float col[3];
1682         
1683         BIF_GetThemeColor3fv(TH_BACK, col);
1684         glClearColor(col[0], col[1], col[2], 0.0);
1685         glClear(GL_COLOR_BUFFER_BIT);
1686
1687         calc_scrollrcts(sa, &(snode->v2d), sa->winx, sa->winy);
1688         
1689         myortho2(snode->v2d.cur.xmin, snode->v2d.cur.xmax, snode->v2d.cur.ymin, snode->v2d.cur.ymax);
1690         bwin_clear_viewmat(sa->win);    /* clear buttons view */
1691         glLoadIdentity();
1692         
1693         /* always free, blocks here have no unique identifier (1 block per node) */
1694         uiFreeBlocksWin(&sa->uiblocks, sa->win);
1695
1696         /* only set once */
1697         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1698         glEnable(GL_MAP1_VERTEX_3);
1699
1700         /* aspect+font, set each time */
1701         snode->aspect= (snode->v2d.cur.xmax - snode->v2d.cur.xmin)/((float)sa->winx);
1702         snode->curfont= uiSetCurFont_ext(snode->aspect);
1703
1704         /* backdrop */
1705         draw_nodespace_grid(snode);
1706         
1707         /* nodes */
1708         snode_set_context(snode);
1709         
1710         if(snode->nodetree) {
1711                 bNode *node;
1712                 
1713                 /* for now, we set drawing coordinates on each redraw */
1714                 for(node= snode->nodetree->nodes.first; node; node= node->next) {
1715                         if(node->flag & NODE_GROUP_EDIT)
1716                                 node_update_group(node);
1717                         else if(node->flag & NODE_HIDDEN)
1718                                 node_update_hidden(node);
1719                         else
1720                                 node_update(node);
1721                 }
1722
1723                 node_draw_nodetree(sa, snode, snode->nodetree);
1724                         
1725                 /* active group */
1726                 for(node= snode->nodetree->nodes.first; node; node= node->next) {
1727                         if(node->flag & NODE_GROUP_EDIT)
1728                                 node_draw_group(sa, snode, node);
1729                 }
1730         }
1731         
1732         /* restore viewport (not needed yet) */
1733         mywinset(sa->win);
1734
1735         /* ortho at pixel level curarea */
1736         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
1737
1738         draw_area_emboss(sa);
1739         curarea->win_swap= WIN_BACK_OK;
1740         
1741         /* in the end, this is a delayed previewrender test, to allow buttons to be first */
1742         if(snode->flag & SNODE_DO_PREVIEW) {
1743                 addafterqueue(sa->win, RENDERPREVIEW, 1);
1744                 snode->flag &= ~SNODE_DO_PREVIEW;
1745         }
1746 }