Sculpt:
[blender.git] / source / blender / editors / mesh / mesh_ops.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_object_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_userdef_types.h"
39 #include "DNA_view3d_types.h"
40 #include "DNA_windowmanager_types.h"
41
42 #include "BLI_math.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_editVert.h"
45
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_mesh.h"
49 #include "BKE_utildefines.h"
50
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "ED_mesh.h"
58 #include "ED_object.h"
59 #include "ED_screen.h"
60 #include "ED_transform.h"
61 #include "ED_view3d.h"
62
63 #include "UI_interface.h"
64
65 #include "mesh_intern.h"
66
67 /**************************** registration **********************************/
68
69 void ED_operatortypes_mesh(void)
70 {
71         WM_operatortype_append(MESH_OT_select_all_toggle);
72         WM_operatortype_append(MESH_OT_select_more);
73         WM_operatortype_append(MESH_OT_select_less);
74         WM_operatortype_append(MESH_OT_select_inverse);
75         WM_operatortype_append(MESH_OT_select_non_manifold);
76         WM_operatortype_append(MESH_OT_select_linked);
77         WM_operatortype_append(MESH_OT_select_linked_pick);
78         WM_operatortype_append(MESH_OT_select_random);
79         WM_operatortype_append(MESH_OT_selection_type);
80         WM_operatortype_append(MESH_OT_hide);
81         WM_operatortype_append(MESH_OT_reveal);
82         WM_operatortype_append(MESH_OT_select_by_number_vertices);
83         WM_operatortype_append(MESH_OT_select_mirror);
84         WM_operatortype_append(MESH_OT_normals_make_consistent);
85         WM_operatortype_append(MESH_OT_merge);
86         WM_operatortype_append(MESH_OT_subdivide);
87         WM_operatortype_append(MESH_OT_faces_select_linked_flat);
88         WM_operatortype_append(MESH_OT_edges_select_sharp);
89         WM_operatortype_append(MESH_OT_primitive_plane_add);
90         WM_operatortype_append(MESH_OT_primitive_cube_add);
91         WM_operatortype_append(MESH_OT_primitive_circle_add);
92         WM_operatortype_append(MESH_OT_primitive_tube_add);
93         WM_operatortype_append(MESH_OT_primitive_cone_add);
94         WM_operatortype_append(MESH_OT_primitive_grid_add);
95         WM_operatortype_append(MESH_OT_primitive_monkey_add);
96         WM_operatortype_append(MESH_OT_primitive_uv_sphere_add);
97         WM_operatortype_append(MESH_OT_primitive_ico_sphere_add);
98         WM_operatortype_append(MESH_OT_fgon_clear);
99         WM_operatortype_append(MESH_OT_fgon_make);
100         WM_operatortype_append(MESH_OT_duplicate);
101         WM_operatortype_append(MESH_OT_remove_doubles);
102         WM_operatortype_append(MESH_OT_extrude);
103         WM_operatortype_append(MESH_OT_spin);
104         WM_operatortype_append(MESH_OT_screw);
105         
106         WM_operatortype_append(MESH_OT_split);
107         WM_operatortype_append(MESH_OT_extrude_repeat);
108         WM_operatortype_append(MESH_OT_edge_rotate);
109         WM_operatortype_append(MESH_OT_select_vertex_path);
110         WM_operatortype_append(MESH_OT_loop_to_region);
111         WM_operatortype_append(MESH_OT_region_to_loop);
112         WM_operatortype_append(MESH_OT_select_axis);
113         
114         WM_operatortype_append(MESH_OT_uvs_rotate);
115         WM_operatortype_append(MESH_OT_uvs_mirror);
116         WM_operatortype_append(MESH_OT_colors_rotate);
117         WM_operatortype_append(MESH_OT_colors_mirror);
118         
119         WM_operatortype_append(MESH_OT_fill);
120         WM_operatortype_append(MESH_OT_beauty_fill);
121         WM_operatortype_append(MESH_OT_quads_convert_to_tris);
122         WM_operatortype_append(MESH_OT_tris_convert_to_quads);
123         WM_operatortype_append(MESH_OT_edge_flip);
124         WM_operatortype_append(MESH_OT_faces_shade_smooth);
125         WM_operatortype_append(MESH_OT_faces_shade_flat);
126
127         WM_operatortype_append(MESH_OT_delete);
128
129         WM_operatortype_append(MESH_OT_separate);
130         WM_operatortype_append(MESH_OT_dupli_extrude_cursor);
131         WM_operatortype_append(MESH_OT_loop_select);
132         WM_operatortype_append(MESH_OT_edge_face_add);
133         WM_operatortype_append(MESH_OT_select_shortest_path);
134         WM_operatortype_append(MESH_OT_select_similar);
135         WM_operatortype_append(MESH_OT_loop_multi_select);
136         WM_operatortype_append(MESH_OT_mark_seam);
137         WM_operatortype_append(MESH_OT_mark_sharp);
138         WM_operatortype_append(MESH_OT_vertices_smooth);
139         WM_operatortype_append(MESH_OT_flip_normals);
140         WM_operatortype_append(MESH_OT_knife_cut);
141         WM_operatortype_append(MESH_OT_rip);
142         WM_operatortype_append(MESH_OT_blend_from_shape);
143         WM_operatortype_append(MESH_OT_shape_propagate_to_all);
144         
145         WM_operatortype_append(MESH_OT_uv_texture_add);
146         WM_operatortype_append(MESH_OT_uv_texture_remove);
147         WM_operatortype_append(MESH_OT_vertex_color_add);
148         WM_operatortype_append(MESH_OT_vertex_color_remove);
149         WM_operatortype_append(MESH_OT_sticky_add);
150         WM_operatortype_append(MESH_OT_sticky_remove);
151         
152         WM_operatortype_append(MESH_OT_edgering_select);
153         WM_operatortype_append(MESH_OT_loopcut);
154 }
155
156 int ED_operator_editmesh_face_select(bContext *C)
157 {
158         Object *obedit= CTX_data_edit_object(C);
159         if(obedit && obedit->type==OB_MESH) {
160                 EditMesh *em = ((Mesh *)obedit->data)->edit_mesh;
161                 if (em && em->selectmode & SCE_SELECT_FACE) {
162                         return 1;
163                 }
164         }
165         return 0;
166 }
167
168 void ED_operatormacros_mesh(void)
169 {
170         wmOperatorType *ot;
171         wmOperatorTypeMacro *otmacro;
172         int constraint_axis[3] = {0, 0, 1};
173         
174         ot= WM_operatortype_append_macro("MESH_OT_loopcut_slide", "Loopcut and Slide", OPTYPE_UNDO|OPTYPE_REGISTER);
175         WM_operatortype_macro_define(ot, "MESH_OT_loopcut");
176         WM_operatortype_macro_define(ot, "TFM_OT_edge_slide");
177
178         ot= WM_operatortype_append_macro("MESH_OT_duplicate_move", "Add Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
179         WM_operatortype_macro_define(ot, "MESH_OT_duplicate");
180         otmacro= WM_operatortype_macro_define(ot, "TFM_OT_translate");
181         RNA_enum_set(otmacro->ptr, "proportional", 0);
182
183         ot= WM_operatortype_append_macro("MESH_OT_rip_move", "Rip", OPTYPE_UNDO|OPTYPE_REGISTER);
184         WM_operatortype_macro_define(ot, "MESH_OT_rip");
185         otmacro= WM_operatortype_macro_define(ot, "TFM_OT_translate");
186         RNA_enum_set(otmacro->ptr, "proportional", 0);
187
188         ot= WM_operatortype_append_macro("MESH_OT_extrude_move_along_normals", "Extrude Along Normals", OPTYPE_UNDO|OPTYPE_REGISTER);
189         ot->poll = ED_operator_editmesh_face_select; /* restrict extrude along normals to face select */
190         WM_operatortype_macro_define(ot, "MESH_OT_extrude");
191         otmacro= WM_operatortype_macro_define(ot, "TFM_OT_translate");
192         RNA_enum_set(otmacro->ptr, "proportional", 0);
193         RNA_enum_set(otmacro->ptr, "constraint_orientation", V3D_MANIP_NORMAL);
194         RNA_boolean_set_array(otmacro->ptr, "constraint_axis", constraint_axis);
195
196         ot= WM_operatortype_append_macro("MESH_OT_extrude_move", "Extrude", OPTYPE_UNDO|OPTYPE_REGISTER);
197         WM_operatortype_macro_define(ot, "MESH_OT_extrude");
198         otmacro= WM_operatortype_macro_define(ot, "TFM_OT_translate");
199         RNA_enum_set(otmacro->ptr, "proportional", 0);
200 }
201
202 /* note mesh keymap also for other space? */
203 void ED_keymap_mesh(wmKeyConfig *keyconf)
204 {       
205         wmKeyMap *keymap;
206         wmKeyMapItem *kmi;
207         
208         keymap= WM_keymap_find(keyconf, "EditMesh", 0, 0);
209         keymap->poll= ED_operator_editmesh;
210         
211         WM_keymap_add_item(keymap, "MESH_OT_loopcut_slide", RKEY, KM_PRESS, KM_CTRL, 0);
212
213         /* selecting */
214         /* standard mouse selection goes via space_view3d */
215         WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
216         kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT, 0);
217         RNA_boolean_set(kmi->ptr, "extend", 1);
218
219         kmi= WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_ALT|KM_CTRL, 0);
220         kmi= WM_keymap_add_item(keymap, "MESH_OT_edgering_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT|KM_CTRL, 0);
221         RNA_boolean_set(kmi->ptr, "extend", 1);
222
223         WM_keymap_add_item(keymap, "MESH_OT_select_shortest_path", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
224
225         WM_keymap_add_item(keymap, "MESH_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
226         WM_keymap_add_item(keymap, "MESH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
227         WM_keymap_add_item(keymap, "MESH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
228         WM_keymap_add_item(keymap, "MESH_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
229         WM_keymap_add_item(keymap, "MESH_OT_select_non_manifold", MKEY, KM_PRESS, (KM_CTRL|KM_SHIFT|KM_ALT), 0);
230         
231         WM_keymap_add_item(keymap, "MESH_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
232         WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
233         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "deselect", 1);
234         
235         RNA_float_set(WM_keymap_add_item(keymap, "MESH_OT_faces_select_linked_flat", FKEY, KM_PRESS, (KM_CTRL|KM_SHIFT|KM_ALT), 0)->ptr,"sharpness",135.0);
236         RNA_float_set(WM_keymap_add_item(keymap, "MESH_OT_edges_select_sharp", SKEY, KM_PRESS, (KM_CTRL|KM_SHIFT|KM_ALT), 0)->ptr,"sharpness",135.0);
237
238         WM_keymap_add_item(keymap, "MESH_OT_select_similar", GKEY, KM_PRESS, KM_SHIFT, 0);
239         
240         /* selection mode */
241         WM_keymap_add_item(keymap, "MESH_OT_selection_type", TABKEY, KM_PRESS, KM_CTRL, 0);
242         
243         /* hide */
244         WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, 0, 0);
245         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "unselected", 1);
246         WM_keymap_add_item(keymap, "MESH_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
247
248         /* tools */
249         WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
250         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "inside", 1);
251         
252         WM_keymap_add_item(keymap, "MESH_OT_extrude_move_along_normals", EKEY, KM_PRESS, 0, 0); /* this first so it's selected if possible */
253         WM_keymap_add_item(keymap, "MESH_OT_extrude_move", EKEY, KM_PRESS, 0, 0);
254         
255         WM_keymap_add_item(keymap, "MESH_OT_spin", RKEY, KM_PRESS, KM_ALT, 0);
256         WM_keymap_add_item(keymap, "MESH_OT_screw", NINEKEY, KM_PRESS, KM_CTRL, 0);
257         
258         WM_keymap_add_item(keymap, "MESH_OT_fill", FKEY, KM_PRESS, KM_SHIFT, 0);
259         WM_keymap_add_item(keymap, "MESH_OT_beauty_fill", FKEY, KM_PRESS, KM_ALT, 0);
260         WM_keymap_add_item(keymap, "MESH_OT_quads_convert_to_tris", TKEY, KM_PRESS, KM_CTRL, 0);
261         WM_keymap_add_item(keymap, "MESH_OT_tris_convert_to_quads", JKEY, KM_PRESS, KM_ALT, 0);
262         WM_keymap_add_item(keymap, "MESH_OT_edge_flip", FKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
263
264         WM_keymap_add_item(keymap, "MESH_OT_extrude_repeat", FOURKEY, KM_PRESS, KM_ALT, 0);
265         WM_keymap_add_item(keymap, "MESH_OT_edge_rotate", FIVEKEY, KM_PRESS, KM_CTRL, 0);
266         
267         WM_keymap_add_item(keymap, "MESH_OT_loop_to_region",SIXKEY, KM_PRESS, KM_CTRL, 0);
268         WM_keymap_add_item(keymap, "MESH_OT_region_to_loop",SIXKEY, KM_PRESS, KM_ALT, 0);
269         
270         WM_keymap_add_item(keymap, "MESH_OT_uvs_rotate",SEVENKEY, KM_PRESS, KM_CTRL, 0);
271         WM_keymap_add_item(keymap, "MESH_OT_uvs_mirror",SEVENKEY, KM_PRESS, KM_ALT, 0);
272         WM_keymap_add_item(keymap, "MESH_OT_colors_rotate",EIGHTKEY, KM_PRESS, KM_CTRL, 0);
273         WM_keymap_add_item(keymap, "MESH_OT_colors_mirror",EIGHTKEY, KM_PRESS, KM_ALT, 0);
274
275         WM_keymap_add_item(keymap, "MESH_OT_rip_move",VKEY, KM_PRESS, 0, 0);
276         WM_keymap_add_item(keymap, "MESH_OT_merge", MKEY, KM_PRESS, KM_ALT, 0);
277
278         /* add/remove */
279         WM_keymap_add_item(keymap, "MESH_OT_edge_face_add", FKEY, KM_PRESS, 0, 0);
280         WM_keymap_add_item(keymap, "MESH_OT_skin", FKEY, KM_PRESS, KM_CTRL|KM_ALT, 0); /* python */
281         WM_keymap_add_item(keymap, "MESH_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
282         
283         WM_keymap_add_menu(keymap, "INFO_MT_mesh_add", AKEY, KM_PRESS, KM_SHIFT, 0);
284         
285         WM_keymap_add_item(keymap, "MESH_OT_separate", PKEY, KM_PRESS, 0, 0);
286         WM_keymap_add_item(keymap, "MESH_OT_split", YKEY, KM_PRESS, 0, 0);
287
288         /* use KM_CLICK because same key is used for tweaks */
289         WM_keymap_add_item(keymap, "MESH_OT_dupli_extrude_cursor", LEFTMOUSE, KM_CLICK, KM_CTRL, 0);
290         
291         WM_keymap_add_item(keymap, "MESH_OT_delete", XKEY, KM_PRESS, 0, 0);
292         WM_keymap_add_item(keymap, "MESH_OT_delete", DELKEY, KM_PRESS, 0, 0);
293         
294         WM_keymap_add_item(keymap, "MESH_OT_fgon_make", FKEY, KM_PRESS, KM_ALT, 0);
295         WM_keymap_add_item(keymap, "MESH_OT_fgon_clear", FKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0);
296         
297         WM_keymap_add_item(keymap, "MESH_OT_knife_cut", LEFTMOUSE, KM_PRESS, 0, KKEY);
298
299         /* menus */
300         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_specials", WKEY, KM_PRESS, 0, 0);
301         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_faces", FKEY, KM_PRESS, KM_CTRL, 0);
302         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_edges", EKEY, KM_PRESS, KM_CTRL, 0);
303         WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_mesh_vertices", VKEY, KM_PRESS, KM_CTRL, 0);
304         WM_keymap_add_menu(keymap, "VIEW3D_MT_hook", HKEY, KM_PRESS, KM_CTRL, 0);
305         WM_keymap_add_menu(keymap, "VIEW3D_MT_uv_map", UKEY, KM_PRESS, 0, 0);
306         WM_keymap_add_menu(keymap, "VIEW3D_MT_vertex_group", GKEY, KM_PRESS, KM_CTRL, 0);
307         
308         ED_object_generic_keymap(keyconf, keymap, TRUE);
309 }
310