Merging r48257 through r48263 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_node / drawnode.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Bob Holcomb, Thomas Dinges
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_node/drawnode.c
29  *  \ingroup spnode
30  */
31
32
33 #include <math.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_utildefines.h"
40
41 #include "DNA_node_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_screen_types.h"
47
48 #include "BKE_context.h"
49 #include "BKE_curve.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_library.h"
53 #include "BKE_main.h"
54 #include "BKE_node.h"
55 #include "BKE_tracking.h"
56
57 #include "BLF_api.h"
58 #include "BLF_translation.h"
59
60 #include "NOD_composite.h"
61 #include "NOD_shader.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h"
65
66 #include "BLF_api.h"
67
68 #include "MEM_guardedalloc.h"
69
70
71 #include "RNA_access.h"
72
73 #include "ED_node.h"
74
75 #include "WM_api.h"
76 #include "WM_types.h"
77
78 #include "UI_interface.h"
79 #include "UI_resources.h"
80
81 #include "IMB_imbuf.h"
82 #include "IMB_imbuf_types.h"
83
84 #include "node_intern.h"
85
86 // XXX interface.h
87 extern void ui_dropshadow(rctf *rct, float radius, float aspect, float alpha, int select);
88
89 /* ****************** SOCKET BUTTON DRAW FUNCTIONS ***************** */
90
91 static void node_sync_cb(bContext *UNUSED(C), void *snode_v, void *node_v)
92 {
93         SpaceNode *snode = snode_v;
94         
95         if (snode->treetype == NTREE_SHADER) {
96                 nodeShaderSynchronizeID(node_v, 1);
97                 // allqueue(REDRAWBUTSSHADING, 0);
98         }
99 }
100
101 static void node_socket_button_label(const bContext *UNUSED(C), uiBlock *block,
102                                      bNodeTree *UNUSED(ntree), bNode *UNUSED(node), bNodeSocket *sock,
103                                      const char *UNUSED(name), int x, int y, int width)
104 {
105         uiDefBut(block, LABEL, 0, IFACE_(sock->name), x, y, width, NODE_DY, NULL, 0, 0, 0, 0, "");
106 }
107
108 static void node_socket_button_default(const bContext *C, uiBlock *block,
109                                        bNodeTree *ntree, bNode *node, bNodeSocket *sock,
110                                        const char *name, int x, int y, int width)
111 {
112         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
113                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
114         else {
115                 PointerRNA ptr;
116                 uiBut *bt;
117                 
118                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
119                 
120                 bt = uiDefButR(block, NUM, B_NODE_EXEC, IFACE_(name),
121                                x, y + 1, width, NODE_DY - 2,
122                                &ptr, "default_value", 0, 0, 0, -1, -1, NULL);
123                 if (node)
124                         uiButSetFunc(bt, node_sync_cb, CTX_wm_space_node(C), node);
125         }
126 }
127
128 typedef struct SocketComponentMenuArgs {
129         PointerRNA ptr;
130         int x, y, width;
131         uiButHandleFunc cb;
132         void *arg1, *arg2;
133 } SocketComponentMenuArgs;
134 /* NOTE: this is a block-menu, needs 0 events, otherwise the menu closes */
135 static uiBlock *socket_component_menu(bContext *C, ARegion *ar, void *args_v)
136 {
137         SocketComponentMenuArgs *args = (SocketComponentMenuArgs *)args_v;
138         uiBlock *block;
139         uiLayout *layout;
140         
141         block = uiBeginBlock(C, ar, __func__, UI_EMBOSS);
142         uiBlockSetFlag(block, UI_BLOCK_KEEP_OPEN);
143         
144         layout = uiLayoutColumn(uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, args->x, args->y + 2, args->width, NODE_DY, UI_GetStyle()), FALSE);
145         
146         uiItemR(layout, &args->ptr, "default_value", UI_ITEM_R_EXPAND, "", ICON_NONE);
147         
148         return block;
149 }
150 static void node_socket_button_components(const bContext *C, uiBlock *block,
151                                           bNodeTree *ntree, bNode *node, bNodeSocket *sock,
152                                           const char *name, int x, int y, int width)
153 {
154         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
155                 node_socket_button_label(C, block, ntree, node, sock, name, x, y, width);
156         else {
157                 PointerRNA ptr;
158                 SocketComponentMenuArgs *args;
159                 
160                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
161                 
162                 args = MEM_callocN(sizeof(SocketComponentMenuArgs), "SocketComponentMenuArgs");
163                 
164                 args->ptr = ptr;
165                 args->x = x;
166                 args->y = y;
167                 args->width = width;
168                 args->cb = node_sync_cb;
169                 args->arg1 = CTX_wm_space_node(C);
170                 args->arg2 = node;
171                 
172                 uiDefBlockButN(block, socket_component_menu, args, IFACE_(name), x, y + 1, width, NODE_DY - 2, "");
173         }
174 }
175
176 static void node_socket_button_color(const bContext *C, uiBlock *block,
177                                      bNodeTree *ntree, bNode *node, bNodeSocket *sock,
178                                      const char *name, int x, int y, int width)
179 {
180         if (sock->link || (sock->flag & SOCK_HIDE_VALUE))
181                 node_socket_button_label(C, block, ntree, node, sock, IFACE_(name), x, y, width);
182         else {
183                 PointerRNA ptr;
184                 uiBut *bt;
185                 int labelw = width - 40;
186                 RNA_pointer_create(&ntree->id, &RNA_NodeSocket, sock, &ptr);
187                 
188                 bt = uiDefButR(block, COL, B_NODE_EXEC, "",
189                                x, y + 2, (labelw > 0 ? 40 : width), NODE_DY - 2,
190                                &ptr, "default_value", 0, 0, 0, -1, -1, NULL);
191                 if (node)
192                         uiButSetFunc(bt, node_sync_cb, CTX_wm_space_node(C), node);
193                 
194                 if (name[0] != '\0' && labelw > 0)
195                         uiDefBut(block, LABEL, 0, IFACE_(name), x + 40, y + 2, labelw, NODE_DY - 2, NULL, 0, 0, 0, 0, "");
196         }
197 }
198
199 /* standard draw function, display the default input value */
200 static void node_draw_input_default(const bContext *C, uiBlock *block,
201                                     bNodeTree *ntree, bNode *node, bNodeSocket *sock,
202                                     const char *name, int x, int y, int width)
203 {
204         bNodeSocketType *stype = ntreeGetSocketType(sock->type);
205         if (stype->buttonfunc)
206                 stype->buttonfunc(C, block, ntree, node, sock, name, x, y, width);
207         else
208                 node_socket_button_label(C, block, ntree, node, sock, IFACE_(name), x, y, width);
209 }
210
211 static void node_draw_output_default(const bContext *C, uiBlock *block,
212                                      bNodeTree *UNUSED(ntree), bNode *node, bNodeSocket *sock,
213                                      const char *name, int UNUSED(x), int UNUSED(y), int UNUSED(width))
214 {
215         SpaceNode *snode = CTX_wm_space_node(C);
216         float slen;
217         int ofs = 0;
218         const char *ui_name = IFACE_(name);
219         UI_ThemeColor(TH_TEXT);
220         slen = snode->aspect * UI_GetStringWidth(ui_name);
221         while (slen > node->width) {
222                 ofs++;
223                 slen = snode->aspect * UI_GetStringWidth(ui_name + ofs);
224         }
225         uiDefBut(block, LABEL, 0, ui_name + ofs, (short)(sock->locx - 15.0f - slen), (short)(sock->locy - 9.0f),
226                  (short)(node->width - NODE_DY), NODE_DY,  NULL, 0, 0, 0, 0, "");
227 }
228
229 /* ****************** BASE DRAW FUNCTIONS FOR NEW OPERATOR NODES ***************** */
230
231 #if 0 /* UNUSED */
232 static void node_draw_socket_new(bNodeSocket *sock, float size)
233 {
234         float x = sock->locx, y = sock->locy;
235         
236         /* 16 values of sin function */
237         static float si[16] = {
238                 0.00000000f, 0.39435585f, 0.72479278f, 0.93775213f,
239                 0.99871650f, 0.89780453f, 0.65137248f, 0.29936312f,
240                 -0.10116832f, -0.48530196f, -0.79077573f, -0.96807711f,
241                 -0.98846832f, -0.84864425f, -0.57126821f, -0.20129852f
242         };
243         /* 16 values of cos function */
244         static float co[16] = {
245                 1.00000000f, 0.91895781f, 0.68896691f, 0.34730525f,
246                 -0.05064916f, -0.44039415f, -0.75875812f, -0.95413925f,
247                 -0.99486932f, -0.87434661f, -0.61210598f, -0.25065253f,
248                 0.15142777f, 0.52896401f, 0.82076344f, 0.97952994f,
249         };
250         int a;
251         
252         glColor3ub(180, 180, 180);
253         
254         glBegin(GL_POLYGON);
255         for (a = 0; a < 16; a++)
256                 glVertex2f(x + size * si[a], y + size * co[a]);
257         glEnd();
258         
259         glColor4ub(0, 0, 0, 150);
260         glEnable(GL_BLEND);
261         glEnable(GL_LINE_SMOOTH);
262         glBegin(GL_LINE_LOOP);
263         for (a = 0; a < 16; a++)
264                 glVertex2f(x + size * si[a], y + size * co[a]);
265         glEnd();
266         glDisable(GL_LINE_SMOOTH);
267         glDisable(GL_BLEND);
268 }
269 #endif
270
271 /* ****************** BUTTON CALLBACKS FOR ALL TREES ***************** */
272
273 static void node_buts_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
274 {
275         PointerRNA sockptr;
276         PropertyRNA *prop;
277         
278         /* first socket stores value */
279         prop = RNA_struct_find_property(ptr, "outputs");
280         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
281         
282         uiItemR(layout, &sockptr, "default_value", 0, "", ICON_NONE);
283 }
284
285 static void node_buts_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
286 {
287         uiLayout *col;
288         PointerRNA sockptr;
289         PropertyRNA *prop;
290         
291         /* first socket stores value */
292         prop = RNA_struct_find_property(ptr, "outputs");
293         RNA_property_collection_lookup_int(ptr, prop, 0, &sockptr);
294         
295         col = uiLayoutColumn(layout, FALSE);
296         uiTemplateColorWheel(col, &sockptr, "default_value", 1, 0, 0, 0);
297         uiItemR(col, &sockptr, "default_value", 0, "", ICON_NONE);
298 }
299
300 static void node_buts_mix_rgb(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
301 {       
302         uiLayout *row;
303
304         bNodeTree *ntree = (bNodeTree *)ptr->id.data;
305
306         row = uiLayoutRow(layout, TRUE);
307         uiItemR(row, ptr, "blend_type", 0, "", ICON_NONE);
308         if (ntree->type == NTREE_COMPOSIT)
309                 uiItemR(row, ptr, "use_alpha", 0, "", ICON_IMAGE_RGB_ALPHA);
310 }
311
312 static void node_buts_time(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
313 {
314         uiLayout *row;
315 #if 0
316         /* XXX no context access here .. */
317         bNode *node = ptr->data;
318         CurveMapping *cumap = node->storage;
319         
320         if (cumap) {
321                 cumap->flag |= CUMA_DRAW_CFRA;
322                 if (node->custom1 < node->custom2)
323                         cumap->sample[0] = (float)(CFRA - node->custom1) / (float)(node->custom2 - node->custom1);
324         }
325 #endif
326
327         uiTemplateCurveMapping(layout, ptr, "curve", 's', 0, 0);
328
329         row = uiLayoutRow(layout, TRUE);
330         uiItemR(row, ptr, "frame_start", 0, IFACE_("Sta"), ICON_NONE);
331         uiItemR(row, ptr, "frame_end", 0, IFACE_("End"), ICON_NONE);
332 }
333
334 static void node_buts_colorramp(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
335 {
336         uiTemplateColorRamp(layout, ptr, "color_ramp", 0);
337 }
338
339 static void node_buts_curvevec(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
340 {
341         uiTemplateCurveMapping(layout, ptr, "mapping", 'v', 0, 0);
342 }
343
344 static float _sample_col[4];    // bad bad, 2.5 will do better?
345 #define SAMPLE_FLT_ISNONE FLT_MAX
346 void ED_node_sample_set(const float col[4])
347 {
348         if (col) {
349                 copy_v4_v4(_sample_col, col);
350         }
351         else {
352                 copy_v4_fl(_sample_col, SAMPLE_FLT_ISNONE);
353         }
354 }
355
356 static void node_buts_curvecol(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
357 {
358         bNode *node = ptr->data;
359         CurveMapping *cumap = node->storage;
360
361         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
362                 cumap->flag |= CUMA_DRAW_SAMPLE;
363                 copy_v3_v3(cumap->sample, _sample_col);
364         }
365         else {
366                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
367         }
368
369         uiTemplateCurveMapping(layout, ptr, "mapping", 'c', 0, 0);
370 }
371
372 static void node_normal_cb(bContext *C, void *ntree_v, void *node_v)
373 {
374         Main *bmain = CTX_data_main(C);
375
376         ED_node_generic_update(bmain, ntree_v, node_v);
377         WM_event_add_notifier(C, NC_NODE | NA_EDITED, ntree_v);
378 }
379
380 static void node_buts_normal(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
381 {
382         uiBlock *block = uiLayoutAbsoluteBlock(layout);
383         bNodeTree *ntree = ptr->id.data;
384         bNode *node = ptr->data;
385         rctf *butr = &node->butr;
386         bNodeSocket *sock = node->outputs.first;     /* first socket stores normal */
387         float *nor = ((bNodeSocketValueVector *)sock->default_value)->value;
388         uiBut *bt;
389         
390         bt = uiDefButF(block, BUT_NORMAL, B_NODE_EXEC, "",
391                        (short)butr->xmin, (short)butr->xmin,
392                        butr->xmax - butr->xmin, butr->xmax - butr->xmin,
393                        nor, 0.0f, 1.0f, 0, 0, "");
394         uiButSetFunc(bt, node_normal_cb, ntree, node);
395 }
396 #if 0 // not used in 2.5x yet
397 static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
398 {
399         Main *bmain = CTX_data_main(C);
400         bNodeTree *ntree = ntree_v;
401         bNode *node = node_v;
402         Tex *tex;
403         
404         if (node->menunr < 1) return;
405         
406         if (node->id) {
407                 node->id->us--;
408                 node->id = NULL;
409         }
410         tex = BLI_findlink(&bmain->tex, node->menunr - 1);
411
412         node->id = &tex->id;
413         id_us_plus(node->id);
414         BLI_strncpy(node->name, node->id->name + 2, sizeof(node->name));
415         
416         nodeSetActive(ntree, node);
417         
418         if (ntree->type == NTREE_TEXTURE)
419                 ntreeTexCheckCyclics(ntree);
420         
421         // allqueue(REDRAWBUTSSHADING, 0);
422         // allqueue(REDRAWNODE, 0);
423         NodeTagChanged(ntree, node); 
424         
425         node->menunr = 0;
426 }
427 #endif
428
429 static void node_buts_texture(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
430 {
431         bNode *node = ptr->data;
432
433         short multi = (
434             node->id &&
435             ((Tex *)node->id)->use_nodes &&
436             (node->type != CMP_NODE_TEXTURE) &&
437             (node->type != TEX_NODE_TEXTURE)
438             );
439         
440         uiItemR(layout, ptr, "texture", 0, "", ICON_NONE);
441         
442         if (multi) {
443                 /* Number Drawing not optimal here, better have a list*/
444                 uiItemR(layout, ptr, "node_output", 0, "", ICON_NONE);
445         }
446 }
447
448 static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
449
450         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
451 }
452
453 static int node_resize_area_default(bNode *node, int x, int y)
454 {
455         if (node->flag & NODE_HIDDEN) {
456                 rctf totr = node->totr;
457                 /* right part of node */
458                 totr.xmin = node->totr.xmax - 20.0f;
459                 if (BLI_in_rctf(&totr, x, y))
460                         return NODE_RESIZE_RIGHT;
461                 else
462                         return 0;
463         }
464         else {
465                 const float size = 10.0f;
466                 rctf totr = node->totr;
467                 int dir = 0;
468                 
469                 if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
470                         dir |= NODE_RESIZE_RIGHT;
471                 if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
472                         dir |= NODE_RESIZE_LEFT;
473                 return dir;
474         }
475 }
476
477 /* ****************** BUTTON CALLBACKS FOR COMMON NODES ***************** */
478
479 /* width of socket columns in group display */
480 #define NODE_GROUP_FRAME        120
481
482 /* based on settings in node, sets drawing rect info. each redraw! */
483 /* note: this assumes only 1 group at a time is drawn (linked data) */
484 /* in node->totr the entire boundbox for the group is stored */
485 static void node_update_group(const bContext *C, bNodeTree *ntree, bNode *gnode)
486 {
487         if (!(gnode->flag & NODE_GROUP_EDIT)) {
488                 node_update_default(C, ntree, gnode);
489         }
490         else {
491                 bNodeTree *ngroup = (bNodeTree *)gnode->id;
492                 bNode *node;
493                 bNodeSocket *sock, *gsock;
494                 float locx, locy;
495                 rctf *rect = &gnode->totr;
496                 float node_group_frame = U.dpi * NODE_GROUP_FRAME / 72;
497                 float group_header = 26 * U.dpi / 72;
498                 int counter;
499                 int dy;
500                 
501                 /* get "global" coords */
502                 nodeToView(gnode, 0.0f, 0.0f, &locx, &locy);
503                 
504                 /* center them, is a bit of abuse of locx and locy though */
505                 node_update_nodetree(C, ngroup, locx, locy);
506                 
507                 rect->xmin = rect->xmax = locx;
508                 rect->ymin = rect->ymax = locy;
509                 
510                 counter = 1;
511                 for (node = ngroup->nodes.first; node; node = node->next) {
512                         if (counter) {
513                                 *rect = node->totr;
514                                 counter = 0;
515                         }
516                         else
517                                 BLI_union_rctf(rect, &node->totr);
518                 }
519                 
520                 /* add some room for links to group sockets */
521                 rect->xmin -= 4 * NODE_DY;
522                 rect->xmax += 4 * NODE_DY;
523                 rect->ymin -= NODE_DY;
524                 rect->ymax += NODE_DY;
525                 
526                 /* input sockets */
527                 dy = 0.5f * (rect->ymin + rect->ymax) + NODE_DY * (BLI_countlist(&gnode->inputs) - 1);
528                 gsock = ngroup->inputs.first;
529                 sock = gnode->inputs.first;
530                 while (gsock || sock) {
531                         while (sock && !sock->groupsock) {
532                                 sock->locx = rect->xmin - node_group_frame;
533                                 sock->locy = dy;
534
535                                 /* prevent long socket lists from growing out of the group box */
536                                 if (dy - 3 * NODE_DYS < rect->ymin)
537                                         rect->ymin = dy - 3 * NODE_DYS;
538                                 if (dy + 3 * NODE_DYS > rect->ymax)
539                                         rect->ymax = dy + 3 * NODE_DYS;
540                                 dy -= 2 * NODE_DY;
541                                 
542                                 sock = sock->next;
543                         }
544                         while (gsock && (!sock || sock->groupsock != gsock)) {
545                                 gsock->locx = rect->xmin;
546                                 gsock->locy = dy;
547                                 
548                                 /* prevent long socket lists from growing out of the group box */
549                                 if (dy - 3 * NODE_DYS < rect->ymin)
550                                         rect->ymin = dy - 3 * NODE_DYS;
551                                 if (dy + 3 * NODE_DYS > rect->ymax)
552                                         rect->ymax = dy + 3 * NODE_DYS;
553                                 dy -= 2 * NODE_DY;
554                                 
555                                 gsock = gsock->next;
556                         }
557                         while (sock && gsock && sock->groupsock == gsock) {
558                                 gsock->locx = rect->xmin;
559                                 sock->locx = rect->xmin - node_group_frame;
560                                 sock->locy = gsock->locy = dy;
561                                 
562                                 /* prevent long socket lists from growing out of the group box */
563                                 if (dy - 3 * NODE_DYS < rect->ymin)
564                                         rect->ymin = dy - 3 * NODE_DYS;
565                                 if (dy + 3 * NODE_DYS > rect->ymax)
566                                         rect->ymax = dy + 3 * NODE_DYS;
567                                 dy -= 2 * NODE_DY;
568                                 
569                                 sock = sock->next;
570                                 gsock = gsock->next;
571                         }
572                 }
573                 
574                 /* output sockets */
575                 dy = 0.5f * (rect->ymin + rect->ymax) + NODE_DY * (BLI_countlist(&gnode->outputs) - 1);
576                 gsock = ngroup->outputs.first;
577                 sock = gnode->outputs.first;
578                 while (gsock || sock) {
579                         while (sock && !sock->groupsock) {
580                                 sock->locx = rect->xmax + node_group_frame;
581                                 sock->locy = dy - NODE_DYS;
582                                 
583                                 /* prevent long socket lists from growing out of the group box */
584                                 if (dy - 3 * NODE_DYS < rect->ymin)
585                                         rect->ymin = dy - 3 * NODE_DYS;
586                                 if (dy + 3 * NODE_DYS > rect->ymax)
587                                         rect->ymax = dy + 3 * NODE_DYS;
588                                 dy -= 2 * NODE_DY;
589                                 
590                                 sock = sock->next;
591                         }
592                         while (gsock && (!sock || sock->groupsock != gsock)) {
593                                 gsock->locx = rect->xmax;
594                                 gsock->locy = dy - NODE_DYS;
595                                 
596                                 /* prevent long socket lists from growing out of the group box */
597                                 if (dy - 3 * NODE_DYS < rect->ymin)
598                                         rect->ymin = dy - 3 * NODE_DYS;
599                                 if (dy + 3 * NODE_DYS > rect->ymax)
600                                         rect->ymax = dy + 3 * NODE_DYS;
601                                 dy -= 2 * NODE_DY;
602                                 
603                                 gsock = gsock->next;
604                         }
605                         while (sock && gsock && sock->groupsock == gsock) {
606                                 gsock->locx = rect->xmax;
607                                 sock->locx = rect->xmax + node_group_frame;
608                                 sock->locy = gsock->locy = dy - NODE_DYS;
609                                 
610                                 /* prevent long socket lists from growing out of the group box */
611                                 if (dy - 3 * NODE_DYS < rect->ymin)
612                                         rect->ymin = dy - 3 * NODE_DYS;
613                                 if (dy + 3 * NODE_DYS > rect->ymax)
614                                         rect->ymax = dy + 3 * NODE_DYS;
615                                 dy -= 2 * NODE_DY;
616                                 
617                                 sock = sock->next;
618                                 gsock = gsock->next;
619                         }
620                 }
621                 
622                 /* Set the block bounds to clip mouse events from underlying nodes.
623                  * Add margin for header and input/output columns.
624                  */
625                 uiExplicitBoundsBlock(gnode->block,
626                                       rect->xmin - node_group_frame,
627                                       rect->ymin,
628                                       rect->xmax + node_group_frame,
629                                       rect->ymax + group_header);
630         }
631 }
632
633 static void update_group_input_cb(bContext *UNUSED(C), void *UNUSED(snode_v), void *ngroup_v)
634 {
635         bNodeTree *ngroup = (bNodeTree *)ngroup_v;
636         
637         ngroup->update |= NTREE_UPDATE_GROUP_IN;
638         ntreeUpdateTree(ngroup);
639 }
640
641 static void update_group_output_cb(bContext *UNUSED(C), void *UNUSED(snode_v), void *ngroup_v)
642 {
643         bNodeTree *ngroup = (bNodeTree *)ngroup_v;
644         
645         ngroup->update |= NTREE_UPDATE_GROUP_OUT;
646         ntreeUpdateTree(ngroup);
647 }
648
649 static void draw_group_socket_name(SpaceNode *snode, bNode *gnode, bNodeSocket *sock, int in_out, float xoffset, float yoffset)
650 {
651         bNodeTree *ngroup = (bNodeTree *)gnode->id;
652         uiBut *bt;
653         const char *ui_name = IFACE_(sock->name);
654         
655         if (sock->flag & SOCK_DYNAMIC) {
656                 bt = uiDefBut(gnode->block, TEX, 0, "", 
657                               sock->locx + xoffset, sock->locy + 1 + yoffset, 72, NODE_DY,
658                               sock->name, 0, sizeof(sock->name), 0, 0, "");
659                 if (in_out == SOCK_IN)
660                         uiButSetFunc(bt, update_group_input_cb, snode, ngroup);
661                 else
662                         uiButSetFunc(bt, update_group_output_cb, snode, ngroup);
663         }
664         else {
665                 uiDefBut(gnode->block, LABEL, 0, ui_name,
666                          sock->locx + xoffset, sock->locy + 1 + yoffset, 72, NODE_DY,
667                          NULL, 0, sizeof(ui_name), 0, 0, "");
668         }
669 }
670
671 static void draw_group_socket(const bContext *C, SpaceNode *snode, bNodeTree *ntree, bNode *gnode, bNodeSocket *sock, bNodeSocket *gsock, int index, int in_out)
672 {
673         bNodeTree *ngroup = (bNodeTree *)gnode->id;
674         bNodeSocketType *stype = ntreeGetSocketType(gsock ? gsock->type : sock->type);
675         uiBut *bt;
676         float offset;
677         int draw_value;
678         float node_group_frame = U.dpi * NODE_GROUP_FRAME / 72;
679         float socket_size = NODE_SOCKSIZE * U.dpi / 72;
680         float arrowbutw = 0.8f * UI_UNIT_X;
681         /* layout stuff for buttons on group left frame */
682         float colw = 0.6f * node_group_frame;
683         float col1 = 6 - node_group_frame;
684         float col2 = col1 + colw + 6;
685         float col3 = -arrowbutw - 6;
686         /* layout stuff for buttons on group right frame */
687         float cor1 = 6;
688         float cor2 = cor1 + arrowbutw + 6;
689         float cor3 = cor2 + arrowbutw + 6;
690         
691         /* node and group socket circles */
692         if (sock)
693                 node_socket_circle_draw(ntree, sock, socket_size, sock->flag & SELECT);
694         if (gsock)
695                 node_socket_circle_draw(ngroup, gsock, socket_size, gsock->flag & SELECT);
696         
697         /* socket name */
698         offset = (in_out == SOCK_IN ? col1 : cor3);
699         if (!gsock)
700                 offset += (in_out == SOCK_IN ? node_group_frame : -node_group_frame);
701         
702         /* draw both name and value button if:
703          * 1) input: not internal
704          * 2) output: (node type uses const outputs) and (group output is unlinked)
705          */
706         draw_value = 0;
707         switch (in_out) {
708                 case SOCK_IN:
709                         draw_value = !(gsock && (gsock->flag & SOCK_INTERNAL));
710                         break;
711                 case SOCK_OUT:
712                         if (gnode->typeinfo->flag & NODE_CONST_OUTPUT)
713                                 draw_value = !(gsock && gsock->link);
714                         break;
715         }
716         if (draw_value) {
717                 /* both name and value buttons */
718                 if (gsock) {
719                         draw_group_socket_name(snode, gnode, gsock, in_out, offset, 0);
720                         if (stype->buttonfunc)
721                                 stype->buttonfunc(C, gnode->block, ngroup, NULL, gsock, "", gsock->locx + offset, gsock->locy - NODE_DY, colw);
722                 }
723                 else {
724                         draw_group_socket_name(snode, gnode, sock, in_out, offset, 0);
725                         if (stype->buttonfunc)
726                                 stype->buttonfunc(C, gnode->block, ngroup, NULL, sock, "", sock->locx + offset, sock->locy - NODE_DY, colw);
727                 }
728         }
729         else {
730                 /* only name, no value button */
731                 if (gsock)
732                         draw_group_socket_name(snode, gnode, gsock, in_out, offset, -NODE_DYS);
733                 else
734                         draw_group_socket_name(snode, gnode, sock, in_out, offset, -NODE_DYS);
735         }
736         
737         if (gsock && (gsock->flag & SOCK_DYNAMIC)) {
738                 /* up/down buttons */
739                 offset = (in_out == SOCK_IN ? col2 : cor2);
740                 uiBlockSetDirection(gnode->block, UI_TOP);
741                 uiBlockBeginAlign(gnode->block);
742                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_move_up", 0, ICON_TRIA_UP,
743                                    gsock->locx + offset, gsock->locy, arrowbutw, arrowbutw, "");
744                 if (!gsock->prev || !(gsock->prev->flag & SOCK_DYNAMIC))
745                         uiButSetFlag(bt, UI_BUT_DISABLED);
746                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
747                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
748                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_move_down", 0, ICON_TRIA_DOWN,
749                                    gsock->locx + offset, gsock->locy - arrowbutw, arrowbutw, arrowbutw, "");
750                 if (!gsock->next || !(gsock->next->flag & SOCK_DYNAMIC))
751                         uiButSetFlag(bt, UI_BUT_DISABLED);
752                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
753                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
754                 uiBlockEndAlign(gnode->block);
755                 uiBlockSetDirection(gnode->block, 0);
756                 
757                 /* remove button */
758                 offset = (in_out == SOCK_IN ? col3 : cor1);
759                 uiBlockSetEmboss(gnode->block, UI_EMBOSSN);
760                 bt = uiDefIconButO(gnode->block, BUT, "NODE_OT_group_socket_remove", 0, ICON_X,
761                                    gsock->locx + offset, gsock->locy - 0.5f * arrowbutw, arrowbutw, arrowbutw, "");
762                 RNA_int_set(uiButGetOperatorPtrRNA(bt), "index", index);
763                 RNA_enum_set(uiButGetOperatorPtrRNA(bt), "in_out", in_out);
764                 uiBlockSetEmboss(gnode->block, UI_EMBOSS);
765         }
766 }
767
768 /* groups are, on creation, centered around 0,0 */
769 static void node_draw_group(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *ntree, bNode *gnode)
770 {
771         if (!(gnode->flag & NODE_GROUP_EDIT)) {
772                 node_draw_default(C, ar, snode, ntree, gnode);
773         }
774         else {
775                 bNodeTree *ngroup = (bNodeTree *)gnode->id;
776                 bNodeSocket *sock, *gsock;
777                 uiLayout *layout;
778                 PointerRNA ptr;
779                 rctf rect = gnode->totr;
780                 float node_group_frame = U.dpi * NODE_GROUP_FRAME / 72;
781                 float group_header = 26 * U.dpi / 72;
782                 
783                 int index;
784                 
785                 /* backdrop header */
786                 glEnable(GL_BLEND);
787                 uiSetRoundBox(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT);
788                 UI_ThemeColorShadeAlpha(TH_NODE_GROUP, 0, -70);
789                 uiDrawBox(GL_POLYGON, rect.xmin - node_group_frame, rect.ymax, rect.xmax + node_group_frame, rect.ymax + group_header, BASIS_RAD);
790                 
791                 /* backdrop body */
792                 UI_ThemeColorShadeAlpha(TH_BACK, -8, -70);
793                 uiSetRoundBox(UI_CNR_NONE);
794                 uiDrawBox(GL_POLYGON, rect.xmin, rect.ymin, rect.xmax, rect.ymax, BASIS_RAD);
795         
796                 /* input column */
797                 UI_ThemeColorShadeAlpha(TH_BACK, 10, -50);
798                 uiSetRoundBox(UI_CNR_BOTTOM_LEFT);
799                 uiDrawBox(GL_POLYGON, rect.xmin - node_group_frame, rect.ymin, rect.xmin, rect.ymax, BASIS_RAD);
800         
801                 /* output column */
802                 UI_ThemeColorShadeAlpha(TH_BACK, 10, -50);
803                 uiSetRoundBox(UI_CNR_BOTTOM_RIGHT);
804                 uiDrawBox(GL_POLYGON, rect.xmax, rect.ymin, rect.xmax + node_group_frame, rect.ymax, BASIS_RAD);
805         
806                 /* input column separator */
807                 glColor4ub(200, 200, 200, 140);
808                 glBegin(GL_LINES);
809                 glVertex2f(rect.xmin, rect.ymin);
810                 glVertex2f(rect.xmin, rect.ymax);
811                 glEnd();
812         
813                 /* output column separator */
814                 glColor4ub(200, 200, 200, 140);
815                 glBegin(GL_LINES);
816                 glVertex2f(rect.xmax, rect.ymin);
817                 glVertex2f(rect.xmax, rect.ymax);
818                 glEnd();
819         
820                 /* group node outline */
821                 uiSetRoundBox(UI_CNR_ALL);
822                 glColor4ub(200, 200, 200, 140);
823                 glEnable(GL_LINE_SMOOTH);
824                 uiDrawBox(GL_LINE_LOOP, rect.xmin - node_group_frame, rect.ymin, rect.xmax + node_group_frame, rect.ymax + group_header, BASIS_RAD);
825                 glDisable(GL_LINE_SMOOTH);
826                 glDisable(GL_BLEND);
827                 
828                 /* backdrop title */
829                 UI_ThemeColor(TH_TEXT_HI);
830         
831                 layout = uiBlockLayout(gnode->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, (short)(rect.xmin + 15), (short)(rect.ymax + group_header),
832                                        MIN2((int)(rect.xmax - rect.xmin - 18.0f), node_group_frame + 20), group_header, UI_GetStyle());
833                 RNA_pointer_create(&ntree->id, &RNA_Node, gnode, &ptr);
834                 uiTemplateIDBrowse(layout, (bContext *)C, &ptr, "node_tree", NULL, NULL, NULL);
835                 uiBlockLayoutResolve(gnode->block, NULL, NULL);
836         
837                 /* draw the internal tree nodes and links */
838                 node_draw_nodetree(C, ar, snode, ngroup);
839         
840                 /* group sockets */
841                 gsock = ngroup->inputs.first;
842                 sock = gnode->inputs.first;
843                 index = 0;
844                 while (gsock || sock) {
845                         while (sock && !sock->groupsock) {
846                                 draw_group_socket(C, snode, ntree, gnode, sock, NULL, index, SOCK_IN);
847                                 sock = sock->next;
848                         }
849                         while (gsock && (!sock || sock->groupsock != gsock)) {
850                                 draw_group_socket(C, snode, ntree, gnode, NULL, gsock, index, SOCK_IN);
851                                 gsock = gsock->next;
852                                 ++index;
853                         }
854                         while (sock && gsock && sock->groupsock == gsock) {
855                                 draw_group_socket(C, snode, ntree, gnode, sock, gsock, index, SOCK_IN);
856                                 sock = sock->next;
857                                 gsock = gsock->next;
858                                 ++index;
859                         }
860                 }
861                 gsock = ngroup->outputs.first;
862                 sock = gnode->outputs.first;
863                 index = 0;
864                 while (gsock || sock) {
865                         while (sock && !sock->groupsock) {
866                                 draw_group_socket(C, snode, ntree, gnode, sock, NULL, index, SOCK_OUT);
867                                 sock = sock->next;
868                         }
869                         while (gsock && (!sock || sock->groupsock != gsock)) {
870                                 draw_group_socket(C, snode, ntree, gnode, NULL, gsock, index, SOCK_OUT);
871                                 gsock = gsock->next;
872                                 ++index;
873                         }
874                         while (sock && gsock && sock->groupsock == gsock) {
875                                 draw_group_socket(C, snode, ntree, gnode, sock, gsock, index, SOCK_OUT);
876                                 sock = sock->next;
877                                 gsock = gsock->next;
878                                 ++index;
879                         }
880                 }
881                 
882                 uiEndBlock(C, gnode->block);
883                 uiDrawBlock(C, gnode->block);
884                 gnode->block = NULL;
885         }
886 }
887
888 void node_uifunc_group(uiLayout *layout, bContext *C, PointerRNA *ptr)
889 {
890         uiTemplateIDBrowse(layout, C, ptr, "node_tree", NULL, NULL, NULL);
891 }
892
893 static void node_common_buts_whileloop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
894 {
895         uiItemR(layout, ptr, "max_iterations", 0, NULL, 0);
896 }
897
898 /* XXX Does a bounding box update by iterating over all children.
899  * Not ideal to do this in every draw call, but doing as transform callback doesn't work,
900  * since the child node totr rects are not updated properly at that point.
901  */
902 static void node_update_frame(const bContext *UNUSED(C), bNodeTree *ntree, bNode *node)
903 {
904         const float margin = 30.0f;
905         NodeFrame *data = (NodeFrame *)node->storage;
906         int bbinit;
907         bNode *tnode;
908         rctf rect, noderect;
909         float xmax, ymax;
910         
911         /* init rect from current frame size */
912         nodeToView(node, node->offsetx, node->offsety, &rect.xmin, &rect.ymax);
913         nodeToView(node, node->offsetx + node->width, node->offsety - node->height, &rect.xmax, &rect.ymin);
914         
915         /* frame can be resized manually only if shrinking is disabled or no children are attached */
916         data->flag |= NODE_FRAME_RESIZEABLE;
917         /* for shrinking bbox, initialize the rect from first child node */
918         bbinit = (data->flag & NODE_FRAME_SHRINK);
919         /* fit bounding box to all children */
920         for (tnode = ntree->nodes.first; tnode; tnode = tnode->next) {
921                 if (tnode->parent != node)
922                         continue;
923                 
924                 /* add margin to node rect */
925                 noderect = tnode->totr;
926                 noderect.xmin -= margin;
927                 noderect.xmax += margin;
928                 noderect.ymin -= margin;
929                 noderect.ymax += margin;
930                 
931                 /* first child initializes frame */
932                 if (bbinit) {
933                         bbinit = 0;
934                         rect = noderect;
935                         data->flag &= ~NODE_FRAME_RESIZEABLE;
936                 }
937                 else
938                         BLI_union_rctf(&rect, &noderect);
939         }
940         
941         /* now adjust the frame size from view-space bounding box */
942         nodeFromView(node, rect.xmin, rect.ymax, &node->offsetx, &node->offsety);
943         nodeFromView(node, rect.xmax, rect.ymin, &xmax, &ymax);
944         node->width = xmax - node->offsetx;
945         node->height = -ymax + node->offsety;
946         
947         node->totr = rect;
948 }
949
950 static void node_draw_frame_label(bNode *node)
951 {
952         /* XXX font id is crap design */
953         const int fontid = blf_mono_font;
954         NodeFrame *data = (NodeFrame *)node->storage;
955         rctf *rct = &node->totr;
956         int color_id = node_get_colorid(node);
957         char label[128];
958         /* XXX a bit hacky, should use separate align values for x and y */
959         float width, ascender;
960         float x, y;
961         
962         BLI_strncpy(label, nodeLabel(node), sizeof(label));
963         BLF_size(fontid, data->label_size, U.dpi);
964         
965         /* title color */
966         UI_ThemeColorBlendShade(TH_TEXT, color_id, 0.8f, 10);
967
968         width = BLF_width(fontid, label);
969         ascender = BLF_ascender(fontid);
970         
971         x = 0.5f * (rct->xmin + rct->xmax) - 0.5f * width;
972         y = rct->ymax - NODE_DYS - ascender;
973         
974         BLF_position(fontid, x, y, 0);
975         BLF_draw(fontid, label, BLF_DRAW_STR_DUMMY_MAX);
976 }
977
978 static void node_draw_frame(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *UNUSED(ntree), bNode *node)
979 {
980         rctf *rct = &node->totr;
981         int color_id = node_get_colorid(node);
982         unsigned char color[4];
983         float alpha;
984         
985         UI_GetThemeColor4ubv(TH_NODE_FRAME, color);
986         alpha = (float)(color[3])/255.0f;
987         
988         /* skip if out of view */
989         if (node->totr.xmax < ar->v2d.cur.xmin || node->totr.xmin > ar->v2d.cur.xmax ||
990             node->totr.ymax < ar->v2d.cur.ymin || node->totr.ymin > ar->v2d.cur.ymax) {
991                 
992                 uiEndBlock(C, node->block);
993                 node->block = NULL;
994                 return;
995         }
996         
997         /* shadow */
998         node_draw_shadow(snode, node, BASIS_RAD, alpha);
999         
1000         /* body */
1001         if (node->flag & NODE_CUSTOM_COLOR)
1002                 glColor4f(node->color[0], node->color[1], node->color[2], alpha);
1003         else
1004                 UI_ThemeColor4(TH_NODE_FRAME);
1005         glEnable(GL_BLEND);
1006         uiSetRoundBox(UI_CNR_ALL);
1007         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
1008         glDisable(GL_BLEND);
1009
1010         /* outline active and selected emphasis */
1011         if (node->flag & (NODE_ACTIVE | SELECT)) {
1012                 glEnable(GL_BLEND);
1013                 glEnable(GL_LINE_SMOOTH);
1014                 
1015                 if (node->flag & NODE_ACTIVE)
1016                         UI_ThemeColorShadeAlpha(TH_ACTIVE, 0, -40);
1017                 else
1018                         UI_ThemeColorShadeAlpha(TH_SELECT, 0, -40);
1019                 uiSetRoundBox(UI_CNR_ALL);
1020                 uiDrawBox(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
1021                 
1022                 glDisable(GL_LINE_SMOOTH);
1023                 glDisable(GL_BLEND);
1024         }
1025         
1026         /* label */
1027         node_draw_frame_label(node);
1028         
1029         UI_ThemeClearColor(color_id);
1030                 
1031         uiEndBlock(C, node->block);
1032         uiDrawBlock(C, node->block);
1033         node->block = NULL;
1034 }
1035
1036 static int node_resize_area_frame(bNode *node, int x, int y)
1037 {
1038         const float size = 10.0f;
1039         NodeFrame *data = (NodeFrame *)node->storage;
1040         rctf totr = node->totr;
1041         int dir = 0;
1042         
1043         /* shrinking frame size is determined by child nodes */
1044         if (!(data->flag & NODE_FRAME_RESIZEABLE))
1045                 return 0;
1046         
1047         if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
1048                 dir |= NODE_RESIZE_RIGHT;
1049         if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
1050                 dir |= NODE_RESIZE_LEFT;
1051         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymax - size && y < totr.ymax)
1052                 dir |= NODE_RESIZE_TOP;
1053         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymin && y < totr.ymin + size)
1054                 dir |= NODE_RESIZE_BOTTOM;
1055         
1056         return dir;
1057 }
1058
1059 static void node_buts_frame_details(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1060 {
1061         uiItemR(layout, ptr, "label_size", 0, IFACE_("Label Size"), ICON_NONE);
1062         uiItemR(layout, ptr, "shrink", 0, IFACE_("Shrink"), ICON_NONE);
1063 }
1064
1065
1066 #define NODE_REROUTE_SIZE       8.0f
1067
1068 static void node_update_reroute(const bContext *UNUSED(C), bNodeTree *UNUSED(ntree), bNode *node)
1069 {
1070         bNodeSocket *nsock;
1071         float locx, locy;
1072         float size = NODE_REROUTE_SIZE;
1073         
1074         /* get "global" coords */
1075         nodeToView(node, 0.0f, 0.0f, &locx, &locy);
1076         
1077         /* reroute node has exactly one input and one output, both in the same place */
1078         nsock= node->outputs.first;
1079         nsock->locx= locx;
1080         nsock->locy= locy;
1081
1082         nsock= node->inputs.first;
1083         nsock->locx= locx;
1084         nsock->locy= locy;
1085         
1086         node->width = size*2;
1087         node->totr.xmin= locx - size;
1088         node->totr.xmax= locx + size;
1089         node->totr.ymax= locy + size;
1090         node->totr.ymin= locy - size;
1091 }
1092
1093 static void node_draw_reroute(const bContext *C, ARegion *ar, SpaceNode *UNUSED(snode),  bNodeTree *ntree, bNode *node)
1094 {
1095         bNodeSocket *sock;
1096         #if 0   /* UNUSED */
1097         rctf *rct= &node->totr;
1098         float size = NODE_REROUTE_SIZE;
1099         #endif
1100         float socket_size= NODE_SOCKSIZE;
1101
1102         /* skip if out of view */
1103         if (node->totr.xmax < ar->v2d.cur.xmin || node->totr.xmin > ar->v2d.cur.xmax ||
1104                         node->totr.ymax < ar->v2d.cur.ymin || node->totr.ymin > ar->v2d.cur.ymax) {
1105
1106                 uiEndBlock(C, node->block);
1107                 node->block= NULL;
1108                 return;
1109         }
1110
1111         /* XXX only kept for debugging
1112          * selection state is indicated by socket outline below!
1113          */
1114 #if 0
1115         /* body */
1116         uiSetRoundBox(15);
1117         UI_ThemeColor4(TH_NODE);
1118         glEnable(GL_BLEND);
1119         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
1120         glDisable(GL_BLEND);
1121
1122         /* outline active and selected emphasis */
1123         if (node->flag & (NODE_ACTIVE | SELECT)) {
1124                 glEnable(GL_BLEND);
1125                 glEnable(GL_LINE_SMOOTH);
1126                         /* using different shades of TH_TEXT_HI for the empasis, like triangle */
1127                         if (node->flag & NODE_ACTIVE)
1128                                 UI_ThemeColorShadeAlpha(TH_TEXT_HI, 0, -40);
1129                         else
1130                                 UI_ThemeColorShadeAlpha(TH_TEXT_HI, -20, -120);
1131                         uiDrawBox(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
1132
1133                 glDisable(GL_LINE_SMOOTH);
1134                 glDisable(GL_BLEND);
1135         }
1136 #endif
1137
1138         /* only draw input socket. as they all are placed on the same position.
1139          * highlight also if node itself is selected, since we don't display the node body separately!
1140          */
1141         for (sock= node->inputs.first; sock; sock= sock->next) {
1142                 node_socket_circle_draw(ntree, sock, socket_size, (sock->flag & SELECT) || (node->flag & SELECT));
1143         }
1144
1145         uiEndBlock(C, node->block);
1146         uiDrawBlock(C, node->block);
1147         node->block= NULL;
1148 }
1149
1150 /* Special tweak area for reroute node.
1151  * Since this node is quite small, we use a larger tweak area for grabbing than for selection.
1152  */
1153 static int node_tweak_area_reroute(bNode *node, int x, int y)
1154 {
1155         /* square of tweak radius */
1156         static const float tweak_radius_sq = 576;       /* 24*24 */
1157         
1158         bNodeSocket *sock = node->inputs.first;
1159         float dx = sock->locx - x;
1160         float dy = sock->locy - y;
1161         return (dx*dx + dy*dy <= tweak_radius_sq);
1162 }
1163
1164 static void node_common_set_butfunc(bNodeType *ntype)
1165 {
1166         switch (ntype->type) {
1167                 case NODE_GROUP:
1168                         ntype->uifunc = node_uifunc_group;
1169                         ntype->drawfunc = node_draw_group;
1170                         ntype->drawupdatefunc = node_update_group;
1171                         break;
1172                 case NODE_FORLOOP:
1173 //                      ntype->uifunc= node_common_buts_group;
1174                         ntype->drawfunc = node_draw_group;
1175                         ntype->drawupdatefunc = node_update_group;
1176                         break;
1177                 case NODE_WHILELOOP:
1178                         ntype->uifunc = node_common_buts_whileloop;
1179                         ntype->drawfunc = node_draw_group;
1180                         ntype->drawupdatefunc = node_update_group;
1181                         break;
1182                 case NODE_FRAME:
1183                         ntype->drawfunc = node_draw_frame;
1184                         ntype->drawupdatefunc = node_update_frame;
1185                         ntype->uifuncbut = node_buts_frame_details;
1186                         ntype->resize_area_func = node_resize_area_frame;
1187                         break;
1188                 case NODE_REROUTE:
1189                         ntype->drawfunc= node_draw_reroute;
1190                         ntype->drawupdatefunc= node_update_reroute;
1191                         ntype->tweak_area_func= node_tweak_area_reroute;
1192                         break;
1193         }
1194 }
1195
1196 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
1197
1198 static void node_buts_image_user(uiLayout *layout, bContext *C, PointerRNA *imaptr, PointerRNA *iuserptr)
1199 {
1200         uiLayout *col;
1201         int source;
1202
1203         if (!imaptr->data)
1204                 return;
1205
1206         col = uiLayoutColumn(layout, FALSE);
1207         
1208         uiItemR(col, imaptr, "source", 0, "", ICON_NONE);
1209         
1210         source = RNA_enum_get(imaptr, "source");
1211
1212         if (source == IMA_SRC_SEQUENCE) {
1213                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
1214                 Scene *scene = CTX_data_scene(C);
1215                 ImageUser *iuser = iuserptr->data;
1216                 char numstr[32];
1217                 const int framenr = BKE_image_user_frame_get(iuser, CFRA, 0);
1218                 BLI_snprintf(numstr, sizeof(numstr), IFACE_("Frame: %d"), framenr);
1219                 uiItemL(layout, numstr, ICON_NONE);
1220         }
1221
1222         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1223                 col = uiLayoutColumn(layout, TRUE);
1224                 uiItemR(col, iuserptr, "frame_duration", 0, NULL, ICON_NONE);
1225                 uiItemR(col, iuserptr, "frame_start", 0, NULL, ICON_NONE);
1226                 uiItemR(col, iuserptr, "frame_offset", 0, NULL, ICON_NONE);
1227                 uiItemR(col, iuserptr, "use_cyclic", 0, NULL, ICON_NONE);
1228                 uiItemR(col, iuserptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
1229         }
1230
1231         col = uiLayoutColumn(layout, FALSE);
1232
1233         if (RNA_enum_get(imaptr, "type") == IMA_TYPE_MULTILAYER)
1234                 uiItemR(col, iuserptr, "layer", 0, NULL, ICON_NONE);
1235
1236 }
1237
1238 static void node_shader_buts_material(uiLayout *layout, bContext *C, PointerRNA *ptr)
1239 {
1240         bNode *node = ptr->data;
1241         uiLayout *col;
1242         
1243         uiTemplateID(layout, C, ptr, "material", "MATERIAL_OT_new", NULL, NULL);
1244         
1245         if (!node->id) return;
1246         
1247         col = uiLayoutColumn(layout, FALSE);
1248         uiItemR(col, ptr, "use_diffuse", 0, NULL, ICON_NONE);
1249         uiItemR(col, ptr, "use_specular", 0, NULL, ICON_NONE);
1250         uiItemR(col, ptr, "invert_normal", 0, NULL, ICON_NONE);
1251 }
1252
1253 static void node_shader_buts_mapping(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1254 {
1255         uiLayout *row;
1256         
1257         uiItemL(layout, IFACE_("Location:"), ICON_NONE);
1258         row = uiLayoutRow(layout, TRUE);
1259         uiItemR(row, ptr, "translation", 0, "", ICON_NONE);
1260         
1261         uiItemL(layout, IFACE_("Rotation:"), ICON_NONE);
1262         row = uiLayoutRow(layout, TRUE);
1263         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
1264         
1265         uiItemL(layout, IFACE_("Scale:"), ICON_NONE);
1266         row = uiLayoutRow(layout, TRUE);
1267         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
1268         
1269         row = uiLayoutRow(layout, TRUE);
1270         uiItemR(row, ptr, "use_min", 0, "Min", ICON_NONE);
1271         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
1272         
1273         row = uiLayoutRow(layout, TRUE);
1274         uiItemR(row, ptr, "use_max", 0, "Max", ICON_NONE);
1275         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
1276 }
1277
1278 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1279
1280         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
1281 }
1282
1283 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
1284 {
1285         PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
1286         uiLayout *col;
1287
1288         col = uiLayoutColumn(layout, FALSE);
1289
1290         if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
1291                 PointerRNA dataptr = RNA_pointer_get(&obptr, "data");
1292
1293                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
1294                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
1295         }
1296         else {
1297                 uiItemR(col, ptr, "uv_layer", 0, IFACE_("UV"), ICON_NONE);
1298                 uiItemR(col, ptr, "color_layer", 0, IFACE_("VCol"), ICON_NONE);
1299         }
1300 }
1301
1302 static void node_shader_buts_attribute(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1303 {
1304         uiItemR(layout, ptr, "attribute_name", 0, IFACE_("Name"), ICON_NONE);
1305 }
1306
1307 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1308 {
1309         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
1310         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
1311
1312         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1313         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1314
1315         node_buts_image_user(layout, C, &imaptr, &iuserptr);
1316 }
1317
1318 static void node_shader_buts_tex_environment(uiLayout *layout, bContext *C, PointerRNA *ptr)
1319 {
1320         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
1321         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
1322
1323         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1324         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1325         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
1326
1327         node_buts_image_user(layout, C, &imaptr, &iuserptr);
1328 }
1329
1330 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1331 {
1332         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
1333         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
1334 }
1335
1336 static void node_shader_buts_tex_gradient(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1337 {
1338         uiItemR(layout, ptr, "gradient_type", 0, "", ICON_NONE);
1339 }
1340
1341 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1342 {
1343         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1344 }
1345
1346 static void node_shader_buts_tex_wave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1347 {
1348         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
1349 }
1350
1351 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1352 {
1353         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
1354 }
1355
1356 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1357 {
1358         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
1359 }
1360
1361 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1362 {
1363         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
1364 }
1365
1366 /* only once called */
1367 static void node_shader_set_butfunc(bNodeType *ntype)
1368 {
1369         switch (ntype->type) {
1370                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1371
1372                 case SH_NODE_MATERIAL:
1373                 case SH_NODE_MATERIAL_EXT:
1374                         ntype->uifunc = node_shader_buts_material;
1375                         break;
1376                 case SH_NODE_TEXTURE:
1377                         ntype->uifunc = node_buts_texture;
1378                         break;
1379                 case SH_NODE_NORMAL:
1380                         ntype->uifunc = node_buts_normal;
1381                         break;
1382                 case SH_NODE_CURVE_VEC:
1383                         ntype->uifunc = node_buts_curvevec;
1384                         break;
1385                 case SH_NODE_CURVE_RGB:
1386                         ntype->uifunc = node_buts_curvecol;
1387                         break;
1388                 case SH_NODE_MAPPING:
1389                         ntype->uifunc = node_shader_buts_mapping;
1390                         break;
1391                 case SH_NODE_VALUE:
1392                         ntype->uifunc = node_buts_value;
1393                         break;
1394                 case SH_NODE_RGB:
1395                         ntype->uifunc = node_buts_rgb;
1396                         break;
1397                 case SH_NODE_MIX_RGB:
1398                         ntype->uifunc = node_buts_mix_rgb;
1399                         break;
1400                 case SH_NODE_VALTORGB:
1401                         ntype->uifunc = node_buts_colorramp;
1402                         break;
1403                 case SH_NODE_MATH: 
1404                         ntype->uifunc = node_buts_math;
1405                         break; 
1406                 case SH_NODE_VECT_MATH: 
1407                         ntype->uifunc = node_shader_buts_vect_math;
1408                         break; 
1409                 case SH_NODE_GEOMETRY:
1410                         ntype->uifunc = node_shader_buts_geometry;
1411                         break;
1412                 case SH_NODE_ATTRIBUTE:
1413                         ntype->uifunc = node_shader_buts_attribute;
1414                         break;
1415                 case SH_NODE_TEX_SKY:
1416                         ntype->uifunc = node_shader_buts_tex_sky;
1417                         break;
1418                 case SH_NODE_TEX_IMAGE:
1419                         ntype->uifunc = node_shader_buts_tex_image;
1420                         break;
1421                 case SH_NODE_TEX_ENVIRONMENT:
1422                         ntype->uifunc = node_shader_buts_tex_environment;
1423                         break;
1424                 case SH_NODE_TEX_GRADIENT:
1425                         ntype->uifunc = node_shader_buts_tex_gradient;
1426                         break;
1427                 case SH_NODE_TEX_MAGIC:
1428                         ntype->uifunc = node_shader_buts_tex_magic;
1429                         break;
1430                 case SH_NODE_TEX_WAVE:
1431                         ntype->uifunc = node_shader_buts_tex_wave;
1432                         break;
1433                 case SH_NODE_TEX_MUSGRAVE:
1434                         ntype->uifunc = node_shader_buts_tex_musgrave;
1435                         break;
1436                 case SH_NODE_TEX_VORONOI:
1437                         ntype->uifunc = node_shader_buts_tex_voronoi;
1438                         break;
1439                 case SH_NODE_BSDF_GLOSSY:
1440                 case SH_NODE_BSDF_GLASS:
1441                         ntype->uifunc = node_shader_buts_glossy;
1442                         break;
1443         }
1444 }
1445
1446 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
1447
1448 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1449 {
1450         bNode *node = ptr->data;
1451         PointerRNA imaptr, iuserptr;
1452         
1453         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1454         
1455         if (!node->id) return;
1456         
1457         imaptr = RNA_pointer_get(ptr, "image");
1458         RNA_pointer_create((ID *)ptr->id.data, &RNA_ImageUser, node->storage, &iuserptr);
1459         
1460         node_buts_image_user(layout, C, &imaptr, &iuserptr);
1461 }
1462
1463 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
1464 {
1465         bNode *node = ptr->data;
1466         uiLayout *col, *row;
1467         PointerRNA op_ptr;
1468         PointerRNA scn_ptr;
1469         PropertyRNA *prop;
1470         const char *layer_name;
1471         char scene_name[MAX_ID_NAME - 2];
1472         wmOperatorType *ot = WM_operatortype_find("RENDER_OT_render", 1);
1473
1474         BLI_assert(ot != 0);
1475
1476         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
1477         
1478         if (!node->id) return;
1479
1480         col = uiLayoutColumn(layout, FALSE);
1481         row = uiLayoutRow(col, FALSE);
1482         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
1483         
1484         prop = RNA_struct_find_property(ptr, "layer");
1485         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
1486                 return;
1487         
1488         scn_ptr = RNA_pointer_get(ptr, "scene");
1489         RNA_string_get(&scn_ptr, "name", scene_name);
1490         
1491         WM_operator_properties_create_ptr(&op_ptr, ot);
1492         RNA_string_set(&op_ptr, "layer", layer_name);
1493         RNA_string_set(&op_ptr, "scene", scene_name);
1494         uiItemFullO_ptr(row, ot, "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
1495
1496 }
1497
1498
1499 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1500 {
1501         uiLayout *col, *row;
1502         
1503         col = uiLayoutColumn(layout, FALSE);
1504         
1505         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
1506         if (RNA_enum_get(ptr, "filter_type") != R_FILTER_FAST_GAUSS) {
1507                 uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
1508                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1509         }
1510         
1511         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
1512         
1513         if (RNA_boolean_get(ptr, "use_relative")) {
1514                 uiItemL(col, IFACE_("Aspect Correction"), 0);
1515                 row = uiLayoutRow(layout, TRUE);
1516                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, 0);
1517                 
1518                 col = uiLayoutColumn(layout, TRUE);
1519                 uiItemR(col, ptr, "factor_x", 0, IFACE_("X"), ICON_NONE);
1520                 uiItemR(col, ptr, "factor_y", 0, IFACE_("Y"), ICON_NONE);
1521         }
1522         else {
1523                 col = uiLayoutColumn(layout, TRUE);
1524                 uiItemR(col, ptr, "size_x", 0, IFACE_("X"), ICON_NONE);
1525                 uiItemR(col, ptr, "size_y", 0, IFACE_("Y"), ICON_NONE);
1526         }
1527 }
1528
1529 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1530 {
1531         uiLayout *col;
1532         
1533         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1534         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
1535         
1536         col = uiLayoutColumn(layout, TRUE);
1537         uiItemL(col, IFACE_("Center:"), ICON_NONE);
1538         uiItemR(col, ptr, "center_x", 0, IFACE_("X"), ICON_NONE);
1539         uiItemR(col, ptr, "center_y", 0, IFACE_("Y"), ICON_NONE);
1540         
1541         uiItemS(layout);
1542         
1543         col = uiLayoutColumn(layout, TRUE);
1544         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
1545         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1546         
1547         uiItemS(layout);
1548         
1549         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
1550         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
1551 }
1552
1553 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1554 {       
1555         uiLayout *col;
1556         
1557         col = uiLayoutColumn(layout, TRUE);
1558         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
1559         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
1560         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
1561 }
1562
1563 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1564 {
1565         uiLayout *sub, *col;
1566         
1567         col = uiLayoutColumn(layout, FALSE);
1568         uiItemL(col, IFACE_("Bokeh Type:"), ICON_NONE);
1569         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
1570         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1571
1572         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1573
1574         col = uiLayoutColumn(layout, FALSE);
1575         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer") == TRUE);
1576         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
1577
1578         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
1579         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1580
1581         col = uiLayoutColumn(layout, FALSE);
1582         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
1583         
1584         col = uiLayoutColumn(layout, FALSE);
1585         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
1586         sub = uiLayoutColumn(col, FALSE);
1587         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer") == FALSE);
1588         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
1589 }
1590
1591 /* qdn: glare node */
1592 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1593 {       
1594         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
1595         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
1596
1597         if (RNA_enum_get(ptr, "glare_type") != 1) {
1598                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1599         
1600                 if (RNA_enum_get(ptr, "glare_type") != 0)
1601                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1602         }
1603         
1604         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
1605         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1606
1607         if (RNA_enum_get(ptr, "glare_type") == 2) {
1608                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
1609                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
1610         }
1611         if (RNA_enum_get(ptr, "glare_type") == 0 || RNA_enum_get(ptr, "glare_type") == 2) {
1612                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1613                 
1614                 if (RNA_enum_get(ptr, "glare_type") == 0)
1615                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
1616         }
1617         if (RNA_enum_get(ptr, "glare_type") == 1) {
1618                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
1619         }
1620 }
1621
1622 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1623 {       
1624         uiLayout *col;
1625
1626         col = uiLayoutColumn(layout, FALSE);
1627         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
1628         if (RNA_enum_get(ptr, "tonemap_type") == 0) {
1629                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1630                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1631                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
1632         }
1633         else {
1634                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
1635                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1636                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1637                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1638         }
1639 }
1640
1641 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1642 {
1643         uiLayout *col;
1644
1645         col = uiLayoutColumn(layout, FALSE);
1646         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
1647
1648         col = uiLayoutColumn(col, FALSE);
1649         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector") == FALSE);
1650         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
1651         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
1652 }
1653
1654 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1655 {
1656         uiLayout *col;
1657         
1658         col = uiLayoutColumn(layout, FALSE);
1659         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
1660         uiItemR(col, ptr, "factor", 0, IFACE_("Blur"), ICON_NONE);
1661         
1662         col = uiLayoutColumn(layout, TRUE);
1663         uiItemL(col, IFACE_("Speed:"), ICON_NONE);
1664         uiItemR(col, ptr, "speed_min", 0, IFACE_("Min"), ICON_NONE);
1665         uiItemR(col, ptr, "speed_max", 0, IFACE_("Max"), ICON_NONE);
1666
1667         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
1668 }
1669
1670 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1671 {
1672         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1673 }
1674
1675 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1676 {
1677         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1678 }
1679
1680 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1681 {
1682         uiLayout *col;
1683
1684         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
1685         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
1686
1687         col = uiLayoutColumn(layout, TRUE);
1688         if (RNA_boolean_get(ptr, "relative")) {
1689                 uiItemR(col, ptr, "rel_min_x", 0, IFACE_("Left"), ICON_NONE);
1690                 uiItemR(col, ptr, "rel_max_x", 0, IFACE_("Right"), ICON_NONE);
1691                 uiItemR(col, ptr, "rel_min_y", 0, IFACE_("Up"), ICON_NONE);
1692                 uiItemR(col, ptr, "rel_max_y", 0, IFACE_("Down"), ICON_NONE);
1693         }
1694         else {
1695                 uiItemR(col, ptr, "min_x", 0, IFACE_("Left"), ICON_NONE);
1696                 uiItemR(col, ptr, "max_x", 0, IFACE_("Right"), ICON_NONE);
1697                 uiItemR(col, ptr, "min_y", 0, IFACE_("Up"), ICON_NONE);
1698                 uiItemR(col, ptr, "max_y", 0, IFACE_("Down"), ICON_NONE);
1699         }
1700 }
1701
1702 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1703 {
1704         uiLayout *row, *col;
1705         
1706         col = uiLayoutColumn(layout, FALSE);
1707         row = uiLayoutRow(col, FALSE);
1708         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1709         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
1710 }
1711
1712 static void node_composit_buts_double_edge_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1713 {
1714         uiLayout *col;
1715
1716         col = uiLayoutColumn(layout, FALSE);
1717
1718         uiItemL(col, IFACE_("Inner Edge:"), ICON_NONE);
1719         uiItemR(col, ptr, "inner_mode", 0, "", ICON_NONE);
1720         uiItemL(col, IFACE_("Buffer Edge:"), ICON_NONE);
1721         uiItemR(col, ptr, "edge_mode", 0, "", ICON_NONE);
1722 }
1723
1724 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1725 {
1726         uiLayout *sub, *col;
1727         
1728         col = uiLayoutColumn(layout, TRUE);
1729         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1730         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
1731         
1732         col = uiLayoutColumn(layout, TRUE);
1733         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
1734         sub = uiLayoutColumn(col, FALSE);
1735         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1736         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
1737         
1738         col = uiLayoutColumn(layout, TRUE);
1739         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
1740         sub = uiLayoutColumn(col, FALSE);
1741         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1742         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
1743 }
1744
1745 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1746 {       
1747         uiLayout *col;
1748         
1749         col = uiLayoutColumn(layout, TRUE);
1750         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
1751         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
1752 }
1753
1754 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1755 {       
1756         uiLayout *col;
1757         
1758         col = uiLayoutColumn(layout, TRUE);
1759         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
1760 }
1761
1762
1763 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1764 {
1765         uiLayout *col;
1766         
1767         col = uiLayoutColumn(layout, FALSE);
1768         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1769         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1770         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1771 }
1772
1773 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1774 {
1775         uiItemR(layout, ptr, "type", 0, NULL, ICON_NONE);
1776         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1777         switch (RNA_enum_get(ptr, "type")) {
1778                 case CMP_NODE_DILATEERODE_DISTANCE_THRESH:
1779                         uiItemR(layout, ptr, "edge", 0, NULL, ICON_NONE);
1780                         break;
1781                 case CMP_NODE_DILATEERODE_DISTANCE_FEATHER:
1782                         uiItemR(layout, ptr, "falloff", 0, NULL, ICON_NONE);
1783                         break;
1784         }
1785 }
1786
1787 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1788 {
1789         uiLayout *col;
1790         
1791         col = uiLayoutColumn(layout, TRUE);
1792         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1793         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1794 }
1795
1796 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1797 {
1798         uiLayout *col, *row;
1799         
1800         col = uiLayoutColumn(layout, TRUE);
1801    
1802         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1803         row = uiLayoutRow(layout, FALSE);
1804         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1805
1806         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1807         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1808 }
1809
1810 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1811 {
1812         uiLayout *row, *col;
1813         
1814         uiItemL(layout, IFACE_("Despill Channel:"), ICON_NONE);
1815         row = uiLayoutRow(layout, FALSE);
1816         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1817
1818         col = uiLayoutColumn(layout, FALSE);
1819         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1820
1821         if (RNA_enum_get(ptr, "limit_method") == 0) {
1822                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1823                 row = uiLayoutRow(col, FALSE);
1824                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1825         }
1826
1827         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1828         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1829         if (RNA_boolean_get(ptr, "use_unspill") == TRUE) {
1830                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1831                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1832                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1833         }
1834 }
1835
1836 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1837 {
1838         uiLayout *col;
1839         
1840         col = uiLayoutColumn(layout, FALSE);
1841         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1842         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1843         
1844         col = uiLayoutColumn(layout, TRUE);
1845         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1846         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1847         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1848 }
1849
1850 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1851 {
1852         uiLayout *col;
1853         
1854         col = uiLayoutColumn(layout, TRUE);
1855         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1856         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1857         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1858 }
1859
1860 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1861 {       
1862         uiLayout *col, *row;
1863
1864         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1865         row = uiLayoutRow(layout, FALSE);
1866         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1867
1868         col = uiLayoutColumn(layout, FALSE);
1869         uiItemL(col, IFACE_("Key Channel:"), ICON_NONE);
1870         row = uiLayoutRow(col, FALSE);
1871         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1872
1873         col = uiLayoutColumn(layout, FALSE);
1874
1875         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1876         if (RNA_enum_get(ptr, "limit_method") == 0) {
1877                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1878                 row = uiLayoutRow(col, FALSE);
1879                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1880         }
1881
1882         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1883         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1884 }
1885
1886 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1887 {
1888         uiLayout *col;
1889         
1890         col = uiLayoutColumn(layout, TRUE);
1891         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1892         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1893 }
1894
1895 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1896 {
1897         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1898 }
1899
1900 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1901 {
1902         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1903         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
1904 }
1905
1906 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1907 static void node_draw_input_file_output(const bContext *C, uiBlock *block,
1908                                         bNodeTree *ntree, bNode *node, bNodeSocket *sock,
1909                                         const char *UNUSED(name), int x, int y, int width)
1910 {
1911         uiLayout *layout, *row;
1912         PointerRNA nodeptr, inputptr, imfptr;
1913         int imtype;
1914         int rx, ry;
1915         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
1916         
1917         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, x, y + NODE_DY, width, 20, UI_GetStyle());
1918         row = uiLayoutRow(layout, FALSE);
1919         
1920         imfptr = RNA_pointer_get(&nodeptr, "format");
1921         imtype = RNA_enum_get(&imfptr, "file_format");
1922         if (imtype == R_IMF_IMTYPE_MULTILAYER) {
1923                 NodeImageMultiFileSocket *input = sock->storage;
1924                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotLayer, input, &inputptr);
1925                 
1926                 uiItemL(row, input->layer, 0);
1927         }
1928         else {
1929                 NodeImageMultiFileSocket *input = sock->storage;
1930                 PropertyRNA *imtype_prop;
1931                 const char *imtype_name;
1932                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotFile, input, &inputptr);
1933                 
1934                 uiItemL(row, input->path, 0);
1935                 
1936                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1937                         imfptr = RNA_pointer_get(&inputptr, "format");
1938                 
1939                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1940                 RNA_property_enum_name((bContext *)C, &imfptr, imtype_prop, RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1941                 uiBlockSetEmboss(block, UI_EMBOSSP);
1942                 uiItemL(row, imtype_name, 0);
1943                 uiBlockSetEmboss(block, UI_EMBOSSN);
1944         }
1945         
1946         uiBlockLayoutResolve(block, &rx, &ry);
1947 }
1948 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1949 {
1950         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1951         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1952         
1953         if (multilayer)
1954                 uiItemL(layout, IFACE_("Path:"), 0);
1955         else
1956                 uiItemL(layout, IFACE_("Base Path:"), 0);
1957         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1958 }
1959 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1960 {
1961         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1962         PointerRNA active_input_ptr, op_ptr;
1963         uiLayout *row;
1964         int active_index;
1965         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1966         
1967         node_composit_buts_file_output(layout, C, ptr);
1968         uiTemplateImageSettings(layout, &imfptr);
1969         
1970         uiItemS(layout);
1971         
1972         uiItemO(layout, IFACE_("Add Input"), ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
1973         
1974         active_index = RNA_int_get(ptr, "active_input_index");
1975         /* using different collection properties if multilayer format is enabled */
1976         if (multilayer) {
1977                 uiTemplateList(layout, C, ptr, "layer_slots", ptr, "active_input_index", NULL, 0, 0, 0);
1978                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "layer_slots"), active_index, &active_input_ptr);
1979         }
1980         else {
1981                 uiTemplateList(layout, C, ptr, "file_slots", ptr, "active_input_index", NULL, 0, 0, 0);
1982                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_slots"), active_index, &active_input_ptr);
1983         }
1984         /* XXX collection lookup does not return the ID part of the pointer, setting this manually here */
1985         active_input_ptr.id.data = ptr->id.data;
1986         
1987         row = uiLayoutRow(layout, TRUE);
1988         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1989         RNA_enum_set(&op_ptr, "direction", 1);
1990         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "", ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
1991         RNA_enum_set(&op_ptr, "direction", 2);
1992         
1993         if (active_input_ptr.data) {
1994                 if (multilayer) {
1995                         uiLayout *row, *col;
1996                         col = uiLayoutColumn(layout, TRUE);
1997                         
1998                         uiItemL(col, IFACE_("Layer:"), 0);
1999                         row = uiLayoutRow(col, FALSE);
2000                         uiItemR(row, &active_input_ptr, "name", 0, "", 0);
2001                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2002                 }
2003                 else {
2004                         uiLayout *row, *col;
2005                         col = uiLayoutColumn(layout, TRUE);
2006                         
2007                         uiItemL(col, IFACE_("File Path:"), 0);
2008                         row = uiLayoutRow(col, FALSE);
2009                         uiItemR(row, &active_input_ptr, "path", 0, "", 0);
2010                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "", ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2011                         
2012                         /* format details for individual files */
2013                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
2014                         
2015                         col = uiLayoutColumn(layout, TRUE);
2016                         uiItemL(col, IFACE_("Format:"), 0);
2017                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, 0);
2018                         
2019                         col = uiLayoutColumn(layout, FALSE);
2020                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format") == FALSE);
2021                         uiTemplateImageSettings(col, &imfptr);
2022                 }
2023         }
2024 }
2025
2026 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2027 {
2028         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
2029
2030         if (RNA_enum_get(ptr, "space") == CMP_SCALE_RENDERPERCENT) {
2031                 uiLayout *row;
2032                 uiItemR(layout, ptr, "frame_method", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2033                 row = uiLayoutRow(layout, TRUE);
2034                 uiItemR(row, ptr, "offset_x", 0, "X", ICON_NONE);
2035                 uiItemR(row, ptr, "offset_y", 0, "Y", ICON_NONE);
2036         }
2037 }
2038
2039 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2040 {
2041         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
2042 }
2043
2044 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2045 {
2046         uiLayout *col;
2047         
2048         col = uiLayoutColumn(layout, FALSE);
2049         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
2050         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
2051 }
2052
2053 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2054 {
2055         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
2056 }
2057
2058 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2059 {
2060         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2061 }
2062
2063 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2064 {
2065         uiLayout *split, *col, *row;
2066         
2067         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2068         
2069         if (RNA_enum_get(ptr, "correction_method") == 0) {
2070         
2071                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2072                 col = uiLayoutColumn(split, FALSE);
2073                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
2074                 row = uiLayoutRow(col, FALSE);
2075                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
2076                 
2077                 col = uiLayoutColumn(split, FALSE);
2078                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
2079                 row = uiLayoutRow(col, FALSE);
2080                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
2081                 
2082                 col = uiLayoutColumn(split, FALSE);
2083                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
2084                 row = uiLayoutRow(col, FALSE);
2085                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
2086
2087         }
2088         else {
2089                 
2090                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2091                 col = uiLayoutColumn(split, FALSE);
2092                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
2093                 row = uiLayoutRow(col, FALSE);
2094                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
2095                 
2096                 col = uiLayoutColumn(split, FALSE);
2097                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
2098                 row = uiLayoutRow(col, FALSE);
2099                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
2100                 
2101                 col = uiLayoutColumn(split, FALSE);
2102                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
2103                 row = uiLayoutRow(col, FALSE);
2104                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
2105         }
2106
2107 }
2108 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2109 {
2110         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2111
2112         if (RNA_enum_get(ptr, "correction_method") == 0) {
2113
2114                 uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
2115                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
2116
2117                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
2118                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
2119
2120                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
2121                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
2122         }
2123         else {
2124                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
2125                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
2126
2127                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
2128                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
2129
2130                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
2131                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
2132         }
2133 }
2134
2135
2136 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2137 {
2138         bNode *node = ptr->data;
2139         CurveMapping *cumap = node->storage;
2140
2141         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
2142                 cumap->flag |= CUMA_DRAW_SAMPLE;
2143                 copy_v3_v3(cumap->sample, _sample_col);
2144         }
2145         else {
2146                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
2147         }
2148
2149         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
2150 }
2151
2152 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2153
2154         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
2155 }
2156
2157 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
2158 {
2159         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2160 }
2161
2162 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
2163 {
2164         bNode *node = ptr->data;
2165
2166         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2167
2168         if (!node->id)
2169                 return;
2170
2171         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2172 }
2173
2174 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2175 {
2176         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2177 }
2178
2179 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
2180 {
2181         bNode *node = ptr->data;
2182
2183         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2184
2185         if (!node->id)
2186                 return;
2187
2188         uiItemR(layout, ptr, "distortion_type", 0, "", 0);
2189 }
2190
2191 static void node_composit_buts_colorcorrection(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2192 {
2193         uiLayout *row;
2194         
2195         row = uiLayoutRow(layout, FALSE);
2196         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2197         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2198         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2199
2200         row = uiLayoutRow(layout, FALSE);
2201         uiItemL(row, "", 0);
2202         uiItemL(row, IFACE_("Saturation"), 0);
2203         uiItemL(row, IFACE_("Contrast"), 0);
2204         uiItemL(row, IFACE_("Gamma"), 0);
2205         uiItemL(row, IFACE_("Gain"), 0);
2206         uiItemL(row, IFACE_("Lift"), 0);
2207
2208         row = uiLayoutRow(layout, FALSE);
2209         uiItemL(row, IFACE_("Master"), 0);
2210         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2211         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2212         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2213         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2214         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2215
2216         row = uiLayoutRow(layout, FALSE);
2217         uiItemL(row, IFACE_("Highlights"), 0);
2218         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2219         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2220         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2221         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2222         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2223
2224         row = uiLayoutRow(layout, FALSE);
2225         uiItemL(row, IFACE_("Midtones"), 0);
2226         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2227         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2228         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2229         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2230         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2231
2232         row = uiLayoutRow(layout, FALSE);
2233         uiItemL(row, IFACE_("Shadows"), 0);
2234         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2235         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2236         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2237         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2238         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2239
2240         row = uiLayoutRow(layout, FALSE);
2241         uiItemR(row, ptr, "midtones_start", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2242         uiItemR(row, ptr, "midtones_end", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2243 }
2244
2245 static void node_composit_buts_colorcorrection_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2246 {
2247         uiLayout *row;
2248         
2249         row = uiLayoutRow(layout, FALSE);
2250         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2251         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2252         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2253         row = layout;
2254         uiItemL(row, IFACE_("Saturation"), 0);
2255         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2256         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2257         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2258         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2259
2260         uiItemL(row, IFACE_("Contrast"), 0);
2261         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2262         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2263         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2264         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2265
2266         uiItemL(row, IFACE_("Gamma"), 0);
2267         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2268         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2269         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2270         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2271
2272         uiItemL(row, IFACE_("Gain"), 0);
2273         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2274         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2275         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2276         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2277         
2278         uiItemL(row, IFACE_("Lift"), 0);
2279         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2280         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2281         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2282         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2283
2284         row = uiLayoutRow(layout, FALSE);
2285         uiItemR(row, ptr, "midtones_start", 0, NULL, ICON_NONE);
2286         uiItemR(row, ptr, "midtones_end", 0, NULL, ICON_NONE);
2287 }
2288
2289 static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2290 {
2291         uiItemR(layout, ptr, "check", 0, NULL, ICON_NONE);
2292 }
2293
2294 static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2295 {
2296         uiLayout *row;
2297         
2298         row = uiLayoutRow(layout, TRUE);
2299         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2300         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2301         
2302         row = uiLayoutRow(layout, TRUE);
2303         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2304         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2305
2306         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2307         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2308 }
2309
2310 static void node_composit_buts_bokehimage(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2311 {
2312         uiItemR(layout, ptr, "flaps", 0, NULL, ICON_NONE);
2313         uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
2314         uiItemR(layout, ptr, "rounding", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2315         uiItemR(layout, ptr, "catadioptric", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2316         uiItemR(layout, ptr, "shift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2317 }
2318
2319 void node_composit_backdrop_viewer(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2320 {
2321 //      node_composit_backdrop_canvas(snode, backdrop, node, x, y);
2322         if (node->custom1 == 0) {
2323                 const float backdropWidth = backdrop->x;
2324                 const float backdropHeight = backdrop->y;
2325                 const float cx  = x + snode->zoom * backdropWidth * node->custom3;
2326                 const float cy = y + snode->zoom * backdropHeight * node->custom4;
2327
2328                 glColor3f(1.0, 1.0, 1.0);
2329
2330                 glBegin(GL_LINES);
2331                 glVertex2f(cx - 25, cy - 25);
2332                 glVertex2f(cx + 25, cy + 25);
2333                 glVertex2f(cx + 25, cy - 25);
2334                 glVertex2f(cx - 25, cy + 25);
2335                 glEnd();
2336         }
2337 }
2338
2339 void node_composit_backdrop_boxmask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2340 {
2341         NodeBoxMask *boxmask = node->storage;
2342         const float backdropWidth = backdrop->x;
2343         const float backdropHeight = backdrop->y;
2344         const float aspect = backdropWidth / backdropHeight;
2345         const float rad = DEG2RADF(-boxmask->rotation);
2346         const float cosine = cosf(rad);
2347         const float sine = sinf(rad);
2348         const float halveBoxWidth = backdropWidth * (boxmask->width / 2.0f);
2349         const float halveBoxHeight = backdropHeight * (boxmask->height / 2.0f) * aspect;
2350
2351         float cx, cy, x1, x2, x3, x4;
2352         float y1, y2, y3, y4;
2353
2354
2355         /* keep this, saves us from a version patch */
2356         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2357
2358         glColor3f(1.0, 1.0, 1.0);
2359
2360         cx  = x + snode->zoom * backdropWidth * boxmask->x;
2361         cy = y + snode->zoom * backdropHeight * boxmask->y;
2362
2363         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2364         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2365         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2366         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2367         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2368         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2369         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2370         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2371
2372         glBegin(GL_LINE_LOOP);
2373         glVertex2f(x1, y1);
2374         glVertex2f(x2, y2);
2375         glVertex2f(x3, y3);
2376         glVertex2f(x4, y4);
2377         glEnd();
2378 }
2379
2380 void node_composit_backdrop_ellipsemask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2381 {
2382         NodeEllipseMask *ellipsemask = node->storage;
2383         const float backdropWidth = backdrop->x;
2384         const float backdropHeight = backdrop->y;
2385         const float aspect = backdropWidth / backdropHeight;
2386         const float rad = DEG2RADF(-ellipsemask->rotation);
2387         const float cosine = cosf(rad);
2388         const float sine = sinf(rad);
2389         const float halveBoxWidth = backdropWidth * (ellipsemask->width / 2.0f);
2390         const float halveBoxHeight = backdropHeight * (ellipsemask->height / 2.0f) * aspect;
2391
2392         float cx, cy, x1, x2, x3, x4;
2393         float y1, y2, y3, y4;
2394
2395
2396         /* keep this, saves us from a version patch */
2397         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2398
2399         glColor3f(1.0, 1.0, 1.0);
2400
2401         cx  = x + snode->zoom * backdropWidth * ellipsemask->x;
2402         cy = y + snode->zoom * backdropHeight * ellipsemask->y;
2403
2404         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2405         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2406         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2407         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2408         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2409         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2410         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2411         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2412
2413         glBegin(GL_LINE_LOOP);
2414
2415         glVertex2f(x1, y1);
2416         glVertex2f(x2, y2);
2417         glVertex2f(x3, y3);
2418         glVertex2f(x4, y4);
2419         glEnd();
2420 }
2421
2422 static void node_composit_buts_ellipsemask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2423 {
2424         uiLayout *row;
2425         row = uiLayoutRow(layout, TRUE);
2426         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2427         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2428         row = uiLayoutRow(layout, TRUE);
2429         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2430         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2431
2432         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2433         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2434 }
2435
2436 static void node_composit_buts_viewer_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2437 {
2438         uiLayout *col;
2439         
2440         uiItemR(layout, ptr, "tile_order", 0, NULL, ICON_NONE);
2441         if (RNA_enum_get(ptr, "tile_order") == 0) {
2442                 col = uiLayoutColumn(layout, TRUE);
2443                 uiItemR(col, ptr, "center_x", 0, NULL, ICON_NONE);
2444                 uiItemR(col, ptr, "center_y", 0, NULL, ICON_NONE);
2445         }
2446 }
2447
2448 static void node_composit_buts_mask(uiLayout *layout, bContext *C, PointerRNA *ptr)
2449 {
2450         uiTemplateID(layout, C, ptr, "mask", NULL, NULL, NULL);
2451         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
2452         uiItemR(layout, ptr, "use_feather", 0, NULL, ICON_NONE);
2453
2454 }
2455
2456 static void node_composit_buts_keyingscreen(uiLayout *layout, bContext *C, PointerRNA *ptr)
2457 {
2458         bNode *node= ptr->data;
2459
2460         uiTemplateID(layout, C, ptr, "clip", NULL, NULL, NULL);
2461
2462         if (node->id) {
2463                 MovieClip *clip = (MovieClip *) node->id;
2464                 uiLayout *col;
2465                 PointerRNA tracking_ptr;
2466
2467                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, &clip->tracking, &tracking_ptr);
2468
2469                 col = uiLayoutColumn(layout, TRUE);
2470                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2471         }
2472 }
2473
2474 static void node_composit_buts_keying(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2475 {
2476         /* bNode *node= ptr->data; */ /* UNUSED */
2477
2478         uiItemR(layout, ptr, "blur_pre", 0, NULL, ICON_NONE);
2479         uiItemR(layout, ptr, "screen_balance", 0, NULL, ICON_NONE);
2480         uiItemR(layout, ptr, "despill_factor", 0, NULL, ICON_NONE);
2481         uiItemR(layout, ptr, "edge_kernel_radius", 0, NULL, ICON_NONE);
2482         uiItemR(layout, ptr, "edge_kernel_tolerance", 0, NULL, ICON_NONE);
2483         uiItemR(layout, ptr, "clip_black", 0, NULL, ICON_NONE);
2484         uiItemR(layout, ptr, "clip_white", 0, NULL, ICON_NONE);
2485         uiItemR(layout, ptr, "dilate_distance", 0, NULL, ICON_NONE);
2486         uiItemR(layout, ptr, "feather_falloff", 0, NULL, ICON_NONE);
2487         uiItemR(layout, ptr, "feather_distance", 0, NULL, ICON_NONE);
2488         uiItemR(layout, ptr, "blur_post", 0, NULL, ICON_NONE);
2489 }
2490
2491 static void node_composit_buts_trackpos(uiLayout *layout, bContext *C, PointerRNA *ptr)
2492 {
2493         bNode *node= ptr->data;
2494
2495         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2496
2497         if (node->id) {
2498                 MovieClip *clip = (MovieClip *) node->id;
2499                 MovieTracking *tracking = &clip->tracking;
2500                 MovieTrackingObject *object;
2501                 uiLayout *col;
2502                 PointerRNA tracking_ptr;
2503                 NodeTrackPosData *data = node->storage;
2504
2505                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, tracking, &tracking_ptr);
2506
2507                 col = uiLayoutColumn(layout, 0);
2508                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2509
2510                 object = BKE_tracking_object_get_named(tracking, data->tracking_object);
2511                 if (object) {
2512                         PointerRNA object_ptr;
2513
2514                         RNA_pointer_create(&clip->id, &RNA_MovieTrackingObject, object, &object_ptr);
2515
2516                         uiItemPointerR(col, ptr, "track_name", &object_ptr, "tracks", "", ICON_ANIM_DATA);
2517                 }
2518                 else {
2519                         uiItemR(layout, ptr, "track_name", 0, "", ICON_ANIM_DATA);
2520                 }
2521
2522                 uiItemR(layout, ptr, "use_relative", 0, NULL, ICON_NONE);
2523         }
2524 }
2525
2526 /* only once called */
2527 static void node_composit_set_butfunc(bNodeType *ntype)
2528 {
2529         switch (ntype->type) {
2530                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2531
2532                 case CMP_NODE_IMAGE:
2533                         ntype->uifunc = node_composit_buts_image;
2534                         break;
2535                 case CMP_NODE_R_LAYERS:
2536                         ntype->uifunc = node_composit_buts_renderlayers;
2537                         break;
2538                 case CMP_NODE_NORMAL:
2539                         ntype->uifunc = node_buts_normal;
2540                         break;
2541                 case CMP_NODE_CURVE_VEC:
2542                         ntype->uifunc = node_buts_curvevec;
2543                         break;
2544                 case CMP_NODE_CURVE_RGB:
2545                         ntype->uifunc = node_buts_curvecol;
2546                         break;
2547                 case CMP_NODE_VALUE:
2548                         ntype->uifunc = node_buts_value;
2549                         break;
2550                 case CMP_NODE_RGB:
2551                         ntype->uifunc = node_buts_rgb;
2552                         break;
2553                 case CMP_NODE_FLIP:
2554                         ntype->uifunc = node_composit_buts_flip;
2555                         break;
2556                 case CMP_NODE_SPLITVIEWER:
2557                         ntype->uifunc = node_composit_buts_splitviewer;
2558                         break;
2559                 case CMP_NODE_MIX_RGB:
2560                         ntype->uifunc = node_buts_mix_rgb;
2561                         break;
2562                 case CMP_NODE_VALTORGB:
2563                         ntype->uifunc = node_buts_colorramp;
2564                         break;
2565                 case CMP_NODE_CROP:
2566                         ntype->uifunc = node_composit_buts_crop;
2567                         break;
2568                 case CMP_NODE_BLUR:
2569                         ntype->uifunc = node_composit_buts_blur;
2570                         break;
2571                 case CMP_NODE_DBLUR:
2572                         ntype->uifunc = node_composit_buts_dblur;
2573                         break;
2574                 case CMP_NODE_BILATERALBLUR:
2575                         ntype->uifunc = node_composit_buts_bilateralblur;
2576                         break;
2577                 case CMP_NODE_DEFOCUS:
2578                         ntype->uifunc = node_composit_buts_defocus;
2579                         break;
2580                 case CMP_NODE_GLARE:
2581                         ntype->uifunc = node_composit_buts_glare;
2582                         break;
2583                 case CMP_NODE_TONEMAP:
2584                         ntype->uifunc = node_composit_buts_tonemap;
2585                         break;
2586                 case CMP_NODE_LENSDIST:
2587                         ntype->uifunc = node_composit_buts_lensdist;
2588                         break;
2589                 case CMP_NODE_VECBLUR:
2590                         ntype->uifunc = node_composit_buts_vecblur;
2591                         break;
2592                 case CMP_NODE_FILTER:
2593                         ntype->uifunc = node_composit_buts_filter;
2594                         break;
2595                 case CMP_NODE_MAP_VALUE:
2596                         ntype->uifunc = node_composit_buts_map_value;
2597                         break;
2598                 case CMP_NODE_TIME:
2599                         ntype->uifunc = node_buts_time;
2600                         break;
2601                 case CMP_NODE_ALPHAOVER:
2602                         ntype->uifunc = node_composit_buts_alphaover;
2603                         break;
2604                 case CMP_NODE_HUE_SAT:
2605                         ntype->uifunc = node_composit_buts_hue_sat;
2606                         break;
2607                 case CMP_NODE_TEXTURE:
2608                         ntype->uifunc = node_buts_texture;
2609                         break;
2610                 case CMP_NODE_DILATEERODE:
2611                         ntype->uifunc = node_composit_buts_dilateerode;
2612                         break;
2613                 case CMP_NODE_OUTPUT_FILE:
2614                         ntype->uifunc = node_composit_buts_file_output;
2615                         ntype->uifuncbut = node_composit_buts_file_output_details;
2616                         ntype->drawinputfunc = node_draw_input_file_output;
2617                         break;
2618                 case CMP_NODE_DIFF_MATTE:
2619                         ntype->uifunc = node_composit_buts_diff_matte;
2620                         break;
2621                 case CMP_NODE_DIST_MATTE:
2622                         ntype->uifunc = node_composit_buts_distance_matte;
2623                         break;
2624                 case CMP_NODE_COLOR_SPILL:
2625                         ntype->uifunc = node_composit_buts_color_spill;
2626                         break;
2627                 case CMP_NODE_CHROMA_MATTE:
2628                         ntype->uifunc = node_composit_buts_chroma_matte;
2629                         break;
2630                 case CMP_NODE_COLOR_MATTE:
2631                         ntype->uifunc = node_composit_buts_color_matte;
2632                         break;
2633                 case CMP_NODE_SCALE:
2634                         ntype->uifunc = node_composit_buts_scale;
2635                         break;
2636                 case CMP_NODE_ROTATE:
2637                         ntype->uifunc = node_composit_buts_rotate;
2638                         break;
2639                 case CMP_NODE_CHANNEL_MATTE:
2640                         ntype->uifunc = node_composit_buts_channel_matte;
2641                         break;
2642                 case CMP_NODE_LUMA_MATTE:
2643                         ntype->uifunc = node_composit_buts_luma_matte;
2644                         break;
2645                 case CMP_NODE_MAP_UV:
2646                         ntype->uifunc = node_composit_buts_map_uv;
2647                         break;
2648                 case CMP_NODE_ID_MASK:
2649                         ntype->uifunc = node_composit_buts_id_mask;
2650                         break;
2651                 case CMP_NODE_DOUBLEEDGEMASK:
2652                         ntype->uifunc = node_composit_buts_double_edge_mask;
2653                         break;
2654                 case CMP_NODE_MATH:
2655                         ntype->uifunc = node_buts_math;
2656                         break;
2657                 case CMP_NODE_INVERT:
2658                         ntype->uifunc = node_composit_buts_invert;
2659                         break;
2660                 case CMP_NODE_PREMULKEY:
2661                         ntype->uifunc = node_composit_buts_premulkey;
2662                         break;
2663                 case CMP_NODE_VIEW_LEVELS:
2664                         ntype->uifunc = node_composit_buts_view_levels;
2665                         break;
2666                 case CMP_NODE_COLORBALANCE:
2667                         ntype->uifunc = node_composit_buts_colorbalance;
2668                         ntype->uifuncbut = node_composit_buts_colorbalance_but;
2669                         break;
2670                 case CMP_NODE_HUECORRECT:
2671                         ntype->uifunc = node_composit_buts_huecorrect;
2672                         break;
2673                 case CMP_NODE_ZCOMBINE:
2674                         ntype->uifunc = node_composit_buts_zcombine;
2675                         break;
2676                 case CMP_NODE_COMBYCCA:
2677                 case CMP_NODE_SEPYCCA:
2678                         ntype->uifunc = node_composit_buts_ycc;
2679                         break;
2680                 case CMP_NODE_MOVIECLIP:
2681                         ntype->uifunc = node_composit_buts_movieclip;
2682                         break;
2683                 case CMP_NODE_STABILIZE2D:
2684                         ntype->uifunc = node_composit_buts_stabilize2d;
2685                         break;
2686                 case CMP_NODE_TRANSFORM:
2687                         ntype->uifunc = node_composit_buts_transform;
2688                         break;
2689                 case CMP_NODE_MOVIEDISTORTION:
2690                         ntype->uifunc = node_composit_buts_moviedistortion;
2691                         break;
2692                 case CMP_NODE_COLORCORRECTION:
2693                         ntype->uifunc = node_composit_buts_colorcorrection;
2694                         ntype->uifuncbut = node_composit_buts_colorcorrection_but;
2695                         break;
2696                 case CMP_NODE_SWITCH:
2697                         ntype->uifunc = node_composit_buts_switch;
2698                         break;
2699                 case CMP_NODE_MASK_BOX:
2700                         ntype->uifunc = node_composit_buts_boxmask;
2701                         ntype->uibackdropfunc = node_composit_backdrop_boxmask;
2702                         break;
2703                 case CMP_NODE_MASK_ELLIPSE:
2704                         ntype->uifunc = node_composit_buts_ellipsemask;
2705                         ntype->uibackdropfunc = node_composit_backdrop_ellipsemask;
2706                         break;
2707                 case CMP_NODE_BOKEHIMAGE:
2708                         ntype->uifunc = node_composit_buts_bokehimage;
2709                         break;
2710                 case CMP_NODE_VIEWER:
2711                         ntype->uifunc = NULL;
2712                         ntype->uifuncbut = node_composit_buts_viewer_but;
2713                         ntype->uibackdropfunc = node_composit_backdrop_viewer;
2714                         break;
2715                 case CMP_NODE_MASK:
2716                         ntype->uifunc= node_composit_buts_mask;
2717                         break;
2718                 case CMP_NODE_KEYINGSCREEN:
2719                         ntype->uifunc = node_composit_buts_keyingscreen;
2720                         break;
2721                 case CMP_NODE_KEYING:
2722                         ntype->uifunc = node_composit_buts_keying;
2723                         break;
2724                 case CMP_NODE_TRACKPOS:
2725                         ntype->uifunc = node_composit_buts_trackpos;
2726                         break;
2727                 default:
2728                         ntype->uifunc = NULL;
2729         }
2730 }
2731
2732 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2733
2734 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2735 {
2736         uiLayout *col;
2737         
2738         col = uiLayoutColumn(layout, TRUE);
2739         uiItemR(col, ptr, "offset", 0, IFACE_("Offset"), ICON_NONE);
2740         uiItemR(col, ptr, "offset_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2741         
2742         col = uiLayoutColumn(layout, TRUE);
2743         uiItemR(col, ptr, "squash", 0, IFACE_("Squash"), ICON_NONE);
2744         uiItemR(col, ptr, "squash_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2745 }
2746
2747 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2748 {
2749         PointerRNA tex_ptr;
2750         bNode *node = ptr->data;
2751         ID *id = ptr->id.data;
2752         Tex *tex = (Tex *)node->storage;
2753         uiLayout *col, *row;
2754         
2755         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2756
2757         col = uiLayoutColumn(layout, FALSE);
2758
2759         switch (tex->type) {
2760                 case TEX_BLEND:
2761                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2762                         row = uiLayoutRow(col, FALSE);
2763                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2764                         break;
2765
2766                 case TEX_MARBLE:
2767                         row = uiLayoutRow(col, FALSE);
2768                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2769                         row = uiLayoutRow(col, FALSE);
2770                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2771                         row = uiLayoutRow(col, FALSE);
2772                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2773                         row = uiLayoutRow(col, FALSE);
2774                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2775                         break;
2776
2777                 case TEX_MAGIC:
2778                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2779                         break;
2780
2781                 case TEX_STUCCI:
2782                         row = uiLayoutRow(col, FALSE);
2783                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2784                         row = uiLayoutRow(col, FALSE);
2785                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2786                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2787                         break;
2788
2789                 case TEX_WOOD:
2790                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2791                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2792                         row = uiLayoutRow(col, FALSE);
2793                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2794                         row = uiLayoutRow(col, FALSE);
2795                         uiLayoutSetActive(row, !(RNA_enum_get(&tex_ptr, "wood_type") == TEX_BAND || RNA_enum_get(&tex_ptr, "wood_type") == TEX_RING));
2796                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2797                         break;
2798                         
2799                 case TEX_CLOUDS:
2800                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2801                         row = uiLayoutRow(col, FALSE);
2802                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2803                         row = uiLayoutRow(col, FALSE);
2804                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2805                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, IFACE_("Depth"), ICON_NONE);
2806                         break;
2807                         
2808                 case TEX_DISTNOISE:
2809                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2810                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2811                         break;
2812
2813                 case TEX_MUSGRAVE:
2814                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2815                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2816                         break;
2817                 case TEX_VORONOI:
2818                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2819                         if (tex->vn_distm == TEX_MINKOVSKY) {
2820                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2821                         }
2822                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2823                         break;
2824         }
2825 }
2826
2827 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2828 {
2829         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2830 }
2831
2832 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2833 {
2834         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2835 }
2836
2837 /* only once called */
2838 static void node_texture_set_butfunc(bNodeType *ntype)
2839 {
2840         if (ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX) {
2841                 ntype->uifunc = node_texture_buts_proc;
2842         }
2843         else {
2844                 switch (ntype->type) {
2845
2846                         case TEX_NODE_MATH:
2847                                 ntype->uifunc = node_buts_math;
2848                                 break;
2849
2850                         case TEX_NODE_MIX_RGB:
2851                                 ntype->uifunc = node_buts_mix_rgb;
2852                                 break;
2853
2854                         case TEX_NODE_VALTORGB:
2855                                 ntype->uifunc = node_buts_colorramp;
2856                                 break;
2857
2858                         case TEX_NODE_CURVE_RGB:
2859                                 ntype->uifunc = node_buts_curvecol;
2860                                 break;
2861
2862                         case TEX_NODE_CURVE_TIME:
2863                                 ntype->uifunc = node_buts_time;
2864                                 break;
2865
2866                         case TEX_NODE_TEXTURE:
2867                                 ntype->uifunc = node_buts_texture;
2868                                 break;
2869
2870                         case TEX_NODE_BRICKS:
2871                                 ntype->uifunc = node_texture_buts_bricks;
2872                                 break;
2873
2874                         case TEX_NODE_IMAGE:
2875                                 ntype->uifunc = node_texture_buts_image;
2876                                 break;
2877
2878                         case TEX_NODE_OUTPUT:
2879                                 ntype->uifunc = node_texture_buts_output;
2880                                 break;
2881                 }
2882         }
2883 }
2884
2885 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2886
2887 void ED_init_node_butfuncs(void)
2888 {
2889         bNodeTreeType *treetype;
2890         bNodeType *ntype;
2891         bNodeSocketType *stype;
2892         int i;
2893         
2894         /* node type ui functions */
2895         for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2896                 treetype = ntreeGetType(i);
2897                 if (treetype) {
2898                         for (ntype = treetype->node_types.first; ntype; ntype = ntype->next) {
2899                                 /* default ui functions */
2900                                 ntype->drawfunc = node_draw_default;
2901                                 ntype->drawupdatefunc = node_update_default;
2902                                 ntype->select_area_func = node_select_area_default;
2903                                 ntype->tweak_area_func = node_tweak_area_default;
2904                                 ntype->uifunc = NULL;
2905                                 ntype->uifuncbut = NULL;
2906                                 ntype->drawinputfunc = node_draw_input_default;
2907                                 ntype->drawoutputfunc = node_draw_output_default;
2908                                 ntype->resize_area_func = node_resize_area_default;
2909                                 
2910                                 node_common_set_butfunc(ntype);
2911                                 
2912                                 switch (i) {
2913                                         case NTREE_COMPOSIT:
2914                                                 node_composit_set_butfunc(ntype);
2915                                                 break;
2916                                         case NTREE_SHADER:
2917                                                 node_shader_set_butfunc(ntype);
2918                                                 break;
2919                                         case NTREE_TEXTURE:
2920                                                 node_texture_set_butfunc(ntype);
2921                                                 break;
2922                                 }
2923                         }
2924                 }
2925         }
2926         
2927         /* socket type ui functions */
2928         for (i = 0; i < NUM_SOCKET_TYPES; ++i) {
2929                 stype = ntreeGetSocketType(i);
2930                 if (stype) {
2931                         switch (stype->type) {
2932                                 case SOCK_FLOAT:
2933                                 case SOCK_INT:
2934                                 case SOCK_BOOLEAN:
2935                                         stype->buttonfunc = node_socket_button_default;
2936                                         break;
2937                                 case SOCK_VECTOR:
2938                                         stype->buttonfunc = node_socket_button_components;
2939                                         break;
2940                                 case SOCK_RGBA:
2941                                         stype->buttonfunc = node_socket_button_color;
2942                                         break;
2943                                 case SOCK_SHADER:
2944                                         stype->buttonfunc = node_socket_button_label;
2945                                         break;
2946                                 default:
2947                                         stype->buttonfunc = NULL;
2948                         }
2949                 }
2950         }
2951 }
2952
2953 /* ************** Generic drawing ************** */
2954
2955 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
2956 {
2957         
2958         if ((snode->flag & SNODE_BACKDRAW) && snode->treetype == NTREE_COMPOSIT) {
2959                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2960                 void *lock;
2961                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
2962                 if (ibuf) {
2963                         float x, y; 
2964                         
2965                         glMatrixMode(GL_PROJECTION);
2966                         glPushMatrix();
2967                         glMatrixMode(GL_MODELVIEW);
2968                         glPushMatrix();
2969
2970                         /* keep this, saves us from a version patch */
2971                         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2972                         
2973                         /* somehow the offset has to be calculated inverse */
2974                         
2975                         glaDefine2DArea(&ar->winrct);
2976                         /* ortho at pixel level curarea */
2977                         wmOrtho2(-0.375, ar->winx - 0.375, -0.375, ar->winy - 0.375);
2978                         
2979                         x = (ar->winx - snode->zoom * ibuf->x) / 2 + snode->xof;
2980                         y = (ar->winy - snode->zoom * ibuf->y) / 2 + snode->yof;
2981                         
2982                         if (!ibuf->rect) {
2983                                 if (color_manage)
2984                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
2985                                 else
2986                                         ibuf->profile = IB_PROFILE_NONE;
2987                                 IMB_rect_from_float(ibuf);
2988                         }
2989
2990                         if (ibuf->rect) {
2991                                 if (snode->flag & SNODE_SHOW_ALPHA) {
2992                                         glPixelZoom(snode->zoom, snode->zoom);
2993                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
2994                                         if (ENDIAN_ORDER == B_ENDIAN)
2995                                                 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
2996                                         
2997                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
2998                                         
2999                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
3000                                         glPixelZoom(1.0f, 1.0f);
3001                                 }
3002                                 else if (snode->flag & SNODE_USE_ALPHA) {
3003                                         glEnable(GL_BLEND);
3004                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3005                                         glPixelZoom(snode->zoom, snode->zoom);
3006                                         
3007                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
3008                                         
3009                                         glPixelZoom(1.0f, 1.0f);
3010                                         glDisable(GL_BLEND);
3011                                 }
3012                                 else {
3013                                         glPixelZoom(snode->zoom, snode->zoom);
3014                                         
3015                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
3016                                         
3017                                         glPixelZoom(1.0f, 1.0f);
3018                                 }
3019                         }
3020
3021                         /// @note draw selected info on backdrop
3022                         if (snode->edittree) {
3023                                 bNode *node = snode->edittree->nodes.first;
3024                                 while (node) {
3025                                         if (node->flag & NODE_SELECT) {
3026                                                 if (node->typeinfo->uibackdropfunc) {
3027                                                         node->typeinfo->uibackdropfunc(snode, ibuf, node, x, y);
3028                                                 }
3029                                         }
3030                                         node = node->next;
3031                                 }
3032                         }
3033                         
3034                         glMatrixMode(GL_PROJECTION);
3035                         glPopMatrix();
3036                         glMatrixMode(GL_MODELVIEW);
3037                         glPopMatrix();
3038                 }
3039
3040                 BKE_image_release_ibuf(ima, lock);
3041         }
3042 }
3043
3044 #if 0
3045 /* note: needs to be userpref or opengl profile option */
3046 static void draw_nodespace_back_tex(ScrArea *sa, SpaceNode *snode)
3047 {
3048
3049         draw_nodespace_grid(snode);
3050         
3051         if (snode->flag & SNODE_BACKDRAW) {
3052                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
3053                 ImBuf *ibuf = BKE_image_get_ibuf(ima, NULL);
3054                 if (ibuf) {
3055                         int x, y;
3056                         float zoom = 1.0;
3057
3058                         glMatrixMode(GL_PROJECTION);
3059                         glPushMatrix();
3060                         glMatrixMode(GL_MODELVIEW);
3061                         glPushMatrix();
3062                         
3063                         glaDefine2DArea(&sa->winrct);
3064
3065                         if (ibuf->x > sa->winx || ibuf->y > sa->winy) {
3066                                 float zoomx, zoomy;
3067                                 zoomx = (float)sa->winx / ibuf->x;
3068                                 zoomy = (float)sa->winy / ibuf->y;
3069                                 zoom = MIN2(zoomx, zoomy);
3070                         }
3071                         
3072                         x = (sa->winx - zoom * ibuf->x) / 2 + snode->xof;
3073                         y = (sa->winy - zoom * ibuf->y) / 2 + snode->yof;
3074
3075                         glPixelZoom(zoom, zoom);
3076
3077                         glColor4f(1.0, 1.0, 1.0, 1.0);
3078                         if (ibuf->rect)
3079                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
3080                         else if (ibuf->channels == 4)
3081                                 glaDrawPixelsTex(x, y, ibuf->x, ibuf->y, GL_FLOAT, ibuf->rect_float);
3082
3083                         glPixelZoom(1.0, 1.0);
3084
3085                         glMatrixMode(GL_PROJECTION);
3086                         glPopMatrix();
3087                         glMatrixMode(GL_MODELVIEW);
3088                         glPopMatrix();
3089                 }
3090         }
3091 }
3092 #endif
3093
3094 /* if v2d not NULL, it clips and returns 0 if not visible */
3095 int node_link_bezier_points(View2D *v2d, SpaceNode *snode, bNodeLink *link, float coord_array[][2], int resol)
3096 {
3097         float dist, vec[4][2];
3098         float deltax, deltay;
3099         int toreroute, fromreroute;
3100         /* in v0 and v3 we put begin/end points */
3101         if (link->fromsock) {
3102                 vec[0][0] = link->fromsock->locx;
3103                 vec[0][1] = link->fromsock->locy;
3104                 fromreroute = (link->fromnode && link->fromnode->type == NODE_REROUTE);
3105         }
3106         else {
3107                 if (snode == NULL) return 0;
3108                 vec[0][0] = snode->mx;
3109                 vec[0][1] = snode->my;