Merging r50625 through r51896 from trunk into soc-2011-tomato
[blender-staging.git] / source / blender / editors / mask / mask_edit.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) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/mask/mask_edit.c
29  *  \ingroup edmask
30  */
31
32
33 #include "BLI_math.h"
34
35 #include "BKE_context.h"
36 #include "BKE_mask.h"
37
38 #include "DNA_scene_types.h"
39
40 #include "WM_api.h"
41 #include "WM_types.h"
42
43 #include "ED_screen.h"
44 #include "ED_mask.h"  /* own include */
45 #include "ED_image.h"
46 #include "ED_object.h" /* ED_keymap_proportional_maskmode only */
47 #include "ED_clip.h"
48 #include "ED_sequencer.h"
49 #include "ED_transform.h"
50
51 #include "UI_view2d.h"
52
53 #include "RNA_access.h"
54
55 #include "mask_intern.h"  /* own include */
56
57 /********************** generic poll functions *********************/
58
59 int ED_maskedit_poll(bContext *C)
60 {
61         ScrArea *sa = CTX_wm_area(C);
62         if (sa) {
63                 switch (sa->spacetype) {
64                         case SPACE_CLIP:
65                                 return ED_space_clip_maskedit_poll(C);
66                         case SPACE_SEQ:
67                                 return ED_space_sequencer_maskedit_poll(C);
68                         case SPACE_IMAGE:
69                                 return ED_space_image_maskedit_poll(C);
70                 }
71         }
72         return FALSE;
73 }
74
75 int ED_maskedit_mask_poll(bContext *C)
76 {
77         ScrArea *sa = CTX_wm_area(C);
78         if (sa) {
79                 switch (sa->spacetype) {
80                         case SPACE_CLIP:
81                                 return ED_space_clip_maskedit_mask_poll(C);
82                         case SPACE_SEQ:
83                                 return ED_space_sequencer_maskedit_mask_poll(C);
84                         case SPACE_IMAGE:
85                                 return ED_space_image_maskedit_mask_poll(C);
86                 }
87         }
88         return FALSE;
89 }
90
91 /********************** registration *********************/
92
93 /* takes event->mval */
94 void ED_mask_mouse_pos(ScrArea *sa, ARegion *ar, const int mval[2], float co[2])
95 {
96         if (sa) {
97                 switch (sa->spacetype) {
98                         case SPACE_CLIP:
99                         {
100                                 SpaceClip *sc = sa->spacedata.first;
101                                 ED_clip_mouse_pos(sc, ar, mval, co);
102                                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
103                                 break;
104                         }
105                         case SPACE_SEQ:
106                         {
107                                 UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &co[0], &co[1]);
108                                 break;
109                         }
110                         case SPACE_IMAGE:
111                         {
112                                 SpaceImage *sima = sa->spacedata.first;
113                                 ED_image_mouse_pos(sima, ar, mval, co);
114                                 BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co);
115                                 break;
116                         }
117                         default:
118                                 /* possible other spaces from which mask editing is available */
119                                 BLI_assert(0);
120                                 zero_v2(co);
121                                 break;
122                 }
123         }
124         else {
125                 BLI_assert(0);
126                 zero_v2(co);
127         }
128 }
129
130 /* input:  x/y   - mval space
131  * output: xr/yr - mask point space */
132 void ED_mask_point_pos(ScrArea *sa, ARegion *ar, float x, float y, float *xr, float *yr)
133 {
134         float co[2];
135
136         if (sa) {
137                 switch (sa->spacetype) {
138                         case SPACE_CLIP:
139                         {
140                                 SpaceClip *sc = sa->spacedata.first;
141                                 ED_clip_point_stable_pos(sc, ar, x, y, &co[0], &co[1]);
142                                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
143                                 break;
144                         }
145                         case SPACE_SEQ:
146                                 zero_v2(co); /* MASKTODO */
147                                 break;
148                         case SPACE_IMAGE:
149                         {
150                                 SpaceImage *sima = sa->spacedata.first;
151                                 ED_image_point_pos(sima, ar, x, y, &co[0], &co[1]);
152                                 BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co);
153                                 break;
154                         }
155                         default:
156                                 /* possible other spaces from which mask editing is available */
157                                 BLI_assert(0);
158                                 zero_v2(co);
159                                 break;
160                 }
161         }
162         else {
163                 BLI_assert(0);
164                 zero_v2(co);
165         }
166
167         *xr = co[0];
168         *yr = co[1];
169 }
170
171 void ED_mask_point_pos__reverse(ScrArea *sa, ARegion *ar, float x, float y, float *xr, float *yr)
172 {
173         float co[2];
174
175         if (sa) {
176                 switch (sa->spacetype) {
177                         case SPACE_CLIP:
178                         {
179                                 SpaceClip *sc = sa->spacedata.first;
180                                 co[0] = x;
181                                 co[1] = y;
182                                 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co);
183                                 ED_clip_point_stable_pos__reverse(sc, ar, co, co);
184                                 break;
185                         }
186                         case SPACE_SEQ:
187                                 zero_v2(co); /* MASKTODO */
188                                 break;
189                         case SPACE_IMAGE:
190                         {
191                                 SpaceImage *sima = sa->spacedata.first;
192                                 co[0] = x;
193                                 co[1] = y;
194                                 BKE_mask_coord_to_image(sima->image, &sima->iuser, co, co);
195                                 ED_image_point_pos__reverse(sima, ar, co, co);
196                                 break;
197                         }
198                         default:
199                                 /* possible other spaces from which mask editing is available */
200                                 BLI_assert(0);
201                                 zero_v2(co);
202                                 break;
203                 }
204         }
205         else {
206                 BLI_assert(0);
207                 zero_v2(co);
208         }
209
210         *xr = co[0];
211         *yr = co[1];
212 }
213
214 void ED_mask_get_size(ScrArea *sa, int *width, int *height)
215 {
216         if (sa && sa->spacedata.first) {
217                 switch (sa->spacetype) {
218                         case SPACE_CLIP:
219                         {
220                                 SpaceClip *sc = sa->spacedata.first;
221                                 ED_space_clip_get_size(sc, width, height);
222                                 break;
223                         }
224                         case SPACE_SEQ:
225                         {
226 //                              Scene *scene = CTX_data_scene(C);
227 //                              *width = (scene->r.size * scene->r.xsch) / 100;
228 //                              *height = (scene->r.size * scene->r.ysch) / 100;
229                                 break;
230                         }
231                         case SPACE_IMAGE:
232                         {
233                                 SpaceImage *sima = sa->spacedata.first;
234                                 ED_space_image_get_size(sima, width, height);
235                                 break;
236                         }
237                         default:
238                                 /* possible other spaces from which mask editing is available */
239                                 BLI_assert(0);
240                                 *width = 0;
241                                 *height = 0;
242                                 break;
243                 }
244         }
245         else {
246                 BLI_assert(0);
247                 *width = 0;
248                 *height = 0;
249         }
250 }
251
252 void ED_mask_zoom(ScrArea *sa, ARegion *ar, float *zoomx, float *zoomy)
253 {
254         if (sa && sa->spacedata.first) {
255                 switch (sa->spacetype) {
256                         case SPACE_CLIP:
257                         {
258                                 SpaceClip *sc = sa->spacedata.first;
259                                 ED_space_clip_get_zoom(sc, ar, zoomx, zoomy);
260                                 break;
261                         }
262                         case SPACE_SEQ:
263                         {
264                                 *zoomx = *zoomy = 1.0f;
265                                 break;
266                         }
267                         case SPACE_IMAGE:
268                         {
269                                 SpaceImage *sima = sa->spacedata.first;
270                                 ED_space_image_get_zoom(sima, ar, zoomx, zoomy);
271                                 break;
272                         }
273                         default:
274                                 /* possible other spaces from which mask editing is available */
275                                 BLI_assert(0);
276                                 *zoomx = *zoomy = 1.0f;
277                                 break;
278                 }
279         }
280         else {
281                 BLI_assert(0);
282                 *zoomx = *zoomy = 1.0f;
283         }
284 }
285
286 void ED_mask_get_aspect(ScrArea *sa, ARegion *UNUSED(ar), float *aspx, float *aspy)
287 {
288         if (sa && sa->spacedata.first) {
289                 switch (sa->spacetype) {
290                         case SPACE_CLIP:
291                         {
292                                 SpaceClip *sc = sa->spacedata.first;
293                                 ED_space_clip_get_aspect(sc, aspx, aspy);
294                                 break;
295                         }
296                         case SPACE_SEQ:
297                         {
298                                 *aspx = *aspy = 1.0f;  /* MASKTODO - render aspect? */
299                                 break;
300                         }
301                         case SPACE_IMAGE:
302                         {
303                                 SpaceImage *sima = sa->spacedata.first;
304                                 ED_space_image_get_aspect(sima, aspx, aspy);
305                                 break;
306                         }
307                         default:
308                                 /* possible other spaces from which mask editing is available */
309                                 BLI_assert(0);
310                                 *aspx = *aspy = 1.0f;
311                                 break;
312                 }
313         }
314         else {
315                 BLI_assert(0);
316                 *aspx = *aspy = 1.0f;
317         }
318 }
319
320 void ED_mask_pixelspace_factor(ScrArea *sa, ARegion *ar, float *scalex, float *scaley)
321 {
322         if (sa && sa->spacedata.first) {
323                 switch (sa->spacetype) {
324                         case SPACE_CLIP:
325                         {
326                                 SpaceClip *sc = sa->spacedata.first;
327                                 int width, height;
328                                 float zoomx, zoomy, aspx, aspy;
329
330                                 ED_space_clip_get_size(sc, &width, &height);
331                                 ED_space_clip_get_zoom(sc, ar, &zoomx, &zoomy);
332                                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
333
334                                 *scalex = ((float)width * aspx) * zoomx;
335                                 *scaley = ((float)height * aspy) * zoomy;
336                                 break;
337                         }
338                         case SPACE_SEQ:
339                         {
340                                 *scalex = *scaley = 1.0f;  /* MASKTODO? */
341                                 break;
342                         }
343                         case SPACE_IMAGE:
344                         {
345                                 SpaceImage *sima = sa->spacedata.first;
346                                 int width, height;
347                                 float zoomx, zoomy, aspx, aspy;
348
349                                 ED_space_image_get_size(sima, &width, &height);
350                                 ED_space_image_get_zoom(sima, ar, &zoomx, &zoomy);
351                                 ED_space_image_get_aspect(sima, &aspx, &aspy);
352
353                                 *scalex = ((float)width * aspx) * zoomx;
354                                 *scaley = ((float)height * aspy) * zoomy;
355                                 break;
356                         }
357                         default:
358                                 /* possible other spaces from which mask editing is available */
359                                 BLI_assert(0);
360                                 *scalex = *scaley = 1.0f;
361                                 break;
362                 }
363         }
364         else {
365                 BLI_assert(0);
366                 *scalex = *scaley = 1.0f;
367         }
368 }
369
370 /********************** registration *********************/
371
372 void ED_operatortypes_mask(void)
373 {
374         WM_operatortype_append(MASK_OT_new);
375
376         /* mask layers */
377         WM_operatortype_append(MASK_OT_layer_new);
378         WM_operatortype_append(MASK_OT_layer_remove);
379
380         /* add */
381         WM_operatortype_append(MASK_OT_add_vertex);
382         WM_operatortype_append(MASK_OT_add_feather_vertex);
383
384         /* geometry */
385         WM_operatortype_append(MASK_OT_switch_direction);
386         WM_operatortype_append(MASK_OT_normals_make_consistent);
387         WM_operatortype_append(MASK_OT_delete);
388
389         /* select */
390         WM_operatortype_append(MASK_OT_select);
391         WM_operatortype_append(MASK_OT_select_all);
392         WM_operatortype_append(MASK_OT_select_border);
393         WM_operatortype_append(MASK_OT_select_lasso);
394         WM_operatortype_append(MASK_OT_select_circle);
395         WM_operatortype_append(MASK_OT_select_linked_pick);
396         WM_operatortype_append(MASK_OT_select_linked);
397
398         /* hide/reveal */
399         WM_operatortype_append(MASK_OT_hide_view_clear);
400         WM_operatortype_append(MASK_OT_hide_view_set);
401
402         /* feather */
403         WM_operatortype_append(MASK_OT_feather_weight_clear);
404
405         /* shape */
406         WM_operatortype_append(MASK_OT_slide_point);
407         WM_operatortype_append(MASK_OT_cyclic_toggle);
408         WM_operatortype_append(MASK_OT_handle_type_set);
409
410         /* relationships */
411         WM_operatortype_append(MASK_OT_parent_set);
412         WM_operatortype_append(MASK_OT_parent_clear);
413
414         /* shapekeys */
415         WM_operatortype_append(MASK_OT_shape_key_insert);
416         WM_operatortype_append(MASK_OT_shape_key_clear);
417         WM_operatortype_append(MASK_OT_shape_key_feather_reset);
418         WM_operatortype_append(MASK_OT_shape_key_rekey);
419
420         /* layers */
421         WM_operatortype_append(MASK_OT_layer_move);
422 }
423
424 void ED_keymap_mask(wmKeyConfig *keyconf)
425 {
426         wmKeyMap *keymap;
427         wmKeyMapItem *kmi;
428
429         keymap = WM_keymap_find(keyconf, "Mask Editing", 0, 0);
430         keymap->poll = ED_maskedit_poll;
431
432         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
433
434         /* mask mode supports PET now */
435         ED_keymap_proportional_cycle(keyconf, keymap);
436         ED_keymap_proportional_maskmode(keyconf, keymap);
437
438         /* geometry */
439         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
440         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
441         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
442         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
443
444         /* selection */
445         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
446         RNA_boolean_set(kmi->ptr, "extend", FALSE);
447         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
448         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
449         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
450         RNA_boolean_set(kmi->ptr, "extend", FALSE);
451         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
452         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
453
454         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
455         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
456         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
457         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
458
459         WM_keymap_add_item(keymap, "MASK_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
460         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
461         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
462         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
463         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
464
465         WM_keymap_add_item(keymap, "MASK_OT_select_border", BKEY, KM_PRESS, 0, 0);
466         WM_keymap_add_item(keymap, "MASK_OT_select_circle", CKEY, KM_PRESS, 0, 0);
467
468         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
469         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
470         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
471         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
472
473         /* hide/reveal */
474         WM_keymap_add_item(keymap, "MASK_OT_hide_view_clear", HKEY, KM_PRESS, KM_ALT, 0);
475         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, 0, 0);
476         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
477
478         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, KM_SHIFT, 0);
479         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
480
481         /* select clip while in maker view,
482          * this matches View3D functionality where you can select an
483          * object while in editmode to allow vertex parenting */
484         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
485         RNA_boolean_set(kmi->ptr, "extend", FALSE);
486
487         /* shape */
488         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
489         WM_keymap_add_item(keymap, "MASK_OT_slide_point", ACTIONMOUSE, KM_PRESS, 0, 0);
490         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
491         WM_keymap_add_item(keymap, "MASK_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
492         // WM_keymap_add_item(keymap, "MASK_OT_feather_weight_clear", SKEY, KM_PRESS, KM_ALT, 0);
493         /* ... matches curve editmode */
494
495         /* relationships */
496         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
497         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
498
499         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
500         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
501
502         /* for image editor only */
503         WM_keymap_add_item(keymap, "UV_OT_cursor_set", ACTIONMOUSE, KM_PRESS, 0, 0);
504
505         /* Transform (don't use transform_keymap_for_space() since this maps to different spaces) */
506         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
507         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", EVT_TWEAK_S, KM_ANY, 0, 0);
508         WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
509         WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
510         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0);
511         RNA_enum_set(kmi->ptr, "mode", TFM_MASK_SHRINKFATTEN);
512 }
513
514 void ED_operatormacros_mask(void)
515 {
516         /* XXX: just for sample */
517         wmOperatorType *ot;
518         wmOperatorTypeMacro *otmacro;
519
520         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide",
521                                           "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
522         ot->description = "Add new vertex and slide it";
523         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
524         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
525         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
526
527         ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", "Add Feather Vertex and Slide",
528                                           "Add new vertex to feather and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
529         ot->description = "Add new feather vertex and slide it";
530         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
531         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
532         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
533 }