4bb65ec4ed47823c3d1068c2115be617b65abcdb
[blender-staging.git] / source / blender / editors / space_view3d / view3d_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) 2008 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
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_screen.h"
56 #include "ED_transform.h"
57
58 #include "view3d_intern.h"
59
60
61 /* ************************** registration **********************************/
62
63 void view3d_operatortypes(void)
64 {
65         WM_operatortype_append(VIEW3D_OT_rotate);
66         WM_operatortype_append(VIEW3D_OT_move);
67         WM_operatortype_append(VIEW3D_OT_zoom);
68         WM_operatortype_append(VIEW3D_OT_view_all);
69         WM_operatortype_append(VIEW3D_OT_viewnumpad);
70         WM_operatortype_append(VIEW3D_OT_view_orbit);
71         WM_operatortype_append(VIEW3D_OT_view_pan);
72         WM_operatortype_append(VIEW3D_OT_view_persportho);
73         WM_operatortype_append(VIEW3D_OT_view_center);
74         WM_operatortype_append(VIEW3D_OT_view_center_cursor);
75         WM_operatortype_append(VIEW3D_OT_select);
76         WM_operatortype_append(VIEW3D_OT_select_border);
77         WM_operatortype_append(VIEW3D_OT_clip_border);
78         WM_operatortype_append(VIEW3D_OT_select_circle);
79         WM_operatortype_append(VIEW3D_OT_smoothview);
80         WM_operatortype_append(VIEW3D_OT_render_border);
81         WM_operatortype_append(VIEW3D_OT_zoom_border);
82         WM_operatortype_append(VIEW3D_OT_manipulator);
83         WM_operatortype_append(VIEW3D_OT_cursor3d);
84         WM_operatortype_append(VIEW3D_OT_select_lasso);
85         WM_operatortype_append(VIEW3D_OT_setcameratoview);
86         WM_operatortype_append(VIEW3D_OT_setobjectascamera);
87         WM_operatortype_append(VIEW3D_OT_localview);
88         WM_operatortype_append(VIEW3D_OT_game_start);
89         WM_operatortype_append(VIEW3D_OT_fly);
90         WM_operatortype_append(VIEW3D_OT_layers);
91         
92         WM_operatortype_append(VIEW3D_OT_properties);
93         WM_operatortype_append(VIEW3D_OT_toolbar);
94         
95         WM_operatortype_append(VIEW3D_OT_snap_selected_to_grid);
96         WM_operatortype_append(VIEW3D_OT_snap_selected_to_cursor);
97         WM_operatortype_append(VIEW3D_OT_snap_selected_to_center);
98         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_grid);
99         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_selected);
100         WM_operatortype_append(VIEW3D_OT_snap_cursor_to_active);
101         WM_operatortype_append(VIEW3D_OT_snap_menu);
102                 
103         transform_operatortypes();
104 }
105
106 void view3d_keymap(wmKeyConfig *keyconf)
107 {
108         wmKeyMap *keymap;
109         wmKeyMapItem *kmi;
110         
111         keymap= WM_keymap_find(keyconf, "3D View Generic", SPACE_VIEW3D, 0);
112         
113         WM_keymap_add_item(keymap, "VIEW3D_OT_properties", NKEY, KM_PRESS, 0, 0);
114         WM_keymap_add_item(keymap, "VIEW3D_OT_toolbar", TKEY, KM_PRESS, 0, 0);
115         
116         /* only for region 3D window */
117         keymap= WM_keymap_find(keyconf, "3D View", SPACE_VIEW3D, 0);
118         
119         WM_keymap_verify_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_ANY, 0);
120         /*
121          * Doesn't work with KM_SHIFT, have to use KM_ANY and filter in invoke
122          * */
123         // WM_keymap_verify_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
124         
125         WM_keymap_verify_item(keymap, "VIEW3D_OT_cursor3d", ACTIONMOUSE, KM_PRESS, 0, 0);
126         
127         WM_keymap_verify_item(keymap, "VIEW3D_OT_rotate", MIDDLEMOUSE, KM_PRESS, 0, 0);
128         WM_keymap_verify_item(keymap, "VIEW3D_OT_move", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
129         WM_keymap_verify_item(keymap, "VIEW3D_OT_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
130         WM_keymap_verify_item(keymap, "VIEW3D_OT_view_center", PADPERIOD, KM_PRESS, 0, 0);
131         WM_keymap_verify_item(keymap, "VIEW3D_OT_view_center_cursor", PADPERIOD, KM_PRESS, KM_CTRL, 0);
132         
133         WM_keymap_verify_item(keymap, "VIEW3D_OT_fly", FKEY, KM_PRESS, KM_SHIFT, 0);
134
135         WM_keymap_verify_item(keymap, "VIEW3D_OT_smoothview", TIMER1, KM_ANY, KM_ANY, 0);
136         
137         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_zoom", PADPLUSKEY, KM_PRESS, 0, 0)->ptr, "delta", 1);
138         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_zoom", PADMINUS, KM_PRESS, 0, 0)->ptr, "delta", -1);
139         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_zoom", WHEELINMOUSE, KM_PRESS, 0, 0)->ptr, "delta", 1);
140         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_zoom", WHEELOUTMOUSE, KM_PRESS, 0, 0)->ptr, "delta", -1);
141
142         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_all", HOMEKEY, KM_PRESS, 0, 0)->ptr, "center", 0);
143         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_all", CKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "center", 1);
144
145         /* numpad view hotkeys*/
146         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD0, KM_PRESS, 0, 0)->ptr, "type", RV3D_VIEW_CAMERA);
147         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD1, KM_PRESS, 0, 0)->ptr, "type", RV3D_VIEW_FRONT);
148         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_orbit", PAD2, KM_PRESS, 0, 0)->ptr, "type", V3D_VIEW_STEPDOWN);
149         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD3, KM_PRESS, 0, 0)->ptr, "type", RV3D_VIEW_RIGHT);
150         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_orbit", PAD4, KM_PRESS, 0, 0)->ptr, "type", V3D_VIEW_STEPLEFT);
151         WM_keymap_add_item(keymap, "VIEW3D_OT_view_persportho", PAD5, KM_PRESS, 0, 0);
152         
153         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_orbit", PAD6, KM_PRESS, 0, 0)->ptr, "type", V3D_VIEW_STEPRIGHT);
154         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD7, KM_PRESS, 0, 0)->ptr, "type", RV3D_VIEW_TOP);
155         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_orbit", PAD8, KM_PRESS, 0, 0)->ptr, "type", V3D_VIEW_STEPUP);
156         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD1, KM_PRESS, KM_CTRL, 0)->ptr, "type", RV3D_VIEW_BACK);
157         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD3, KM_PRESS, KM_CTRL, 0)->ptr, "type", RV3D_VIEW_LEFT);
158         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD7, KM_PRESS, KM_CTRL, 0)->ptr, "type", RV3D_VIEW_BOTTOM);
159         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_pan", PAD2, KM_PRESS, KM_CTRL, 0)->ptr, "type", V3D_VIEW_PANDOWN);
160         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_pan", PAD4, KM_PRESS, KM_CTRL, 0)->ptr, "type", V3D_VIEW_PANLEFT);
161         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_pan", PAD6, KM_PRESS, KM_CTRL, 0)->ptr, "type", V3D_VIEW_PANRIGHT);
162         RNA_enum_set(WM_keymap_add_item(keymap, "VIEW3D_OT_view_pan", PAD8, KM_PRESS, KM_CTRL, 0)->ptr, "type", V3D_VIEW_PANUP);
163
164     /* active aligned, replaces '*' key in 2.4x */
165     kmi= WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD1, KM_PRESS, KM_SHIFT, 0);
166     RNA_enum_set(kmi->ptr, "type", RV3D_VIEW_FRONT);
167     RNA_boolean_set(kmi->ptr, "align_active", TRUE);
168     kmi= WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD3, KM_PRESS, KM_SHIFT, 0);
169     RNA_enum_set(kmi->ptr, "type", RV3D_VIEW_RIGHT);
170     RNA_boolean_set(kmi->ptr, "align_active", TRUE);
171     kmi= WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD7, KM_PRESS, KM_SHIFT, 0);
172     RNA_enum_set(kmi->ptr, "type", RV3D_VIEW_TOP);
173     RNA_boolean_set(kmi->ptr, "align_active", TRUE);
174     kmi= WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD1, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
175     RNA_enum_set(kmi->ptr, "type", RV3D_VIEW_BACK);
176     RNA_boolean_set(kmi->ptr, "align_active", TRUE);
177     kmi= WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD3, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
178     RNA_enum_set(kmi->ptr, "type", RV3D_VIEW_LEFT);
179     RNA_boolean_set(kmi->ptr, "align_active", TRUE);
180     kmi= WM_keymap_add_item(keymap, "VIEW3D_OT_viewnumpad", PAD7, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
181     RNA_enum_set(kmi->ptr, "type", RV3D_VIEW_BOTTOM);
182     RNA_boolean_set(kmi->ptr, "align_active", TRUE);
183
184         WM_keymap_add_item(keymap, "VIEW3D_OT_localview", PADSLASHKEY, KM_PRESS, 0, 0);
185         
186         /* layers, shift + alt are properties set in invoke() */
187         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", ACCENTGRAVEKEY, KM_PRESS, 0, 0)->ptr, "nr", 0);
188         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", ONEKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 1);
189         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", TWOKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 2);
190         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", THREEKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 3);
191         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", FOURKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 4);
192         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", FIVEKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 5);
193         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", SIXKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 6);
194         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", SEVENKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 7);
195         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", EIGHTKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 8);
196         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", NINEKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 9);
197         RNA_int_set(WM_keymap_add_item(keymap, "VIEW3D_OT_layers", ZEROKEY, KM_PRESS, KM_ANY, 0)->ptr, "nr", 10);
198         
199         /* drawtype */
200
201         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle_enum", ZKEY, KM_PRESS, 0, 0);
202         RNA_string_set(kmi->ptr, "path", "space_data.viewport_shading");
203         RNA_string_set(kmi->ptr, "value_1", "SOLID");
204         RNA_string_set(kmi->ptr, "value_2", "WIREFRAME");
205
206         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle_enum", ZKEY, KM_PRESS, KM_ALT, 0);
207         RNA_string_set(kmi->ptr, "path", "space_data.viewport_shading");
208         RNA_string_set(kmi->ptr, "value_1", "TEXTURED");
209         RNA_string_set(kmi->ptr, "value_2", "SOLID");
210
211         /* selection*/
212         WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
213         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", TRUE);
214         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL, 0)->ptr, "center", TRUE);
215         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0)->ptr, "enumerate", TRUE);
216
217         /* selection key-combinations */
218         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
219         RNA_boolean_set(kmi->ptr, "center", TRUE);
220         RNA_boolean_set(kmi->ptr, "extend", TRUE);
221         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT, 0);
222         RNA_boolean_set(kmi->ptr, "center", TRUE);
223         RNA_boolean_set(kmi->ptr, "enumerate", TRUE);
224         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_ALT, 0);
225         RNA_boolean_set(kmi->ptr, "extend", TRUE);
226         RNA_boolean_set(kmi->ptr, "enumerate", TRUE);
227         kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT|KM_CTRL|KM_ALT, 0);
228         RNA_boolean_set(kmi->ptr, "center", TRUE);
229         RNA_boolean_set(kmi->ptr, "extend", TRUE);
230         RNA_boolean_set(kmi->ptr, "enumerate", TRUE);
231
232         WM_keymap_add_item(keymap, "VIEW3D_OT_select_border", BKEY, KM_PRESS, 0, 0);
233         WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0);
234         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW3D_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT|KM_CTRL, 0)->ptr, "deselect", 1);
235         WM_keymap_add_item(keymap, "VIEW3D_OT_select_circle", CKEY, KM_PRESS, 0, 0);
236         
237         WM_keymap_add_item(keymap, "VIEW3D_OT_clip_border", BKEY, KM_PRESS, KM_ALT, 0);
238         WM_keymap_add_item(keymap, "VIEW3D_OT_zoom_border", BKEY, KM_PRESS, KM_SHIFT, 0);
239         WM_keymap_add_item(keymap, "VIEW3D_OT_render_border", BKEY, KM_PRESS, KM_SHIFT, 0);
240         
241         WM_keymap_add_item(keymap, "VIEW3D_OT_camera_to_view", PAD0, KM_PRESS, KM_ALT|KM_CTRL, 0);
242         WM_keymap_add_item(keymap, "VIEW3D_OT_object_as_camera", PAD0, KM_PRESS, KM_CTRL, 0);
243         
244         WM_keymap_add_item(keymap, "VIEW3D_OT_snap_menu", SKEY, KM_PRESS, KM_SHIFT, 0);
245
246         /* context ops */
247         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, 0, 0);
248         RNA_string_set(kmi->ptr, "path", "space_data.pivot_point");
249         RNA_string_set(kmi->ptr, "value", "BOUNDING_BOX_CENTER");
250
251         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", COMMAKEY, KM_PRESS, KM_CTRL, 0); /* 2.4x allowed Comma+Shift too, rather not use both */
252         RNA_string_set(kmi->ptr, "path", "space_data.pivot_point");
253         RNA_string_set(kmi->ptr, "value", "MEDIAN_POINT");
254
255         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", COMMAKEY, KM_PRESS, KM_ALT, 0); /* new in 2.5 */
256         RNA_string_set(kmi->ptr, "path", "space_data.pivot_point_align");
257
258         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SPACEKEY, KM_PRESS, KM_CTRL, 0); /* new in 2.5 */
259         RNA_string_set(kmi->ptr, "path", "space_data.manipulator");
260
261         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, 0, 0);
262         RNA_string_set(kmi->ptr, "path", "space_data.pivot_point");
263         RNA_string_set(kmi->ptr, "value", "CURSOR");
264
265         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, KM_CTRL, 0);
266         RNA_string_set(kmi->ptr, "path", "space_data.pivot_point");
267         RNA_string_set(kmi->ptr, "value", "INDIVIDUAL_ORIGINS");
268
269         kmi = WM_keymap_add_item(keymap, "WM_OT_context_set_enum", PERIODKEY, KM_PRESS, KM_ALT, 0);
270         RNA_string_set(kmi->ptr, "path", "space_data.pivot_point");
271         RNA_string_set(kmi->ptr, "value", "ACTIVE_ELEMENT");
272
273         transform_keymap_for_space(keyconf, keymap, SPACE_VIEW3D);
274
275         fly_modal_keymap(keyconf);
276         viewrotate_modal_keymap(keyconf);
277         viewmove_modal_keymap(keyconf);
278         viewzoom_modal_keymap(keyconf);
279 }
280