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