proportional editing keys now work when editing masks.
[blender.git] / source / blender / editors / mask / mask_editor.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_ops.c
29  *  \ingroup edmask
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_math.h"
36
37 #include "BKE_context.h"
38 #include "BKE_mask.h"
39
40 #include "WM_api.h"
41 #include "WM_types.h"
42
43 #include "ED_screen.h"
44 #include "ED_mask.h"
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_maskediting_poll(bContext *C)
56 {
57         SpaceClip *sc = CTX_wm_space_clip(C);
58
59         if (sc) {
60                 return ED_space_clip_maskediting_poll(C);
61         }
62
63         return FALSE;
64 }
65
66 int ED_maskediting_mask_poll(bContext *C)
67 {
68         SpaceClip *sc = CTX_wm_space_clip(C);
69
70         if (sc) {
71                 return ED_space_clip_maskediting_mask_poll(C);
72         }
73
74         return FALSE;
75 }
76
77 /********************** registration *********************/
78
79 void ED_mask_mouse_pos(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(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(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(bContext *C, int *width, int *height)
136 {
137         SpaceClip *sc = CTX_wm_space_clip(C);
138
139         if (sc) {
140                 ED_space_clip_mask_size(sc, width, height);
141         }
142         else {
143                 /* possible other spaces from which mask editing is available */
144                 *width = 0;
145                 *height = 0;
146         }
147 }
148
149 void ED_mask_aspect(bContext *C, float *aspx, float *aspy)
150 {
151         SpaceClip *sc = CTX_wm_space_clip(C);
152
153         if (sc) {
154                 ED_space_clip_mask_aspect(sc, aspx, aspy);
155         }
156         else {
157                 /* possible other spaces from which mask editing is available */
158                 *aspx = 1.0f;
159                 *aspy = 1.0f;
160         }
161 }
162
163 void ED_mask_pixelspace_factor(bContext *C, float *scalex, float *scaley)
164 {
165         SpaceClip *sc = CTX_wm_space_clip(C);
166
167         if (sc) {
168                 ARegion *ar = CTX_wm_region(C);
169                 int width, height;
170                 float zoomx, zoomy, aspx, aspy;
171
172                 ED_space_clip_size(sc, &width, &height);
173                 ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
174                 ED_space_clip_aspect(sc, &aspx, &aspy);
175
176                 *scalex = ((float)width * aspx) * zoomx;
177                 *scaley = ((float)height * aspy) * zoomy;
178         }
179         else {
180                 /* possible other spaces from which mask editing is available */
181                 *scalex = 1.0f;
182                 *scaley = 1.0f;
183         }
184 }
185
186 /********************** registration *********************/
187
188 void ED_operatortypes_mask(void)
189 {
190         WM_operatortype_append(MASK_OT_new);
191
192         /* mask objects */
193         WM_operatortype_append(MASK_OT_object_new);
194         WM_operatortype_append(MASK_OT_object_remove);
195
196         /* geometry */
197         WM_operatortype_append(MASK_OT_add_vertex);
198         WM_operatortype_append(MASK_OT_add_feather_vertex);
199         WM_operatortype_append(MASK_OT_delete);
200
201         /* select */
202         WM_operatortype_append(MASK_OT_select);
203         WM_operatortype_append(MASK_OT_select_all);
204         WM_operatortype_append(MASK_OT_select_border);
205         WM_operatortype_append(MASK_OT_select_lasso);
206         WM_operatortype_append(MASK_OT_select_circle);
207
208         /* hide/reveal */
209         WM_operatortype_append(MASK_OT_hide_view_clear);
210         WM_operatortype_append(MASK_OT_hide_view_set);
211
212         /* shape */
213         WM_operatortype_append(MASK_OT_slide_point);
214         WM_operatortype_append(MASK_OT_cyclic_toggle);
215         WM_operatortype_append(MASK_OT_handle_type_set);
216
217         /* relationships */
218         WM_operatortype_append(MASK_OT_parent_set);
219         WM_operatortype_append(MASK_OT_parent_clear);
220
221         /* shapekeys */
222         WM_operatortype_append(MASK_OT_shape_key_insert);
223         WM_operatortype_append(MASK_OT_shape_key_clear);
224 }
225
226 void ED_keymap_mask(wmKeyConfig *keyconf)
227 {
228         wmKeyMap *keymap;
229         wmKeyMapItem *kmi;
230
231         keymap = WM_keymap_find(keyconf, "Mask Editor", 0, 0);
232         keymap->poll = ED_maskediting_poll;
233
234         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
235
236         /* mask mode supports PET now */
237         ED_keymap_proportional_cycle(keyconf, keymap);
238         ED_keymap_proportional_maskmode(keyconf, keymap);
239
240         /* geometry */
241         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
242         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
243         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
244         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
245
246         /* selection */
247         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
248         RNA_boolean_set(kmi->ptr, "extend", FALSE);
249         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
250         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
251         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
252         RNA_boolean_set(kmi->ptr, "extend", FALSE);
253         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
254         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
255
256         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
257         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
258         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
259         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
260
261         WM_keymap_add_item(keymap, "MASK_OT_select_border", BKEY, KM_PRESS, 0, 0);
262         WM_keymap_add_item(keymap, "MASK_OT_select_circle", CKEY, KM_PRESS, 0, 0);
263
264         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
265         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
266         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
267         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
268
269         /* hide/reveal */
270         WM_keymap_add_item(keymap, "MASK_OT_hide_view_clear", HKEY, KM_PRESS, KM_ALT, 0);
271         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, 0, 0);
272         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
273
274         kmi = WM_keymap_add_item(keymap, "MASK_OT_hide_view_set", HKEY, KM_PRESS, KM_SHIFT, 0);
275         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
276
277         /* select clip while in maker view,
278          * this matches View3D functionality where you can select an
279          * object while in editmode to allow vertex parenting */
280         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
281         RNA_boolean_set(kmi->ptr, "extend", FALSE);
282
283         /* shape */
284         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
285         WM_keymap_add_item(keymap, "MASK_OT_slide_point", LEFTMOUSE, KM_PRESS, 0, 0);
286         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
287
288         /* relationships */
289         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
290         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
291
292         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
293         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
294
295
296         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
297 }
298
299 void ED_operatormacros_mask(void)
300 {
301         /* XXX: just for sample */
302         wmOperatorType *ot;
303         wmOperatorTypeMacro *otmacro;
304
305         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide", "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
306         ot->description = "Add new vertex and slide it";
307         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
308         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
309         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
310
311         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);
312         ot->description = "Add new feather vertex and slide it";
313         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
314         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
315         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
316 }