38e12c46060ded60a3c73f1d8588a9b6ae85495e
[blender.git] / source / blender / editors / space_nla / nla_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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung (major recode)
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_nla/nla_ops.c
30  *  \ingroup spnla
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "DNA_scene_types.h"
38
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_context.h"
45 #include "BKE_screen.h"
46
47 #include "ED_anim_api.h"
48 #include "ED_markers.h"
49 #include "ED_screen.h"
50 #include "ED_transform.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "RNA_access.h"
56
57
58 #include "nla_intern.h" // own include
59
60 /* ************************** poll callbacks for operators **********************************/
61
62 /* tweakmode is NOT enabled */
63 int nlaop_poll_tweakmode_off (bContext *C)
64 {
65         Scene *scene;
66         
67         /* for now, we check 2 things: 
68          *      1) active editor must be NLA
69          *      2) tweakmode is currently set as a 'per-scene' flag 
70          *         so that it will affect entire NLA data-sets,
71          *         but not all AnimData blocks will be in tweakmode for 
72          *         various reasons
73          */
74         if (ED_operator_nla_active(C) == 0)
75                 return 0;
76         
77         scene= CTX_data_scene(C);
78         if ((scene == NULL) || (scene->flag & SCE_NLA_EDIT_ON))
79                 return 0;
80         
81         return 1;
82 }
83
84 /* tweakmode IS enabled */
85 int nlaop_poll_tweakmode_on (bContext *C)
86 {
87         Scene *scene;
88         
89         /* for now, we check 2 things: 
90          *      1) active editor must be NLA
91          *      2) tweakmode is currently set as a 'per-scene' flag 
92          *         so that it will affect entire NLA data-sets,
93          *         but not all AnimData blocks will be in tweakmode for 
94          *         various reasons
95          */
96         if (ED_operator_nla_active(C) == 0)
97                 return 0;
98         
99         scene= CTX_data_scene(C);
100         if ((scene == NULL) || !(scene->flag & SCE_NLA_EDIT_ON))
101                 return 0;
102         
103         return 1;
104 }
105
106 /* is tweakmode enabled - for use in NLA operator code */
107 short nlaedit_is_tweakmode_on (bAnimContext *ac)
108 {
109         if (ac && ac->scene)
110                 return (ac->scene->flag & SCE_NLA_EDIT_ON);
111         return 0;
112 }
113
114 /* ************************** registration - operator types **********************************/
115
116 void nla_operatortypes(void)
117 {
118         /* view */
119         WM_operatortype_append(NLA_OT_properties);
120         
121         /* channels */
122         WM_operatortype_append(NLA_OT_channels_click);
123         
124         WM_operatortype_append(NLA_OT_tracks_add);
125         WM_operatortype_append(NLA_OT_delete_tracks);
126         
127         /* select */
128         WM_operatortype_append(NLA_OT_click_select);
129         WM_operatortype_append(NLA_OT_select_border);
130         WM_operatortype_append(NLA_OT_select_all_toggle);
131         WM_operatortype_append(NLA_OT_select_leftright);
132         
133         /* view */
134         WM_operatortype_append(NLA_OT_view_all);
135         WM_operatortype_append(NLA_OT_view_selected);
136         
137         /* edit */
138         WM_operatortype_append(NLA_OT_tweakmode_enter);
139         WM_operatortype_append(NLA_OT_tweakmode_exit);
140         
141         WM_operatortype_append(NLA_OT_actionclip_add);
142         WM_operatortype_append(NLA_OT_transition_add);
143         
144         WM_operatortype_append(NLA_OT_meta_add);
145         WM_operatortype_append(NLA_OT_meta_remove);
146         
147         WM_operatortype_append(NLA_OT_duplicate);
148         WM_operatortype_append(NLA_OT_delete);
149         WM_operatortype_append(NLA_OT_split);
150         
151         WM_operatortype_append(NLA_OT_mute_toggle);
152         
153         WM_operatortype_append(NLA_OT_swap);
154         WM_operatortype_append(NLA_OT_move_up);
155         WM_operatortype_append(NLA_OT_move_down);
156         
157         WM_operatortype_append(NLA_OT_action_sync_length);
158         
159         WM_operatortype_append(NLA_OT_apply_scale);
160         WM_operatortype_append(NLA_OT_clear_scale);
161         
162         WM_operatortype_append(NLA_OT_snap);
163         
164         WM_operatortype_append(NLA_OT_fmodifier_add);
165         WM_operatortype_append(NLA_OT_fmodifier_copy);
166         WM_operatortype_append(NLA_OT_fmodifier_paste);
167 }
168
169 /* ************************** registration - keymaps **********************************/
170
171 static void nla_keymap_channels(wmKeyMap *keymap)
172 {
173         /* NLA-specific (different to standard channels keymap) -------------------------- */
174         /* selection */
175                 /* click-select */
176                 // XXX for now, only leftmouse.... 
177         WM_keymap_add_item(keymap, "NLA_OT_channels_click", LEFTMOUSE, KM_PRESS, 0, 0);
178         RNA_boolean_set(WM_keymap_add_item(keymap, "NLA_OT_channels_click", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "extend", 1);
179         
180         /* channel operations */
181                 /* add tracks */
182         WM_keymap_add_item(keymap, "NLA_OT_tracks_add", AKEY, KM_PRESS, KM_SHIFT, 0);
183         RNA_boolean_set(WM_keymap_add_item(keymap, "NLA_OT_tracks_add", AKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "above_selected", 1);
184         
185                 /* delete tracks */
186         WM_keymap_add_item(keymap, "NLA_OT_delete_tracks", XKEY, KM_PRESS, 0, 0);
187         WM_keymap_add_item(keymap, "NLA_OT_delete_tracks", DELKEY, KM_PRESS, 0, 0);
188 }
189
190 static void nla_keymap_main (wmKeyConfig *keyconf, wmKeyMap *keymap)
191 {
192         wmKeyMapItem *kmi;
193         
194         /* selection */
195                 /* click select */
196         WM_keymap_add_item(keymap, "NLA_OT_click_select", SELECTMOUSE, KM_PRESS, 0, 0);
197         kmi= WM_keymap_add_item(keymap, "NLA_OT_click_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
198                 RNA_boolean_set(kmi->ptr, "extend", 1);
199                 
200                 /* select left/right */
201         WM_keymap_add_item(keymap, "NLA_OT_select_leftright", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
202         kmi= WM_keymap_add_item(keymap, "NLA_OT_select_leftright", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
203                 RNA_boolean_set(kmi->ptr, "extend", 1);
204         
205         kmi= WM_keymap_add_item(keymap, "NLA_OT_select_leftright", LEFTBRACKETKEY, KM_PRESS, 0, 0);
206                 RNA_enum_set(kmi->ptr, "mode", NLAEDIT_LRSEL_LEFT);
207         kmi= WM_keymap_add_item(keymap, "NLA_OT_select_leftright", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
208                 RNA_enum_set(kmi->ptr, "mode", NLAEDIT_LRSEL_RIGHT);
209                 
210         
211                 /* deselect all */
212         WM_keymap_add_item(keymap, "NLA_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
213         RNA_boolean_set(WM_keymap_add_item(keymap, "NLA_OT_select_all_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "invert", 1);
214         
215                 /* borderselect */
216         WM_keymap_add_item(keymap, "NLA_OT_select_border", BKEY, KM_PRESS, 0, 0);
217         RNA_boolean_set(WM_keymap_add_item(keymap, "NLA_OT_select_border", BKEY, KM_PRESS, KM_ALT, 0)->ptr, "axis_range", 1);
218         
219         /* view*/
220                 /* auto-set range */
221         //WM_keymap_add_item(keymap, "NLA_OT_previewrange_set", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
222         WM_keymap_add_item(keymap, "NLA_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
223         WM_keymap_add_item(keymap, "NLA_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
224         
225         /* editing */
226                 /* tweakmode 
227                  *      - enter and exit are separate operators with the same hotkey... 
228                  *        This works as they use different poll()'s
229                  */
230         WM_keymap_add_item(keymap, "NLA_OT_tweakmode_enter", TABKEY, KM_PRESS, 0, 0);
231         WM_keymap_add_item(keymap, "NLA_OT_tweakmode_exit", TABKEY, KM_PRESS, 0, 0);
232                 
233                 /* add strips */
234         WM_keymap_add_item(keymap, "NLA_OT_actionclip_add", AKEY, KM_PRESS, KM_SHIFT, 0);
235         WM_keymap_add_item(keymap, "NLA_OT_transition_add", TKEY, KM_PRESS, KM_SHIFT, 0);
236         
237                 /* meta-strips */
238         WM_keymap_add_item(keymap, "NLA_OT_meta_add", GKEY, KM_PRESS, KM_SHIFT, 0);
239         WM_keymap_add_item(keymap, "NLA_OT_meta_remove", GKEY, KM_PRESS, KM_ALT, 0);
240                 
241                 /* duplicate */
242         WM_keymap_add_item(keymap, "NLA_OT_duplicate", DKEY, KM_PRESS, KM_SHIFT, 0);    
243                 
244                 /* delete */
245         WM_keymap_add_item(keymap, "NLA_OT_delete", XKEY, KM_PRESS, 0, 0);
246         WM_keymap_add_item(keymap, "NLA_OT_delete", DELKEY, KM_PRESS, 0, 0);
247
248                 /* split */
249         WM_keymap_add_item(keymap, "NLA_OT_split", YKEY, KM_PRESS, 0, 0);
250         
251                 /* toggles */
252         WM_keymap_add_item(keymap, "NLA_OT_mute_toggle", HKEY, KM_PRESS, 0, 0);
253         
254                 /* swap */
255         WM_keymap_add_item(keymap, "NLA_OT_swap", FKEY, KM_PRESS, KM_ALT, 0);
256                 
257                 /* move up */
258         WM_keymap_add_item(keymap, "NLA_OT_move_up", PAGEUPKEY, KM_PRESS, 0, 0);
259                 /* move down */
260         WM_keymap_add_item(keymap, "NLA_OT_move_down", PAGEDOWNKEY, KM_PRESS, 0, 0);
261         
262                 /* apply scale */
263         WM_keymap_add_item(keymap, "NLA_OT_apply_scale", AKEY, KM_PRESS, KM_CTRL, 0);
264                 /* clear scale */
265         WM_keymap_add_item(keymap, "NLA_OT_clear_scale", SKEY, KM_PRESS, KM_ALT, 0);
266         
267                 /* snap */
268         WM_keymap_add_item(keymap, "NLA_OT_snap", SKEY, KM_PRESS, KM_SHIFT, 0);
269         
270                 /* add f-modifier */
271         WM_keymap_add_item(keymap, "NLA_OT_fmodifier_add", MKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
272         
273         /* transform system */
274         transform_keymap_for_space(keyconf, keymap, SPACE_NLA);
275         
276         /* special markers hotkeys for anim editors: see note in definition of this function */
277         ED_marker_keymap_animedit_conflictfree(keymap);
278 }
279
280 /* --------------- */
281
282 void nla_keymap(wmKeyConfig *keyconf)
283 {
284         wmKeyMap *keymap;
285         
286         /* keymap for all regions */
287         keymap= WM_keymap_find(keyconf, "NLA Generic", SPACE_NLA, 0);
288         WM_keymap_add_item(keymap, "NLA_OT_properties", NKEY, KM_PRESS, 0, 0);
289         
290         /* channels */
291         /* Channels are not directly handled by the NLA Editor module, but are inherited from the Animation module. 
292          * Most of the relevant operations, keymaps, drawing, etc. can therefore all be found in that module instead, as there
293          * are many similarities with the other Animation Editors.
294          *
295          * However, those operations which involve clicking on channels and/or the placement of them in the view are implemented here instead
296          */
297         keymap= WM_keymap_find(keyconf, "NLA Channels", SPACE_NLA, 0);
298         nla_keymap_channels(keymap);
299         
300         /* data */
301         keymap= WM_keymap_find(keyconf, "NLA Editor", SPACE_NLA, 0);
302         nla_keymap_main(keyconf, keymap);
303 }
304