mask - border & lasso select (lasso uses Ctrl+Alt - as with clip view)
[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_clip.h"
46 #include "ED_transform.h"
47
48 #include "RNA_access.h"
49
50 #include "mask_intern.h"  /* own include */
51
52 /********************** generic poll functions *********************/
53
54 int ED_maskediting_poll(bContext *C)
55 {
56         SpaceClip *sc = CTX_wm_space_clip(C);
57
58         if (sc) {
59                 return ED_space_clip_maskediting_poll(C);
60         }
61
62         return FALSE;
63 }
64
65 int ED_maskediting_mask_poll(bContext *C)
66 {
67         SpaceClip *sc = CTX_wm_space_clip(C);
68
69         if (sc) {
70                 return ED_space_clip_maskediting_mask_poll(C);
71         }
72
73         return FALSE;
74 }
75
76 /********************** registration *********************/
77
78 void ED_mask_mouse_pos(bContext *C, wmEvent *event, float co[2])
79 {
80         SpaceClip *sc = CTX_wm_space_clip(C);
81
82         if (sc) {
83                 ED_clip_mouse_pos(C, event, co);
84                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
85         }
86         else {
87                 /* possible other spaces from which mask editing is available */
88                 zero_v2(co);
89         }
90 }
91
92 /* input:  x/y   - mval space
93  * output: xr/yr - mask point space */
94 void ED_mask_point_pos(bContext *C, float x, float y, float *xr, float *yr)
95 {
96         SpaceClip *sc = CTX_wm_space_clip(C);
97         float co[2];
98
99         if (sc) {
100                 ED_clip_point_stable_pos(C, x, y, &co[0], &co[1]);
101                 BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co);
102         }
103         else {
104                 /* possible other spaces from which mask editing is available */
105                 zero_v2(co);
106         }
107
108         *xr = co[0];
109         *yr = co[1];
110 }
111
112 void ED_mask_point_pos__reverse(bContext *C, float x, float y, float *xr, float *yr)
113 {
114         SpaceClip *sc = CTX_wm_space_clip(C);
115         ARegion *ar = CTX_wm_region(C);
116
117         float co[2];
118
119         if (sc && ar) {
120                 co[0] = x;
121                 co[1] = y;
122                 BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co);
123                 ED_clip_point_stable_pos__reverse(sc, ar, co, co);
124         }
125         else {
126                 /* possible other spaces from which mask editing is available */
127                 zero_v2(co);
128         }
129
130         *xr = co[0];
131         *yr = co[1];
132 }
133
134 void ED_mask_size(bContext *C, int *width, int *height)
135 {
136         SpaceClip *sc = CTX_wm_space_clip(C);
137
138         if (sc) {
139                 ED_space_clip_mask_size(sc, width, height);
140         }
141         else {
142                 /* possible other spaces from which mask editing is available */
143                 *width = 0;
144                 *height = 0;
145         }
146 }
147
148 void ED_mask_aspect(bContext *C, float *aspx, float *aspy)
149 {
150         SpaceClip *sc = CTX_wm_space_clip(C);
151
152         if (sc) {
153                 ED_space_clip_mask_aspect(sc, aspx, aspy);
154         }
155         else {
156                 /* possible other spaces from which mask editing is available */
157                 *aspx = 1.0f;
158                 *aspy = 1.0f;
159         }
160 }
161
162 void ED_mask_pixelspace_factor(bContext *C, float *scalex, float *scaley)
163 {
164         SpaceClip *sc = CTX_wm_space_clip(C);
165
166         if (sc) {
167                 ARegion *ar = CTX_wm_region(C);
168                 int width, height;
169                 float zoomx, zoomy, aspx, aspy;
170
171                 ED_space_clip_size(sc, &width, &height);
172                 ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
173                 ED_space_clip_aspect(sc, &aspx, &aspy);
174
175                 *scalex = ((float)width * aspx) * zoomx;
176                 *scaley = ((float)height * aspy) * zoomy;
177         }
178         else {
179                 /* possible other spaces from which mask editing is available */
180                 *scalex = 1.0f;
181                 *scaley = 1.0f;
182         }
183 }
184
185 /********************** registration *********************/
186
187 void ED_operatortypes_mask(void)
188 {
189         WM_operatortype_append(MASK_OT_new);
190
191         /* mask objects */
192         WM_operatortype_append(MASK_OT_object_new);
193         WM_operatortype_append(MASK_OT_object_remove);
194
195         /* geometry */
196         WM_operatortype_append(MASK_OT_add_vertex);
197         WM_operatortype_append(MASK_OT_add_feather_vertex);
198         WM_operatortype_append(MASK_OT_delete);
199
200         /* select */
201         WM_operatortype_append(MASK_OT_select);
202         WM_operatortype_append(MASK_OT_select_all);
203         WM_operatortype_append(MASK_OT_select_border);
204         WM_operatortype_append(MASK_OT_select_lasso);
205
206         /* shape */
207         WM_operatortype_append(MASK_OT_slide_point);
208         WM_operatortype_append(MASK_OT_cyclic_toggle);
209         WM_operatortype_append(MASK_OT_handle_type_set);
210
211         /* relationships */
212         WM_operatortype_append(MASK_OT_parent_set);
213         WM_operatortype_append(MASK_OT_parent_clear);
214
215         /* shapekeys */
216         WM_operatortype_append(MASK_OT_shape_key_insert);
217         WM_operatortype_append(MASK_OT_shape_key_clear);
218 }
219
220 void ED_keymap_mask(wmKeyConfig *keyconf)
221 {
222         wmKeyMap *keymap;
223         wmKeyMapItem *kmi;
224
225         keymap = WM_keymap_find(keyconf, "Mask Editor", 0, 0);
226         keymap->poll = ED_maskediting_poll;
227
228         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
229
230         /* geometry */
231         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
232         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
233         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
234         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
235
236         /* selection */
237         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
238         RNA_boolean_set(kmi->ptr, "extend", FALSE);
239         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
240         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
241         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
242         RNA_boolean_set(kmi->ptr, "extend", FALSE);
243         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
244         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
245
246         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
247         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
248         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
249         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
250
251         WM_keymap_add_item(keymap, "MASK_OT_select_border", BKEY, KM_PRESS, 0, 0);
252
253         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
254         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
255         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
256         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
257
258         /* select clip while in maker view,
259          * this matches View3D functionality where you can select an
260          * object while in editmode to allow vertex parenting */
261         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
262         RNA_boolean_set(kmi->ptr, "extend", FALSE);
263
264         /* shape */
265         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
266         WM_keymap_add_item(keymap, "MASK_OT_slide_point", LEFTMOUSE, KM_PRESS, 0, 0);
267         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
268
269         /* relationships */
270         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
271         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
272
273         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
274         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
275
276
277         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
278 }
279
280 void ED_operatormacros_mask(void)
281 {
282         /* XXX: just for sample */
283         wmOperatorType *ot;
284         wmOperatorTypeMacro *otmacro;
285
286         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide", "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
287         ot->description = "Add new vertex and slide it";
288         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
289         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
290         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
291
292         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);
293         ot->description = "Add new feather vertex and slide it";
294         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
295         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
296         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
297 }