9d44e8efc349caef0ab0265fa5630a67787dea59
[blender-staging.git] / source / blender / editors / armature / armature_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
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_utildefines.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "ED_armature.h"
56 #include "ED_screen.h"
57 #include "ED_object.h"
58
59 #include "armature_intern.h"
60
61 /* ************************** quick tests **********************************/
62
63 /*  XXX This is a quick test operator to print names of all EditBones in context
64  *              that should be removed once tool coding starts...
65  */
66
67 static int armature_test_exec (bContext *C, wmOperator *op)
68 {
69         printf("EditMode Armature Test: \n");
70         
71         printf("\tSelected Bones \n");
72         CTX_DATA_BEGIN(C, EditBone*, ebone, selected_bones)
73         {
74                 printf("\t\tEditBone '%s' \n", ebone->name);
75         }
76         CTX_DATA_END;
77         
78         printf("\tEditable Bones \n");
79         CTX_DATA_BEGIN(C, EditBone*, ebone, selected_editable_bones) 
80         {
81                 printf("\t\tEditBone '%s' \n", ebone->name);
82         }
83         CTX_DATA_END;
84         
85         printf("\tActive Bone \n");
86         {
87                 EditBone *ebone= CTX_data_active_bone(C);
88                 if (ebone) printf("\t\tEditBone '%s' \n", ebone->name);
89                 else printf("\t\t<None> \n");
90         }
91         
92         return OPERATOR_FINISHED;
93 }
94
95 void ARMATURE_OT_test(wmOperatorType *ot)
96 {
97         /* identifiers */
98         ot->name= "Test Context";
99         ot->idname= "ARMATURE_OT_test";
100         
101         /* api callbacks */
102         ot->exec= armature_test_exec;
103 }
104
105 /* ************************** registration **********************************/
106
107 /* Both operators ARMATURE_OT_xxx and POSE_OT_xxx here */
108 void ED_operatortypes_armature(void)
109 {
110         /* EDIT ARMATURE */
111         WM_operatortype_append(ARMATURE_OT_bone_primitive_add);
112         
113         WM_operatortype_append(ARMATURE_OT_bones_align);
114         WM_operatortype_append(ARMATURE_OT_calculate_roll);
115         WM_operatortype_append(ARMATURE_OT_switch_direction);
116         WM_operatortype_append(ARMATURE_OT_subdivs);
117         WM_operatortype_append(ARMATURE_OT_subdivide_simple);
118         WM_operatortype_append(ARMATURE_OT_subdivide_multi);
119         
120         WM_operatortype_append(ARMATURE_OT_parent_set);
121         WM_operatortype_append(ARMATURE_OT_parent_clear);
122         
123         WM_operatortype_append(ARMATURE_OT_select_all_toggle);
124         WM_operatortype_append(ARMATURE_OT_select_inverse);
125         WM_operatortype_append(ARMATURE_OT_select_hierarchy);
126         WM_operatortype_append(ARMATURE_OT_select_linked);
127
128         WM_operatortype_append(ARMATURE_OT_delete);
129         WM_operatortype_append(ARMATURE_OT_duplicate_selected);
130         WM_operatortype_append(ARMATURE_OT_extrude);
131         WM_operatortype_append(ARMATURE_OT_click_extrude);
132         
133         WM_operatortype_append(ARMATURE_OT_autoside_names);
134         WM_operatortype_append(ARMATURE_OT_flip_names);
135
136         /* SKETCH */    
137         WM_operatortype_append(SKETCH_OT_gesture);
138         WM_operatortype_append(SKETCH_OT_delete);
139         WM_operatortype_append(SKETCH_OT_draw_stroke);
140         WM_operatortype_append(SKETCH_OT_draw_preview);
141         WM_operatortype_append(SKETCH_OT_finish_stroke);
142         WM_operatortype_append(SKETCH_OT_cancel_stroke);
143         WM_operatortype_append(SKETCH_OT_select);
144
145         /* POSE */
146         WM_operatortype_append(POSE_OT_hide);
147         WM_operatortype_append(POSE_OT_reveal);
148         
149         WM_operatortype_append(POSE_OT_rot_clear);
150         WM_operatortype_append(POSE_OT_loc_clear);
151         WM_operatortype_append(POSE_OT_scale_clear);
152         
153         WM_operatortype_append(POSE_OT_copy);
154         WM_operatortype_append(POSE_OT_paste);
155         
156         WM_operatortype_append(POSE_OT_select_all_toggle);
157         WM_operatortype_append(POSE_OT_select_inverse);
158
159         WM_operatortype_append(POSE_OT_select_parent);
160         WM_operatortype_append(POSE_OT_select_hierarchy);
161         WM_operatortype_append(POSE_OT_select_linked);
162         WM_operatortype_append(POSE_OT_select_constraint_target);
163         
164         WM_operatortype_append(POSE_OT_groups_menu);
165         WM_operatortype_append(POSE_OT_group_add);
166         WM_operatortype_append(POSE_OT_group_remove);
167         WM_operatortype_append(POSE_OT_group_assign);
168         WM_operatortype_append(POSE_OT_group_unassign);
169         
170         WM_operatortype_append(POSE_OT_paths_calculate);
171         WM_operatortype_append(POSE_OT_paths_clear);
172         
173         WM_operatortype_append(POSE_OT_autoside_names);
174         WM_operatortype_append(POSE_OT_flip_names);
175         
176         
177         /* POSELIB */
178         WM_operatortype_append(POSELIB_OT_browse_interactive);
179         
180         WM_operatortype_append(POSELIB_OT_pose_add);
181         WM_operatortype_append(POSELIB_OT_pose_remove);
182         WM_operatortype_append(POSELIB_OT_pose_rename);
183         
184         /* TESTS */
185         WM_operatortype_append(ARMATURE_OT_test); // XXX temp test for context iterators... to be removed
186 }
187
188 void ED_keymap_armature(wmWindowManager *wm)
189 {
190         ListBase *keymap;
191         wmKeymapItem *kmi;
192         
193         /* Armature ------------------------ */
194         keymap= WM_keymap_listbase(wm, "Armature", 0, 0);
195         
196         /* only set in editmode armature, by space_view3d listener */
197 //      WM_keymap_add_item(keymap, "ARMATURE_OT_hide", HKEY, KM_PRESS, 0, 0);
198         WM_keymap_add_item(keymap, "ARMATURE_OT_bones_align", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
199         WM_keymap_add_item(keymap, "ARMATURE_OT_calculate_roll", NKEY, KM_PRESS, KM_CTRL, 0);
200         
201         WM_keymap_add_item(keymap, "ARMATURE_OT_switch_direction", FKEY, KM_PRESS, KM_ALT, 0);
202         
203         WM_keymap_add_item(keymap, "ARMATURE_OT_bone_primitive_add", AKEY, KM_PRESS, KM_SHIFT, 0);
204                 /* only the menu-version of subdivide is registered in keymaps for now */
205         WM_keymap_add_item(keymap, "ARMATURE_OT_subdivs", SKEY, KM_PRESS, KM_ALT, 0);
206         
207         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_set", PKEY, KM_PRESS, KM_CTRL, 0);
208         WM_keymap_add_item(keymap, "ARMATURE_OT_parent_clear", PKEY, KM_PRESS, KM_ALT, 0);
209         
210         WM_keymap_add_item(keymap, "ARMATURE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
211         WM_keymap_add_item(keymap, "ARMATURE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
212         
213         WM_keymap_add_item(keymap, "ARMATURE_OT_test", TKEY, KM_PRESS, 0, 0);  // XXX temp test for context iterators... to be removed
214
215         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
216                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
217         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
218                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
219                 RNA_boolean_set(kmi->ptr, "extend", 1);
220         
221         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
222                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
223         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
224                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
225                 RNA_boolean_set(kmi->ptr, "extend", 1);
226
227         WM_keymap_add_item(keymap, "ARMATURE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
228         WM_keymap_add_item(keymap, "ARMATURE_OT_delete", XKEY, KM_PRESS, 0, 0);
229         WM_keymap_add_item(keymap, "ARMATURE_OT_duplicate_selected", DKEY, KM_PRESS, KM_SHIFT, 0);
230         WM_keymap_add_item(keymap, "ARMATURE_OT_extrude", EKEY, KM_PRESS, 0, 0);
231         kmi= WM_keymap_add_item(keymap, "ARMATURE_OT_extrude", EKEY, KM_PRESS, KM_SHIFT, 0);
232                 RNA_boolean_set(kmi->ptr, "forked", 1);
233         WM_keymap_add_item(keymap, "ARMATURE_OT_click_extrude", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
234         
235         /* Armature -> Etch-A-Ton ------------------------ */
236         WM_keymap_add_item(keymap, "SKETCH_OT_delete", XKEY, KM_PRESS, 0, 0);
237         WM_keymap_add_item(keymap, "SKETCH_OT_finish_stroke", SELECTMOUSE, KM_PRESS, 0, 0);
238         WM_keymap_add_item(keymap, "SKETCH_OT_cancel_stroke", ESCKEY, KM_PRESS, 0, 0);
239         WM_keymap_add_item(keymap, "SKETCH_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
240
241         /* Pose ------------------------ */
242         /* only set in posemode, by space_view3d listener */
243         keymap= WM_keymap_listbase(wm, "Pose", 0, 0);
244         
245         WM_keymap_add_item(keymap, "POSE_OT_hide", HKEY, KM_PRESS, 0, 0);
246         kmi= WM_keymap_add_item(keymap, "POSE_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0);
247                 RNA_boolean_set(kmi->ptr, "unselected", 1);
248         WM_keymap_add_item(keymap, "POSE_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0);
249         /*clear pose*/
250         WM_keymap_add_item(keymap, "POSE_OT_rot_clear", RKEY, KM_PRESS, KM_ALT, 0);
251         WM_keymap_add_item(keymap, "POSE_OT_loc_clear", GKEY, KM_PRESS, KM_ALT, 0);
252         WM_keymap_add_item(keymap, "POSE_OT_scale_clear", SKEY, KM_PRESS, KM_ALT, 0);
253         
254                 // for now, we include hotkeys for copy/paste
255         WM_keymap_add_item(keymap, "POSE_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
256         WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
257         kmi= WM_keymap_add_item(keymap, "POSE_OT_paste", VKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
258                 RNA_boolean_set(kmi->ptr, "flipped", 1);
259         
260         WM_keymap_add_item(keymap, "POSE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
261         WM_keymap_add_item(keymap, "POSE_OT_select_inverse", IKEY, KM_PRESS, KM_CTRL, 0);
262
263         WM_keymap_add_item(keymap, "POSE_OT_select_parent", PKEY, KM_PRESS, KM_SHIFT, 0);
264
265         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, 0, 0);
266                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
267         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", LEFTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
268                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_PARENT);
269                 RNA_boolean_set(kmi->ptr, "extend", 1);
270         
271         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
272                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
273         kmi= WM_keymap_add_item(keymap, "POSE_OT_select_hierarchy", RIGHTBRACKETKEY, KM_PRESS, KM_SHIFT, 0);
274                 RNA_enum_set(kmi->ptr, "direction", BONE_SELECT_CHILD);
275                 RNA_boolean_set(kmi->ptr, "extend", 1);
276
277         WM_keymap_add_item(keymap, "POSE_OT_select_linked", LKEY, KM_PRESS, 0, 0);
278         
279         WM_keymap_add_item(keymap, "POSE_OT_constraint_add_with_targets", CKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
280         WM_keymap_add_item(keymap, "POSE_OT_constraints_clear", CKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
281         WM_keymap_add_item(keymap, "POSE_OT_ik_add", IKEY, KM_PRESS, /*KM_CTRL|*/KM_SHIFT, 0);
282         WM_keymap_add_item(keymap, "POSE_OT_ik_clear", IKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
283         
284         WM_keymap_add_item(keymap, "POSE_OT_groups_menu", GKEY, KM_PRESS, KM_CTRL, 0);
285         
286         // XXX this should probably be in screen instead... here for testing purposes in the meantime... - Aligorith
287         WM_keymap_verify_item(keymap, "ANIM_OT_insert_keyframe_menu", IKEY, KM_PRESS, 0, 0);
288         WM_keymap_verify_item(keymap, "ANIM_OT_delete_keyframe_v3d", IKEY, KM_PRESS, KM_ALT, 0);
289         
290         /* Pose -> PoseLib ------------- */
291         /* only set in posemode, by space_view3d listener */
292         WM_keymap_add_item(keymap, "POSELIB_OT_browse_interactive", LKEY, KM_PRESS, KM_CTRL, 0);
293         
294         WM_keymap_add_item(keymap, "POSELIB_OT_pose_add", LKEY, KM_PRESS, KM_SHIFT, 0);
295         WM_keymap_add_item(keymap, "POSELIB_OT_pose_remove", LKEY, KM_PRESS, KM_ALT, 0);
296         WM_keymap_add_item(keymap, "POSELIB_OT_pose_rename", LKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
297 }
298