doxygen: add newline after \file
[blender.git] / source / blender / editors / mask / mask_edit.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2012 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup edmask
22  */
23
24
25 #include "BLI_math.h"
26
27 #include "BKE_context.h"
28 #include "BKE_mask.h"
29
30 #include "DNA_mask_types.h"
31 #include "DNA_scene_types.h"
32
33 #include "WM_api.h"
34 #include "WM_types.h"
35
36 #include "ED_screen.h"
37 #include "ED_select_utils.h"
38 #include "ED_mask.h"  /* own include */
39 #include "ED_image.h"
40 #include "ED_object.h" /* ED_keymap_proportional_maskmode only */
41 #include "ED_clip.h"
42 #include "ED_sequencer.h"
43 #include "ED_transform.h"
44
45 #include "UI_view2d.h"
46
47 #include "RNA_access.h"
48
49 #include "mask_intern.h"  /* own include */
50
51 /********************** generic poll functions *********************/
52
53 bool ED_maskedit_poll(bContext *C)
54 {
55         ScrArea *sa = CTX_wm_area(C);
56         if (sa) {
57                 switch (sa->spacetype) {
58                         case SPACE_CLIP:
59                                 return ED_space_clip_maskedit_poll(C);
60                         case SPACE_SEQ:
61                                 return ED_space_sequencer_maskedit_poll(C);
62                         case SPACE_IMAGE:
63                                 return ED_space_image_maskedit_poll(C);
64                 }
65         }
66         return false;
67 }
68
69 bool ED_maskedit_mask_poll(bContext *C)
70 {
71         ScrArea *sa = CTX_wm_area(C);
72         if (sa) {
73                 switch (sa->spacetype) {
74                         case SPACE_CLIP:
75                                 return ED_space_clip_maskedit_mask_poll(C);
76                         case SPACE_SEQ:
77                                 return ED_space_sequencer_maskedit_mask_poll(C);
78                         case SPACE_IMAGE:
79                                 return ED_space_image_maskedit_mask_poll(C);
80                 }
81         }
82         return false;
83 }
84
85 /********************** registration *********************/
86
87 /* takes event->mval */
88 void ED_mask_mouse_pos(ScrArea *sa, ARegion *ar, const int mval[2], float co[2])
89 {
90         if (sa) {
91                 switch (sa->spacetype) {
92                         case SPACE_CLIP:
93                         {
94                                 SpaceClip *sc = sa->spacedata.first;
95                                 ED_clip_mouse_pos(sc, ar, mval, co);
96                                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
97                                 break;
98                         }
99                         case SPACE_SEQ:
100                         {
101                                 UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &co[0], &co[1]);
102                                 break;
103                         }
104                         case SPACE_IMAGE:
105                         {
106                                 SpaceImage *sima = sa->spacedata.first;
107                                 ED_image_mouse_pos(sima, ar, mval, co);
108                                 BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co);
109                                 break;
110                         }
111                         default:
112                                 /* possible other spaces from which mask editing is available */
113                                 BLI_assert(0);
114                                 zero_v2(co);
115                                 break;
116                 }
117         }
118         else {
119                 BLI_assert(0);
120                 zero_v2(co);
121         }
122 }
123
124 /* input:  x/y   - mval space
125  * output: xr/yr - mask point space */
126 void ED_mask_point_pos(ScrArea *sa, ARegion *ar, float x, float y, float *xr, float *yr)
127 {
128         float co[2];
129
130         if (sa) {
131                 switch (sa->spacetype) {
132                         case SPACE_CLIP:
133                         {
134                                 SpaceClip *sc = sa->spacedata.first;
135                                 ED_clip_point_stable_pos(sc, ar, x, y, &co[0], &co[1]);
136                                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
137                                 break;
138                         }
139                         case SPACE_SEQ:
140                                 zero_v2(co); /* MASKTODO */
141                                 break;
142                         case SPACE_IMAGE:
143                         {
144                                 SpaceImage *sima = sa->spacedata.first;
145                                 ED_image_point_pos(sima, ar, x, y, &co[0], &co[1]);
146                                 BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co);
147                                 break;
148                         }
149                         default:
150                                 /* possible other spaces from which mask editing is available */
151                                 BLI_assert(0);
152                                 zero_v2(co);
153                                 break;
154                 }
155         }
156         else {
157                 BLI_assert(0);
158                 zero_v2(co);
159         }
160
161         *xr = co[0];
162         *yr = co[1];
163 }
164
165 void ED_mask_point_pos__reverse(ScrArea *sa, ARegion *ar, float x, float y, float *xr, float *yr)
166 {
167         float co[2];
168
169         if (sa) {
170                 switch (sa->spacetype) {
171                         case SPACE_CLIP:
172                         {
173                                 SpaceClip *sc = sa->spacedata.first;
174                                 co[0] = x;
175                                 co[1] = y;
176                                 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co);
177                                 ED_clip_point_stable_pos__reverse(sc, ar, co, co);
178                                 break;
179                         }
180                         case SPACE_SEQ:
181                                 zero_v2(co); /* MASKTODO */
182                                 break;
183                         case SPACE_IMAGE:
184                         {
185                                 SpaceImage *sima = sa->spacedata.first;
186                                 co[0] = x;
187                                 co[1] = y;
188                                 BKE_mask_coord_to_image(sima->image, &sima->iuser, co, co);
189                                 ED_image_point_pos__reverse(sima, ar, co, co);
190                                 break;
191                         }
192                         default:
193                                 /* possible other spaces from which mask editing is available */
194                                 BLI_assert(0);
195                                 zero_v2(co);
196                                 break;
197                 }
198         }
199         else {
200                 BLI_assert(0);
201                 zero_v2(co);
202         }
203
204         *xr = co[0];
205         *yr = co[1];
206 }
207
208 void ED_mask_get_size(ScrArea *sa, int *width, int *height)
209 {
210         if (sa && sa->spacedata.first) {
211                 switch (sa->spacetype) {
212                         case SPACE_CLIP:
213                         {
214                                 SpaceClip *sc = sa->spacedata.first;
215                                 ED_space_clip_get_size(sc, width, height);
216                                 break;
217                         }
218                         case SPACE_SEQ:
219                         {
220 //                              Scene *scene = CTX_data_scene(C);
221 //                              *width = (scene->r.size * scene->r.xsch) / 100;
222 //                              *height = (scene->r.size * scene->r.ysch) / 100;
223                                 break;
224                         }
225                         case SPACE_IMAGE:
226                         {
227                                 SpaceImage *sima = sa->spacedata.first;
228                                 ED_space_image_get_size(sima, width, height);
229                                 break;
230                         }
231                         default:
232                                 /* possible other spaces from which mask editing is available */
233                                 BLI_assert(0);
234                                 *width = 0;
235                                 *height = 0;
236                                 break;
237                 }
238         }
239         else {
240                 BLI_assert(0);
241                 *width = 0;
242                 *height = 0;
243         }
244 }
245
246 void ED_mask_zoom(ScrArea *sa, ARegion *ar, float *zoomx, float *zoomy)
247 {
248         if (sa && sa->spacedata.first) {
249                 switch (sa->spacetype) {
250                         case SPACE_CLIP:
251                         {
252                                 SpaceClip *sc = sa->spacedata.first;
253                                 ED_space_clip_get_zoom(sc, ar, zoomx, zoomy);
254                                 break;
255                         }
256                         case SPACE_SEQ:
257                         {
258                                 *zoomx = *zoomy = 1.0f;
259                                 break;
260                         }
261                         case SPACE_IMAGE:
262                         {
263                                 SpaceImage *sima = sa->spacedata.first;
264                                 ED_space_image_get_zoom(sima, ar, zoomx, zoomy);
265                                 break;
266                         }
267                         default:
268                                 /* possible other spaces from which mask editing is available */
269                                 BLI_assert(0);
270                                 *zoomx = *zoomy = 1.0f;
271                                 break;
272                 }
273         }
274         else {
275                 BLI_assert(0);
276                 *zoomx = *zoomy = 1.0f;
277         }
278 }
279
280 void ED_mask_get_aspect(ScrArea *sa, ARegion *UNUSED(ar), float *aspx, float *aspy)
281 {
282         if (sa && sa->spacedata.first) {
283                 switch (sa->spacetype) {
284                         case SPACE_CLIP:
285                         {
286                                 SpaceClip *sc = sa->spacedata.first;
287                                 ED_space_clip_get_aspect(sc, aspx, aspy);
288                                 break;
289                         }
290                         case SPACE_SEQ:
291                         {
292                                 *aspx = *aspy = 1.0f;  /* MASKTODO - render aspect? */
293                                 break;
294                         }
295                         case SPACE_IMAGE:
296                         {
297                                 SpaceImage *sima = sa->spacedata.first;
298                                 ED_space_image_get_aspect(sima, aspx, aspy);
299                                 break;
300                         }
301                         default:
302                                 /* possible other spaces from which mask editing is available */
303                                 BLI_assert(0);
304                                 *aspx = *aspy = 1.0f;
305                                 break;
306                 }
307         }
308         else {
309                 BLI_assert(0);
310                 *aspx = *aspy = 1.0f;
311         }
312 }
313
314 void ED_mask_pixelspace_factor(ScrArea *sa, ARegion *ar, float *scalex, float *scaley)
315 {
316         if (sa && sa->spacedata.first) {
317                 switch (sa->spacetype) {
318                         case SPACE_CLIP:
319                         {
320                                 SpaceClip *sc = sa->spacedata.first;
321                                 float aspx, aspy;
322
323                                 UI_view2d_scale_get(&ar->v2d, scalex, scaley);
324                                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
325
326                                 *scalex *= aspx;
327                                 *scaley *= aspy;
328                                 break;
329                         }
330                         case SPACE_SEQ:
331                         {
332                                 *scalex = *scaley = 1.0f;  /* MASKTODO? */
333                                 break;
334                         }
335                         case SPACE_IMAGE:
336                         {
337                                 SpaceImage *sima = sa->spacedata.first;
338                                 float aspx, aspy;
339
340                                 UI_view2d_scale_get(&ar->v2d, scalex, scaley);
341                                 ED_space_image_get_aspect(sima, &aspx, &aspy);
342
343                                 *scalex *= aspx;
344                                 *scaley *= aspy;
345                                 break;
346                         }
347                         default:
348                                 /* possible other spaces from which mask editing is available */
349                                 BLI_assert(0);
350                                 *scalex = *scaley = 1.0f;
351                                 break;
352                 }
353         }
354         else {
355                 BLI_assert(0);
356                 *scalex = *scaley = 1.0f;
357         }
358 }
359
360 void ED_mask_cursor_location_get(ScrArea *sa, float cursor[2])
361 {
362         if (sa) {
363                 switch (sa->spacetype) {
364                         case SPACE_CLIP:
365                         {
366                                 SpaceClip *space_clip = sa->spacedata.first;
367                                 copy_v2_v2(cursor, space_clip->cursor);
368                                 break;
369                         }
370                         case SPACE_SEQ:
371                         {
372                                 zero_v2(cursor);
373                                 break;
374                         }
375                         case SPACE_IMAGE:
376                         {
377                                 SpaceImage *space_image = sa->spacedata.first;
378                                 copy_v2_v2(cursor, space_image->cursor);
379                                 break;
380                         }
381                         default:
382                                 /* possible other spaces from which mask editing is available */
383                                 BLI_assert(0);
384                                 zero_v2(cursor);
385                                 break;
386                 }
387         }
388         else {
389                 BLI_assert(0);
390                 zero_v2(cursor);
391         }
392 }
393
394 bool ED_mask_selected_minmax(const bContext *C, float min[2], float max[2])
395 {
396         Mask *mask = CTX_data_edit_mask(C);
397         MaskLayer *mask_layer;
398         bool ok = false;
399
400         if (mask == NULL)
401                 return ok;
402
403         INIT_MINMAX2(min, max);
404         for (mask_layer = mask->masklayers.first;
405              mask_layer != NULL;
406              mask_layer = mask_layer->next)
407         {
408                 MaskSpline *spline;
409                 if (mask_layer->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
410                         continue;
411                 }
412                 for (spline = mask_layer->splines.first;
413                      spline != NULL;
414                      spline = spline->next)
415                 {
416                         MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
417                         int i;
418                         for (i = 0; i < spline->tot_point; i++) {
419                                 MaskSplinePoint *point = &spline->points[i];
420                                 MaskSplinePoint *deform_point = &points_array[i];
421                                 BezTriple *bezt = &point->bezt;
422                                 float handle[2];
423                                 if (!MASKPOINT_ISSEL_ANY(point)) {
424                                         continue;
425                                 }
426                                 if (bezt->f2 & SELECT) {
427                                         minmax_v2v2_v2(min, max, deform_point->bezt.vec[1]);
428                                 }
429                                 if (BKE_mask_point_handles_mode_get(point) == MASK_HANDLE_MODE_STICK) {
430                                         BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_STICK, handle);
431                                         minmax_v2v2_v2(min, max, handle);
432                                 }
433                                 else {
434                                         if ((bezt->f1 & SELECT) && (bezt->h1 != HD_VECT)) {
435                                                 BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_LEFT, handle);
436                                                 minmax_v2v2_v2(min, max, handle);
437                                         }
438                                         if ((bezt->f3 & SELECT) && (bezt->h2 != HD_VECT)) {
439                                                 BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_RIGHT, handle);
440                                                 minmax_v2v2_v2(min, max, handle);
441                                         }
442                                 }
443                                 ok = true;
444                         }
445                 }
446         }
447         return ok;
448 }
449
450 /********************** registration *********************/
451
452 void ED_operatortypes_mask(void)
453 {
454         WM_operatortype_append(MASK_OT_new);
455
456         /* mask layers */
457         WM_operatortype_append(MASK_OT_layer_new);
458         WM_operatortype_append(MASK_OT_layer_remove);
459
460         /* add */
461         WM_operatortype_append(MASK_OT_add_vertex);
462         WM_operatortype_append(MASK_OT_add_feather_vertex);
463         WM_operatortype_append(MASK_OT_primitive_circle_add);
464         WM_operatortype_append(MASK_OT_primitive_square_add);
465
466         /* geometry */
467         WM_operatortype_append(MASK_OT_switch_direction);
468         WM_operatortype_append(MASK_OT_normals_make_consistent);
469         WM_operatortype_append(MASK_OT_delete);
470
471         /* select */
472         WM_operatortype_append(MASK_OT_select);
473         WM_operatortype_append(MASK_OT_select_all);
474         WM_operatortype_append(MASK_OT_select_box);
475         WM_operatortype_append(MASK_OT_select_lasso);
476         WM_operatortype_append(MASK_OT_select_circle);
477         WM_operatortype_append(MASK_OT_select_linked_pick);
478         WM_operatortype_append(MASK_OT_select_linked);
479         WM_operatortype_append(MASK_OT_select_more);
480         WM_operatortype_append(MASK_OT_select_less);
481
482         /* hide/reveal */
483         WM_operatortype_append(MASK_OT_hide_view_clear);
484         WM_operatortype_append(MASK_OT_hide_view_set);
485
486         /* feather */
487         WM_operatortype_append(MASK_OT_feather_weight_clear);
488
489         /* shape */
490         WM_operatortype_append(MASK_OT_slide_point);
491         WM_operatortype_append(MASK_OT_slide_spline_curvature);
492         WM_operatortype_append(MASK_OT_cyclic_toggle);
493         WM_operatortype_append(MASK_OT_handle_type_set);
494
495         /* relationships */
496         WM_operatortype_append(MASK_OT_parent_set);
497         WM_operatortype_append(MASK_OT_parent_clear);
498
499         /* shapekeys */
500         WM_operatortype_append(MASK_OT_shape_key_insert);
501         WM_operatortype_append(MASK_OT_shape_key_clear);
502         WM_operatortype_append(MASK_OT_shape_key_feather_reset);
503         WM_operatortype_append(MASK_OT_shape_key_rekey);
504
505         /* layers */
506         WM_operatortype_append(MASK_OT_layer_move);
507
508         /* duplicate */
509         WM_operatortype_append(MASK_OT_duplicate);
510
511         /* clipboard */
512         WM_operatortype_append(MASK_OT_copy_splines);
513         WM_operatortype_append(MASK_OT_paste_splines);
514 }
515
516 void ED_keymap_mask(wmKeyConfig *keyconf)
517 {
518         wmKeyMap *keymap = WM_keymap_ensure(keyconf, "Mask Editing", 0, 0);
519         keymap->poll = ED_maskedit_poll;
520 }
521
522 void ED_operatormacros_mask(void)
523 {
524         wmOperatorType *ot;
525         wmOperatorTypeMacro *otmacro;
526
527         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide",
528                                           "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
529         ot->description = "Add new vertex and slide it";
530         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
531         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
532         RNA_boolean_set(otmacro->ptr, "is_new_point", true);
533
534         ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", "Add Feather Vertex and Slide",
535                                           "Add new vertex to feather and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
536         ot->description = "Add new feather vertex and slide it";
537         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
538         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
539         RNA_boolean_set(otmacro->ptr, "slide_feather", true);
540
541         ot = WM_operatortype_append_macro("MASK_OT_duplicate_move", "Add Duplicate", "Duplicate mask and move",
542                                           OPTYPE_UNDO | OPTYPE_REGISTER);
543         WM_operatortype_macro_define(ot, "MASK_OT_duplicate");
544         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
545         RNA_enum_set(otmacro->ptr, "proportional", 0);
546         RNA_boolean_set(otmacro->ptr, "mirror", false);
547 }