Curve Decimate: new tool to simplify bezier curves
[blender.git] / source / blender / editors / curve / curve_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.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/curve/curve_ops.c
28  *  \ingroup edcurve
29  */
30
31
32 #include <stdlib.h>
33 #include <math.h>
34
35
36 #include "DNA_curve_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "RNA_access.h"
40
41 #include "WM_api.h"
42 #include "WM_types.h"
43
44 #include "ED_curve.h"
45 #include "ED_object.h"
46 #include "ED_screen.h"
47 #include "ED_transform.h"
48
49 #include "curve_intern.h"
50
51 /************************* registration ****************************/
52
53 void ED_operatortypes_curve(void)
54 {
55         WM_operatortype_append(FONT_OT_text_insert);
56         WM_operatortype_append(FONT_OT_line_break);
57
58         WM_operatortype_append(FONT_OT_case_toggle);
59         WM_operatortype_append(FONT_OT_case_set);
60         WM_operatortype_append(FONT_OT_style_toggle);
61         WM_operatortype_append(FONT_OT_style_set);
62
63         WM_operatortype_append(FONT_OT_select_all);
64
65         WM_operatortype_append(FONT_OT_text_copy);
66         WM_operatortype_append(FONT_OT_text_cut);
67         WM_operatortype_append(FONT_OT_text_paste);
68         WM_operatortype_append(FONT_OT_text_paste_from_file);
69
70         WM_operatortype_append(FONT_OT_move);
71         WM_operatortype_append(FONT_OT_move_select);
72         WM_operatortype_append(FONT_OT_delete);
73
74         WM_operatortype_append(FONT_OT_change_character);
75         WM_operatortype_append(FONT_OT_change_spacing);
76         
77         WM_operatortype_append(FONT_OT_open);
78         WM_operatortype_append(FONT_OT_unlink);
79         
80         WM_operatortype_append(FONT_OT_textbox_add);
81         WM_operatortype_append(FONT_OT_textbox_remove);
82
83         WM_operatortype_append(CURVE_OT_hide);
84         WM_operatortype_append(CURVE_OT_reveal);
85
86         WM_operatortype_append(CURVE_OT_separate);
87         WM_operatortype_append(CURVE_OT_split);
88         WM_operatortype_append(CURVE_OT_duplicate);
89         WM_operatortype_append(CURVE_OT_delete);
90         WM_operatortype_append(CURVE_OT_dissolve_verts);
91
92         WM_operatortype_append(CURVE_OT_spline_type_set);
93         WM_operatortype_append(CURVE_OT_radius_set);
94         WM_operatortype_append(CURVE_OT_spline_weight_set);
95         WM_operatortype_append(CURVE_OT_handle_type_set);
96         WM_operatortype_append(CURVE_OT_normals_make_consistent);
97         WM_operatortype_append(CURVE_OT_decimate);
98         WM_operatortype_append(CURVE_OT_shade_smooth);
99         WM_operatortype_append(CURVE_OT_shade_flat);
100         WM_operatortype_append(CURVE_OT_tilt_clear);
101         
102         WM_operatortype_append(CURVE_OT_primitive_bezier_curve_add);
103         WM_operatortype_append(CURVE_OT_primitive_bezier_circle_add);
104         WM_operatortype_append(CURVE_OT_primitive_nurbs_curve_add);
105         WM_operatortype_append(CURVE_OT_primitive_nurbs_circle_add);
106         WM_operatortype_append(CURVE_OT_primitive_nurbs_path_add);
107         
108         WM_operatortype_append(SURFACE_OT_primitive_nurbs_surface_curve_add);
109         WM_operatortype_append(SURFACE_OT_primitive_nurbs_surface_circle_add);
110         WM_operatortype_append(SURFACE_OT_primitive_nurbs_surface_surface_add);
111         WM_operatortype_append(SURFACE_OT_primitive_nurbs_surface_cylinder_add);
112         WM_operatortype_append(SURFACE_OT_primitive_nurbs_surface_sphere_add);
113         WM_operatortype_append(SURFACE_OT_primitive_nurbs_surface_torus_add);
114         
115         WM_operatortype_append(CURVE_OT_smooth);
116         WM_operatortype_append(CURVE_OT_smooth_weight);
117         WM_operatortype_append(CURVE_OT_smooth_radius);
118         WM_operatortype_append(CURVE_OT_smooth_tilt);
119
120         WM_operatortype_append(CURVE_OT_de_select_first);
121         WM_operatortype_append(CURVE_OT_de_select_last);
122         WM_operatortype_append(CURVE_OT_select_all);
123         WM_operatortype_append(CURVE_OT_select_linked);
124         WM_operatortype_append(CURVE_OT_select_linked_pick);
125         WM_operatortype_append(CURVE_OT_select_row);
126         WM_operatortype_append(CURVE_OT_select_next);
127         WM_operatortype_append(CURVE_OT_select_previous);
128         WM_operatortype_append(CURVE_OT_select_more);
129         WM_operatortype_append(CURVE_OT_select_less);
130         WM_operatortype_append(CURVE_OT_select_random);
131         WM_operatortype_append(CURVE_OT_select_nth);
132         WM_operatortype_append(CURVE_OT_select_similar);
133         WM_operatortype_append(CURVE_OT_shortest_path_pick);
134
135         WM_operatortype_append(CURVE_OT_switch_direction);
136         WM_operatortype_append(CURVE_OT_subdivide);
137         WM_operatortype_append(CURVE_OT_make_segment);
138         WM_operatortype_append(CURVE_OT_spin);
139         WM_operatortype_append(CURVE_OT_vertex_add);
140         WM_operatortype_append(CURVE_OT_draw);
141         WM_operatortype_append(CURVE_OT_extrude);
142         WM_operatortype_append(CURVE_OT_cyclic_toggle);
143
144         WM_operatortype_append(CURVE_OT_match_texture_space);
145 }
146
147 void ED_operatormacros_curve(void)
148 {
149         wmOperatorType *ot;
150         wmOperatorTypeMacro *otmacro;
151
152         ot = WM_operatortype_append_macro("CURVE_OT_duplicate_move", "Add Duplicate", "Duplicate curve and move",
153                                           OPTYPE_UNDO | OPTYPE_REGISTER);
154         WM_operatortype_macro_define(ot, "CURVE_OT_duplicate");
155         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
156         RNA_enum_set(otmacro->ptr, "proportional", 0);
157         RNA_boolean_set(otmacro->ptr, "mirror", false);
158
159         ot = WM_operatortype_append_macro("CURVE_OT_extrude_move", "Extrude Curve and Move",
160                                           "Extrude curve and move result", OPTYPE_UNDO | OPTYPE_REGISTER);
161         WM_operatortype_macro_define(ot, "CURVE_OT_extrude");
162         otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
163         RNA_enum_set(otmacro->ptr, "proportional", 0);
164         RNA_boolean_set(otmacro->ptr, "mirror", false);
165 }
166
167 void ED_keymap_curve(wmKeyConfig *keyconf)
168 {
169         wmKeyMap *keymap;
170         wmKeyMapItem *kmi;
171         
172         keymap = WM_keymap_find(keyconf, "Font", 0, 0);
173         keymap->poll = ED_operator_editfont;
174         
175         /* only set in editmode font, by space_view3d listener */
176         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_style_toggle", BKEY, KM_PRESS, KM_CTRL, 0)->ptr, "style", CU_CHINFO_BOLD);
177         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_style_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "style", CU_CHINFO_ITALIC);
178         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_style_toggle", UKEY, KM_PRESS, KM_CTRL, 0)->ptr, "style", CU_CHINFO_UNDERLINE);
179         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_style_toggle", PKEY, KM_PRESS, KM_CTRL, 0)->ptr, "style", CU_CHINFO_SMALLCAPS);
180
181         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_SEL);
182         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
183         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_SEL);
184         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", DEL_PREV_SEL); /* same as above [#26623] */
185         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
186
187         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
188         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
189         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
190         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
191         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
192         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
193         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
194         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
195         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
196         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
197
198         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_BEGIN);
199         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_END);
200         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_CHAR);
201         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_CHAR);
202         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", PREV_WORD);
203         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", NEXT_WORD);
204         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_LINE);
205         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_LINE);
206         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_PAGE);
207         RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move_select", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_PAGE);
208
209         RNA_int_set(WM_keymap_add_item(keymap, "FONT_OT_change_spacing", LEFTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "delta", -1);
210         RNA_int_set(WM_keymap_add_item(keymap, "FONT_OT_change_spacing", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "delta", 1);
211         RNA_int_set(WM_keymap_add_item(keymap, "FONT_OT_change_character", UPARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "delta", 1);
212         RNA_int_set(WM_keymap_add_item(keymap, "FONT_OT_change_character", DOWNARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "delta", -1);
213
214         WM_keymap_add_item(keymap, "FONT_OT_select_all", AKEY, KM_PRESS, KM_CTRL, 0);
215
216         WM_keymap_add_item(keymap, "FONT_OT_text_copy", CKEY, KM_PRESS, KM_CTRL, 0);
217         WM_keymap_add_item(keymap, "FONT_OT_text_cut", XKEY, KM_PRESS, KM_CTRL, 0);
218         WM_keymap_add_item(keymap, "FONT_OT_text_paste", VKEY, KM_PRESS, KM_CTRL, 0);
219 #ifdef __APPLE__
220         WM_keymap_add_item(keymap, "FONT_OT_select_all", AKEY, KM_PRESS, KM_OSKEY, 0);
221         WM_keymap_add_item(keymap, "FONT_OT_text_copy", CKEY, KM_PRESS, KM_OSKEY, 0);
222         WM_keymap_add_item(keymap, "FONT_OT_text_cut", XKEY, KM_PRESS, KM_OSKEY, 0);
223         WM_keymap_add_item(keymap, "FONT_OT_text_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
224 #endif
225
226         WM_keymap_add_item(keymap, "FONT_OT_line_break", RETKEY, KM_PRESS, 0, 0);
227         WM_keymap_add_item(keymap, "FONT_OT_text_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
228         kmi = WM_keymap_add_item(keymap, "FONT_OT_text_insert", BACKSPACEKEY, KM_PRESS, KM_ALT, 0);
229         RNA_boolean_set(kmi->ptr, "accent", true); /* accented characters */
230
231         /* only set in editmode curve, by space_view3d listener */
232         keymap = WM_keymap_find(keyconf, "Curve", 0, 0);
233         keymap->poll = ED_operator_editsurfcurve;
234
235         WM_keymap_add_menu(keymap, "INFO_MT_edit_curve_add", AKEY, KM_PRESS, KM_SHIFT, 0);
236         
237         WM_keymap_add_item(keymap, "CURVE_OT_handle_type_set", VKEY, KM_PRESS, 0, 0);
238
239         WM_keymap_add_item(keymap, "CURVE_OT_vertex_add", ACTIONMOUSE, KM_CLICK, KM_CTRL, 0);
240
241         kmi = WM_keymap_add_item(keymap, "CURVE_OT_draw", ACTIONMOUSE, KM_PRESS, KM_SHIFT, 0);
242         RNA_boolean_set(kmi->ptr, "wait_for_input", false);
243
244         kmi = WM_keymap_add_item(keymap, "CURVE_OT_select_all", AKEY, KM_PRESS, 0, 0);
245         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
246         kmi = WM_keymap_add_item(keymap, "CURVE_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
247         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
248
249         WM_keymap_add_item(keymap, "CURVE_OT_select_row", RKEY, KM_PRESS, KM_SHIFT, 0);
250         WM_keymap_add_item(keymap, "CURVE_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
251         WM_keymap_add_item(keymap, "CURVE_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
252         WM_keymap_add_item(keymap, "CURVE_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
253         WM_keymap_add_item(keymap, "CURVE_OT_select_similar", GKEY, KM_PRESS, KM_SHIFT, 0);
254
255         kmi = WM_keymap_add_item(keymap, "CURVE_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
256         RNA_boolean_set(kmi->ptr, "deselect", false);
257         kmi = WM_keymap_add_item(keymap, "CURVE_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0);
258         RNA_boolean_set(kmi->ptr, "deselect", true);
259
260         WM_keymap_add_item(keymap, "CURVE_OT_shortest_path_pick", SELECTMOUSE, KM_CLICK, KM_CTRL, 0);
261
262         WM_keymap_add_item(keymap, "CURVE_OT_separate", PKEY, KM_PRESS, 0, 0);
263         WM_keymap_add_item(keymap, "CURVE_OT_split", YKEY, KM_PRESS, 0, 0);
264         WM_keymap_add_item(keymap, "CURVE_OT_extrude_move", EKEY, KM_PRESS, 0, 0);
265         WM_keymap_add_item(keymap, "CURVE_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
266         WM_keymap_add_item(keymap, "CURVE_OT_make_segment", FKEY, KM_PRESS, 0, 0);
267         WM_keymap_add_item(keymap, "CURVE_OT_cyclic_toggle", CKEY, KM_PRESS, KM_ALT, 0);
268
269         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_curve_delete", XKEY, KM_PRESS, 0, 0);
270         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_curve_delete", DELKEY, KM_PRESS, 0, 0);
271
272         WM_keymap_add_item(keymap, "CURVE_OT_dissolve_verts", XKEY, KM_PRESS, KM_CTRL, 0);
273         WM_keymap_add_item(keymap, "CURVE_OT_dissolve_verts", DELKEY, KM_PRESS, KM_CTRL, 0);
274
275         WM_keymap_add_item(keymap, "CURVE_OT_tilt_clear", TKEY, KM_PRESS, KM_ALT, 0);
276         WM_keymap_add_item(keymap, "TRANSFORM_OT_tilt", TKEY, KM_PRESS, KM_CTRL, 0);
277
278         RNA_enum_set(WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0)->ptr, "mode", TFM_CURVE_SHRINKFATTEN);
279
280         WM_keymap_add_item(keymap, "CURVE_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
281         kmi = WM_keymap_add_item(keymap, "CURVE_OT_hide", HKEY, KM_PRESS, 0, 0);
282         RNA_boolean_set(kmi->ptr, "unselected", false);
283         kmi = WM_keymap_add_item(keymap, "CURVE_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
284         RNA_boolean_set(kmi->ptr, "unselected", true);
285
286         WM_keymap_add_item(keymap, "CURVE_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
287
288         WM_keymap_add_item(keymap, "OBJECT_OT_vertex_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
289         
290         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_curve_specials", WKEY, KM_PRESS, 0, 0);
291
292         /* menus */
293         WM_keymap_add_menu(keymap, "VIEW3D_MT_hook", HKEY, KM_PRESS, KM_CTRL, 0);
294
295         ED_keymap_proportional_cycle(keyconf, keymap);
296         ED_keymap_proportional_editmode(keyconf, keymap, true);
297 }