Lasso Select for GPencil Strokes
[blender.git] / source / blender / editors / gpencil / gpencil_ops.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) 2009, Blender Foundation, Joshua Leung
19  * This is a new part of Blender
20  *
21  * Contributor(s): Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/gpencil/gpencil_ops.c
27  *  \ingroup edgpencil
28  */
29
30
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <stdio.h>
34
35 #include "BLI_sys_types.h"
36
37 #include "BLI_blenlib.h"
38
39 #include "BKE_context.h"
40
41 #include "DNA_gpencil_types.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "RNA_access.h"
47
48 #include "ED_gpencil.h"
49 #include "ED_object.h"
50 #include "ED_transform.h"
51
52 #include "gpencil_intern.h"
53
54 /* ****************************************** */
55 /* Grease Pencil Keymaps */
56
57 /* Generic Drawing Keymap */
58 static void ed_keymap_gpencil_general(wmKeyConfig *keyconf)
59 {
60         wmKeyMap *keymap = WM_keymap_find(keyconf, "Grease Pencil", 0, 0);
61         wmKeyMapItem *kmi;
62         
63         /* Draw  --------------------------------------- */
64         /* draw */
65         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", LEFTMOUSE, KM_PRESS, 0, DKEY);
66         RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW);
67         RNA_boolean_set(kmi->ptr, "wait_for_input", false);
68         
69         /* draw - straight lines */
70         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", LEFTMOUSE, KM_PRESS, KM_CTRL, DKEY);
71         RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW_STRAIGHT);
72         RNA_boolean_set(kmi->ptr, "wait_for_input", false);
73         
74         /* draw - poly lines */
75         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", LEFTMOUSE, KM_PRESS, KM_ALT, DKEY);
76         RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW_POLY);
77         RNA_boolean_set(kmi->ptr, "wait_for_input", false);
78         
79         /* erase */
80         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", RIGHTMOUSE, KM_PRESS, 0, DKEY);
81         RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_ERASER);
82         RNA_boolean_set(kmi->ptr, "wait_for_input", false);
83         
84         /* Viewport Tools ------------------------------- */
85         
86         /* Enter EditMode */
87         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", TABKEY, KM_PRESS, 0, DKEY);
88         RNA_string_set(kmi->ptr, "data_path", "gpencil_data.use_stroke_edit_mode");
89         
90         /* Pie Menu - For standard tools */
91         WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_tool_palette", QKEY, KM_PRESS, 0, DKEY);
92         WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_settings_palette", WKEY, KM_PRESS, 0, DKEY);
93 }
94
95 /* ==================== */
96
97 /* Poll callback for stroke editing mode */
98 static int gp_stroke_editmode_poll(bContext *C)
99 {
100         bGPdata *gpd = CTX_data_gpencil_data(C);
101         return (gpd && (gpd->flag & GP_DATA_STROKE_EDITMODE));
102 }
103
104 /* Stroke Editing Keymap - Only when editmode is enabled */
105 static void ed_keymap_gpencil_editing(wmKeyConfig *keyconf)
106 {
107         wmKeyMap *keymap = WM_keymap_find(keyconf, "Grease Pencil Stroke Edit Mode", 0, 0);
108         wmKeyMapItem *kmi;
109         
110         /* set poll callback - so that this keymap only gets enabled when stroke editmode is enabled */
111         keymap->poll = gp_stroke_editmode_poll;
112         
113         /* Selection ------------------------------------- */
114         /* select all */
115         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_all", AKEY, KM_PRESS, 0, 0);
116         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
117         
118         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
119         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
120         
121         /* circle select */
122         WM_keymap_add_item(keymap, "GPENCIL_OT_select_circle", CKEY, KM_PRESS, 0, 0);
123         
124         /* border select */
125         WM_keymap_add_item(keymap, "GPENCIL_OT_select_border", BKEY, KM_PRESS, 0, 0);
126         
127         /* lasso select */
128         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
129         RNA_boolean_set(kmi->ptr, "deselect", false);
130         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0);
131         RNA_boolean_set(kmi->ptr, "deselect", true);
132         
133         /* normal select */
134         WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
135         
136         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
137         RNA_boolean_set(kmi->ptr, "extend", true);
138         RNA_boolean_set(kmi->ptr, "toggle", true);
139         
140         /* whole stroke select */
141         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
142         RNA_boolean_set(kmi->ptr, "entire_strokes", true);
143         
144         /* select linked */
145         WM_keymap_add_item(keymap, "GPENCIL_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
146         
147         /* select more/less */
148         WM_keymap_add_item(keymap, "GPENCIL_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
149         WM_keymap_add_item(keymap, "GPENCIL_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
150         
151         
152         /* Editing ----------------------------------------- */
153         
154         /* duplicate and move selected points */
155         WM_keymap_add_item(keymap, "GPENCIL_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
156         
157         /* delete */
158         WM_keymap_add_item(keymap, "GPENCIL_OT_delete", XKEY, KM_PRESS, 0, 0);
159         
160         
161         /* Transform Tools */
162         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
163         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
164         
165         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
166         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
167         
168         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
169         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
170         
171         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_mirror", MKEY, KM_PRESS, KM_CTRL, 0);
172         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
173         
174         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_bend", WKEY, KM_PRESS, KM_SHIFT, 0);
175         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
176         
177         WM_keymap_add_item(keymap, "TRANSFORM_OT_tosphere", SKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
178         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
179         
180         WM_keymap_add_item(keymap, "TRANSFORM_OT_shear", SKEY, KM_PRESS, KM_ALT | KM_CTRL | KM_SHIFT, 0);
181         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
182         
183         /* Proportional Editing */
184         ED_keymap_proportional_cycle(keyconf, keymap);
185         ED_keymap_proportional_editmode(keyconf, keymap, true);
186 }
187
188 /* ==================== */
189
190 void ED_keymap_gpencil(wmKeyConfig *keyconf)
191 {
192         ed_keymap_gpencil_general(keyconf);
193         ed_keymap_gpencil_editing(keyconf);
194 }
195
196 /* ****************************************** */
197
198 void ED_operatortypes_gpencil(void)
199 {
200         /* Drawing ----------------------- */
201         
202         WM_operatortype_append(GPENCIL_OT_draw);
203         
204         /* Editing (Strokes) ------------ */
205         
206         WM_operatortype_append(GPENCIL_OT_select);
207         WM_operatortype_append(GPENCIL_OT_select_all);
208         WM_operatortype_append(GPENCIL_OT_select_circle);
209         WM_operatortype_append(GPENCIL_OT_select_border);
210         WM_operatortype_append(GPENCIL_OT_select_lasso);
211         
212         WM_operatortype_append(GPENCIL_OT_select_linked);
213         WM_operatortype_append(GPENCIL_OT_select_more);
214         WM_operatortype_append(GPENCIL_OT_select_less);
215         
216         WM_operatortype_append(GPENCIL_OT_duplicate);
217         WM_operatortype_append(GPENCIL_OT_delete);
218         
219         /* Editing (Buttons) ------------ */
220         
221         WM_operatortype_append(GPENCIL_OT_data_add);
222         WM_operatortype_append(GPENCIL_OT_data_unlink);
223         
224         WM_operatortype_append(GPENCIL_OT_layer_add);
225         WM_operatortype_append(GPENCIL_OT_layer_remove);
226         WM_operatortype_append(GPENCIL_OT_layer_move);
227         
228         WM_operatortype_append(GPENCIL_OT_active_frame_delete);
229         
230         WM_operatortype_append(GPENCIL_OT_convert);
231         
232         /* Editing (Time) --------------- */
233 }
234
235 void ED_operatormacros_gpencil(void)
236 {
237         wmOperatorType *ot;
238         wmOperatorTypeMacro *otmacro;
239
240         ot = WM_operatortype_append_macro("GPENCIL_OT_duplicate_move", "Duplicate Strokes",
241                                           "Make copies of the selected Grease Pencil strokes and move them",
242                                           OPTYPE_UNDO | OPTYPE_REGISTER);
243         WM_operatortype_macro_define(ot, "GPENCIL_OT_duplicate");
244         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
245         RNA_boolean_set(otmacro->ptr, "gpencil_strokes", true);
246 }
247
248 /* ****************************************** */