2a5739db56f8c94d3af24945cbe3424ea1ff9510
[blender.git] / source / blender / editors / space_node / drawnode.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): David Millan Escriva, Juho Vepsäläinen, Bob Holcomb, Thomas Dinges
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_node/drawnode.c
29  *  \ingroup spnode
30  *  \brief lower level node drawing for nodes (boarders, headers etc), also node layout.
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 = UI_GetStyle()->widgetlabel.uifont_id;
973         NodeFrame *data = (NodeFrame *)node->storage;
974         rctf *rct = &node->totr;
975         int color_id = node_get_colorid(node);
976         const char *label = nodeLabel(node);
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         BLF_enable(fontid, BLF_ASPECT);
983         BLF_aspect(fontid, aspect, aspect, 1.0f);
984         BLF_size(fontid, MIN2(24, font_size), U.dpi); /* clamp otherwise it can suck up a LOT of memory */
985         
986         /* title color */
987         UI_ThemeColorBlendShade(TH_TEXT, color_id, 0.8f, 10);
988
989         width = BLF_width(fontid, label);
990         ascender = BLF_ascender(fontid);
991         
992         /* 'x' doesn't need aspect correction */
993         x = 0.5f * (rct->xmin + rct->xmax) - 0.5f * width;
994         y = rct->ymax - (((NODE_DY / 4) / aspect) + (ascender * aspect));
995
996         BLF_position(fontid, x, y, 0);
997         BLF_draw(fontid, label, BLF_DRAW_STR_DUMMY_MAX);
998
999         BLF_disable(fontid, BLF_ASPECT);
1000 }
1001
1002 static void node_draw_frame(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *UNUSED(ntree), bNode *node)
1003 {
1004         rctf *rct = &node->totr;
1005         int color_id = node_get_colorid(node);
1006         unsigned char color[4];
1007         float alpha;
1008         
1009         /* skip if out of view */
1010         if (node->totr.xmax < ar->v2d.cur.xmin || node->totr.xmin > ar->v2d.cur.xmax ||
1011             node->totr.ymax < ar->v2d.cur.ymin || node->totr.ymin > ar->v2d.cur.ymax) {
1012                 
1013                 uiEndBlock(C, node->block);
1014                 node->block = NULL;
1015                 return;
1016         }
1017
1018         UI_GetThemeColor4ubv(TH_NODE_FRAME, color);
1019         alpha = (float)(color[3]) / 255.0f;
1020         
1021         /* shadow */
1022         node_draw_shadow(snode, node, BASIS_RAD, alpha);
1023         
1024         /* body */
1025         if (node->flag & NODE_CUSTOM_COLOR)
1026                 glColor4f(node->color[0], node->color[1], node->color[2], alpha);
1027         else
1028                 UI_ThemeColor4(TH_NODE_FRAME);
1029         glEnable(GL_BLEND);
1030         uiSetRoundBox(UI_CNR_ALL);
1031         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, BASIS_RAD);
1032         glDisable(GL_BLEND);
1033
1034         /* outline active and selected emphasis */
1035         if (node->flag & (NODE_ACTIVE | SELECT)) {
1036                 glEnable(GL_BLEND);
1037                 glEnable(GL_LINE_SMOOTH);
1038                 
1039                 if (node->flag & NODE_ACTIVE)
1040                         UI_ThemeColorShadeAlpha(TH_ACTIVE, 0, -40);
1041                 else
1042                         UI_ThemeColorShadeAlpha(TH_SELECT, 0, -40);
1043                 uiSetRoundBox(UI_CNR_ALL);
1044                 uiDrawBox(GL_LINE_LOOP,
1045                           rct->xmin, rct->ymin,
1046                           rct->xmax, rct->ymax, BASIS_RAD);
1047                 
1048                 glDisable(GL_LINE_SMOOTH);
1049                 glDisable(GL_BLEND);
1050         }
1051         
1052         /* label */
1053         node_draw_frame_label(node, snode->aspect);
1054         
1055         UI_ThemeClearColor(color_id);
1056                 
1057         uiEndBlock(C, node->block);
1058         uiDrawBlock(C, node->block);
1059         node->block = NULL;
1060 }
1061
1062 static int node_resize_area_frame(bNode *node, int x, int y)
1063 {
1064         const float size = 10.0f;
1065         NodeFrame *data = (NodeFrame *)node->storage;
1066         rctf totr = node->totr;
1067         int dir = 0;
1068         
1069         /* shrinking frame size is determined by child nodes */
1070         if (!(data->flag & NODE_FRAME_RESIZEABLE))
1071                 return 0;
1072         
1073         if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax)
1074                 dir |= NODE_RESIZE_RIGHT;
1075         if (x >= totr.xmin && x < totr.xmin + size && y >= totr.ymin && y < totr.ymax)
1076                 dir |= NODE_RESIZE_LEFT;
1077         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymax - size && y < totr.ymax)
1078                 dir |= NODE_RESIZE_TOP;
1079         if (x >= totr.xmin && x < totr.xmax && y >= totr.ymin && y < totr.ymin + size)
1080                 dir |= NODE_RESIZE_BOTTOM;
1081         
1082         return dir;
1083 }
1084
1085 static void node_buts_frame_details(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1086 {
1087         uiItemR(layout, ptr, "label_size", 0, IFACE_("Label Size"), ICON_NONE);
1088         uiItemR(layout, ptr, "shrink", 0, IFACE_("Shrink"), ICON_NONE);
1089 }
1090
1091
1092 #define NODE_REROUTE_SIZE   8.0f
1093
1094 static void node_update_reroute(const bContext *UNUSED(C), bNodeTree *UNUSED(ntree), bNode *node)
1095 {
1096         bNodeSocket *nsock;
1097         float locx, locy;
1098         float size = NODE_REROUTE_SIZE;
1099         
1100         /* get "global" coords */
1101         nodeToView(node, 0.0f, 0.0f, &locx, &locy);
1102         
1103         /* reroute node has exactly one input and one output, both in the same place */
1104         nsock = node->outputs.first;
1105         nsock->locx = locx;
1106         nsock->locy = locy;
1107
1108         nsock = node->inputs.first;
1109         nsock->locx = locx;
1110         nsock->locy = locy;
1111
1112         node->width = size * 2;
1113         node->totr.xmin = locx - size;
1114         node->totr.xmax = locx + size;
1115         node->totr.ymax = locy + size;
1116         node->totr.ymin = locy - size;
1117 }
1118
1119 static void node_draw_reroute(const bContext *C, ARegion *ar, SpaceNode *UNUSED(snode),  bNodeTree *ntree, bNode *node)
1120 {
1121         bNodeSocket *sock;
1122 #if 0   /* UNUSED */
1123         rctf *rct = &node->totr;
1124         float size = NODE_REROUTE_SIZE;
1125 #endif
1126         float socket_size = NODE_SOCKSIZE;
1127
1128         /* skip if out of view */
1129         if (node->totr.xmax < ar->v2d.cur.xmin || node->totr.xmin > ar->v2d.cur.xmax ||
1130             node->totr.ymax < ar->v2d.cur.ymin || node->totr.ymin > ar->v2d.cur.ymax) {
1131
1132                 uiEndBlock(C, node->block);
1133                 node->block = NULL;
1134                 return;
1135         }
1136
1137         /* XXX only kept for debugging
1138          * selection state is indicated by socket outline below!
1139          */
1140 #if 0
1141         /* body */
1142         uiSetRoundBox(15);
1143         UI_ThemeColor4(TH_NODE);
1144         glEnable(GL_BLEND);
1145         uiRoundBox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
1146         glDisable(GL_BLEND);
1147
1148         /* outline active and selected emphasis */
1149         if (node->flag & (NODE_ACTIVE | SELECT)) {
1150                 glEnable(GL_BLEND);
1151                 glEnable(GL_LINE_SMOOTH);
1152                 /* using different shades of TH_TEXT_HI for the empasis, like triangle */
1153                 if (node->flag & NODE_ACTIVE)
1154                         UI_ThemeColorShadeAlpha(TH_TEXT_HI, 0, -40);
1155                 else
1156                         UI_ThemeColorShadeAlpha(TH_TEXT_HI, -20, -120);
1157                 uiDrawBox(GL_LINE_LOOP, rct->xmin, rct->ymin, rct->xmax, rct->ymax, size);
1158
1159                 glDisable(GL_LINE_SMOOTH);
1160                 glDisable(GL_BLEND);
1161         }
1162 #endif
1163
1164         /* only draw input socket. as they all are placed on the same position.
1165          * highlight also if node itself is selected, since we don't display the node body separately!
1166          */
1167         for (sock = node->inputs.first; sock; sock = sock->next) {
1168                 node_socket_circle_draw(ntree, sock, socket_size, (sock->flag & SELECT) || (node->flag & SELECT));
1169         }
1170
1171         uiEndBlock(C, node->block);
1172         uiDrawBlock(C, node->block);
1173         node->block = NULL;
1174 }
1175
1176 /* Special tweak area for reroute node.
1177  * Since this node is quite small, we use a larger tweak area for grabbing than for selection.
1178  */
1179 static int node_tweak_area_reroute(bNode *node, int x, int y)
1180 {
1181         /* square of tweak radius */
1182         static const float tweak_radius_sq = 576;  /* 24 * 24 */
1183         
1184         bNodeSocket *sock = node->inputs.first;
1185         float dx = sock->locx - x;
1186         float dy = sock->locy - y;
1187         return (dx * dx + dy * dy <= tweak_radius_sq);
1188 }
1189
1190 static void node_common_set_butfunc(bNodeType *ntype)
1191 {
1192         switch (ntype->type) {
1193                 case NODE_GROUP:
1194                         ntype->uifunc = node_uifunc_group;
1195                         ntype->drawfunc = node_draw_group;
1196                         ntype->drawupdatefunc = node_update_group;
1197                         break;
1198                 case NODE_FORLOOP:
1199 //                      ntype->uifunc= node_common_buts_group;
1200                         ntype->drawfunc = node_draw_group;
1201                         ntype->drawupdatefunc = node_update_group;
1202                         break;
1203                 case NODE_WHILELOOP:
1204                         ntype->uifunc = node_common_buts_whileloop;
1205                         ntype->drawfunc = node_draw_group;
1206                         ntype->drawupdatefunc = node_update_group;
1207                         break;
1208                 case NODE_FRAME:
1209                         ntype->drawfunc = node_draw_frame;
1210                         ntype->drawupdatefunc = node_update_frame;
1211                         ntype->uifuncbut = node_buts_frame_details;
1212                         ntype->resize_area_func = node_resize_area_frame;
1213                         break;
1214                 case NODE_REROUTE:
1215                         ntype->drawfunc = node_draw_reroute;
1216                         ntype->drawupdatefunc = node_update_reroute;
1217                         ntype->tweak_area_func = node_tweak_area_reroute;
1218                         break;
1219         }
1220 }
1221
1222 /* ****************** BUTTON CALLBACKS FOR SHADER NODES ***************** */
1223
1224 static void node_buts_image_user(uiLayout *layout, bContext *C, PointerRNA *ptr,
1225                                  PointerRNA *imaptr, PointerRNA *iuserptr)
1226 {
1227         uiLayout *col;
1228         int source;
1229
1230         if (!imaptr->data)
1231                 return;
1232
1233         col = uiLayoutColumn(layout, FALSE);
1234         
1235         uiItemR(col, imaptr, "source", 0, "", ICON_NONE);
1236         
1237         source = RNA_enum_get(imaptr, "source");
1238
1239         if (source == IMA_SRC_SEQUENCE) {
1240                 /* don't use iuser->framenr directly because it may not be updated if auto-refresh is off */
1241                 Scene *scene = CTX_data_scene(C);
1242                 ImageUser *iuser = iuserptr->data;
1243                 char numstr[32];
1244                 const int framenr = BKE_image_user_frame_get(iuser, CFRA, 0, NULL);
1245                 BLI_snprintf(numstr, sizeof(numstr), IFACE_("Frame: %d"), framenr);
1246                 uiItemL(layout, numstr, ICON_NONE);
1247         }
1248
1249         if (ELEM(source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1250                 col = uiLayoutColumn(layout, TRUE);
1251                 uiItemR(col, ptr, "frame_duration", 0, NULL, ICON_NONE);
1252                 uiItemR(col, ptr, "frame_start", 0, NULL, ICON_NONE);
1253                 uiItemR(col, ptr, "frame_offset", 0, NULL, ICON_NONE);
1254                 uiItemR(col, ptr, "use_cyclic", 0, NULL, ICON_NONE);
1255                 uiItemR(col, ptr, "use_auto_refresh", UI_ITEM_R_ICON_ONLY, NULL, ICON_NONE);
1256         }
1257
1258         col = uiLayoutColumn(layout, FALSE);
1259
1260         if (RNA_enum_get(imaptr, "type") == IMA_TYPE_MULTILAYER)
1261                 uiItemR(col, ptr, "layer", 0, NULL, ICON_NONE);
1262 }
1263
1264 static void node_shader_buts_material(uiLayout *layout, bContext *C, PointerRNA *ptr)
1265 {
1266         bNode *node = ptr->data;
1267         uiLayout *col;
1268         
1269         uiTemplateID(layout, C, ptr, "material", "MATERIAL_OT_new", NULL, NULL);
1270         
1271         if (!node->id) return;
1272         
1273         col = uiLayoutColumn(layout, FALSE);
1274         uiItemR(col, ptr, "use_diffuse", 0, NULL, ICON_NONE);
1275         uiItemR(col, ptr, "use_specular", 0, NULL, ICON_NONE);
1276         uiItemR(col, ptr, "invert_normal", 0, NULL, ICON_NONE);
1277 }
1278
1279 static void node_shader_buts_mapping(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1280 {
1281         uiLayout *row;
1282         
1283         uiItemL(layout, IFACE_("Location:"), ICON_NONE);
1284         row = uiLayoutRow(layout, TRUE);
1285         uiItemR(row, ptr, "translation", 0, "", ICON_NONE);
1286         
1287         uiItemL(layout, IFACE_("Rotation:"), ICON_NONE);
1288         row = uiLayoutRow(layout, TRUE);
1289         uiItemR(row, ptr, "rotation", 0, "", ICON_NONE);
1290         
1291         uiItemL(layout, IFACE_("Scale:"), ICON_NONE);
1292         row = uiLayoutRow(layout, TRUE);
1293         uiItemR(row, ptr, "scale", 0, "", ICON_NONE);
1294         
1295         row = uiLayoutRow(layout, TRUE);
1296         uiItemR(row, ptr, "use_min", 0, "Min", ICON_NONE);
1297         uiItemR(row, ptr, "min", 0, "", ICON_NONE);
1298         
1299         row = uiLayoutRow(layout, TRUE);
1300         uiItemR(row, ptr, "use_max", 0, "Max", ICON_NONE);
1301         uiItemR(row, ptr, "max", 0, "", ICON_NONE);
1302 }
1303
1304 static void node_shader_buts_vect_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1305
1306         uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);
1307 }
1308
1309 static void node_shader_buts_geometry(uiLayout *layout, bContext *C, PointerRNA *ptr)
1310 {
1311         PointerRNA obptr = CTX_data_pointer_get(C, "active_object");
1312         uiLayout *col;
1313
1314         col = uiLayoutColumn(layout, FALSE);
1315
1316         if (obptr.data && RNA_enum_get(&obptr, "type") == OB_MESH) {
1317                 PointerRNA dataptr = RNA_pointer_get(&obptr, "data");
1318
1319                 uiItemPointerR(col, ptr, "uv_layer", &dataptr, "uv_textures", "", ICON_NONE);
1320                 uiItemPointerR(col, ptr, "color_layer", &dataptr, "vertex_colors", "", ICON_NONE);
1321         }
1322         else {
1323                 uiItemR(col, ptr, "uv_layer", 0, IFACE_("UV"), ICON_NONE);
1324                 uiItemR(col, ptr, "color_layer", 0, IFACE_("VCol"), ICON_NONE);
1325         }
1326 }
1327
1328 static void node_shader_buts_attribute(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1329 {
1330         uiItemR(layout, ptr, "attribute_name", 0, IFACE_("Name"), ICON_NONE);
1331 }
1332
1333 static void node_shader_buts_tex_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1334 {
1335         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
1336         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
1337
1338         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1339         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1340
1341         /* note: image user properties used directly here, unlike compositor image node,
1342          * which redefines them in the node struct RNA to get proper updates.
1343          */
1344         node_buts_image_user(layout, C, &iuserptr, &imaptr, &iuserptr);
1345 }
1346
1347 static void node_shader_buts_tex_environment(uiLayout *layout, bContext *C, PointerRNA *ptr)
1348 {
1349         PointerRNA imaptr = RNA_pointer_get(ptr, "image");
1350         PointerRNA iuserptr = RNA_pointer_get(ptr, "image_user");
1351
1352         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1353         uiItemR(layout, ptr, "color_space", 0, "", ICON_NONE);
1354         uiItemR(layout, ptr, "projection", 0, "", ICON_NONE);
1355
1356         node_buts_image_user(layout, C, ptr, &imaptr, &iuserptr);
1357 }
1358
1359 static void node_shader_buts_tex_sky(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1360 {
1361         uiItemR(layout, ptr, "sun_direction", 0, "", ICON_NONE);
1362         uiItemR(layout, ptr, "turbidity", 0, NULL, ICON_NONE);
1363 }
1364
1365 static void node_shader_buts_tex_gradient(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1366 {
1367         uiItemR(layout, ptr, "gradient_type", 0, "", ICON_NONE);
1368 }
1369
1370 static void node_shader_buts_tex_magic(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1371 {
1372         uiItemR(layout, ptr, "turbulence_depth", 0, NULL, ICON_NONE);
1373 }
1374
1375 static void node_shader_buts_tex_wave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1376 {
1377         uiItemR(layout, ptr, "wave_type", 0, "", ICON_NONE);
1378 }
1379
1380 static void node_shader_buts_tex_musgrave(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1381 {
1382         uiItemR(layout, ptr, "musgrave_type", 0, "", ICON_NONE);
1383 }
1384
1385 static void node_shader_buts_tex_voronoi(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1386 {
1387         uiItemR(layout, ptr, "coloring", 0, "", ICON_NONE);
1388 }
1389
1390 static void node_shader_buts_glossy(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1391 {
1392         uiItemR(layout, ptr, "distribution", 0, "", ICON_NONE);
1393 }
1394
1395 /* only once called */
1396 static void node_shader_set_butfunc(bNodeType *ntype)
1397 {
1398         switch (ntype->type) {
1399                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
1400
1401                 case SH_NODE_MATERIAL:
1402                 case SH_NODE_MATERIAL_EXT:
1403                         ntype->uifunc = node_shader_buts_material;
1404                         break;
1405                 case SH_NODE_TEXTURE:
1406                         ntype->uifunc = node_buts_texture;
1407                         break;
1408                 case SH_NODE_NORMAL:
1409                         ntype->uifunc = node_buts_normal;
1410                         break;
1411                 case SH_NODE_CURVE_VEC:
1412                         ntype->uifunc = node_buts_curvevec;
1413                         break;
1414                 case SH_NODE_CURVE_RGB:
1415                         ntype->uifunc = node_buts_curvecol;
1416                         break;
1417                 case SH_NODE_MAPPING:
1418                         ntype->uifunc = node_shader_buts_mapping;
1419                         break;
1420                 case SH_NODE_VALUE:
1421                         ntype->uifunc = node_buts_value;
1422                         break;
1423                 case SH_NODE_RGB:
1424                         ntype->uifunc = node_buts_rgb;
1425                         break;
1426                 case SH_NODE_MIX_RGB:
1427                         ntype->uifunc = node_buts_mix_rgb;
1428                         break;
1429                 case SH_NODE_VALTORGB:
1430                         ntype->uifunc = node_buts_colorramp;
1431                         break;
1432                 case SH_NODE_MATH: 
1433                         ntype->uifunc = node_buts_math;
1434                         break; 
1435                 case SH_NODE_VECT_MATH: 
1436                         ntype->uifunc = node_shader_buts_vect_math;
1437                         break; 
1438                 case SH_NODE_GEOMETRY:
1439                         ntype->uifunc = node_shader_buts_geometry;
1440                         break;
1441                 case SH_NODE_ATTRIBUTE:
1442                         ntype->uifunc = node_shader_buts_attribute;
1443                         break;
1444                 case SH_NODE_TEX_SKY:
1445                         ntype->uifunc = node_shader_buts_tex_sky;
1446                         break;
1447                 case SH_NODE_TEX_IMAGE:
1448                         ntype->uifunc = node_shader_buts_tex_image;
1449                         break;
1450                 case SH_NODE_TEX_ENVIRONMENT:
1451                         ntype->uifunc = node_shader_buts_tex_environment;
1452                         break;
1453                 case SH_NODE_TEX_GRADIENT:
1454                         ntype->uifunc = node_shader_buts_tex_gradient;
1455                         break;
1456                 case SH_NODE_TEX_MAGIC:
1457                         ntype->uifunc = node_shader_buts_tex_magic;
1458                         break;
1459                 case SH_NODE_TEX_WAVE:
1460                         ntype->uifunc = node_shader_buts_tex_wave;
1461                         break;
1462                 case SH_NODE_TEX_MUSGRAVE:
1463                         ntype->uifunc = node_shader_buts_tex_musgrave;
1464                         break;
1465                 case SH_NODE_TEX_VORONOI:
1466                         ntype->uifunc = node_shader_buts_tex_voronoi;
1467                         break;
1468                 case SH_NODE_BSDF_GLOSSY:
1469                 case SH_NODE_BSDF_GLASS:
1470                         ntype->uifunc = node_shader_buts_glossy;
1471                         break;
1472         }
1473 }
1474
1475 /* ****************** BUTTON CALLBACKS FOR COMPOSITE NODES ***************** */
1476
1477 static void node_composit_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
1478 {
1479         bNode *node = ptr->data;
1480         PointerRNA imaptr, iuserptr;
1481         
1482         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
1483         
1484         if (!node->id) return;
1485         
1486         imaptr = RNA_pointer_get(ptr, "image");
1487         RNA_pointer_create((ID *)ptr->id.data, &RNA_ImageUser, node->storage, &iuserptr);
1488         
1489         node_buts_image_user(layout, C, ptr, &imaptr, &iuserptr);
1490 }
1491
1492 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
1493 {
1494         bNode *node = ptr->data;
1495         uiLayout *col, *row;
1496         PointerRNA op_ptr;
1497         PointerRNA scn_ptr;
1498         PropertyRNA *prop;
1499         const char *layer_name;
1500         char scene_name[MAX_ID_NAME - 2];
1501         wmOperatorType *ot = WM_operatortype_find("RENDER_OT_render", 1);
1502
1503         BLI_assert(ot != 0);
1504
1505         uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL);
1506         
1507         if (!node->id) return;
1508
1509         col = uiLayoutColumn(layout, FALSE);
1510         row = uiLayoutRow(col, FALSE);
1511         uiItemR(row, ptr, "layer", 0, "", ICON_NONE);
1512         
1513         prop = RNA_struct_find_property(ptr, "layer");
1514         if (!(RNA_property_enum_identifier(C, ptr, prop, RNA_property_enum_get(ptr, prop), &layer_name)))
1515                 return;
1516         
1517         scn_ptr = RNA_pointer_get(ptr, "scene");
1518         RNA_string_get(&scn_ptr, "name", scene_name);
1519         
1520         WM_operator_properties_create_ptr(&op_ptr, ot);
1521         RNA_string_set(&op_ptr, "layer", layer_name);
1522         RNA_string_set(&op_ptr, "scene", scene_name);
1523         uiItemFullO_ptr(row, ot, "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0);
1524
1525 }
1526
1527
1528 static void node_composit_buts_blur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1529 {
1530         uiLayout *col, *row;
1531         
1532         col = uiLayoutColumn(layout, FALSE);
1533         
1534         uiItemR(col, ptr, "filter_type", 0, "", ICON_NONE);
1535         if (RNA_enum_get(ptr, "filter_type") != R_FILTER_FAST_GAUSS) {
1536                 uiItemR(col, ptr, "use_bokeh", 0, NULL, ICON_NONE);
1537                 uiItemR(col, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1538         }
1539         
1540         uiItemR(col, ptr, "use_relative", 0, NULL, ICON_NONE);
1541         
1542         if (RNA_boolean_get(ptr, "use_relative")) {
1543                 uiItemL(col, IFACE_("Aspect Correction"), 0);
1544                 row = uiLayoutRow(layout, TRUE);
1545                 uiItemR(row, ptr, "aspect_correction", UI_ITEM_R_EXPAND, NULL, 0);
1546                 
1547                 col = uiLayoutColumn(layout, TRUE);
1548                 uiItemR(col, ptr, "factor_x", 0, IFACE_("X"), ICON_NONE);
1549                 uiItemR(col, ptr, "factor_y", 0, IFACE_("Y"), ICON_NONE);
1550         }
1551         else {
1552                 col = uiLayoutColumn(layout, TRUE);
1553                 uiItemR(col, ptr, "size_x", 0, IFACE_("X"), ICON_NONE);
1554                 uiItemR(col, ptr, "size_y", 0, IFACE_("Y"), ICON_NONE);
1555         }
1556 }
1557
1558 static void node_composit_buts_dblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1559 {
1560         uiLayout *col;
1561         
1562         uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1563         uiItemR(layout, ptr, "use_wrap", 0, NULL, ICON_NONE);
1564         
1565         col = uiLayoutColumn(layout, TRUE);
1566         uiItemL(col, IFACE_("Center:"), ICON_NONE);
1567         uiItemR(col, ptr, "center_x", 0, IFACE_("X"), ICON_NONE);
1568         uiItemR(col, ptr, "center_y", 0, IFACE_("Y"), ICON_NONE);
1569         
1570         uiItemS(layout);
1571         
1572         col = uiLayoutColumn(layout, TRUE);
1573         uiItemR(col, ptr, "distance", 0, NULL, ICON_NONE);
1574         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1575         
1576         uiItemS(layout);
1577         
1578         uiItemR(layout, ptr, "spin", 0, NULL, ICON_NONE);
1579         uiItemR(layout, ptr, "zoom", 0, NULL, ICON_NONE);
1580 }
1581
1582 static void node_composit_buts_bilateralblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1583 {       
1584         uiLayout *col;
1585         
1586         col = uiLayoutColumn(layout, TRUE);
1587         uiItemR(col, ptr, "iterations", 0, NULL, ICON_NONE);
1588         uiItemR(col, ptr, "sigma_color", 0, NULL, ICON_NONE);
1589         uiItemR(col, ptr, "sigma_space", 0, NULL, ICON_NONE);
1590 }
1591
1592 static void node_composit_buts_defocus(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1593 {
1594         uiLayout *sub, *col;
1595         
1596         col = uiLayoutColumn(layout, FALSE);
1597         uiItemL(col, IFACE_("Bokeh Type:"), ICON_NONE);
1598         uiItemR(col, ptr, "bokeh", 0, "", ICON_NONE);
1599         uiItemR(col, ptr, "angle", 0, NULL, ICON_NONE);
1600
1601         uiItemR(layout, ptr, "use_gamma_correction", 0, NULL, ICON_NONE);
1602
1603         col = uiLayoutColumn(layout, FALSE);
1604         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_zbuffer") == TRUE);
1605         uiItemR(col, ptr, "f_stop", 0, NULL, ICON_NONE);
1606
1607         uiItemR(layout, ptr, "blur_max", 0, NULL, ICON_NONE);
1608         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1609
1610         col = uiLayoutColumn(layout, FALSE);
1611         uiItemR(col, ptr, "use_preview", 0, NULL, ICON_NONE);
1612         
1613         col = uiLayoutColumn(layout, FALSE);
1614         uiItemR(col, ptr, "use_zbuffer", 0, NULL, ICON_NONE);
1615         sub = uiLayoutColumn(col, FALSE);
1616         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_zbuffer") == FALSE);
1617         uiItemR(sub, ptr, "z_scale", 0, NULL, ICON_NONE);
1618 }
1619
1620 /* qdn: glare node */
1621 static void node_composit_buts_glare(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1622 {       
1623         uiItemR(layout, ptr, "glare_type", 0, "", ICON_NONE);
1624         uiItemR(layout, ptr, "quality", 0, "", ICON_NONE);
1625
1626         if (RNA_enum_get(ptr, "glare_type") != 1) {
1627                 uiItemR(layout, ptr, "iterations", 0, NULL, ICON_NONE);
1628         
1629                 if (RNA_enum_get(ptr, "glare_type") != 0)
1630                         uiItemR(layout, ptr, "color_modulation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1631         }
1632         
1633         uiItemR(layout, ptr, "mix", 0, NULL, ICON_NONE);
1634         uiItemR(layout, ptr, "threshold", 0, NULL, ICON_NONE);
1635
1636         if (RNA_enum_get(ptr, "glare_type") == 2) {
1637                 uiItemR(layout, ptr, "streaks", 0, NULL, ICON_NONE);
1638                 uiItemR(layout, ptr, "angle_offset", 0, NULL, ICON_NONE);
1639         }
1640         if (RNA_enum_get(ptr, "glare_type") == 0 || RNA_enum_get(ptr, "glare_type") == 2) {
1641                 uiItemR(layout, ptr, "fade", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1642                 
1643                 if (RNA_enum_get(ptr, "glare_type") == 0)
1644                         uiItemR(layout, ptr, "use_rotate_45", 0, NULL, ICON_NONE);
1645         }
1646         if (RNA_enum_get(ptr, "glare_type") == 1) {
1647                 uiItemR(layout, ptr, "size", 0, NULL, ICON_NONE);
1648         }
1649 }
1650
1651 static void node_composit_buts_tonemap(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1652 {       
1653         uiLayout *col;
1654
1655         col = uiLayoutColumn(layout, FALSE);
1656         uiItemR(col, ptr, "tonemap_type", 0, "", ICON_NONE);
1657         if (RNA_enum_get(ptr, "tonemap_type") == 0) {
1658                 uiItemR(col, ptr, "key", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1659                 uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1660                 uiItemR(col, ptr, "gamma", 0, NULL, ICON_NONE);
1661         }
1662         else {
1663                 uiItemR(col, ptr, "intensity", 0, NULL, ICON_NONE);
1664                 uiItemR(col, ptr, "contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1665                 uiItemR(col, ptr, "adaptation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1666                 uiItemR(col, ptr, "correction", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1667         }
1668 }
1669
1670 static void node_composit_buts_lensdist(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1671 {
1672         uiLayout *col;
1673
1674         col = uiLayoutColumn(layout, FALSE);
1675         uiItemR(col, ptr, "use_projector", 0, NULL, ICON_NONE);
1676
1677         col = uiLayoutColumn(col, FALSE);
1678         uiLayoutSetActive(col, RNA_boolean_get(ptr, "use_projector") == FALSE);
1679         uiItemR(col, ptr, "use_jitter", 0, NULL, ICON_NONE);
1680         uiItemR(col, ptr, "use_fit", 0, NULL, ICON_NONE);
1681 }
1682
1683 static void node_composit_buts_vecblur(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1684 {
1685         uiLayout *col;
1686         
1687         col = uiLayoutColumn(layout, FALSE);
1688         uiItemR(col, ptr, "samples", 0, NULL, ICON_NONE);
1689         uiItemR(col, ptr, "factor", 0, IFACE_("Blur"), ICON_NONE);
1690         
1691         col = uiLayoutColumn(layout, TRUE);
1692         uiItemL(col, IFACE_("Speed:"), ICON_NONE);
1693         uiItemR(col, ptr, "speed_min", 0, IFACE_("Min"), ICON_NONE);
1694         uiItemR(col, ptr, "speed_max", 0, IFACE_("Max"), ICON_NONE);
1695
1696         uiItemR(layout, ptr, "use_curved", 0, NULL, ICON_NONE);
1697 }
1698
1699 static void node_composit_buts_filter(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1700 {
1701         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
1702 }
1703
1704 static void node_composit_buts_flip(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1705 {
1706         uiItemR(layout, ptr, "axis", 0, "", ICON_NONE);
1707 }
1708
1709 static void node_composit_buts_crop(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1710 {
1711         uiLayout *col;
1712
1713         uiItemR(layout, ptr, "use_crop_size", 0, NULL, ICON_NONE);
1714         uiItemR(layout, ptr, "relative", 0, NULL, ICON_NONE);
1715
1716         col = uiLayoutColumn(layout, TRUE);
1717         if (RNA_boolean_get(ptr, "relative")) {
1718                 uiItemR(col, ptr, "rel_min_x", 0, IFACE_("Left"), ICON_NONE);
1719                 uiItemR(col, ptr, "rel_max_x", 0, IFACE_("Right"), ICON_NONE);
1720                 uiItemR(col, ptr, "rel_min_y", 0, IFACE_("Up"), ICON_NONE);
1721                 uiItemR(col, ptr, "rel_max_y", 0, IFACE_("Down"), ICON_NONE);
1722         }
1723         else {
1724                 uiItemR(col, ptr, "min_x", 0, IFACE_("Left"), ICON_NONE);
1725                 uiItemR(col, ptr, "max_x", 0, IFACE_("Right"), ICON_NONE);
1726                 uiItemR(col, ptr, "min_y", 0, IFACE_("Up"), ICON_NONE);
1727                 uiItemR(col, ptr, "max_y", 0, IFACE_("Down"), ICON_NONE);
1728         }
1729 }
1730
1731 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1732 {
1733         uiLayout *row, *col;
1734         
1735         col = uiLayoutColumn(layout, FALSE);
1736         row = uiLayoutRow(col, FALSE);
1737         uiItemR(row, ptr, "axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1738         uiItemR(col, ptr, "factor", 0, NULL, ICON_NONE);
1739 }
1740
1741 static void node_composit_buts_double_edge_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1742 {
1743         uiLayout *col;
1744
1745         col = uiLayoutColumn(layout, FALSE);
1746
1747         uiItemL(col, IFACE_("Inner Edge:"), ICON_NONE);
1748         uiItemR(col, ptr, "inner_mode", 0, "", ICON_NONE);
1749         uiItemL(col, IFACE_("Buffer Edge:"), ICON_NONE);
1750         uiItemR(col, ptr, "edge_mode", 0, "", ICON_NONE);
1751 }
1752
1753 static void node_composit_buts_map_value(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1754 {
1755         uiLayout *sub, *col;
1756         
1757         col = uiLayoutColumn(layout, TRUE);
1758         uiItemR(col, ptr, "offset", 0, NULL, ICON_NONE);
1759         uiItemR(col, ptr, "size", 0, NULL, ICON_NONE);
1760         
1761         col = uiLayoutColumn(layout, TRUE);
1762         uiItemR(col, ptr, "use_min", 0, NULL, ICON_NONE);
1763         sub = uiLayoutColumn(col, FALSE);
1764         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_min"));
1765         uiItemR(sub, ptr, "min", 0, "", ICON_NONE);
1766         
1767         col = uiLayoutColumn(layout, TRUE);
1768         uiItemR(col, ptr, "use_max", 0, NULL, ICON_NONE);
1769         sub = uiLayoutColumn(col, FALSE);
1770         uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_max"));
1771         uiItemR(sub, ptr, "max", 0, "", ICON_NONE);
1772 }
1773
1774 static void node_composit_buts_alphaover(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1775 {       
1776         uiLayout *col;
1777         
1778         col = uiLayoutColumn(layout, TRUE);
1779         uiItemR(col, ptr, "use_premultiply", 0, NULL, ICON_NONE);
1780         uiItemR(col, ptr, "premul", 0, NULL, ICON_NONE);
1781 }
1782
1783 static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1784 {       
1785         uiLayout *col;
1786         
1787         col = uiLayoutColumn(layout, TRUE);
1788         uiItemR(col, ptr, "use_alpha", 0, NULL, ICON_NONE);
1789 }
1790
1791
1792 static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1793 {
1794         uiLayout *col;
1795         
1796         col = uiLayoutColumn(layout, FALSE);
1797         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1798         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1799         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1800 }
1801
1802 static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1803 {
1804         uiItemR(layout, ptr, "type", 0, NULL, ICON_NONE);
1805         uiItemR(layout, ptr, "distance", 0, NULL, ICON_NONE);
1806         switch (RNA_enum_get(ptr, "type")) {
1807                 case CMP_NODE_DILATEERODE_DISTANCE_THRESH:
1808                         uiItemR(layout, ptr, "edge", 0, NULL, ICON_NONE);
1809                         break;
1810                 case CMP_NODE_DILATEERODE_DISTANCE_FEATHER:
1811                         uiItemR(layout, ptr, "falloff", 0, NULL, ICON_NONE);
1812                         break;
1813         }
1814 }
1815
1816 static void node_composit_buts_diff_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1817 {
1818         uiLayout *col;
1819         
1820         col = uiLayoutColumn(layout, TRUE);
1821         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1822         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1823 }
1824
1825 static void node_composit_buts_distance_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1826 {
1827         uiLayout *col, *row;
1828         
1829         col = uiLayoutColumn(layout, TRUE);
1830    
1831         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1832         row = uiLayoutRow(layout, FALSE);
1833         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1834
1835         uiItemR(col, ptr, "tolerance", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1836         uiItemR(col, ptr, "falloff", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1837 }
1838
1839 static void node_composit_buts_color_spill(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1840 {
1841         uiLayout *row, *col;
1842         
1843         uiItemL(layout, IFACE_("Despill Channel:"), ICON_NONE);
1844         row = uiLayoutRow(layout, FALSE);
1845         uiItemR(row, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1846
1847         col = uiLayoutColumn(layout, FALSE);
1848         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1849
1850         if (RNA_enum_get(ptr, "limit_method") == 0) {
1851                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1852                 row = uiLayoutRow(col, FALSE);
1853                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1854         }
1855
1856         uiItemR(col, ptr, "ratio", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1857         uiItemR(col, ptr, "use_unspill", 0, NULL, ICON_NONE);
1858         if (RNA_boolean_get(ptr, "use_unspill") == TRUE) {
1859                 uiItemR(col, ptr, "unspill_red", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1860                 uiItemR(col, ptr, "unspill_green", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1861                 uiItemR(col, ptr, "unspill_blue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1862         }
1863 }
1864
1865 static void node_composit_buts_chroma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1866 {
1867         uiLayout *col;
1868         
1869         col = uiLayoutColumn(layout, FALSE);
1870         uiItemR(col, ptr, "tolerance", 0, NULL, ICON_NONE);
1871         uiItemR(col, ptr, "threshold", 0, NULL, ICON_NONE);
1872         
1873         col = uiLayoutColumn(layout, TRUE);
1874         /*uiItemR(col, ptr, "lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now */
1875         uiItemR(col, ptr, "gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1876         /*uiItemR(col, ptr, "shadow_adjust", UI_ITEM_R_SLIDER, NULL, ICON_NONE);  Removed for now*/
1877 }
1878
1879 static void node_composit_buts_color_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1880 {
1881         uiLayout *col;
1882         
1883         col = uiLayoutColumn(layout, TRUE);
1884         uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1885         uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1886         uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1887 }
1888
1889 static void node_composit_buts_channel_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1890 {       
1891         uiLayout *col, *row;
1892
1893         uiItemL(layout, IFACE_("Color Space:"), ICON_NONE);
1894         row = uiLayoutRow(layout, FALSE);
1895         uiItemR(row, ptr, "color_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1896
1897         col = uiLayoutColumn(layout, FALSE);
1898         uiItemL(col, IFACE_("Key Channel:"), ICON_NONE);
1899         row = uiLayoutRow(col, FALSE);
1900         uiItemR(row, ptr, "matte_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1901
1902         col = uiLayoutColumn(layout, FALSE);
1903
1904         uiItemR(col, ptr, "limit_method", 0, NULL, ICON_NONE);
1905         if (RNA_enum_get(ptr, "limit_method") == 0) {
1906                 uiItemL(col, IFACE_("Limiting Channel:"), ICON_NONE);
1907                 row = uiLayoutRow(col, FALSE);
1908                 uiItemR(row, ptr, "limit_channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
1909         }
1910
1911         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1912         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1913 }
1914
1915 static void node_composit_buts_luma_matte(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1916 {
1917         uiLayout *col;
1918         
1919         col = uiLayoutColumn(layout, TRUE);
1920         uiItemR(col, ptr, "limit_max", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1921         uiItemR(col, ptr, "limit_min", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
1922 }
1923
1924 static void node_composit_buts_map_uv(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1925 {
1926         uiItemR(layout, ptr, "alpha", 0, NULL, ICON_NONE);
1927 }
1928
1929 static void node_composit_buts_id_mask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1930 {
1931         uiItemR(layout, ptr, "index", 0, NULL, ICON_NONE);
1932         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
1933 }
1934
1935 /* draw function for file output node sockets, displays only sub-path and format, no value button */
1936 static void node_draw_input_file_output(const bContext *C, uiBlock *block,
1937                                         bNodeTree *ntree, bNode *node, bNodeSocket *sock,
1938                                         const char *UNUSED(name), int x, int y, int width)
1939 {
1940         uiLayout *layout, *row;
1941         PointerRNA nodeptr, inputptr, imfptr;
1942         int imtype;
1943         int rx, ry;
1944         RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
1945         
1946         layout = uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, x, y + NODE_DY, width, 20, UI_GetStyle());
1947         row = uiLayoutRow(layout, FALSE);
1948         
1949         imfptr = RNA_pointer_get(&nodeptr, "format");
1950         imtype = RNA_enum_get(&imfptr, "file_format");
1951         if (imtype == R_IMF_IMTYPE_MULTILAYER) {
1952                 NodeImageMultiFileSocket *input = sock->storage;
1953                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotLayer, input, &inputptr);
1954                 
1955                 uiItemL(row, input->layer, 0);
1956         }
1957         else {
1958                 NodeImageMultiFileSocket *input = sock->storage;
1959                 PropertyRNA *imtype_prop;
1960                 const char *imtype_name;
1961                 RNA_pointer_create(&ntree->id, &RNA_NodeOutputFileSlotFile, input, &inputptr);
1962                 
1963                 uiItemL(row, input->path, 0);
1964                 
1965                 if (!RNA_boolean_get(&inputptr, "use_node_format"))
1966                         imfptr = RNA_pointer_get(&inputptr, "format");
1967                 
1968                 imtype_prop = RNA_struct_find_property(&imfptr, "file_format");
1969                 RNA_property_enum_name((bContext *)C, &imfptr, imtype_prop,
1970                                        RNA_property_enum_get(&imfptr, imtype_prop), &imtype_name);
1971                 uiBlockSetEmboss(block, UI_EMBOSSP);
1972                 uiItemL(row, imtype_name, 0);
1973                 uiBlockSetEmboss(block, UI_EMBOSSN);
1974         }
1975         
1976         uiBlockLayoutResolve(block, &rx, &ry);
1977 }
1978 static void node_composit_buts_file_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
1979 {
1980         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1981         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1982         
1983         if (multilayer)
1984                 uiItemL(layout, IFACE_("Path:"), 0);
1985         else
1986                 uiItemL(layout, IFACE_("Base Path:"), 0);
1987         uiItemR(layout, ptr, "base_path", 0, "", ICON_NONE);
1988 }
1989 static void node_composit_buts_file_output_details(uiLayout *layout, bContext *C, PointerRNA *ptr)
1990 {
1991         PointerRNA imfptr = RNA_pointer_get(ptr, "format");
1992         PointerRNA active_input_ptr, op_ptr;
1993         uiLayout *row;
1994         int active_index;
1995         int multilayer = (RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER);
1996         
1997         node_composit_buts_file_output(layout, C, ptr);
1998         uiTemplateImageSettings(layout, &imfptr);
1999         
2000         uiItemS(layout);
2001         
2002         uiItemO(layout, IFACE_("Add Input"), ICON_ZOOMIN, "NODE_OT_output_file_add_socket");
2003         
2004         active_index = RNA_int_get(ptr, "active_input_index");
2005         /* using different collection properties if multilayer format is enabled */
2006         if (multilayer) {
2007                 uiTemplateList(layout, C, ptr, "layer_slots", ptr, "active_input_index", NULL, 0, 0, 0);
2008                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "layer_slots"),
2009                                                    active_index, &active_input_ptr);
2010         }
2011         else {
2012                 uiTemplateList(layout, C, ptr, "file_slots", ptr, "active_input_index", NULL, 0, 0, 0);
2013                 RNA_property_collection_lookup_int(ptr, RNA_struct_find_property(ptr, "file_slots"),
2014                                                    active_index, &active_input_ptr);
2015         }
2016         /* XXX collection lookup does not return the ID part of the pointer, setting this manually here */
2017         active_input_ptr.id.data = ptr->id.data;
2018         
2019         row = uiLayoutRow(layout, TRUE);
2020         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "",
2021                              ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
2022         RNA_enum_set(&op_ptr, "direction", 1);
2023         op_ptr = uiItemFullO(row, "NODE_OT_output_file_move_active_socket", "",
2024                              ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS);
2025         RNA_enum_set(&op_ptr, "direction", 2);
2026         
2027         if (active_input_ptr.data) {
2028                 if (multilayer) {
2029                         uiLayout *row, *col;
2030                         col = uiLayoutColumn(layout, TRUE);
2031                         
2032                         uiItemL(col, IFACE_("Layer:"), 0);
2033                         row = uiLayoutRow(col, FALSE);
2034                         uiItemR(row, &active_input_ptr, "name", 0, "", 0);
2035                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "",
2036                                     ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2037                 }
2038                 else {
2039                         uiLayout *row, *col;
2040                         col = uiLayoutColumn(layout, TRUE);
2041                         
2042                         uiItemL(col, IFACE_("File Path:"), 0);
2043                         row = uiLayoutRow(col, FALSE);
2044                         uiItemR(row, &active_input_ptr, "path", 0, "", 0);
2045                         uiItemFullO(row, "NODE_OT_output_file_remove_active_socket", "",
2046                                     ICON_X, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_R_ICON_ONLY);
2047                         
2048                         /* format details for individual files */
2049                         imfptr = RNA_pointer_get(&active_input_ptr, "format");
2050                         
2051                         col = uiLayoutColumn(layout, TRUE);
2052                         uiItemL(col, IFACE_("Format:"), 0);
2053                         uiItemR(col, &active_input_ptr, "use_node_format", 0, NULL, 0);
2054                         
2055                         col = uiLayoutColumn(layout, FALSE);
2056                         uiLayoutSetActive(col, RNA_boolean_get(&active_input_ptr, "use_node_format") == FALSE);
2057                         uiTemplateImageSettings(col, &imfptr);
2058                 }
2059         }
2060 }
2061
2062 static void node_composit_buts_scale(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2063 {
2064         uiItemR(layout, ptr, "space", 0, "", ICON_NONE);
2065
2066         if (RNA_enum_get(ptr, "space") == CMP_SCALE_RENDERPERCENT) {
2067                 uiLayout *row;
2068                 uiItemR(layout, ptr, "frame_method", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2069                 row = uiLayoutRow(layout, TRUE);
2070                 uiItemR(row, ptr, "offset_x", 0, "X", ICON_NONE);
2071                 uiItemR(row, ptr, "offset_y", 0, "Y", ICON_NONE);
2072         }
2073 }
2074
2075 static void node_composit_buts_rotate(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2076 {
2077         uiItemR(layout, ptr, "filter_type", 0, "", ICON_NONE);
2078 }
2079
2080 static void node_composit_buts_invert(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2081 {
2082         uiLayout *col;
2083         
2084         col = uiLayoutColumn(layout, FALSE);
2085         uiItemR(col, ptr, "invert_rgb", 0, NULL, ICON_NONE);
2086         uiItemR(col, ptr, "invert_alpha", 0, NULL, ICON_NONE);
2087 }
2088
2089 static void node_composit_buts_premulkey(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2090 {
2091         uiItemR(layout, ptr, "mapping", 0, "", ICON_NONE);
2092 }
2093
2094 static void node_composit_buts_view_levels(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2095 {
2096         uiItemR(layout, ptr, "channel", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2097 }
2098
2099 static void node_composit_buts_colorbalance(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2100 {
2101         uiLayout *split, *col, *row;
2102         
2103         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2104         
2105         if (RNA_enum_get(ptr, "correction_method") == 0) {
2106         
2107                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2108                 col = uiLayoutColumn(split, FALSE);
2109                 uiTemplateColorWheel(col, ptr, "lift", 1, 1, 0, 1);
2110                 row = uiLayoutRow(col, FALSE);
2111                 uiItemR(row, ptr, "lift", 0, NULL, ICON_NONE);
2112                 
2113                 col = uiLayoutColumn(split, FALSE);
2114                 uiTemplateColorWheel(col, ptr, "gamma", 1, 1, 1, 1);
2115                 row = uiLayoutRow(col, FALSE);
2116                 uiItemR(row, ptr, "gamma", 0, NULL, ICON_NONE);
2117                 
2118                 col = uiLayoutColumn(split, FALSE);
2119                 uiTemplateColorWheel(col, ptr, "gain", 1, 1, 1, 1);
2120                 row = uiLayoutRow(col, FALSE);
2121                 uiItemR(row, ptr, "gain", 0, NULL, ICON_NONE);
2122
2123         }
2124         else {
2125                 
2126                 split = uiLayoutSplit(layout, 0.0f, FALSE);
2127                 col = uiLayoutColumn(split, FALSE);
2128                 uiTemplateColorWheel(col, ptr, "offset", 1, 1, 0, 1);
2129                 row = uiLayoutRow(col, FALSE);
2130                 uiItemR(row, ptr, "offset", 0, NULL, ICON_NONE);
2131                 
2132                 col = uiLayoutColumn(split, FALSE);
2133                 uiTemplateColorWheel(col, ptr, "power", 1, 1, 0, 1);
2134                 row = uiLayoutRow(col, FALSE);
2135                 uiItemR(row, ptr, "power", 0, NULL, ICON_NONE);
2136                 
2137                 col = uiLayoutColumn(split, FALSE);
2138                 uiTemplateColorWheel(col, ptr, "slope", 1, 1, 0, 1);
2139                 row = uiLayoutRow(col, FALSE);
2140                 uiItemR(row, ptr, "slope", 0, NULL, ICON_NONE);
2141         }
2142
2143 }
2144 static void node_composit_buts_colorbalance_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2145 {
2146         uiItemR(layout, ptr, "correction_method", 0, NULL, ICON_NONE);
2147
2148         if (RNA_enum_get(ptr, "correction_method") == 0) {
2149
2150                 uiTemplateColorWheel(layout, ptr, "lift", 1, 1, 0, 1);
2151                 uiItemR(layout, ptr, "lift", 0, NULL, ICON_NONE);
2152
2153                 uiTemplateColorWheel(layout, ptr, "gamma", 1, 1, 1, 1);
2154                 uiItemR(layout, ptr, "gamma", 0, NULL, ICON_NONE);
2155
2156                 uiTemplateColorWheel(layout, ptr, "gain", 1, 1, 1, 1);
2157                 uiItemR(layout, ptr, "gain", 0, NULL, ICON_NONE);
2158         }
2159         else {
2160                 uiTemplateColorWheel(layout, ptr, "offset", 1, 1, 0, 1);
2161                 uiItemR(layout, ptr, "offset", 0, NULL, ICON_NONE);
2162
2163                 uiTemplateColorWheel(layout, ptr, "power", 1, 1, 0, 1);
2164                 uiItemR(layout, ptr, "power", 0, NULL, ICON_NONE);
2165
2166                 uiTemplateColorWheel(layout, ptr, "slope", 1, 1, 0, 1);
2167                 uiItemR(layout, ptr, "slope", 0, NULL, ICON_NONE);
2168         }
2169 }
2170
2171
2172 static void node_composit_buts_huecorrect(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2173 {
2174         bNode *node = ptr->data;
2175         CurveMapping *cumap = node->storage;
2176
2177         if (_sample_col[0] != SAMPLE_FLT_ISNONE) {
2178                 cumap->flag |= CUMA_DRAW_SAMPLE;
2179                 copy_v3_v3(cumap->sample, _sample_col);
2180         }
2181         else {
2182                 cumap->flag &= ~CUMA_DRAW_SAMPLE;
2183         }
2184
2185         uiTemplateCurveMapping(layout, ptr, "mapping", 'h', 0, 0);
2186 }
2187
2188 static void node_composit_buts_ycc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2189
2190         uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
2191 }
2192
2193 static void node_composit_buts_movieclip(uiLayout *layout, bContext *C, PointerRNA *ptr)
2194 {
2195         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2196 }
2197
2198 static void node_composit_buts_stabilize2d(uiLayout *layout, bContext *C, PointerRNA *ptr)
2199 {
2200         bNode *node = ptr->data;
2201
2202         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2203
2204         if (!node->id)
2205                 return;
2206
2207         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2208 }
2209
2210 static void node_composit_buts_transform(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2211 {
2212         uiItemR(layout, ptr, "filter_type", 0, "", 0);
2213 }
2214
2215 static void node_composit_buts_moviedistortion(uiLayout *layout, bContext *C, PointerRNA *ptr)
2216 {
2217         bNode *node = ptr->data;
2218
2219         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2220
2221         if (!node->id)
2222                 return;
2223
2224         uiItemR(layout, ptr, "distortion_type", 0, "", 0);
2225 }
2226
2227 static void node_composit_buts_colorcorrection(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2228 {
2229         uiLayout *row;
2230         
2231         row = uiLayoutRow(layout, FALSE);
2232         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2233         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2234         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2235
2236         row = uiLayoutRow(layout, FALSE);
2237         uiItemL(row, "", 0);
2238         uiItemL(row, IFACE_("Saturation"), 0);
2239         uiItemL(row, IFACE_("Contrast"), 0);
2240         uiItemL(row, IFACE_("Gamma"), 0);
2241         uiItemL(row, IFACE_("Gain"), 0);
2242         uiItemL(row, IFACE_("Lift"), 0);
2243
2244         row = uiLayoutRow(layout, FALSE);
2245         uiItemL(row, IFACE_("Master"), 0);
2246         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2247         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2248         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2249         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2250         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2251
2252         row = uiLayoutRow(layout, FALSE);
2253         uiItemL(row, IFACE_("Highlights"), 0);
2254         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2255         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2256         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2257         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2258         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2259
2260         row = uiLayoutRow(layout, FALSE);
2261         uiItemL(row, IFACE_("Midtones"), 0);
2262         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2263         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2264         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2265         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2266         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2267
2268         row = uiLayoutRow(layout, FALSE);
2269         uiItemL(row, IFACE_("Shadows"), 0);
2270         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, "", ICON_NONE);
2271         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, "", ICON_NONE);
2272         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, "", ICON_NONE);
2273         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, "", ICON_NONE);
2274         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, "", ICON_NONE);
2275
2276         row = uiLayoutRow(layout, FALSE);
2277         uiItemR(row, ptr, "midtones_start", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2278         uiItemR(row, ptr, "midtones_end", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2279 }
2280
2281 static void node_composit_buts_colorcorrection_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2282 {
2283         uiLayout *row;
2284         
2285         row = uiLayoutRow(layout, FALSE);
2286         uiItemR(row, ptr, "red", 0, NULL, ICON_NONE);
2287         uiItemR(row, ptr, "green", 0, NULL, ICON_NONE);
2288         uiItemR(row, ptr, "blue", 0, NULL, ICON_NONE);
2289         row = layout;
2290         uiItemL(row, IFACE_("Saturation"), 0);
2291         uiItemR(row, ptr, "master_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2292         uiItemR(row, ptr, "highlights_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2293         uiItemR(row, ptr, "midtones_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2294         uiItemR(row, ptr, "shadows_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2295
2296         uiItemL(row, IFACE_("Contrast"), 0);
2297         uiItemR(row, ptr, "master_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2298         uiItemR(row, ptr, "highlights_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2299         uiItemR(row, ptr, "midtones_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2300         uiItemR(row, ptr, "shadows_contrast", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2301
2302         uiItemL(row, IFACE_("Gamma"), 0);
2303         uiItemR(row, ptr, "master_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2304         uiItemR(row, ptr, "highlights_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2305         uiItemR(row, ptr, "midtones_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2306         uiItemR(row, ptr, "shadows_gamma", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2307
2308         uiItemL(row, IFACE_("Gain"), 0);
2309         uiItemR(row, ptr, "master_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2310         uiItemR(row, ptr, "highlights_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2311         uiItemR(row, ptr, "midtones_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2312         uiItemR(row, ptr, "shadows_gain", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2313         
2314         uiItemL(row, IFACE_("Lift"), 0);
2315         uiItemR(row, ptr, "master_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2316         uiItemR(row, ptr, "highlights_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2317         uiItemR(row, ptr, "midtones_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2318         uiItemR(row, ptr, "shadows_lift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2319
2320         row = uiLayoutRow(layout, FALSE);
2321         uiItemR(row, ptr, "midtones_start", 0, NULL, ICON_NONE);
2322         uiItemR(row, ptr, "midtones_end", 0, NULL, ICON_NONE);
2323 }
2324
2325 static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2326 {
2327         uiItemR(layout, ptr, "check", 0, NULL, ICON_NONE);
2328 }
2329
2330 static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2331 {
2332         uiLayout *row;
2333         
2334         row = uiLayoutRow(layout, TRUE);
2335         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2336         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2337         
2338         row = uiLayoutRow(layout, TRUE);
2339         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2340         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2341
2342         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2343         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2344 }
2345
2346 static void node_composit_buts_bokehimage(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2347 {
2348         uiItemR(layout, ptr, "flaps", 0, NULL, ICON_NONE);
2349         uiItemR(layout, ptr, "angle", 0, NULL, ICON_NONE);
2350         uiItemR(layout, ptr, "rounding", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2351         uiItemR(layout, ptr, "catadioptric", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2352         uiItemR(layout, ptr, "shift", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2353 }
2354
2355 void node_composit_backdrop_viewer(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2356 {
2357 //      node_composit_backdrop_canvas(snode, backdrop, node, x, y);
2358         if (node->custom1 == 0) {
2359                 const float backdropWidth = backdrop->x;
2360                 const float backdropHeight = backdrop->y;
2361                 const float cx  = x + snode->zoom * backdropWidth * node->custom3;
2362                 const float cy = y + snode->zoom * backdropHeight * node->custom4;
2363
2364                 glColor3f(1.0, 1.0, 1.0);
2365
2366                 glBegin(GL_LINES);
2367                 glVertex2f(cx - 25, cy - 25);
2368                 glVertex2f(cx + 25, cy + 25);
2369                 glVertex2f(cx + 25, cy - 25);
2370                 glVertex2f(cx - 25, cy + 25);
2371                 glEnd();
2372         }
2373 }
2374
2375 void node_composit_backdrop_boxmask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2376 {
2377         NodeBoxMask *boxmask = node->storage;
2378         const float backdropWidth = backdrop->x;
2379         const float backdropHeight = backdrop->y;
2380         const float aspect = backdropWidth / backdropHeight;
2381         const float rad = DEG2RADF(-boxmask->rotation);
2382         const float cosine = cosf(rad);
2383         const float sine = sinf(rad);
2384         const float halveBoxWidth = backdropWidth * (boxmask->width / 2.0f);
2385         const float halveBoxHeight = backdropHeight * (boxmask->height / 2.0f) * aspect;
2386
2387         float cx, cy, x1, x2, x3, x4;
2388         float y1, y2, y3, y4;
2389
2390
2391         /* keep this, saves us from a version patch */
2392         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2393
2394         glColor3f(1.0, 1.0, 1.0);
2395
2396         cx  = x + snode->zoom * backdropWidth * boxmask->x;
2397         cy = y + snode->zoom * backdropHeight * boxmask->y;
2398
2399         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2400         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2401         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2402         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2403         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2404         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2405         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2406         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2407
2408         glBegin(GL_LINE_LOOP);
2409         glVertex2f(x1, y1);
2410         glVertex2f(x2, y2);
2411         glVertex2f(x3, y3);
2412         glVertex2f(x4, y4);
2413         glEnd();
2414 }
2415
2416 void node_composit_backdrop_ellipsemask(SpaceNode *snode, ImBuf *backdrop, bNode *node, int x, int y)
2417 {
2418         NodeEllipseMask *ellipsemask = node->storage;
2419         const float backdropWidth = backdrop->x;
2420         const float backdropHeight = backdrop->y;
2421         const float aspect = backdropWidth / backdropHeight;
2422         const float rad = DEG2RADF(-ellipsemask->rotation);
2423         const float cosine = cosf(rad);
2424         const float sine = sinf(rad);
2425         const float halveBoxWidth = backdropWidth * (ellipsemask->width / 2.0f);
2426         const float halveBoxHeight = backdropHeight * (ellipsemask->height / 2.0f) * aspect;
2427
2428         float cx, cy, x1, x2, x3, x4;
2429         float y1, y2, y3, y4;
2430
2431
2432         /* keep this, saves us from a version patch */
2433         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
2434
2435         glColor3f(1.0, 1.0, 1.0);
2436
2437         cx  = x + snode->zoom * backdropWidth * ellipsemask->x;
2438         cy = y + snode->zoom * backdropHeight * ellipsemask->y;
2439
2440         x1 = cx - (cosine * halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2441         x2 = cx - (cosine * -halveBoxWidth + sine * halveBoxHeight) * snode->zoom;
2442         x3 = cx - (cosine * -halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2443         x4 = cx - (cosine * halveBoxWidth + sine * -halveBoxHeight) * snode->zoom;
2444         y1 = cy - (-sine * halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2445         y2 = cy - (-sine * -halveBoxWidth + cosine * halveBoxHeight) * snode->zoom;
2446         y3 = cy - (-sine * -halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2447         y4 = cy - (-sine * halveBoxWidth + cosine * -halveBoxHeight) * snode->zoom;
2448
2449         glBegin(GL_LINE_LOOP);
2450
2451         glVertex2f(x1, y1);
2452         glVertex2f(x2, y2);
2453         glVertex2f(x3, y3);
2454         glVertex2f(x4, y4);
2455         glEnd();
2456 }
2457
2458 static void node_composit_buts_ellipsemask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2459 {
2460         uiLayout *row;
2461         row = uiLayoutRow(layout, TRUE);
2462         uiItemR(row, ptr, "x", 0, NULL, ICON_NONE);
2463         uiItemR(row, ptr, "y", 0, NULL, ICON_NONE);
2464         row = uiLayoutRow(layout, TRUE);
2465         uiItemR(row, ptr, "width", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2466         uiItemR(row, ptr, "height", UI_ITEM_R_SLIDER, NULL, ICON_NONE);
2467
2468         uiItemR(layout, ptr, "rotation", 0, NULL, ICON_NONE);
2469         uiItemR(layout, ptr, "mask_type", 0, NULL, ICON_NONE);
2470 }
2471
2472 static void node_composit_buts_viewer_but(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2473 {
2474         uiLayout *col;
2475         
2476         uiItemR(layout, ptr, "tile_order", 0, NULL, ICON_NONE);
2477         if (RNA_enum_get(ptr, "tile_order") == 0) {
2478                 col = uiLayoutColumn(layout, TRUE);
2479                 uiItemR(col, ptr, "center_x", 0, NULL, ICON_NONE);
2480                 uiItemR(col, ptr, "center_y", 0, NULL, ICON_NONE);
2481         }
2482 }
2483
2484 static void node_composit_buts_mask(uiLayout *layout, bContext *C, PointerRNA *ptr)
2485 {
2486         uiTemplateID(layout, C, ptr, "mask", NULL, NULL, NULL);
2487         uiItemR(layout, ptr, "use_antialiasing", 0, NULL, ICON_NONE);
2488         uiItemR(layout, ptr, "use_feather", 0, NULL, ICON_NONE);
2489
2490 }
2491
2492 static void node_composit_buts_keyingscreen(uiLayout *layout, bContext *C, PointerRNA *ptr)
2493 {
2494         bNode *node = ptr->data;
2495
2496         uiTemplateID(layout, C, ptr, "clip", NULL, NULL, NULL);
2497
2498         if (node->id) {
2499                 MovieClip *clip = (MovieClip *) node->id;
2500                 uiLayout *col;
2501                 PointerRNA tracking_ptr;
2502
2503                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, &clip->tracking, &tracking_ptr);
2504
2505                 col = uiLayoutColumn(layout, TRUE);
2506                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2507         }
2508 }
2509
2510 static void node_composit_buts_keying(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2511 {
2512         /* bNode *node= ptr->data; */ /* UNUSED */
2513
2514         uiItemR(layout, ptr, "blur_pre", 0, NULL, ICON_NONE);
2515         uiItemR(layout, ptr, "screen_balance", 0, NULL, ICON_NONE);
2516         uiItemR(layout, ptr, "despill_factor", 0, NULL, ICON_NONE);
2517         uiItemR(layout, ptr, "despill_balance", 0, NULL, ICON_NONE);
2518         uiItemR(layout, ptr, "edge_kernel_radius", 0, NULL, ICON_NONE);
2519         uiItemR(layout, ptr, "edge_kernel_tolerance", 0, NULL, ICON_NONE);
2520         uiItemR(layout, ptr, "clip_black", 0, NULL, ICON_NONE);
2521         uiItemR(layout, ptr, "clip_white", 0, NULL, ICON_NONE);
2522         uiItemR(layout, ptr, "dilate_distance", 0, NULL, ICON_NONE);
2523         uiItemR(layout, ptr, "feather_falloff", 0, NULL, ICON_NONE);
2524         uiItemR(layout, ptr, "feather_distance", 0, NULL, ICON_NONE);
2525         uiItemR(layout, ptr, "blur_post", 0, NULL, ICON_NONE);
2526 }
2527
2528 static void node_composit_buts_trackpos(uiLayout *layout, bContext *C, PointerRNA *ptr)
2529 {
2530         bNode *node= ptr->data;
2531
2532         uiTemplateID(layout, C, ptr, "clip", NULL, "CLIP_OT_open", NULL);
2533
2534         if (node->id) {
2535                 MovieClip *clip = (MovieClip *) node->id;
2536                 MovieTracking *tracking = &clip->tracking;
2537                 MovieTrackingObject *object;
2538                 uiLayout *col;
2539                 PointerRNA tracking_ptr;
2540                 NodeTrackPosData *data = node->storage;
2541
2542                 RNA_pointer_create(&clip->id, &RNA_MovieTracking, tracking, &tracking_ptr);
2543
2544                 col = uiLayoutColumn(layout, 0);
2545                 uiItemPointerR(col, ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
2546
2547                 object = BKE_tracking_object_get_named(tracking, data->tracking_object);
2548                 if (object) {
2549                         PointerRNA object_ptr;
2550
2551                         RNA_pointer_create(&clip->id, &RNA_MovieTrackingObject, object, &object_ptr);
2552
2553                         uiItemPointerR(col, ptr, "track_name", &object_ptr, "tracks", "", ICON_ANIM_DATA);
2554                 }
2555                 else {
2556                         uiItemR(layout, ptr, "track_name", 0, "", ICON_ANIM_DATA);
2557                 }
2558
2559                 uiItemR(layout, ptr, "use_relative", 0, NULL, ICON_NONE);
2560         }
2561 }
2562
2563 /* only once called */
2564 static void node_composit_set_butfunc(bNodeType *ntype)
2565 {
2566         switch (ntype->type) {
2567                 /* case NODE_GROUP:      note, typeinfo for group is generated... see "XXX ugly hack" */
2568
2569                 case CMP_NODE_IMAGE:
2570                         ntype->uifunc = node_composit_buts_image;
2571                         break;
2572                 case CMP_NODE_R_LAYERS:
2573                         ntype->uifunc = node_composit_buts_renderlayers;
2574                         break;
2575                 case CMP_NODE_NORMAL:
2576                         ntype->uifunc = node_buts_normal;
2577                         break;
2578                 case CMP_NODE_CURVE_VEC:
2579                         ntype->uifunc = node_buts_curvevec;
2580                         break;
2581                 case CMP_NODE_CURVE_RGB:
2582                         ntype->uifunc = node_buts_curvecol;
2583                         break;
2584                 case CMP_NODE_VALUE:
2585                         ntype->uifunc = node_buts_value;
2586                         break;
2587                 case CMP_NODE_RGB:
2588                         ntype->uifunc = node_buts_rgb;
2589                         break;
2590                 case CMP_NODE_FLIP:
2591                         ntype->uifunc = node_composit_buts_flip;
2592                         break;
2593                 case CMP_NODE_SPLITVIEWER:
2594                         ntype->uifunc = node_composit_buts_splitviewer;
2595                         break;
2596                 case CMP_NODE_MIX_RGB:
2597                         ntype->uifunc = node_buts_mix_rgb;
2598                         break;
2599                 case CMP_NODE_VALTORGB:
2600                         ntype->uifunc = node_buts_colorramp;
2601                         break;
2602                 case CMP_NODE_CROP:
2603                         ntype->uifunc = node_composit_buts_crop;
2604                         break;
2605                 case CMP_NODE_BLUR:
2606                         ntype->uifunc = node_composit_buts_blur;
2607                         break;
2608                 case CMP_NODE_DBLUR:
2609                         ntype->uifunc = node_composit_buts_dblur;
2610                         break;
2611                 case CMP_NODE_BILATERALBLUR:
2612                         ntype->uifunc = node_composit_buts_bilateralblur;
2613                         break;
2614                 case CMP_NODE_DEFOCUS:
2615                         ntype->uifunc = node_composit_buts_defocus;
2616                         break;
2617                 case CMP_NODE_GLARE:
2618                         ntype->uifunc = node_composit_buts_glare;
2619                         break;
2620                 case CMP_NODE_TONEMAP:
2621                         ntype->uifunc = node_composit_buts_tonemap;
2622                         break;
2623                 case CMP_NODE_LENSDIST:
2624                         ntype->uifunc = node_composit_buts_lensdist;
2625                         break;
2626                 case CMP_NODE_VECBLUR:
2627                         ntype->uifunc = node_composit_buts_vecblur;
2628                         break;
2629                 case CMP_NODE_FILTER:
2630                         ntype->uifunc = node_composit_buts_filter;
2631                         break;
2632                 case CMP_NODE_MAP_VALUE:
2633                         ntype->uifunc = node_composit_buts_map_value;
2634                         break;
2635                 case CMP_NODE_TIME:
2636                         ntype->uifunc = node_buts_time;
2637                         break;
2638                 case CMP_NODE_ALPHAOVER:
2639                         ntype->uifunc = node_composit_buts_alphaover;
2640                         break;
2641                 case CMP_NODE_HUE_SAT:
2642                         ntype->uifunc = node_composit_buts_hue_sat;
2643                         break;
2644                 case CMP_NODE_TEXTURE:
2645                         ntype->uifunc = node_buts_texture;
2646                         break;
2647                 case CMP_NODE_DILATEERODE:
2648                         ntype->uifunc = node_composit_buts_dilateerode;
2649                         break;
2650                 case CMP_NODE_OUTPUT_FILE:
2651                         ntype->uifunc = node_composit_buts_file_output;
2652                         ntype->uifuncbut = node_composit_buts_file_output_details;
2653                         ntype->drawinputfunc = node_draw_input_file_output;
2654                         break;
2655                 case CMP_NODE_DIFF_MATTE:
2656                         ntype->uifunc = node_composit_buts_diff_matte;
2657                         break;
2658                 case CMP_NODE_DIST_MATTE:
2659                         ntype->uifunc = node_composit_buts_distance_matte;
2660                         break;
2661                 case CMP_NODE_COLOR_SPILL:
2662                         ntype->uifunc = node_composit_buts_color_spill;
2663                         break;
2664                 case CMP_NODE_CHROMA_MATTE:
2665                         ntype->uifunc = node_composit_buts_chroma_matte;
2666                         break;
2667                 case CMP_NODE_COLOR_MATTE:
2668                         ntype->uifunc = node_composit_buts_color_matte;
2669                         break;
2670                 case CMP_NODE_SCALE:
2671                         ntype->uifunc = node_composit_buts_scale;
2672                         break;
2673                 case CMP_NODE_ROTATE:
2674                         ntype->uifunc = node_composit_buts_rotate;
2675                         break;
2676                 case CMP_NODE_CHANNEL_MATTE:
2677                         ntype->uifunc = node_composit_buts_channel_matte;
2678                         break;
2679                 case CMP_NODE_LUMA_MATTE:
2680                         ntype->uifunc = node_composit_buts_luma_matte;
2681                         break;
2682                 case CMP_NODE_MAP_UV:
2683                         ntype->uifunc = node_composit_buts_map_uv;
2684                         break;
2685                 case CMP_NODE_ID_MASK:
2686                         ntype->uifunc = node_composit_buts_id_mask;
2687                         break;
2688                 case CMP_NODE_DOUBLEEDGEMASK:
2689                         ntype->uifunc = node_composit_buts_double_edge_mask;
2690                         break;
2691                 case CMP_NODE_MATH:
2692                         ntype->uifunc = node_buts_math;
2693                         break;
2694                 case CMP_NODE_INVERT:
2695                         ntype->uifunc = node_composit_buts_invert;
2696                         break;
2697                 case CMP_NODE_PREMULKEY:
2698                         ntype->uifunc = node_composit_buts_premulkey;
2699                         break;
2700                 case CMP_NODE_VIEW_LEVELS:
2701                         ntype->uifunc = node_composit_buts_view_levels;
2702                         break;
2703                 case CMP_NODE_COLORBALANCE:
2704                         ntype->uifunc = node_composit_buts_colorbalance;
2705                         ntype->uifuncbut = node_composit_buts_colorbalance_but;
2706                         break;
2707                 case CMP_NODE_HUECORRECT:
2708                         ntype->uifunc = node_composit_buts_huecorrect;
2709                         break;
2710                 case CMP_NODE_ZCOMBINE:
2711                         ntype->uifunc = node_composit_buts_zcombine;
2712                         break;
2713                 case CMP_NODE_COMBYCCA:
2714                 case CMP_NODE_SEPYCCA:
2715                         ntype->uifunc = node_composit_buts_ycc;
2716                         break;
2717                 case CMP_NODE_MOVIECLIP:
2718                         ntype->uifunc = node_composit_buts_movieclip;
2719                         break;
2720                 case CMP_NODE_STABILIZE2D:
2721                         ntype->uifunc = node_composit_buts_stabilize2d;
2722                         break;
2723                 case CMP_NODE_TRANSFORM:
2724                         ntype->uifunc = node_composit_buts_transform;
2725                         break;
2726                 case CMP_NODE_MOVIEDISTORTION:
2727                         ntype->uifunc = node_composit_buts_moviedistortion;
2728                         break;
2729                 case CMP_NODE_COLORCORRECTION:
2730                         ntype->uifunc = node_composit_buts_colorcorrection;
2731                         ntype->uifuncbut = node_composit_buts_colorcorrection_but;
2732                         break;
2733                 case CMP_NODE_SWITCH:
2734                         ntype->uifunc = node_composit_buts_switch;
2735                         break;
2736                 case CMP_NODE_MASK_BOX:
2737                         ntype->uifunc = node_composit_buts_boxmask;
2738                         ntype->uibackdropfunc = node_composit_backdrop_boxmask;
2739                         break;
2740                 case CMP_NODE_MASK_ELLIPSE:
2741                         ntype->uifunc = node_composit_buts_ellipsemask;
2742                         ntype->uibackdropfunc = node_composit_backdrop_ellipsemask;
2743                         break;
2744                 case CMP_NODE_BOKEHIMAGE:
2745                         ntype->uifunc = node_composit_buts_bokehimage;
2746                         break;
2747                 case CMP_NODE_VIEWER:
2748                         ntype->uifunc = NULL;
2749                         ntype->uifuncbut = node_composit_buts_viewer_but;
2750                         ntype->uibackdropfunc = node_composit_backdrop_viewer;
2751                         break;
2752                 case CMP_NODE_MASK:
2753                         ntype->uifunc = node_composit_buts_mask;
2754                         break;
2755                 case CMP_NODE_KEYINGSCREEN:
2756                         ntype->uifunc = node_composit_buts_keyingscreen;
2757                         break;
2758                 case CMP_NODE_KEYING:
2759                         ntype->uifunc = node_composit_buts_keying;
2760                         break;
2761                 case CMP_NODE_TRACKPOS:
2762                         ntype->uifunc = node_composit_buts_trackpos;
2763                         break;
2764                 default:
2765                         ntype->uifunc = NULL;
2766         }
2767 }
2768
2769 /* ****************** BUTTON CALLBACKS FOR TEXTURE NODES ***************** */
2770
2771 static void node_texture_buts_bricks(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2772 {
2773         uiLayout *col;
2774         
2775         col = uiLayoutColumn(layout, TRUE);
2776         uiItemR(col, ptr, "offset", 0, IFACE_("Offset"), ICON_NONE);
2777         uiItemR(col, ptr, "offset_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2778         
2779         col = uiLayoutColumn(layout, TRUE);
2780         uiItemR(col, ptr, "squash", 0, IFACE_("Squash"), ICON_NONE);
2781         uiItemR(col, ptr, "squash_frequency", 0, IFACE_("Frequency"), ICON_NONE);
2782 }
2783
2784 static void node_texture_buts_proc(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2785 {
2786         PointerRNA tex_ptr;
2787         bNode *node = ptr->data;
2788         ID *id = ptr->id.data;
2789         Tex *tex = (Tex *)node->storage;
2790         uiLayout *col, *row;
2791         
2792         RNA_pointer_create(id, &RNA_Texture, tex, &tex_ptr);
2793
2794         col = uiLayoutColumn(layout, FALSE);
2795
2796         switch (tex->type) {
2797                 case TEX_BLEND:
2798                         uiItemR(col, &tex_ptr, "progression", 0, "", ICON_NONE);
2799                         row = uiLayoutRow(col, FALSE);
2800                         uiItemR(row, &tex_ptr, "use_flip_axis", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2801                         break;
2802
2803                 case TEX_MARBLE:
2804                         row = uiLayoutRow(col, FALSE);
2805                         uiItemR(row, &tex_ptr, "marble_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2806                         row = uiLayoutRow(col, FALSE);
2807                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2808                         row = uiLayoutRow(col, FALSE);
2809                         uiItemR(row, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2810                         row = uiLayoutRow(col, FALSE);
2811                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2812                         break;
2813
2814                 case TEX_MAGIC:
2815                         uiItemR(col, &tex_ptr, "noise_depth", 0, NULL, ICON_NONE);
2816                         break;
2817
2818                 case TEX_STUCCI:
2819                         row = uiLayoutRow(col, FALSE);
2820                         uiItemR(row, &tex_ptr, "stucci_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2821                         row = uiLayoutRow(col, FALSE);
2822                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2823                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2824                         break;
2825
2826                 case TEX_WOOD:
2827                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2828                         uiItemR(col, &tex_ptr, "wood_type", 0, "", ICON_NONE);
2829                         row = uiLayoutRow(col, FALSE);
2830                         uiItemR(row, &tex_ptr, "noise_basis_2", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2831                         row = uiLayoutRow(col, FALSE);
2832                         uiLayoutSetActive(row, !(ELEM(tex->stype, TEX_BAND, TEX_RING)));
2833                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2834                         break;
2835                         
2836                 case TEX_CLOUDS:
2837                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2838                         row = uiLayoutRow(col, FALSE);
2839                         uiItemR(row, &tex_ptr, "cloud_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2840                         row = uiLayoutRow(col, FALSE);
2841                         uiItemR(row, &tex_ptr, "noise_type", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
2842                         uiItemR(col, &tex_ptr, "noise_depth", UI_ITEM_R_EXPAND, IFACE_("Depth"), ICON_NONE);
2843                         break;
2844                         
2845                 case TEX_DISTNOISE:
2846                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2847                         uiItemR(col, &tex_ptr, "noise_distortion", 0, "", ICON_NONE);
2848                         break;
2849
2850                 case TEX_MUSGRAVE:
2851                         uiItemR(col, &tex_ptr, "musgrave_type", 0, "", ICON_NONE);
2852                         uiItemR(col, &tex_ptr, "noise_basis", 0, "", ICON_NONE);
2853                         break;
2854                 case TEX_VORONOI:
2855                         uiItemR(col, &tex_ptr, "distance_metric", 0, "", ICON_NONE);
2856                         if (tex->vn_distm == TEX_MINKOVSKY) {
2857                                 uiItemR(col, &tex_ptr, "minkovsky_exponent", 0, NULL, ICON_NONE);
2858                         }
2859                         uiItemR(col, &tex_ptr, "color_mode", 0, "", ICON_NONE);
2860                         break;
2861         }
2862 }
2863
2864 static void node_texture_buts_image(uiLayout *layout, bContext *C, PointerRNA *ptr)
2865 {
2866         uiTemplateID(layout, C, ptr, "image", NULL, "IMAGE_OT_open", NULL);
2867 }
2868
2869 static void node_texture_buts_output(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
2870 {
2871         uiItemR(layout, ptr, "filepath", 0, "", ICON_NONE);
2872 }
2873
2874 /* only once called */
2875 static void node_texture_set_butfunc(bNodeType *ntype)
2876 {
2877         if (ntype->type >= TEX_NODE_PROC && ntype->type < TEX_NODE_PROC_MAX) {
2878                 ntype->uifunc = node_texture_buts_proc;
2879         }
2880         else {
2881                 switch (ntype->type) {
2882
2883                         case TEX_NODE_MATH:
2884                                 ntype->uifunc = node_buts_math;
2885                                 break;
2886
2887                         case TEX_NODE_MIX_RGB:
2888                                 ntype->uifunc = node_buts_mix_rgb;
2889                                 break;
2890
2891                         case TEX_NODE_VALTORGB:
2892                                 ntype->uifunc = node_buts_colorramp;
2893                                 break;
2894
2895                         case TEX_NODE_CURVE_RGB:
2896                                 ntype->uifunc = node_buts_curvecol;
2897                                 break;
2898
2899                         case TEX_NODE_CURVE_TIME:
2900                                 ntype->uifunc = node_buts_time;
2901                                 break;
2902
2903                         case TEX_NODE_TEXTURE:
2904                                 ntype->uifunc = node_buts_texture;
2905                                 break;
2906
2907                         case TEX_NODE_BRICKS:
2908                                 ntype->uifunc = node_texture_buts_bricks;
2909                                 break;
2910
2911                         case TEX_NODE_IMAGE:
2912                                 ntype->uifunc = node_texture_buts_image;
2913                                 break;
2914
2915                         case TEX_NODE_OUTPUT:
2916                                 ntype->uifunc = node_texture_buts_output;
2917                                 break;
2918                 }
2919         }
2920 }
2921
2922 /* ******* init draw callbacks for all tree types, only called in usiblender.c, once ************* */
2923
2924 void ED_init_node_butfuncs(void)
2925 {
2926         bNodeTreeType *treetype;
2927         bNodeType *ntype;
2928         bNodeSocketType *stype;
2929         int i;
2930         
2931         /* node type ui functions */
2932         for (i = 0; i < NUM_NTREE_TYPES; ++i) {
2933                 treetype = ntreeGetType(i);
2934                 if (treetype) {
2935                         for (ntype = treetype->node_types.first; ntype; ntype = ntype->next) {
2936                                 /* default ui functions */
2937                                 ntype->drawfunc = node_draw_default;
2938                                 ntype->drawupdatefunc = node_update_default;
2939                                 ntype->select_area_func = node_select_area_default;
2940                                 ntype->tweak_area_func = node_tweak_area_default;
2941                                 ntype->uifunc = NULL;
2942                                 ntype->uifuncbut = NULL;
2943                                 ntype->drawinputfunc = node_draw_input_default;
2944                                 ntype->drawoutputfunc = node_draw_output_default;
2945                                 ntype->resize_area_func = node_resize_area_default;
2946                                 
2947                                 node_common_set_butfunc(ntype);
2948                                 
2949                                 switch (i) {
2950                                         case NTREE_COMPOSIT:
2951                                                 node_composit_set_butfunc(ntype);
2952                                                 break;
2953                                         case NTREE_SHADER:
2954                                                 node_shader_set_butfunc(ntype);
2955                                                 break;
2956                                         case NTREE_TEXTURE:
2957                                                 node_texture_set_butfunc(ntype);
2958                                                 break;
2959                                 }
2960                         }
2961                 }
2962         }
2963         
2964         /* socket type ui functions */
2965         for (i = 0; i < NUM_SOCKET_TYPES; ++i) {
2966                 stype = ntreeGetSocketType(i);
2967                 if (stype) {
2968                         switch (stype->type) {
2969                                 case SOCK_FLOAT:
2970                                 case SOCK_INT:
2971                                 case SOCK_BOOLEAN:
2972                                         stype->buttonfunc = node_socket_button_default;
2973                                         break;
2974                                 case SOCK_VECTOR:
2975                                         stype->buttonfunc = node_socket_button_components;
2976                                         break;
2977                                 case SOCK_RGBA:
2978                                         stype->buttonfunc = node_socket_button_color;
2979                                         break;
2980                                 case SOCK_SHADER:
2981                                         stype->buttonfunc = node_socket_button_label;
2982                                         break;
2983                                 default:
2984                                         stype->buttonfunc = NULL;
2985                         }
2986                 }
2987         }
2988 }
2989
2990 /* ************** Generic drawing ************** */
2991
2992 void draw_nodespace_back_pix(ARegion *ar, SpaceNode *snode, int color_manage)
2993 {
2994         
2995         if ((snode->flag & SNODE_BACKDRAW) && snode->treetype == NTREE_COMPOSIT) {
2996                 Image *ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
2997                 void *lock;
2998                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
2999                 if (ibuf) {
3000                         float x, y; 
3001                         
3002                         glMatrixMode(GL_PROJECTION);
3003                         glPushMatrix();
3004                         glMatrixMode(GL_MODELVIEW);
3005                         glPushMatrix();
3006
3007                         /* keep this, saves us from a version patch */
3008                         if (snode->zoom == 0.0f) snode->zoom = 1.0f;
3009                         
3010                         /* somehow the offset has to be calculated inverse */
3011                         
3012                         glaDefine2DArea(&ar->winrct);
3013                         /* ortho at pixel level curarea */
3014                         wmOrtho2(-0.375, ar->winx - 0.375, -0.375, ar->winy - 0.375);
3015                         
3016                         x = (ar->winx - snode->zoom * ibuf->x) / 2 + snode->xof;
3017                         y = (ar->winy - snode->zoom * ibuf->y) / 2 + snode->yof;
3018                         
3019                         if (!ibuf->rect) {
3020                                 if (color_manage)
3021                                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
3022                                 else
3023                                         ibuf->profile = IB_PROFILE_NONE;
3024                                 IMB_rect_from_float(ibuf);
3025                         }
3026
3027                         if (ibuf->rect) {
3028                                 if (snode->flag & (SNODE_SHOW_R | SNODE_SHOW_G | SNODE_SHOW_B)) {
3029                                         int ofs;
3030
3031 #ifdef __BIG_ENDIAN__
3032                                         if      (snode->flag & SNODE_SHOW_R) ofs = 2;
3033                                         else if (snode->flag & SNODE_SHOW_G) ofs = 1;
3034                                         else                                 ofs = 0;
3035 #else
3036                                         if      (snode->flag & SNODE_SHOW_R) ofs = 1;
3037                                         else if (snode->flag & SNODE_SHOW_G) ofs = 2;
3038                                         else                                 ofs = 3;
3039 #endif
3040
3041                                         glPixelZoom(snode->zoom, snode->zoom);
3042                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
3043
3044                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT,
3045                                                           ((unsigned char *)ibuf->rect) + ofs);
3046
3047                                         glPixelZoom(1.0f, 1.0f);
3048                                 }
3049                                 else if (snode->flag & SNODE_SHOW_ALPHA) {
3050                                         glPixelZoom(snode->zoom, snode->zoom);
3051                                         /* swap bytes, so alpha is most significant one, then just draw it as luminance int */
3052 #ifdef __BIG_ENDIAN__
3053                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
3054 #endif
3055                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_LUMINANCE, GL_UNSIGNED_INT, ibuf->rect);
3056
3057 #ifdef __BIG_ENDIAN__
3058                                         glPixelStorei(GL_UNPACK_SWAP_BYTES, 0);
3059 #endif
3060                                         glPixelZoom(1.0f, 1.0f);
3061                                 }
3062                                 else if (snode->flag & SNODE_USE_ALPHA) {
3063                                         glEnable(GL_BLEND);
3064                                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3065                                         glPixelZoom(snode->zoom, snode->zoom);
3066                                         
3067                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
3068                                         
3069                                         glPixelZoom(1.0f, 1.0f);
3070                                         glDisable(GL_BLEND);
3071                                 }
3072                                 else {
3073                                         glPixelZoom(snode->zoom, snode->zoom);
3074                                         
3075                                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
3076                                         
3077                                         glPixelZoom(1.0f, 1.0f);
3078                                 }
3079                         }
3080
3081                         /** @note draw selected info on backdrop */
3082                         if (snode->edittree) {
3083                                 bNode *node = snode->edittree->nodes.first;
3084                                 while (node) {
3085                                         if (node->flag & NODE_SELECT) {
3086                                                 if (node->typeinfo->uibackdropfunc) {
3087                                                         node->typeinfo->uibackdropfunc(snode, ibuf, node, x, y);
3088                                                 }
3089                                         }
3090                                         node = node->next;
3091                                 }
3092                         }
3093                         
3094                         glMatrixMode(GL_PROJECTION);
3095                         glPopMatrix();
3096                         glMatrixMode(GL_MODELVIEW);