e46f71b8f537b3abc941da48442a4428436f0c0f
[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         /* normal select */
128         WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
129         
130         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
131         RNA_boolean_set(kmi->ptr, "extend", true);
132         RNA_boolean_set(kmi->ptr, "toggle", true);
133         
134         /* whole stroke select */
135         kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
136         RNA_boolean_set(kmi->ptr, "entire_strokes", true);
137         
138         /* select linked */
139         WM_keymap_add_item(keymap, "GPENCIL_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
140         
141         /* select more/less */
142         WM_keymap_add_item(keymap, "GPENCIL_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
143         WM_keymap_add_item(keymap, "GPENCIL_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
144         
145         
146         /* Editing ----------------------------------------- */
147         
148         /* duplicate and move selected points */
149         WM_keymap_add_item(keymap, "GPENCIL_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
150         
151         /* delete */
152         WM_keymap_add_item(keymap, "GPENCIL_OT_delete", XKEY, KM_PRESS, 0, 0);
153         
154         
155         /* Transform Tools */
156         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0);
157         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
158         
159         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0);
160         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
161         
162         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0);
163         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
164         
165         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_mirror", MKEY, KM_PRESS, KM_CTRL, 0);
166         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
167         
168         kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_bend", WKEY, KM_PRESS, KM_SHIFT, 0);
169         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
170         
171         WM_keymap_add_item(keymap, "TRANSFORM_OT_tosphere", SKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
172         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
173         
174         WM_keymap_add_item(keymap, "TRANSFORM_OT_shear", SKEY, KM_PRESS, KM_ALT | KM_CTRL | KM_SHIFT, 0);
175         RNA_boolean_set(kmi->ptr, "gpencil_strokes", true);
176         
177         /* Proportional Editing */
178         ED_keymap_proportional_cycle(keyconf, keymap);
179         ED_keymap_proportional_editmode(keyconf, keymap, true);
180 }
181
182 /* ==================== */
183
184 void ED_keymap_gpencil(wmKeyConfig *keyconf)
185 {
186         ed_keymap_gpencil_general(keyconf);
187         ed_keymap_gpencil_editing(keyconf);
188 }
189
190 /* ****************************************** */
191
192 void ED_operatortypes_gpencil(void)
193 {
194         /* Drawing ----------------------- */
195         
196         WM_operatortype_append(GPENCIL_OT_draw);
197         
198         /* Editing (Strokes) ------------ */
199         
200         WM_operatortype_append(GPENCIL_OT_select);
201         WM_operatortype_append(GPENCIL_OT_select_all);
202         WM_operatortype_append(GPENCIL_OT_select_circle);
203         WM_operatortype_append(GPENCIL_OT_select_border);
204         
205         WM_operatortype_append(GPENCIL_OT_select_linked);
206         WM_operatortype_append(GPENCIL_OT_select_more);
207         WM_operatortype_append(GPENCIL_OT_select_less);
208         
209         WM_operatortype_append(GPENCIL_OT_duplicate);
210         WM_operatortype_append(GPENCIL_OT_delete);
211         
212         /* Editing (Buttons) ------------ */
213         
214         WM_operatortype_append(GPENCIL_OT_data_add);
215         WM_operatortype_append(GPENCIL_OT_data_unlink);
216         
217         WM_operatortype_append(GPENCIL_OT_layer_add);
218         WM_operatortype_append(GPENCIL_OT_layer_remove);
219         WM_operatortype_append(GPENCIL_OT_layer_move);
220         
221         WM_operatortype_append(GPENCIL_OT_active_frame_delete);
222         
223         WM_operatortype_append(GPENCIL_OT_convert);
224         
225         /* Editing (Time) --------------- */
226 }
227
228 void ED_operatormacros_gpencil(void)
229 {
230         wmOperatorType *ot;
231         wmOperatorTypeMacro *otmacro;
232
233         ot = WM_operatortype_append_macro("GPENCIL_OT_duplicate_move", "Duplicate Strokes",
234                                           "Make copies of the selected Grease Pencil strokes and move them",
235                                           OPTYPE_UNDO | OPTYPE_REGISTER);
236         WM_operatortype_macro_define(ot, "GPENCIL_OT_duplicate");
237         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
238         RNA_boolean_set(otmacro->ptr, "gpencil_strokes", true);
239 }
240
241 /* ****************************************** */