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