0290934cf9abef1262b242f251324b8d6e3dd9bd
[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 void ED_mask_size(bContext *C, int *width, int *height)
93 {
94         SpaceClip *sc = CTX_wm_space_clip(C);
95
96         if (sc) {
97                 ED_space_clip_mask_size(sc, width, height);
98         }
99         else {
100                 /* possible other spaces from which mask editing is available */
101                 *width = 0;
102                 *height = 0;
103         }
104 }
105
106 void ED_mask_aspect(bContext *C, float *aspx, float *aspy)
107 {
108         SpaceClip *sc = CTX_wm_space_clip(C);
109
110         if (sc) {
111                 ED_space_clip_mask_aspect(sc, aspx, aspy);
112         }
113         else {
114                 /* possible other spaces from which mask editing is available */
115                 *aspx = 1.0f;
116                 *aspy = 1.0f;
117         }
118 }
119
120 void ED_mask_pixelspace_factor(bContext *C, float *scalex, float *scaley)
121 {
122         SpaceClip *sc = CTX_wm_space_clip(C);
123
124         if (sc) {
125                 ARegion *ar = CTX_wm_region(C);
126                 int width, height;
127                 float zoomx, zoomy, aspx, aspy;
128
129                 ED_space_clip_size(sc, &width, &height);
130                 ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
131                 ED_space_clip_aspect(sc, &aspx, &aspy);
132
133                 *scalex = ((float)width * aspx) * zoomx;
134                 *scaley = ((float)height * aspy) * zoomy;
135         }
136         else {
137                 /* possible other spaces from which mask editing is available */
138                 *scalex = 1.0f;
139                 *scaley = 1.0f;
140         }
141 }
142
143 /********************** registration *********************/
144
145 void ED_operatortypes_mask(void)
146 {
147         WM_operatortype_append(MASK_OT_new);
148
149         /* mask objects */
150         WM_operatortype_append(MASK_OT_object_new);
151         WM_operatortype_append(MASK_OT_object_remove);
152
153         /* geometry */
154         WM_operatortype_append(MASK_OT_add_vertex);
155         WM_operatortype_append(MASK_OT_add_feather_vertex);
156         WM_operatortype_append(MASK_OT_delete);
157
158         /* select */
159         WM_operatortype_append(MASK_OT_select);
160         WM_operatortype_append(MASK_OT_select_all);
161
162         /* shape */
163         WM_operatortype_append(MASK_OT_slide_point);
164         WM_operatortype_append(MASK_OT_cyclic_toggle);
165         WM_operatortype_append(MASK_OT_handle_type_set);
166
167         /* relationships */
168         WM_operatortype_append(MASK_OT_parent_set);
169         WM_operatortype_append(MASK_OT_parent_clear);
170
171         /* shapekeys */
172         WM_operatortype_append(MASK_OT_shape_key_insert);
173         WM_operatortype_append(MASK_OT_shape_key_clear);
174 }
175
176 void ED_keymap_mask(wmKeyConfig *keyconf)
177 {
178         wmKeyMap *keymap;
179         wmKeyMapItem *kmi;
180
181         keymap = WM_keymap_find(keyconf, "Mask Editor", 0, 0);
182         keymap->poll = ED_maskediting_poll;
183
184         WM_keymap_add_item(keymap, "MASK_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
185
186         /* geometry */
187         WM_keymap_add_item(keymap, "MASK_OT_add_vertex_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
188         WM_keymap_add_item(keymap, "MASK_OT_add_feather_vertex_slide", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
189         WM_keymap_add_item(keymap, "MASK_OT_delete", XKEY, KM_PRESS, 0, 0);
190         WM_keymap_add_item(keymap, "MASK_OT_delete", DELKEY, KM_PRESS, 0, 0);
191
192         /* selection */
193         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
194         RNA_boolean_set(kmi->ptr, "extend", FALSE);
195         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
196         RNA_boolean_set(kmi->ptr, "toggle", FALSE);
197         kmi = WM_keymap_add_item(keymap, "MASK_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
198         RNA_boolean_set(kmi->ptr, "extend", FALSE);
199         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
200         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
201
202         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", AKEY, KM_PRESS, 0, 0);
203         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
204         kmi = WM_keymap_add_item(keymap, "MASK_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
205         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
206
207         /* select clip while in maker view,
208          * this matches View3D functionality where you can select an
209          * object while in editmode to allow vertex parenting */
210         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
211         RNA_boolean_set(kmi->ptr, "extend", FALSE);
212
213         /* shape */
214         WM_keymap_add_item(keymap, "MASK_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
215         WM_keymap_add_item(keymap, "MASK_OT_slide_point", LEFTMOUSE, KM_PRESS, 0, 0);
216         WM_keymap_add_item(keymap, "MASK_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
217
218         /* relationships */
219         WM_keymap_add_item(keymap, "MASK_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
220         WM_keymap_add_item(keymap, "MASK_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
221
222         WM_keymap_add_item(keymap, "MASK_OT_shape_key_insert", IKEY, KM_PRESS, 0, 0);
223         WM_keymap_add_item(keymap, "MASK_OT_shape_key_clear", IKEY, KM_PRESS, KM_ALT, 0);
224
225
226         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
227 }
228
229 void ED_operatormacros_mask(void)
230 {
231         /* XXX: just for sample */
232         wmOperatorType *ot;
233         wmOperatorTypeMacro *otmacro;
234
235         ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide", "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER);
236         ot->description = "Add new vertex and slide it";
237         WM_operatortype_macro_define(ot, "MASK_OT_add_vertex");
238         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
239         RNA_boolean_set(otmacro->ptr, "release_confirm", TRUE);
240
241         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);
242         ot->description = "Add new feather vertex and slide it";
243         WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex");
244         otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point");
245         RNA_boolean_set(otmacro->ptr, "slide_feather", TRUE);
246 }