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