Merging with trunk up to r38631.
[blender.git] / source / blender / editors / space_graph / graph_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) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_graph/graph_ops.c
30  *  \ingroup spgraph
31  */
32
33
34 #include <stdlib.h>
35 #include <math.h>
36
37 #include "DNA_scene_types.h"
38 #include "DNA_anim_types.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42
43 #include "BKE_context.h"
44 #include "BKE_sound.h"
45
46 #include "UI_view2d.h"
47
48 #include "ED_anim_api.h"
49 #include "ED_markers.h"
50 #include "ED_screen.h"
51 #include "ED_transform.h"
52
53 #include "graph_intern.h"
54
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 /* ************************** view-based operators **********************************/
62 // XXX should these really be here?
63
64 /* Set Cursor --------------------------------------------------------------------- */
65 /* The 'cursor' in the Graph Editor consists of two parts:
66  *      1) Current Frame Indicator (as per ANIM_OT_change_frame)
67  *      2) Value Indicator (stored per Graph Editor instance)
68  */
69
70 /* Set the new frame number */
71 static void graphview_cursor_apply(bContext *C, wmOperator *op)
72 {
73         Scene *scene= CTX_data_scene(C);
74         SpaceIpo *sipo= CTX_wm_space_graph(C);
75         
76         /* adjust the frame 
77          * NOTE: sync this part of the code with ANIM_OT_change_frame
78          */
79         CFRA= RNA_int_get(op->ptr, "frame");
80         SUBFRA=0.f;
81         sound_seek_scene(C);
82         
83         /* set the cursor value */
84         sipo->cursorVal= RNA_float_get(op->ptr, "value");
85         
86         /* send notifiers - notifiers for frame should force an update for both vars ok... */
87         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
88 }
89
90 /* ... */
91
92 /* Non-modal callback for running operator without user input */
93 static int graphview_cursor_exec(bContext *C, wmOperator *op)
94 {
95         graphview_cursor_apply(C, op);
96         return OPERATOR_FINISHED;
97 }
98
99 /* ... */
100
101 /* set the operator properties from the initial event */
102 static void graphview_cursor_setprops(bContext *C, wmOperator *op, wmEvent *event)
103 {
104         ARegion *ar= CTX_wm_region(C);
105         float viewx, viewy;
106
107         /* abort if not active region (should not really be possible) */
108         if (ar == NULL)
109                 return;
110
111         /* convert from region coordinates to View2D 'tot' space */
112         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &viewx, &viewy);
113         
114         /* store the values in the operator properties */
115                 /* frame is rounded to the nearest int, since frames are ints */
116         RNA_int_set(op->ptr, "frame", (int)floor(viewx+0.5f));
117         RNA_float_set(op->ptr, "value", viewy);
118 }
119
120 /* Modal Operator init */
121 static int graphview_cursor_invoke(bContext *C, wmOperator *op, wmEvent *event)
122 {
123         /* Change to frame that mouse is over before adding modal handler,
124          * as user could click on a single frame (jump to frame) as well as
125          * click-dragging over a range (modal scrubbing).
126          */
127         graphview_cursor_setprops(C, op, event);
128         
129         /* apply these changes first */
130         graphview_cursor_apply(C, op);
131         
132         /* add temp handler */
133         WM_event_add_modal_handler(C, op);
134         return OPERATOR_RUNNING_MODAL;
135 }
136
137 /* Modal event handling of cursor changing */
138 static int graphview_cursor_modal(bContext *C, wmOperator *op, wmEvent *event)
139 {
140         /* execute the events */
141         switch (event->type) {
142                 case ESCKEY:
143                         return OPERATOR_FINISHED;
144                 
145                 case MOUSEMOVE:
146                         /* set the new values */
147                         graphview_cursor_setprops(C, op, event);
148                         graphview_cursor_apply(C, op);
149                         break;
150                 
151                 case LEFTMOUSE: 
152                 case RIGHTMOUSE:
153                         /* we check for either mouse-button to end, as checking for ACTIONMOUSE (which is used to init 
154                          * the modal op) doesn't work for some reason
155                          */
156                         if (event->val==KM_RELEASE)
157                                 return OPERATOR_FINISHED;
158                         break;
159         }
160
161         return OPERATOR_RUNNING_MODAL;
162 }
163
164 static void GRAPH_OT_cursor_set(wmOperatorType *ot)
165 {
166         /* identifiers */
167         ot->name= "Set Cursor";
168         ot->idname= "GRAPH_OT_cursor_set";
169         ot->description= "Interactively set the current frame number and value cursor";
170         
171         /* api callbacks */
172         ot->exec= graphview_cursor_exec;
173         ot->invoke= graphview_cursor_invoke;
174         ot->modal= graphview_cursor_modal;
175         ot->poll= ED_operator_graphedit_active;
176         
177         /* flags */
178         ot->flag= OPTYPE_BLOCKING|OPTYPE_UNDO;
179
180         /* rna */
181         RNA_def_int(ot->srna, "frame", 0, MINAFRAME, MAXFRAME, "Frame", "", MINAFRAME, MAXFRAME);
182         RNA_def_float(ot->srna, "value", 0, FLT_MIN, FLT_MAX, "Value", "", -100.0f, 100.0f);
183 }
184
185 /* Toggle Handles ----------------------------------------------------------------- */
186
187 static int view_toggle_handles_exec (bContext *C, wmOperator *UNUSED(op))
188 {
189         SpaceIpo *sipo= CTX_wm_space_graph(C);
190         ARegion *ar= CTX_wm_region(C);
191         
192         if (sipo == NULL)
193                 return OPERATOR_CANCELLED;
194         
195         /* toggle flag to hide handles */
196         sipo->flag ^= SIPO_NOHANDLES;
197         
198         /* request refresh of keys area */
199         ED_region_tag_redraw(ar);
200         
201         return OPERATOR_FINISHED;
202 }
203
204 static void GRAPH_OT_view_togglehandles (wmOperatorType *ot)
205 {
206         /* identification */
207         ot->name= "Show/Hide All Handles";
208         ot->idname= "GRAPH_OT_handles_view_toggle";
209         ot->description= "Toggle whether handles are drawn on all keyframes that need them";
210         
211         /* callbacks */
212         ot->exec= view_toggle_handles_exec;
213         ot->poll= ED_operator_graphedit_active;
214 }
215
216 /* ************************** registration - operator types **********************************/
217
218 void graphedit_operatortypes(void)
219 {
220         /* view */
221         WM_operatortype_append(GRAPH_OT_view_togglehandles);
222         WM_operatortype_append(GRAPH_OT_cursor_set);
223         
224         WM_operatortype_append(GRAPH_OT_previewrange_set);
225         WM_operatortype_append(GRAPH_OT_view_all);
226         WM_operatortype_append(GRAPH_OT_view_selected);
227         WM_operatortype_append(GRAPH_OT_properties);
228         
229         WM_operatortype_append(GRAPH_OT_ghost_curves_create);
230         WM_operatortype_append(GRAPH_OT_ghost_curves_clear);
231         
232         /* keyframes */
233                 /* selection */
234         WM_operatortype_append(GRAPH_OT_clickselect);
235         WM_operatortype_append(GRAPH_OT_select_all_toggle);
236         WM_operatortype_append(GRAPH_OT_select_border);
237         WM_operatortype_append(GRAPH_OT_select_column);
238         WM_operatortype_append(GRAPH_OT_select_linked);
239         WM_operatortype_append(GRAPH_OT_select_more);
240         WM_operatortype_append(GRAPH_OT_select_less);
241         WM_operatortype_append(GRAPH_OT_select_leftright);
242         
243                 /* editing */
244         WM_operatortype_append(GRAPH_OT_snap);
245         WM_operatortype_append(GRAPH_OT_mirror);
246         WM_operatortype_append(GRAPH_OT_frame_jump);
247         WM_operatortype_append(GRAPH_OT_handle_type);
248         WM_operatortype_append(GRAPH_OT_interpolation_type);
249         WM_operatortype_append(GRAPH_OT_extrapolation_type);
250         WM_operatortype_append(GRAPH_OT_sample);
251         WM_operatortype_append(GRAPH_OT_bake);
252         WM_operatortype_append(GRAPH_OT_sound_bake);
253         WM_operatortype_append(GRAPH_OT_smooth);
254         WM_operatortype_append(GRAPH_OT_clean);
255         WM_operatortype_append(GRAPH_OT_euler_filter);
256         WM_operatortype_append(GRAPH_OT_delete);
257         WM_operatortype_append(GRAPH_OT_duplicate);
258         
259         WM_operatortype_append(GRAPH_OT_copy);
260         WM_operatortype_append(GRAPH_OT_paste);
261         
262         WM_operatortype_append(GRAPH_OT_keyframe_insert);
263         WM_operatortype_append(GRAPH_OT_click_insert);
264         
265         /* F-Curve Modifiers */
266         WM_operatortype_append(GRAPH_OT_fmodifier_add);
267         WM_operatortype_append(GRAPH_OT_fmodifier_copy);
268         WM_operatortype_append(GRAPH_OT_fmodifier_paste);
269 }
270
271 void ED_operatormacros_graph(void)
272 {
273         wmOperatorType *ot;
274         wmOperatorTypeMacro *otmacro;
275         
276         ot= WM_operatortype_append_macro("GRAPH_OT_duplicate_move", "Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
277         if (ot) {
278                 WM_operatortype_macro_define(ot, "GRAPH_OT_duplicate");
279                 otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_transform");
280                 RNA_enum_set(otmacro->ptr, "mode", TFM_TIME_DUPLICATE);
281         }
282 }
283
284
285 /* ************************** registration - keymaps **********************************/
286
287 static void graphedit_keymap_keyframes (wmKeyConfig *keyconf, wmKeyMap *keymap)
288 {
289         wmKeyMapItem *kmi;
290         
291         /* view */
292         WM_keymap_add_item(keymap, "GRAPH_OT_handles_view_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
293                 /* NOTE: 'ACTIONMOUSE' not 'LEFTMOUSE', as user may have swapped mouse-buttons
294                  * This keymap is supposed to override ANIM_OT_change_frame, which does the same except it doesn't do y-values
295                  */
296         WM_keymap_add_item(keymap, "GRAPH_OT_cursor_set", ACTIONMOUSE, KM_PRESS, 0, 0);
297         
298         
299         /* graph_select.c - selection tools */
300                 /* click-select */
301         WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, 0, 0);
302         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
303                 RNA_boolean_set(kmi->ptr, "column", 1);
304         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
305                 RNA_boolean_set(kmi->ptr, "extend", 1);
306         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_ALT|KM_SHIFT, 0);
307                 RNA_boolean_set(kmi->ptr, "extend", 1);
308                 RNA_boolean_set(kmi->ptr, "column", 1);
309         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT, 0);
310                 RNA_boolean_set(kmi->ptr, "curves", 1);
311         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT|KM_SHIFT, 0);
312                 RNA_boolean_set(kmi->ptr, "curves", 1);
313                 RNA_boolean_set(kmi->ptr, "extend", 1);
314         
315         /* select left/right */
316         WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
317         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
318                 RNA_boolean_set(kmi->ptr, "extend", 1);
319         
320         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", LEFTBRACKETKEY, KM_PRESS, 0, 0);
321                 RNA_enum_set(kmi->ptr, "mode", GRAPHKEYS_LRSEL_LEFT);
322         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
323                 RNA_enum_set(kmi->ptr, "mode", GRAPHKEYS_LRSEL_RIGHT);
324         
325                 /* deselect all */
326         WM_keymap_add_item(keymap, "GRAPH_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
327         RNA_boolean_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_all_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "invert", 1);
328         
329                 /* borderselect */
330         WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, 0, 0);
331         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_ALT, 0);
332                 RNA_boolean_set(kmi->ptr, "axis_range", 1);
333                 
334         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_CTRL, 0);
335                 RNA_boolean_set(kmi->ptr, "include_handles", 1);
336         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
337                 RNA_boolean_set(kmi->ptr, "axis_range", 1);
338                 RNA_boolean_set(kmi->ptr, "include_handles", 1);
339                 
340                 /* column select */
341         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, 0, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_KEYS);
342         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_CTRL, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_CFRA);
343         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_MARKERS_COLUMN);
344         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_ALT, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_MARKERS_BETWEEN);
345         
346                 /* select more/less */
347         WM_keymap_add_item(keymap, "GRAPH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
348         WM_keymap_add_item(keymap, "GRAPH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
349         
350                 /* select linked */
351         WM_keymap_add_item(keymap, "GRAPH_OT_select_linked", LKEY, KM_PRESS, 0, 0);
352         
353         
354         /* graph_edit.c */
355                 /* snap - current frame to selected keys */
356                 // TODO: maybe since this is called jump, we're better to have it on <something>-J?
357         WM_keymap_add_item(keymap, "GRAPH_OT_frame_jump", SKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
358                 
359                 /* menu + single-step transform */
360         WM_keymap_add_item(keymap, "GRAPH_OT_snap", SKEY, KM_PRESS, KM_SHIFT, 0);
361         WM_keymap_add_item(keymap, "GRAPH_OT_mirror", MKEY, KM_PRESS, KM_SHIFT, 0);
362         
363         WM_keymap_add_item(keymap, "GRAPH_OT_handle_type", VKEY, KM_PRESS, 0, 0);
364
365         WM_keymap_add_item(keymap, "GRAPH_OT_interpolation_type", TKEY, KM_PRESS, 0, 0);
366         
367                 /* destructive */
368         WM_keymap_add_item(keymap, "GRAPH_OT_clean", OKEY, KM_PRESS, 0, 0);
369         WM_keymap_add_item(keymap, "GRAPH_OT_smooth", OKEY, KM_PRESS, KM_ALT, 0);
370         WM_keymap_add_item(keymap, "GRAPH_OT_sample", OKEY, KM_PRESS, KM_SHIFT, 0);
371         
372         WM_keymap_add_item(keymap, "GRAPH_OT_bake", CKEY, KM_PRESS, KM_ALT, 0);
373         
374         WM_keymap_add_item(keymap, "GRAPH_OT_delete", XKEY, KM_PRESS, 0, 0);
375         WM_keymap_add_item(keymap, "GRAPH_OT_delete", DELKEY, KM_PRESS, 0, 0);
376         
377         WM_keymap_add_item(keymap, "GRAPH_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
378         
379                 /* insertkey */
380         WM_keymap_add_item(keymap, "GRAPH_OT_keyframe_insert", IKEY, KM_PRESS, 0, 0);
381         WM_keymap_add_item(keymap, "GRAPH_OT_click_insert", LEFTMOUSE, KM_CLICK, KM_CTRL, 0);
382         
383                 /* copy/paste */
384         WM_keymap_add_item(keymap, "GRAPH_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
385         WM_keymap_add_item(keymap, "GRAPH_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
386         
387                 /* auto-set range */
388         WM_keymap_add_item(keymap, "GRAPH_OT_previewrange_set", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
389         WM_keymap_add_item(keymap, "GRAPH_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
390         WM_keymap_add_item(keymap, "GRAPH_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
391         
392                 /* F-Modifiers */
393         RNA_boolean_set(WM_keymap_add_item(keymap, "GRAPH_OT_fmodifier_add", MKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "only_active", 0);
394         
395         /* animation module */
396                 /* channels list 
397                  * NOTE: these operators were originally for the channels list, but are added here too for convenience...
398                  */
399         WM_keymap_add_item(keymap, "ANIM_OT_channels_editable_toggle", TABKEY, KM_PRESS, 0, 0);
400         
401         /* transform system */
402         transform_keymap_for_space(keyconf, keymap, SPACE_IPO);
403         
404         /* special markers hotkeys for anim editors: see note in definition of this function */
405         ED_marker_keymap_animedit_conflictfree(keymap);
406 }
407
408 /* --------------- */
409
410 void graphedit_keymap(wmKeyConfig *keyconf)
411 {
412         wmKeyMap *keymap;
413         
414         /* keymap for all regions */
415         keymap= WM_keymap_find(keyconf, "Graph Editor Generic", SPACE_IPO, 0);
416         WM_keymap_add_item(keymap, "GRAPH_OT_properties", NKEY, KM_PRESS, 0, 0);
417                 /* extrapolation works on channels, not keys */
418         WM_keymap_add_item(keymap, "GRAPH_OT_extrapolation_type", EKEY, KM_PRESS, KM_SHIFT, 0);
419
420         /* channels */
421         /* Channels are not directly handled by the Graph Editor module, but are inherited from the Animation module. 
422          * All the relevant operations, keymaps, drawing, etc. can therefore all be found in that module instead, as these
423          * are all used for the Graph Editor too.
424          */
425         
426         /* keyframes */
427         keymap= WM_keymap_find(keyconf, "Graph Editor", SPACE_IPO, 0);
428         graphedit_keymap_keyframes(keyconf, keymap);
429 }
430