Fix #34941: Space.draw_handler_add now supports PRE_VIEW and POST_VIEW callbacks
[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                                 float aspx, aspy;
328
329                                 UI_view2d_getscale(&ar->v2d, scalex, scaley);
330                                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
331
332                                 *scalex *= aspx;
333                                 *scaley *= aspy;
334                                 break;
335                         }
336                         case SPACE_SEQ:
337                         {
338                                 *scalex = *scaley = 1.0f;  /* MASKTODO? */
339                                 break;
340                         }
341                         case SPACE_IMAGE:
342                         {
343                                 SpaceImage *sima = sa->spacedata.first;
344                                 float aspx, aspy;
345
346                                 UI_view2d_getscale(&ar->v2d, scalex, scaley);
347                                 ED_space_image_get_aspect(sima, &aspx, &aspy);
348
349                                 *scalex *= aspx;
350                                 *scaley *= aspy;
351                                 break;
352                         }
353                         default:
354                                 /* possible other spaces from which mask editing is available */
355                                 BLI_assert(0);
356                                 *scalex = *scaley = 1.0f;
357                                 break;
358                 }
359         }
360         else {
361                 BLI_assert(0);
362                 *scalex = *scaley = 1.0f;
363         }
364 }
365
366 /********************** registration *********************/
367
368 void ED_operatortypes_mask(void)
369 {
370         WM_operatortype_append(MASK_OT_new);
371
372         /* mask layers */
373         WM_operatortype_append(MASK_OT_layer_new);
374         WM_operatortype_append(MASK_OT_layer_remove);
375
376         /* add */
377         WM_operatortype_append(MASK_OT_add_vertex);
378         WM_operatortype_append(MASK_OT_add_feather_vertex);
379
380         /* geometry */
381         WM_operatortype_append(MASK_OT_switch_direction);
382         WM_operatortype_append(MASK_OT_normals_make_consistent);
383         WM_operatortype_append(MASK_OT_delete);
384
385         /* select */
386         WM_operatortype_append(MASK_OT_select);
387         WM_operatortype_append(MASK_OT_select_all);
388         WM_operatortype_append(MASK_OT_select_border);
389         WM_operatortype_append(MASK_OT_select_lasso);
390         WM_operatortype_append(MASK_OT_select_circle);
391         WM_operatortype_append(MASK_OT_select_linked_pick);
392         WM_operatortype_append(MASK_OT_select_linked);
393
394         /* hide/reveal */
395         WM_operatortype_append(MASK_OT_hide_view_clear);
396         WM_operatortype_append(MASK_OT_hide_view_set);
397
398         /* feather */
399         WM_operatortype_append(MASK_OT_feather_weight_clear);
400
401         /* shape */
402         WM_operatortype_append(MASK_OT_slide_point);
403         WM_operatortype_append(MASK_OT_cyclic_toggle);
404         WM_operatortype_append(MASK_OT_handle_type_set);
405
406         /* relationships */
407         WM_operatortype_append(MASK_OT_parent_set);
408         WM_operatortype_append(MASK_OT_parent_clear);
409
410         /* shapekeys */
411         WM_operatortype_append(MASK_OT_shape_key_insert);
412         WM_operatortype_append(MASK_OT_shape_key_clear);
413         WM_operatortype_append(MASK_OT_shape_key_feather_reset);
414         WM_operatortype_append(MASK_OT_shape_key_rekey);
415
416         /* layers */
417         WM_operatortype_append(MASK_OT_layer_move);
418 }
419
420 void ED_keymap_mask(wmKeyConfig *keyconf)
421 {
422         wmKeyMap *keymap;
423         wmKeyMapItem *kmi;
424
425         keymap = WM_keymap_find(keyconf, "Mask Editing", 0, 0);
426         keymap->poll = ED_maskedit_poll;
427
428         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
429
430         /* mask mode supports PET now */
431         ED_keymap_proportional_cycle(keyconf, keymap);
432         ED_keymap_proportional_maskmode(keyconf, keymap);
433
434         /* geometry */
435         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", ACTIONMOUSE, KM_PRESS, KM_CTRL, 0);
436         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
437         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
438         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
439
440         /* selection */
441         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
442         RNA_boolean_set(kmi->ptr, "extend", FALSE);
443         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
444         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
445         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
446         RNA_boolean_set(kmi->ptr, "extend", FALSE);
447         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
448         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
449
450         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
451         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
452         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
453         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
454
455         WM_keymap_add_item(keymap, "MASK_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
456         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
457         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
458         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
459         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
460
461         WM_keymap_add_item(keymap, "MASK_OT_select_border", BKEY, KM_PRESS, 0, 0);
462         WM_keymap_add_item(keymap, "MASK_OT_select_circle", CKEY, KM_PRESS, 0, 0);
463
464         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
465         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
466         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
467         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
468
469         /* hide/reveal */
470         WM_keymap_add_item(keymap, "MASK_OT_hide_view_clear", HKEY, KM_PRESS, KM_ALT, 0);
471         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, 0, 0);
472         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
473
474         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, KM_SHIFT, 0);
475         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
476
477         /* select clip while in maker view,
478          * this matches View3D functionality where you can select an
479          * object while in editmode to allow vertex parenting */
480         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
481         RNA_boolean_set(kmi->ptr, "extend", FALSE);
482
483         /* shape */
484         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
485         WM_keymap_add_item(keymap, "MASK_OT_slide_point", ACTIONMOUSE, KM_PRESS, 0, 0);
486         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
487         WM_keymap_add_item(keymap, "MASK_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
488         // WM_keymap_add_item(keymap, "MASK_OT_feather_weight_clear", SKEY, KM_PRESS, KM_ALT, 0);
489         /* ... matches curve editmode */
490
491         /* relationships */
492         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
493         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
494
495         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
496         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
497
498         /* for image editor only */
499         WM_keymap_add_item(keymap, "UV_OT_cursor_set", ACTIONMOUSE, KM_PRESS, 0, 0);
500
501         /* Transform (don't use transform_keymap_for_space() since this maps to different spaces) */
502         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
503         WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", EVT_TWEAK_S, KM_ANY, 0, 0);
504         WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
505         WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
506         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0);
507         RNA_enum_set(kmi->ptr, "mode", TFM_MASK_SHRINKFATTEN);
508 }
509
510 void ED_operatormacros_mask(void)
511 {
512         /* XXX: just for sample */
513         wmOperatorType *ot;
514         wmOperatorTypeMacro *otmacro;
515
516         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide",
517                                           "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
518         ot->description = "Add new vertex and slide it";
519         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
520         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
521         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
522
523         ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", "Add Feather Vertex and Slide",
524                                           "Add new vertex to feather and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
525         ot->description = "Add new feather vertex and slide it";
526         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
527         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
528         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
529 }