Changes in clip editor's public api to make it's more clear
[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_object.h" /* ED_keymap_proportional_maskmode only */
46 #include "ED_clip.h"
47 #include "ED_transform.h"
48
49 #include "RNA_access.h"
50
51 #include "mask_intern.h"  /* own include */
52
53 /********************** generic poll functions *********************/
54
55 int ED_maskedit_poll(bContext *C)
56 {
57         SpaceClip *sc = CTX_wm_space_clip(C);
58
59         if (sc) {
60                 return ED_space_clip_maskedit_poll(C);
61         }
62
63         return FALSE;
64 }
65
66 int ED_maskedit_mask_poll(bContext *C)
67 {
68         SpaceClip *sc = CTX_wm_space_clip(C);
69
70         if (sc) {
71                 return ED_space_clip_maskedit_mask_poll(C);
72         }
73
74         return FALSE;
75 }
76
77 /********************** registration *********************/
78
79 void ED_mask_mouse_pos(const bContext *C, wmEvent *event, float co[2])
80 {
81         SpaceClip *sc = CTX_wm_space_clip(C);
82
83         if (sc) {
84                 ED_clip_mouse_pos(C, event, co);
85                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
86         }
87         else {
88                 /* possible other spaces from which mask editing is available */
89                 zero_v2(co);
90         }
91 }
92
93 /* input:  x/y   - mval space
94  * output: xr/yr - mask point space */
95 void ED_mask_point_pos(const bContext *C, float x, float y, float *xr, float *yr)
96 {
97         SpaceClip *sc = CTX_wm_space_clip(C);
98         float co[2];
99
100         if (sc) {
101                 ED_clip_point_stable_pos(C, x, y, &co[0], &co[1]);
102                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
103         }
104         else {
105                 /* possible other spaces from which mask editing is available */
106                 zero_v2(co);
107         }
108
109         *xr = co[0];
110         *yr = co[1];
111 }
112
113 void ED_mask_point_pos__reverse(const bContext *C, float x, float y, float *xr, float *yr)
114 {
115         SpaceClip *sc = CTX_wm_space_clip(C);
116         ARegion *ar = CTX_wm_region(C);
117
118         float co[2];
119
120         if (sc && ar) {
121                 co[0] = x;
122                 co[1] = y;
123                 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co);
124                 ED_clip_point_stable_pos__reverse(sc, ar, co, co);
125         }
126         else {
127                 /* possible other spaces from which mask editing is available */
128                 zero_v2(co);
129         }
130
131         *xr = co[0];
132         *yr = co[1];
133 }
134
135 void ED_mask_size(const bContext *C, int *width, int *height)
136 {
137         ScrArea *sa = CTX_wm_area(C);
138         if (sa && sa->spacedata.first) {
139                 if (sa->spacetype == SPACE_CLIP) {
140                         SpaceClip *sc = sa->spacedata.first;
141                         ED_space_clip_get_mask_size(sc, width, height);
142                         return;
143                 }
144                 else if (sa->spacetype == SPACE_SEQ) {
145                         Scene *scene = CTX_data_scene(C);
146                         *width = (scene->r.size * scene->r.xsch) / 100;
147                         *height = (scene->r.size * scene->r.ysch) / 100;
148                         return;
149                 }
150         }
151
152         /* possible other spaces from which mask editing is available */
153         *width = 0;
154         *height = 0;
155 }
156
157 void ED_mask_aspect(const bContext *C, float *aspx, float *aspy)
158 {
159         SpaceClip *sc = CTX_wm_space_clip(C);
160
161         if (sc) {
162                 ED_space_clip_get_mask_aspect(sc, aspx, aspy);
163         }
164         else {
165                 /* possible other spaces from which mask editing is available */
166                 *aspx = 1.0f;
167                 *aspy = 1.0f;
168         }
169 }
170
171 void ED_mask_pixelspace_factor(const bContext *C, float *scalex, float *scaley)
172 {
173         SpaceClip *sc = CTX_wm_space_clip(C);
174
175         if (sc) {
176                 ARegion *ar = CTX_wm_region(C);
177                 int width, height;
178                 float zoomx, zoomy, aspx, aspy;
179
180                 ED_space_clip_get_clip_size(sc, &width, &height);
181                 ED_space_clip_get_zoom(sc, ar, &zoomx, &zoomy);
182                 ED_space_clip_get_clip_aspect(sc, &aspx, &aspy);
183
184                 *scalex = ((float)width * aspx) * zoomx;
185                 *scaley = ((float)height * aspy) * zoomy;
186         }
187         else {
188                 /* possible other spaces from which mask editing is available */
189                 *scalex = 1.0f;
190                 *scaley = 1.0f;
191         }
192 }
193
194 /********************** registration *********************/
195
196 void ED_operatortypes_mask(void)
197 {
198         WM_operatortype_append(MASK_OT_new);
199
200         /* mask layers */
201         WM_operatortype_append(MASK_OT_layer_new);
202         WM_operatortype_append(MASK_OT_layer_remove);
203
204         /* add */
205         WM_operatortype_append(MASK_OT_add_vertex);
206         WM_operatortype_append(MASK_OT_add_feather_vertex);
207
208         /* geometry */
209         WM_operatortype_append(MASK_OT_switch_direction);
210         WM_operatortype_append(MASK_OT_normals_make_consistent);
211         WM_operatortype_append(MASK_OT_delete);
212
213         /* select */
214         WM_operatortype_append(MASK_OT_select);
215         WM_operatortype_append(MASK_OT_select_all);
216         WM_operatortype_append(MASK_OT_select_border);
217         WM_operatortype_append(MASK_OT_select_lasso);
218         WM_operatortype_append(MASK_OT_select_circle);
219         WM_operatortype_append(MASK_OT_select_linked_pick);
220         WM_operatortype_append(MASK_OT_select_linked);
221
222         /* hide/reveal */
223         WM_operatortype_append(MASK_OT_hide_view_clear);
224         WM_operatortype_append(MASK_OT_hide_view_set);
225
226         /* feather */
227         WM_operatortype_append(MASK_OT_feather_weight_clear);
228
229         /* shape */
230         WM_operatortype_append(MASK_OT_slide_point);
231         WM_operatortype_append(MASK_OT_cyclic_toggle);
232         WM_operatortype_append(MASK_OT_handle_type_set);
233
234         /* relationships */
235         WM_operatortype_append(MASK_OT_parent_set);
236         WM_operatortype_append(MASK_OT_parent_clear);
237
238         /* shapekeys */
239         WM_operatortype_append(MASK_OT_shape_key_insert);
240         WM_operatortype_append(MASK_OT_shape_key_clear);
241         WM_operatortype_append(MASK_OT_shape_key_feather_reset);
242 }
243
244 void ED_keymap_mask(wmKeyConfig *keyconf)
245 {
246         wmKeyMap *keymap;
247         wmKeyMapItem *kmi;
248
249         keymap = WM_keymap_find(keyconf, "Mask Editing", 0, 0);
250         keymap->poll = ED_maskedit_poll;
251
252         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
253
254         /* mask mode supports PET now */
255         ED_keymap_proportional_cycle(keyconf, keymap);
256         ED_keymap_proportional_maskmode(keyconf, keymap);
257
258         /* geometry */
259         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
260         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
261         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
262         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
263
264         /* selection */
265         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
266         RNA_boolean_set(kmi->ptr, "extend", FALSE);
267         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
268         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
269         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
270         RNA_boolean_set(kmi->ptr, "extend", FALSE);
271         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
272         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
273
274         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
275         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
276         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
277         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
278
279         WM_keymap_add_item(keymap, "MASK_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
280         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
281         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
282         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
283         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
284
285         WM_keymap_add_item(keymap, "MASK_OT_select_border", BKEY, KM_PRESS, 0, 0);
286         WM_keymap_add_item(keymap, "MASK_OT_select_circle", CKEY, KM_PRESS, 0, 0);
287
288         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
289         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
290         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
291         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
292
293         /* hide/reveal */
294         WM_keymap_add_item(keymap, "MASK_OT_hide_view_clear", HKEY, KM_PRESS, KM_ALT, 0);
295         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, 0, 0);
296         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
297
298         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, KM_SHIFT, 0);
299         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
300
301         /* select clip while in maker view,
302          * this matches View3D functionality where you can select an
303          * object while in editmode to allow vertex parenting */
304         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
305         RNA_boolean_set(kmi->ptr, "extend", FALSE);
306
307         /* shape */
308         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
309         WM_keymap_add_item(keymap, "MASK_OT_slide_point", LEFTMOUSE, KM_PRESS, 0, 0);
310         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
311         WM_keymap_add_item(keymap, "MASK_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
312         // WM_keymap_add_item(keymap, "MASK_OT_feather_weight_clear", SKEY, KM_PRESS, KM_ALT, 0);
313         /* ... matches curve editmode */
314         RNA_enum_set(WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0)->ptr, "mode", TFM_MASK_SHRINKFATTEN);
315
316         /* relationships */
317         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
318         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
319
320         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
321         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
322
323
324         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
325 }
326
327 void ED_operatormacros_mask(void)
328 {
329         /* XXX: just for sample */
330         wmOperatorType *ot;
331         wmOperatorTypeMacro *otmacro;
332
333         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide", "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
334         ot->description = "Add new vertex and slide it";
335         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
336         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
337         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
338
339         ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", "Add Feather Vertex and Slide", "Add new vertex to feater and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
340         ot->description = "Add new feather vertex and slide it";
341         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
342         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
343         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
344 }