2.5: Various Fixes
[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_arithb.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_screen.h"
58 #include "ED_mesh.h"
59 #include "ED_view3d.h"
60
61 #include "BIF_transform.h"
62
63 #include "mesh_intern.h"
64
65
66 static int mesh_add_duplicate_exec(bContext *C, wmOperator *op)
67 {
68         Object *ob= CTX_data_edit_object(C);
69         EditMesh *em= BKE_mesh_get_editmesh(ob->data);
70
71         adduplicateflag(em, SELECT);
72         
73         BKE_mesh_end_editmesh(ob->data, em);
74         return OPERATOR_FINISHED;
75 }
76
77 static int mesh_add_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *event)
78 {
79         WM_cursor_wait(1);
80         mesh_add_duplicate_exec(C, op);
81         WM_cursor_wait(0);
82         
83         RNA_int_set(op->ptr, "mode", TFM_TRANSLATION);
84         WM_operator_name_call(C, "TFM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
85         
86         return OPERATOR_FINISHED;
87 }
88
89 static void MESH_OT_duplicate_add(wmOperatorType *ot)
90 {
91         
92         /* identifiers */
93         ot->name= "Add Duplicate";
94         ot->idname= "MESH_OT_duplicate_add";
95         
96         /* api callbacks */
97         ot->invoke= mesh_add_duplicate_invoke;
98         ot->exec= mesh_add_duplicate_exec;
99         
100         ot->poll= ED_operator_editmesh;
101         
102         /* to give to transform */
103         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
104 }
105
106
107 /* ************************** registration **********************************/
108
109 void ED_operatortypes_mesh(void)
110 {
111         WM_operatortype_append(MESH_OT_select_all_toggle);
112         WM_operatortype_append(MESH_OT_select_more);
113         WM_operatortype_append(MESH_OT_select_less);
114         WM_operatortype_append(MESH_OT_select_invert);
115         WM_operatortype_append(MESH_OT_select_non_manifold);
116         WM_operatortype_append(MESH_OT_select_linked);
117         WM_operatortype_append(MESH_OT_select_linked_pick);
118         WM_operatortype_append(MESH_OT_select_random);
119         WM_operatortype_append(MESH_OT_selection_type);
120         WM_operatortype_append(MESH_OT_hide);
121         WM_operatortype_append(MESH_OT_reveal);
122         WM_operatortype_append(MESH_OT_select_by_number_vertices);
123         WM_operatortype_append(MESH_OT_normals_make_consistent);
124         WM_operatortype_append(MESH_OT_subdivide);
125         WM_operatortype_append(MESH_OT_subdivide_multi);
126         WM_operatortype_append(MESH_OT_subdivide_multi_fractal);
127         WM_operatortype_append(MESH_OT_subdivide_smooth);
128         WM_operatortype_append(MESH_OT_subdivs);
129         WM_operatortype_append(MESH_OT_faces_select_linked_flat);
130         WM_operatortype_append(MESH_OT_edges_select_sharp);
131         WM_operatortype_append(MESH_OT_primitive_plane_add);
132         WM_operatortype_append(MESH_OT_primitive_cube_add);
133         WM_operatortype_append(MESH_OT_primitive_circle_add);
134         WM_operatortype_append(MESH_OT_primitive_cylinder_add);
135         WM_operatortype_append(MESH_OT_primitive_tube_add);
136         WM_operatortype_append(MESH_OT_primitive_cone_add);
137         WM_operatortype_append(MESH_OT_primitive_grid_add);
138         WM_operatortype_append(MESH_OT_primitive_monkey_add);
139         WM_operatortype_append(MESH_OT_primitive_uv_sphere_add);
140         WM_operatortype_append(MESH_OT_primitive_ico_sphere_add);
141         WM_operatortype_append(MESH_OT_fgon_clear);
142         WM_operatortype_append(MESH_OT_fgon_make);
143         WM_operatortype_append(MESH_OT_duplicate_add);
144         WM_operatortype_append(MESH_OT_remove_doubles);
145         WM_operatortype_append(MESH_OT_extrude);
146         WM_operatortype_append(MESH_OT_spin);
147         WM_operatortype_append(MESH_OT_screw);
148         
149         WM_operatortype_append(MESH_OT_vertices_transform_to_sphere);
150         WM_operatortype_append(MESH_OT_split);
151         WM_operatortype_append(MESH_OT_extrude_repeat);
152         WM_operatortype_append(MESH_OT_edge_rotate);
153         WM_operatortype_append(MESH_OT_select_vertex_path);
154         WM_operatortype_append(MESH_OT_loop_to_region);
155         WM_operatortype_append(MESH_OT_region_to_loop);
156         
157         WM_operatortype_append(MESH_OT_uvs_rotate);
158         WM_operatortype_append(MESH_OT_uvs_mirror);
159         WM_operatortype_append(MESH_OT_colors_rotate);
160         WM_operatortype_append(MESH_OT_colors_mirror);
161         
162         WM_operatortype_append(MESH_OT_fill);
163         WM_operatortype_append(MESH_OT_beauty_fill);
164         WM_operatortype_append(MESH_OT_quads_convert_to_tris);
165         WM_operatortype_append(MESH_OT_tris_convert_to_quads);
166         WM_operatortype_append(MESH_OT_edge_flip);
167         WM_operatortype_append(MESH_OT_faces_shade_smooth);
168         WM_operatortype_append(MESH_OT_faces_shade_solid);
169
170         WM_operatortype_append(MESH_OT_delete);
171
172         WM_operatortype_append(MESH_OT_separate);
173         WM_operatortype_append(MESH_OT_dupli_extrude_cursor);
174         WM_operatortype_append(MESH_OT_loop_select);
175         WM_operatortype_append(MESH_OT_edge_face_add);
176         WM_operatortype_append(MESH_OT_select_shortest_path);
177         WM_operatortype_append(MESH_OT_select_similar);
178         WM_operatortype_append(MESH_OT_loop_multi_select);
179         WM_operatortype_append(MESH_OT_mark_seam);
180         WM_operatortype_append(MESH_OT_mark_sharp);
181         WM_operatortype_append(MESH_OT_vertices_smooth);
182         WM_operatortype_append(MESH_OT_flip_editnormals);
183         WM_operatortype_append(MESH_OT_knife_cut);
184         WM_operatortype_append(MESH_OT_rip);
185         
186         WM_operatortype_append(MESH_OT_uv_texture_add);
187         WM_operatortype_append(MESH_OT_uv_texture_remove);
188         WM_operatortype_append(MESH_OT_vertex_color_add);
189         WM_operatortype_append(MESH_OT_vertex_color_remove);
190         WM_operatortype_append(MESH_OT_sticky_add);
191         WM_operatortype_append(MESH_OT_sticky_remove);
192 }
193
194 /* note mesh keymap also for other space? */
195 void ED_keymap_mesh(wmWindowManager *wm)
196 {       
197         ListBase *keymap= WM_keymap_listbase(wm, "EditMesh", 0, 0);
198         wmKeymapItem *kmi;
199         
200         /* selecting */
201         /* standard mouse selection goes via space_view3d */
202         WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
203         kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT, 0);
204         RNA_boolean_set(kmi->ptr, "extend", 1);
205         kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_ALT|KM_CTRL, 0);
206         RNA_boolean_set(kmi->ptr, "ring", 1);
207         kmi= WM_keymap_add_item(keymap, "MESH_OT_loop_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT|KM_CTRL, 0);
208         RNA_boolean_set(kmi->ptr, "extend", 1);
209         RNA_boolean_set(kmi->ptr, "ring", 1);
210
211         WM_keymap_add_item(keymap, "MESH_OT_select_shortest_path", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
212         
213         WM_keymap_add_item(keymap, "MESH_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
214         WM_keymap_add_item(keymap, "MESH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
215         WM_keymap_add_item(keymap, "MESH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
216         WM_keymap_add_item(keymap, "MESH_OT_select_invert", IKEY, KM_PRESS, KM_CTRL, 0);
217         WM_keymap_add_item(keymap, "MESH_OT_select_non_manifold", MKEY, KM_PRESS, (KM_CTRL|KM_SHIFT|KM_ALT), 0);
218         
219         WM_keymap_add_item(keymap, "MESH_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0);
220         WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, 0, 0);
221         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_select_linked_pick", LKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "deselect", 1);
222         
223         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);
224         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);
225         
226         WM_keymap_add_item(keymap, "MESH_OT_vertices_transform_to_sphere", SKEY, KM_PRESS, KM_CTRL|KM_SHIFT , 0);
227
228         WM_keymap_add_item(keymap, "MESH_OT_mark_seam", ONEKEY, KM_PRESS, KM_CTRL , 0);
229         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_mark_seam", ONEKEY, KM_PRESS, KM_ALT , 0)->ptr,"clear",1);
230
231         WM_keymap_add_item(keymap, "MESH_OT_mark_sharp", TWOKEY, KM_PRESS, KM_CTRL , 0);
232         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_mark_sharp", TWOKEY, KM_PRESS, KM_ALT , 0)->ptr,"set",1);
233
234         WM_keymap_add_item(keymap, "MESH_OT_select_similar", GKEY, KM_PRESS, KM_SHIFT, 0);
235         
236         /* selection mode */
237         WM_keymap_add_item(keymap, "MESH_OT_selection_type", TABKEY, KM_PRESS, KM_CTRL, 0);
238         
239         /* hide */
240         WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, 0, 0);
241         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "unselected", 1);
242         WM_keymap_add_item(keymap, "MESH_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
243         
244         /* tools */
245         WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_CTRL, 0);
246         RNA_boolean_set(WM_keymap_add_item(keymap, "MESH_OT_normals_make_consistent", NKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "inside", 1);
247         WM_keymap_add_item(keymap, "MESH_OT_vertices_smooth", THREEKEY, KM_PRESS, KM_CTRL , 0);
248         WM_keymap_add_item(keymap, "MESH_OT_flip_editnormals", THREEKEY, KM_PRESS, KM_ALT , 0);
249         
250         WM_keymap_add_item(keymap, "MESH_OT_subdivs", WKEY, KM_PRESS, 0, 0); // this is the menu
251         /*WM_keymap_add_item(keymap, "MESH_OT_subdivide_multi", WKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
252         WM_keymap_add_item(keymap, "MESH_OT_subdivide_multi_fractal", WKEY, KM_PRESS, KM_ALT, 0);
253         WM_keymap_add_item(keymap, "MESH_OT_subdivide_smooth", WKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);*/
254         WM_keymap_add_item(keymap, "MESH_OT_remove_doubles", VKEY, KM_PRESS, KM_CTRL, 0);
255         WM_keymap_add_item(keymap, "MESH_OT_extrude", EKEY, KM_PRESS, 0, 0);
256         
257         WM_keymap_add_item(keymap, "MESH_OT_spin", RKEY, KM_PRESS, KM_ALT, 0);
258         WM_keymap_add_item(keymap, "MESH_OT_screw", NINEKEY, KM_PRESS, KM_CTRL, 0);
259         
260         WM_keymap_add_item(keymap, "VIEW3D_OT_editmesh_face_toolbox", FKEY, KM_PRESS, KM_CTRL, 0); /* operators below are in this toolbox */
261         WM_keymap_add_item(keymap, "MESH_OT_fill", FKEY, KM_PRESS, KM_SHIFT, 0);
262         WM_keymap_add_item(keymap, "MESH_OT_beauty_fill", FKEY, KM_PRESS, KM_ALT, 0);
263         WM_keymap_add_item(keymap, "MESH_OT_quads_convert_to_tris", TKEY, KM_PRESS, KM_CTRL, 0);
264         WM_keymap_add_item(keymap, "MESH_OT_tris_convert_to_quads", JKEY, KM_PRESS, KM_ALT, 0);
265         
266         WM_keymap_add_item(keymap, "MESH_OT_split", FOURKEY, KM_PRESS, KM_CTRL, 0);
267         WM_keymap_add_item(keymap, "MESH_OT_extrude_repeat", FOURKEY, KM_PRESS, KM_ALT, 0);
268         WM_keymap_add_item(keymap, "MESH_OT_edge_rotate", FIVEKEY, KM_PRESS, KM_CTRL, 0);
269         
270         WM_keymap_add_item(keymap, "MESH_OT_loop_to_region",SIXKEY, KM_PRESS, KM_CTRL, 0);
271         WM_keymap_add_item(keymap, "MESH_OT_region_to_loop",SIXKEY, KM_PRESS, KM_ALT, 0);
272         
273         WM_keymap_add_item(keymap, "MESH_OT_uvs_rotate",SEVENKEY, KM_PRESS, KM_CTRL, 0);
274         WM_keymap_add_item(keymap, "MESH_OT_uvs_mirror",SEVENKEY, KM_PRESS, KM_ALT, 0);
275         WM_keymap_add_item(keymap, "MESH_OT_colors_rotate",EIGHTKEY, KM_PRESS, KM_CTRL, 0);
276         WM_keymap_add_item(keymap, "MESH_OT_colors_mirror",EIGHTKEY, KM_PRESS, KM_ALT, 0);
277
278         WM_keymap_add_item(keymap, "MESH_OT_rip",VKEY, KM_PRESS, 0, 0);
279
280         /* add/remove */
281         WM_keymap_add_item(keymap, "MESH_OT_edge_face_add", FKEY, KM_PRESS, 0, 0);
282         WM_keymap_add_item(keymap, "MESH_OT_duplicate_add", DKEY, KM_PRESS, KM_SHIFT, 0);
283         WM_keymap_add_item(keymap, "OBJECT_OT_mesh_add", AKEY, KM_PRESS, KM_SHIFT, 0);
284         WM_keymap_add_item(keymap, "MESH_OT_separate", PKEY, KM_PRESS, KM_SHIFT, 0);
285                                                 /* use KM_RELEASE because same key is used for tweaks */
286         WM_keymap_add_item(keymap, "MESH_OT_dupli_extrude_cursor", LEFTMOUSE, KM_RELEASE, KM_CTRL, 0);
287         
288         WM_keymap_add_item(keymap, "MESH_OT_delete", XKEY, KM_PRESS, 0, 0);
289         
290         WM_keymap_add_item(keymap, "MESH_OT_fgon_make", FKEY, KM_PRESS, KM_ALT, 0);
291         WM_keymap_add_item(keymap, "MESH_OT_fgon_clear", FKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0);
292         
293         WM_keymap_add_item(keymap, "MESH_OT_knife_cut", LEFTMOUSE, KM_PRESS, KM_ALT|KM_CTRL, 0);
294         
295         /* UV's */
296         WM_keymap_add_item(keymap, "UV_OT_mapping_menu", UKEY, KM_PRESS, 0, 0);
297 }
298