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