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