Proxies building progress bar used to be displayed wrong in cases
[blender.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 "RNA_access.h"
52
53 #include "mask_intern.h"  /* own include */
54
55 /********************** generic poll functions *********************/
56
57 int ED_maskedit_poll(bContext *C)
58 {
59         ScrArea *sa = CTX_wm_area(C);
60         if (sa) {
61                 switch (sa->spacetype) {
62                         case SPACE_CLIP:
63                                 return ED_space_clip_maskedit_poll(C);
64                         case SPACE_SEQ:
65                                 return ED_space_sequencer_maskedit_poll(C);
66                         case SPACE_IMAGE:
67                                 return ED_space_image_maskedit_poll(C);
68                 }
69         }
70         return FALSE;
71 }
72
73 int ED_maskedit_mask_poll(bContext *C)
74 {
75         ScrArea *sa = CTX_wm_area(C);
76         if (sa) {
77                 switch (sa->spacetype) {
78                         case SPACE_CLIP:
79                                 return ED_space_clip_maskedit_mask_poll(C);
80                         case SPACE_SEQ:
81                                 return ED_space_sequencer_maskedit_mask_poll(C);
82                         case SPACE_IMAGE:
83                                 return ED_space_image_maskedit_mask_poll(C);
84                 }
85         }
86         return FALSE;
87 }
88
89 /********************** registration *********************/
90
91 void ED_mask_mouse_pos(const bContext *C, wmEvent *event, float co[2])
92 {
93         ScrArea *sa = CTX_wm_area(C);
94         if (sa) {
95                 switch (sa->spacetype) {
96                         case SPACE_CLIP:
97                         {
98                                 SpaceClip *sc = sa->spacedata.first;
99                                 ED_clip_mouse_pos(C, event, co);
100                                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
101                                 break;
102                         }
103                         case SPACE_SEQ:
104                                 zero_v2(co); /* MASKTODO */
105                                 break;
106                         case SPACE_IMAGE:
107                                 zero_v2(co); /* MASKTODO */
108                                 break;
109                         default:
110                                 /* possible other spaces from which mask editing is available */
111                                 BLI_assert(0);
112                                 zero_v2(co);
113                                 break;
114                 }
115         }
116         else {
117                 BLI_assert(0);
118                 zero_v2(co);
119         }
120 }
121
122 /* input:  x/y   - mval space
123  * output: xr/yr - mask point space */
124 void ED_mask_point_pos(const bContext *C, float x, float y, float *xr, float *yr)
125 {
126         ScrArea *sa = CTX_wm_area(C);
127         float co[2];
128
129         if (sa) {
130                 switch (sa->spacetype) {
131                         case SPACE_CLIP:
132                         {
133                                 SpaceClip *sc = sa->spacedata.first;
134                                 ED_clip_point_stable_pos(C, x, y, &co[0], &co[1]);
135                                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
136                                 break;
137                         }
138                         case SPACE_SEQ:
139                                 zero_v2(co); /* MASKTODO */
140                                 break;
141                         case SPACE_IMAGE:
142                                 zero_v2(co); /* MASKTODO */
143                                 break;
144                         default:
145                                 /* possible other spaces from which mask editing is available */
146                                 BLI_assert(0);
147                                 zero_v2(co);
148                                 break;
149                 }
150         }
151         else {
152                 BLI_assert(0);
153                 zero_v2(co);
154         }
155
156         *xr = co[0];
157         *yr = co[1];
158 }
159
160 void ED_mask_point_pos__reverse(const bContext *C, float x, float y, float *xr, float *yr)
161 {
162         ScrArea *sa = CTX_wm_area(C);
163         float co[2];
164
165         if (sa) {
166                 switch (sa->spacetype) {
167                         case SPACE_CLIP:
168                         {
169                                 SpaceClip *sc = sa->spacedata.first;
170                                 co[0] = x;
171                                 co[1] = y;
172                                 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co);
173                                 ED_clip_point_stable_pos__reverse(C, co, co);
174                                 break;
175                         }
176                         case SPACE_SEQ:
177                                 zero_v2(co); /* MASKTODO */
178                                 break;
179                         case SPACE_IMAGE:
180                                 zero_v2(co); /* MASKTODO */
181                                 break;
182                         default:
183                                 /* possible other spaces from which mask editing is available */
184                                 BLI_assert(0);
185                                 zero_v2(co);
186                                 break;
187                 }
188         }
189         else {
190                 BLI_assert(0);
191                 zero_v2(co);
192         }
193
194         *xr = co[0];
195         *yr = co[1];
196 }
197
198 void ED_mask_size(const bContext *C, int *width, int *height)
199 {
200         ScrArea *sa = CTX_wm_area(C);
201         if (sa && sa->spacedata.first) {
202                 switch (sa->spacetype) {
203                         case SPACE_CLIP:
204                         {
205                                 ED_space_clip_get_size(C, width, height);
206                                 break;
207                         }
208                         case SPACE_SEQ:
209                         {
210                                 Scene *scene = CTX_data_scene(C);
211                                 *width = (scene->r.size * scene->r.xsch) / 100;
212                                 *height = (scene->r.size * scene->r.ysch) / 100;
213                                 break;
214                         }
215                         case SPACE_IMAGE:
216                         {
217                                 SpaceImage *sima = sa->spacedata.first;
218                                 ED_space_image_size(sima, width, height);
219                                 break;
220                         }
221                         default:
222                                 /* possible other spaces from which mask editing is available */
223                                 BLI_assert(0);
224                                 *width = 0;
225                                 *height = 0;
226                                 break;
227                 }
228         }
229         else {
230                 BLI_assert(0);
231                 *width = 0;
232                 *height = 0;
233         }
234 }
235
236 void ED_mask_aspect(const bContext *C, float *aspx, float *aspy)
237 {
238         ScrArea *sa = CTX_wm_area(C);
239         if (sa && sa->spacedata.first) {
240                 switch (sa->spacetype) {
241                         case SPACE_CLIP:
242                         {
243                                 SpaceClip *sc = sa->spacedata.first;
244                                 ED_space_clip_get_aspect(sc, aspx, aspy);
245                                 break;
246                         }
247                         case SPACE_SEQ:
248                         {
249                                 *aspx = *aspy = 1.0f;  /* MASKTODO - render aspect? */
250                                 break;
251                         }
252                         case SPACE_IMAGE:
253                         {
254                                 // SpaceImage *sima = sa->spacedata.first;
255                                 *aspx = *aspy = 1.0f;  /* MASKTODO - image aspect? */
256                                 break;
257                         }
258                         default:
259                                 /* possible other spaces from which mask editing is available */
260                                 BLI_assert(0);
261                                 *aspx = *aspy = 1.0f;
262                                 break;
263                 }
264         }
265         else {
266                 BLI_assert(0);
267                 *aspx = *aspy = 1.0f;
268         }
269 }
270
271 void ED_mask_pixelspace_factor(const bContext *C, float *scalex, float *scaley)
272 {
273         SpaceClip *sc = CTX_wm_space_clip(C);
274
275         /* MASKTODO */
276
277         if (sc) {
278                 int width, height;
279                 float zoomx, zoomy, aspx, aspy;
280
281                 ED_space_clip_get_size(C, &width, &height);
282                 ED_space_clip_get_zoom(C, &zoomx, &zoomy);
283                 ED_space_clip_get_aspect(sc, &aspx, &aspy);
284
285                 *scalex = ((float)width * aspx) * zoomx;
286                 *scaley = ((float)height * aspy) * zoomy;
287         }
288         else {
289                 /* possible other spaces from which mask editing is available */
290                 *scalex = 1.0f;
291                 *scaley = 1.0f;
292         }
293 }
294
295 /********************** registration *********************/
296
297 void ED_operatortypes_mask(void)
298 {
299         WM_operatortype_append(MASK_OT_new);
300
301         /* mask layers */
302         WM_operatortype_append(MASK_OT_layer_new);
303         WM_operatortype_append(MASK_OT_layer_remove);
304
305         /* add */
306         WM_operatortype_append(MASK_OT_add_vertex);
307         WM_operatortype_append(MASK_OT_add_feather_vertex);
308
309         /* geometry */
310         WM_operatortype_append(MASK_OT_switch_direction);
311         WM_operatortype_append(MASK_OT_normals_make_consistent);
312         WM_operatortype_append(MASK_OT_delete);
313
314         /* select */
315         WM_operatortype_append(MASK_OT_select);
316         WM_operatortype_append(MASK_OT_select_all);
317         WM_operatortype_append(MASK_OT_select_border);
318         WM_operatortype_append(MASK_OT_select_lasso);
319         WM_operatortype_append(MASK_OT_select_circle);
320         WM_operatortype_append(MASK_OT_select_linked_pick);
321         WM_operatortype_append(MASK_OT_select_linked);
322
323         /* hide/reveal */
324         WM_operatortype_append(MASK_OT_hide_view_clear);
325         WM_operatortype_append(MASK_OT_hide_view_set);
326
327         /* feather */
328         WM_operatortype_append(MASK_OT_feather_weight_clear);
329
330         /* shape */
331         WM_operatortype_append(MASK_OT_slide_point);
332         WM_operatortype_append(MASK_OT_cyclic_toggle);
333         WM_operatortype_append(MASK_OT_handle_type_set);
334
335         /* relationships */
336         WM_operatortype_append(MASK_OT_parent_set);
337         WM_operatortype_append(MASK_OT_parent_clear);
338
339         /* shapekeys */
340         WM_operatortype_append(MASK_OT_shape_key_insert);
341         WM_operatortype_append(MASK_OT_shape_key_clear);
342         WM_operatortype_append(MASK_OT_shape_key_feather_reset);
343         WM_operatortype_append(MASK_OT_shape_key_rekey);
344
345         /* layers */
346         WM_operatortype_append(MASK_OT_layer_move);
347 }
348
349 void ED_keymap_mask(wmKeyConfig *keyconf)
350 {
351         wmKeyMap *keymap;
352         wmKeyMapItem *kmi;
353
354         keymap = WM_keymap_find(keyconf, "Mask Editing", 0, 0);
355         keymap->poll = ED_maskedit_poll;
356
357         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
358
359         /* mask mode supports PET now */
360         ED_keymap_proportional_cycle(keyconf, keymap);
361         ED_keymap_proportional_maskmode(keyconf, keymap);
362
363         /* geometry */
364         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
365         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
366         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
367         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
368
369         /* selection */
370         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
371         RNA_boolean_set(kmi->ptr, "extend", FALSE);
372         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
373         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
374         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
375         RNA_boolean_set(kmi->ptr, "extend", FALSE);
376         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
377         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
378
379         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
380         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
381         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
382         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
383
384         WM_keymap_add_item(keymap, "MASK_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
385         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
386         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
387         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
388         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
389
390         WM_keymap_add_item(keymap, "MASK_OT_select_border", BKEY, KM_PRESS, 0, 0);
391         WM_keymap_add_item(keymap, "MASK_OT_select_circle", CKEY, KM_PRESS, 0, 0);
392
393         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
394         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
395         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
396         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
397
398         /* hide/reveal */
399         WM_keymap_add_item(keymap, "MASK_OT_hide_view_clear", HKEY, KM_PRESS, KM_ALT, 0);
400         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, 0, 0);
401         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
402
403         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, KM_SHIFT, 0);
404         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
405
406         /* select clip while in maker view,
407          * this matches View3D functionality where you can select an
408          * object while in editmode to allow vertex parenting */
409         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
410         RNA_boolean_set(kmi->ptr, "extend", FALSE);
411
412         /* shape */
413         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
414         WM_keymap_add_item(keymap, "MASK_OT_slide_point", LEFTMOUSE, KM_PRESS, 0, 0);
415         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
416         WM_keymap_add_item(keymap, "MASK_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
417         // WM_keymap_add_item(keymap, "MASK_OT_feather_weight_clear", SKEY, KM_PRESS, KM_ALT, 0);
418         /* ... matches curve editmode */
419         RNA_enum_set(WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0)->ptr,
420                      "mode", TFM_MASK_SHRINKFATTEN);
421
422         /* relationships */
423         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
424         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
425
426         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
427         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
428
429
430         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
431 }
432
433 void ED_operatormacros_mask(void)
434 {
435         /* XXX: just for sample */
436         wmOperatorType *ot;
437         wmOperatorTypeMacro *otmacro;
438
439         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide",
440                                           "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
441         ot->description = "Add new vertex and slide it";
442         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
443         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
444         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
445
446         ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", "Add Feather Vertex and Slide",
447                                           "Add new vertex to feather and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
448         ot->description = "Add new feather vertex and slide it";
449         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
450         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
451         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
452 }