Patch #36622, by Henrik Aarnio: Fit backdrop image to the area dimensions.
[blender.git] / source / blender / editors / space_node / node_view.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) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation, Nathan Letwory
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_node/node_view.c
28  *  \ingroup spnode
29  */
30
31 #include "DNA_node_types.h"
32
33 #include "BLI_rect.h"
34 #include "BLI_utildefines.h"
35 #include "BLI_math.h"
36
37 #include "BKE_context.h"
38 #include "BKE_image.h"
39 #include "BKE_screen.h"
40 #include "BKE_node.h"
41
42 #include "ED_node.h"  /* own include */
43 #include "ED_screen.h"
44 #include "ED_space_api.h"
45 #include "ED_image.h"
46
47 #include "UI_view2d.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_view2d.h"
56
57 #include "MEM_guardedalloc.h"
58
59 #include "IMB_colormanagement.h"
60 #include "IMB_imbuf.h"
61 #include "IMB_imbuf_types.h"
62
63 #include "node_intern.h"  /* own include */
64 #include "NOD_composite.h"
65
66
67 /* **************** View All Operator ************** */
68
69 int space_node_view_flag(bContext *C, SpaceNode *snode, ARegion *ar, const int node_flag)
70 {
71         bNode *node;
72         rctf cur_new;
73         float oldwidth, oldheight, width, height;
74         float oldasp, asp;
75         int tot = 0;
76         int has_frame = FALSE;
77         
78         oldwidth  = BLI_rctf_size_x(&ar->v2d.cur);
79         oldheight = BLI_rctf_size_y(&ar->v2d.cur);
80
81         oldasp = oldwidth / oldheight;
82
83         BLI_rctf_init_minmax(&cur_new);
84
85         if (snode->edittree) {
86                 for (node = snode->edittree->nodes.first; node; node = node->next) {
87                         if ((node->flag & node_flag) == node_flag) {
88                                 BLI_rctf_union(&cur_new, &node->totr);
89                                 tot++;
90
91                                 if (node->type == NODE_FRAME) {
92                                         has_frame = TRUE;
93                                 }
94                         }
95                 }
96         }
97
98         if (tot) {
99                 width  = BLI_rctf_size_x(&cur_new);
100                 height = BLI_rctf_size_y(&cur_new);
101                 asp = width / height;
102
103                 /* for single non-frame nodes, don't zoom in, just pan view,
104                  * but do allow zooming out, this allows for big nodes to be zoomed out */
105                 if ((tot == 1) &&
106                     (has_frame == FALSE) &&
107                     ((oldwidth * oldheight) > (width * height)))
108                 {
109                         /* center, don't zoom */
110                         BLI_rctf_resize(&cur_new, oldwidth, oldheight);
111                 }
112                 else {
113                         if (oldasp < asp) {
114                                 const float height_new = width / oldasp;
115                                 cur_new.ymin = cur_new.ymin - height_new / 2.0f;
116                                 cur_new.ymax = cur_new.ymax + height_new / 2.0f;
117                         }
118                         else {
119                                 const float width_new = height * oldasp;
120                                 cur_new.xmin = cur_new.xmin - width_new / 2.0f;
121                                 cur_new.xmax = cur_new.xmax + width_new / 2.0f;
122                         }
123
124                         /* add some padding */
125                         BLI_rctf_scale(&cur_new, 1.1f);
126                 }
127
128                 UI_view2d_smooth_view(C, ar, &cur_new);
129         }
130
131         return (tot != 0);
132 }
133
134 static int node_view_all_exec(bContext *C, wmOperator *UNUSED(op))
135 {
136         ARegion *ar = CTX_wm_region(C);
137         SpaceNode *snode = CTX_wm_space_node(C);
138
139         /* is this really needed? */
140         snode->xof = 0;
141         snode->yof = 0;
142
143         if (space_node_view_flag(C, snode, ar, 0)) {
144                 return OPERATOR_FINISHED;
145         }
146         else {
147                 return OPERATOR_CANCELLED;
148         }
149 }
150
151 void NODE_OT_view_all(wmOperatorType *ot)
152 {
153         /* identifiers */
154         ot->name = "View All";
155         ot->idname = "NODE_OT_view_all";
156         ot->description = "Resize view so you can see all nodes";
157         
158         /* api callbacks */
159         ot->exec = node_view_all_exec;
160         ot->poll = ED_operator_node_active;
161         
162         /* flags */
163         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
164 }
165
166 static int node_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
167 {
168         ARegion *ar = CTX_wm_region(C);
169         SpaceNode *snode = CTX_wm_space_node(C);
170
171         if (space_node_view_flag(C, snode, ar, NODE_SELECT)) {
172                 return OPERATOR_FINISHED;
173         }
174         else {
175                 return OPERATOR_CANCELLED;
176         }
177 }
178
179 void NODE_OT_view_selected(wmOperatorType *ot)
180 {
181         /* identifiers */
182         ot->name = "View Selected";
183         ot->idname = "NODE_OT_view_selected";
184         ot->description = "Resize view so you can see selected nodes";
185
186         /* api callbacks */
187         ot->exec = node_view_selected_exec;
188         ot->poll = ED_operator_node_active;
189
190         /* flags */
191         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
192 }
193
194 /* **************** Backround Image Operators ************** */
195
196 typedef struct NodeViewMove {
197         int mvalo[2];
198         int xmin, ymin, xmax, ymax;
199 } NodeViewMove;
200
201 static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, const wmEvent *event)
202 {
203         SpaceNode *snode = CTX_wm_space_node(C);
204         ARegion *ar = CTX_wm_region(C);
205         NodeViewMove *nvm = op->customdata;
206
207         switch (event->type) {
208                 case MOUSEMOVE:
209
210                         snode->xof -= (nvm->mvalo[0] - event->mval[0]);
211                         snode->yof -= (nvm->mvalo[1] - event->mval[1]);
212                         nvm->mvalo[0] = event->mval[0];
213                         nvm->mvalo[1] = event->mval[1];
214
215                         /* prevent dragging image outside of the window and losing it! */
216                         CLAMP(snode->xof, nvm->xmin, nvm->xmax);
217                         CLAMP(snode->yof, nvm->ymin, nvm->ymax);
218
219                         ED_region_tag_redraw(ar);
220
221                         break;
222
223                 case LEFTMOUSE:
224                 case MIDDLEMOUSE:
225                 case RIGHTMOUSE:
226
227                         MEM_freeN(nvm);
228                         op->customdata = NULL;
229
230                         return OPERATOR_FINISHED;
231         }
232
233         return OPERATOR_RUNNING_MODAL;
234 }
235
236 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, const wmEvent *event)
237 {
238         SpaceNode *snode = CTX_wm_space_node(C);
239         ARegion *ar = CTX_wm_region(C);
240         NodeViewMove *nvm;
241         Image *ima;
242         ImBuf *ibuf;
243         const float pad = 32.0f; /* better be bigger then scrollbars */
244
245         void *lock;
246
247         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
248         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
249
250         if (ibuf == NULL) {
251                 BKE_image_release_ibuf(ima, ibuf, lock);
252                 return OPERATOR_CANCELLED;
253         }
254
255         nvm = MEM_callocN(sizeof(NodeViewMove), "NodeViewMove struct");
256         op->customdata = nvm;
257         nvm->mvalo[0] = event->mval[0];
258         nvm->mvalo[1] = event->mval[1];
259
260         nvm->xmin = -(ar->winx / 2) - (ibuf->x * (0.5f * snode->zoom)) + pad;
261         nvm->xmax =  (ar->winx / 2) + (ibuf->x * (0.5f * snode->zoom)) - pad;
262         nvm->ymin = -(ar->winy / 2) - (ibuf->y * (0.5f * snode->zoom)) + pad;
263         nvm->ymax =  (ar->winy / 2) + (ibuf->y * (0.5f * snode->zoom)) - pad;
264
265         BKE_image_release_ibuf(ima, ibuf, lock);
266
267         /* add modal handler */
268         WM_event_add_modal_handler(C, op);
269
270         return OPERATOR_RUNNING_MODAL;
271 }
272
273 static int snode_bg_viewmove_cancel(bContext *UNUSED(C), wmOperator *op)
274 {
275         MEM_freeN(op->customdata);
276         op->customdata = NULL;
277
278         return OPERATOR_CANCELLED;
279 }
280
281 void NODE_OT_backimage_move(wmOperatorType *ot)
282 {
283         /* identifiers */
284         ot->name = "Background Image Move";
285         ot->description = "Move Node backdrop";
286         ot->idname = "NODE_OT_backimage_move";
287
288         /* api callbacks */
289         ot->invoke = snode_bg_viewmove_invoke;
290         ot->modal = snode_bg_viewmove_modal;
291         ot->poll = composite_node_active;
292         ot->cancel = snode_bg_viewmove_cancel;
293
294         /* flags */
295         ot->flag = OPTYPE_BLOCKING | OPTYPE_GRAB_POINTER;
296 }
297
298 static int backimage_zoom_exec(bContext *C, wmOperator *op)
299 {
300         SpaceNode *snode = CTX_wm_space_node(C);
301         ARegion *ar = CTX_wm_region(C);
302         float fac = RNA_float_get(op->ptr, "factor");
303
304         snode->zoom *= fac;
305         ED_region_tag_redraw(ar);
306
307         return OPERATOR_FINISHED;
308 }
309
310
311 void NODE_OT_backimage_zoom(wmOperatorType *ot)
312 {
313
314         /* identifiers */
315         ot->name = "Background Image Zoom";
316         ot->idname = "NODE_OT_backimage_zoom";
317         ot->description = "Zoom in/out the background image";
318
319         /* api callbacks */
320         ot->exec = backimage_zoom_exec;
321         ot->poll = composite_node_active;
322
323         /* flags */
324         ot->flag = OPTYPE_BLOCKING;
325
326         /* internal */
327         RNA_def_float(ot->srna, "factor", 1.2f, 0.0f, 10.0f, "Factor", "", 0.0f, 10.0f);
328 }
329
330 static int backimage_fit_exec(bContext *C, wmOperator *op)
331 {
332         SpaceNode *snode = CTX_wm_space_node(C);
333         ARegion *ar = CTX_wm_region(C);
334
335         Image *ima;
336         ImBuf *ibuf;
337
338         const float pad = 32.0f;
339
340         void *lock;
341
342         float facx, facy;
343
344         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
345         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
346
347         if (ibuf == NULL) {
348                 BKE_image_release_ibuf(ima, ibuf, lock);
349                 return OPERATOR_CANCELLED;
350         }
351
352         facx = 1.0f * (ar->sizex - pad) / (ibuf->x * snode->zoom);
353         facy = 1.0f * (ar->sizey - pad) / (ibuf->y * snode->zoom);
354
355         BKE_image_release_ibuf(ima, ibuf, lock);
356
357         snode->zoom *= min_ff(facx, facy);
358
359         snode->xof = 0;
360         snode->yof = 0;
361
362         ED_region_tag_redraw(ar);
363
364         return OPERATOR_FINISHED;
365 }
366
367 void NODE_OT_backimage_fit(wmOperatorType *ot)
368 {
369
370         /* identifiers */
371         ot->name = "Background Image Fit";
372         ot->idname = "NODE_OT_backimage_fit";
373         ot->description = "Zoom in/out the background image";
374
375         /* api callbacks */
376         ot->exec = backimage_fit_exec;
377         ot->poll = composite_node_active;
378
379         /* flags */
380         ot->flag = OPTYPE_BLOCKING;
381
382 }
383
384 /******************** sample backdrop operator ********************/
385
386 typedef struct ImageSampleInfo {
387         ARegionType *art;
388         void *draw_handle;
389         int x, y;
390         int channels;
391
392         unsigned char col[4];
393         float colf[4];
394         float linearcol[4];
395         
396         int z;
397         float zf;
398
399         int *zp;
400         float *zfp;
401
402         int draw;
403         int color_manage;
404 } ImageSampleInfo;
405
406 static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
407 {
408         Scene *scene = CTX_data_scene(C);
409         ImageSampleInfo *info = arg_info;
410
411         if (info->draw) {
412                 ED_image_draw_info(scene, ar, info->color_manage, FALSE, info->channels,
413                                    info->x, info->y, info->col, info->colf, info->linearcol,
414                                    info->zp, info->zfp);
415         }
416 }
417
418 /* returns color in SRGB */
419 /* matching ED_space_image_color_sample() */
420 int ED_space_node_color_sample(SpaceNode *snode, ARegion *ar, int mval[2], float r_col[3])
421 {
422         void *lock;
423         Image *ima;
424         ImBuf *ibuf;
425         float fx, fy, bufx, bufy;
426         int ret = FALSE;
427
428         if (STREQ(snode->tree_idname, ntreeType_Composite->idname) || (snode->flag & SNODE_BACKDRAW) == 0) {
429                 /* use viewer image for color sampling only if we're in compositor tree
430                  * with backdrop enabled
431                  */
432                 return FALSE;
433         }
434
435         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
436         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
437         if (!ibuf) {
438                 return FALSE;
439         }
440
441         /* map the mouse coords to the backdrop image space */
442         bufx = ibuf->x * snode->zoom;
443         bufy = ibuf->y * snode->zoom;
444         fx = (bufx > 0.0f ? ((float)mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
445         fy = (bufy > 0.0f ? ((float)mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
446
447         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
448                 float *fp;
449                 unsigned char *cp;
450                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
451
452                 CLAMP(x, 0, ibuf->x - 1);
453                 CLAMP(y, 0, ibuf->y - 1);
454
455                 if (ibuf->rect_float) {
456                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
457                         /* IB_PROFILE_NONE is default but infact its linear */
458                         linearrgb_to_srgb_v3_v3(r_col, fp);
459                         ret = TRUE;
460                 }
461                 else if (ibuf->rect) {
462                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
463                         rgb_uchar_to_float(r_col, cp);
464                         ret = TRUE;
465                 }
466         }
467
468         BKE_image_release_ibuf(ima, ibuf, lock);
469
470         return ret;
471 }
472
473 static void sample_apply(bContext *C, wmOperator *op, const wmEvent *event)
474 {
475         SpaceNode *snode = CTX_wm_space_node(C);
476         ARegion *ar = CTX_wm_region(C);
477         ImageSampleInfo *info = op->customdata;
478         void *lock;
479         Image *ima;
480         ImBuf *ibuf;
481         float fx, fy, bufx, bufy;
482
483         ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
484         ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
485         if (!ibuf) {
486                 info->draw = 0;
487                 return;
488         }
489
490         if (!ibuf->rect) {
491                 IMB_rect_from_float(ibuf);
492         }
493
494         /* map the mouse coords to the backdrop image space */
495         bufx = ibuf->x * snode->zoom;
496         bufy = ibuf->y * snode->zoom;
497         fx = (bufx > 0.0f ? ((float)event->mval[0] - 0.5f * ar->winx - snode->xof) / bufx + 0.5f : 0.0f);
498         fy = (bufy > 0.0f ? ((float)event->mval[1] - 0.5f * ar->winy - snode->yof) / bufy + 0.5f : 0.0f);
499
500         if (fx >= 0.0f && fy >= 0.0f && fx < 1.0f && fy < 1.0f) {
501                 float *fp;
502                 unsigned char *cp;
503                 int x = (int)(fx * ibuf->x), y = (int)(fy * ibuf->y);
504
505                 CLAMP(x, 0, ibuf->x - 1);
506                 CLAMP(y, 0, ibuf->y - 1);
507
508                 info->x = x;
509                 info->y = y;
510                 info->draw = 1;
511                 info->channels = ibuf->channels;
512
513                 info->zp = NULL;
514                 info->zfp = NULL;
515
516                 if (ibuf->rect) {
517                         cp = (unsigned char *)(ibuf->rect + y * ibuf->x + x);
518
519                         info->col[0] = cp[0];
520                         info->col[1] = cp[1];
521                         info->col[2] = cp[2];
522                         info->col[3] = cp[3];
523
524                         info->colf[0] = (float)cp[0] / 255.0f;
525                         info->colf[1] = (float)cp[1] / 255.0f;
526                         info->colf[2] = (float)cp[2] / 255.0f;
527                         info->colf[3] = (float)cp[3] / 255.0f;
528
529                         copy_v4_v4(info->linearcol, info->colf);
530                         IMB_colormanagement_colorspace_to_scene_linear_v4(info->linearcol, false, ibuf->rect_colorspace);
531
532                         info->color_manage = TRUE;
533                 }
534                 if (ibuf->rect_float) {
535                         fp = (ibuf->rect_float + (ibuf->channels) * (y * ibuf->x + x));
536
537                         info->colf[0] = fp[0];
538                         info->colf[1] = fp[1];
539                         info->colf[2] = fp[2];
540                         info->colf[3] = fp[3];
541
542                         info->color_manage = TRUE;
543                 }
544                 
545                 if (ibuf->zbuf) {
546                         info->z = ibuf->zbuf[y * ibuf->x + x];
547                         info->zp = &info->z;
548                 }
549                 if (ibuf->zbuf_float) {
550                         info->zf = ibuf->zbuf_float[y * ibuf->x + x];
551                         info->zfp = &info->zf;
552                 }
553
554                 ED_node_sample_set(info->colf);
555         }
556         else {
557                 info->draw = 0;
558                 ED_node_sample_set(NULL);
559         }
560
561         BKE_image_release_ibuf(ima, ibuf, lock);
562
563         ED_area_tag_redraw(CTX_wm_area(C));
564 }
565
566 static void sample_exit(bContext *C, wmOperator *op)
567 {
568         ImageSampleInfo *info = op->customdata;
569
570         ED_node_sample_set(NULL);
571         ED_region_draw_cb_exit(info->art, info->draw_handle);
572         ED_area_tag_redraw(CTX_wm_area(C));
573         MEM_freeN(info);
574 }
575
576 static int sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
577 {
578         SpaceNode *snode = CTX_wm_space_node(C);
579         ARegion *ar = CTX_wm_region(C);
580         ImageSampleInfo *info;
581
582         if (!ED_node_is_compositor(snode) || !(snode->flag & SNODE_BACKDRAW))
583                 return OPERATOR_CANCELLED;
584
585         info = MEM_callocN(sizeof(ImageSampleInfo), "ImageSampleInfo");
586         info->art = ar->type;
587         info->draw_handle = ED_region_draw_cb_activate(ar->type, sample_draw, info, REGION_DRAW_POST_PIXEL);
588         op->customdata = info;
589
590         sample_apply(C, op, event);
591
592         WM_event_add_modal_handler(C, op);
593
594         return OPERATOR_RUNNING_MODAL;
595 }
596
597 static int sample_modal(bContext *C, wmOperator *op, const wmEvent *event)
598 {
599         switch (event->type) {
600                 case LEFTMOUSE:
601                 case RIGHTMOUSE: // XXX hardcoded
602                         sample_exit(C, op);
603                         return OPERATOR_CANCELLED;
604                 case MOUSEMOVE:
605                         sample_apply(C, op, event);
606                         break;
607         }
608
609         return OPERATOR_RUNNING_MODAL;
610 }
611
612 static int sample_cancel(bContext *C, wmOperator *op)
613 {
614         sample_exit(C, op);
615         return OPERATOR_CANCELLED;
616 }
617
618 void NODE_OT_backimage_sample(wmOperatorType *ot)
619 {
620         /* identifiers */
621         ot->name = "Backimage Sample";
622         ot->idname = "NODE_OT_backimage_sample";
623         ot->description = "Use mouse to sample background image";
624
625         /* api callbacks */
626         ot->invoke = sample_invoke;
627         ot->modal = sample_modal;
628         ot->cancel = sample_cancel;
629         ot->poll = ED_operator_node_active;
630
631         /* flags */
632         ot->flag = OPTYPE_BLOCKING;
633 }