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