"Batch-Edit" Code cleanup: Renaming ED_operator_ipo_active to
[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 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 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         
239                 /* editing */
240         WM_operatortype_append(GRAPH_OT_snap);
241         WM_operatortype_append(GRAPH_OT_mirror);
242         WM_operatortype_append(GRAPH_OT_frame_jump);
243         WM_operatortype_append(GRAPH_OT_handle_type);
244         WM_operatortype_append(GRAPH_OT_interpolation_type);
245         WM_operatortype_append(GRAPH_OT_extrapolation_type);
246         WM_operatortype_append(GRAPH_OT_sample);
247         WM_operatortype_append(GRAPH_OT_bake);
248         WM_operatortype_append(GRAPH_OT_sound_bake);
249         WM_operatortype_append(GRAPH_OT_smooth);
250         WM_operatortype_append(GRAPH_OT_clean);
251         WM_operatortype_append(GRAPH_OT_delete);
252         WM_operatortype_append(GRAPH_OT_duplicate);
253         
254         WM_operatortype_append(GRAPH_OT_copy);
255         WM_operatortype_append(GRAPH_OT_paste);
256         
257         WM_operatortype_append(GRAPH_OT_keyframe_insert);
258         WM_operatortype_append(GRAPH_OT_click_insert);
259         
260         /* F-Curve Modifiers */
261         WM_operatortype_append(GRAPH_OT_fmodifier_add);
262         WM_operatortype_append(GRAPH_OT_fmodifier_copy);
263         WM_operatortype_append(GRAPH_OT_fmodifier_paste);
264 }
265
266 /* ************************** registration - keymaps **********************************/
267
268 static void graphedit_keymap_keyframes (wmKeyConfig *keyconf, wmKeyMap *keymap)
269 {
270         wmKeyMapItem *kmi;
271         
272         /* view */
273         WM_keymap_add_item(keymap, "GRAPH_OT_handles_view_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
274                 /* NOTE: 'ACTIONMOUSE' not 'LEFTMOUSE', as user may have swapped mouse-buttons
275                  * This keymap is supposed to override ANIM_OT_change_frame, which does the same except it doesn't do y-values
276                  */
277         WM_keymap_add_item(keymap, "GRAPH_OT_cursor_set", ACTIONMOUSE, KM_PRESS, 0, 0);
278         
279         
280         /* graph_select.c - selection tools */
281                 /* click-select */
282         WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, 0, 0);
283         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
284                 RNA_boolean_set(kmi->ptr, "column", 1);
285         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
286                 RNA_boolean_set(kmi->ptr, "extend", 1);
287         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_ALT|KM_SHIFT, 0);
288                 RNA_boolean_set(kmi->ptr, "extend", 1);
289                 RNA_boolean_set(kmi->ptr, "column", 1);
290         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
291                 RNA_enum_set(kmi->ptr, "left_right", GRAPHKEYS_LRSEL_TEST);
292         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT, 0);
293                 RNA_boolean_set(kmi->ptr, "curves", 1);
294         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT|KM_SHIFT, 0);
295                 RNA_boolean_set(kmi->ptr, "curves", 1);
296                 RNA_boolean_set(kmi->ptr, "extend", 1);
297         
298                 /* deselect all */
299         WM_keymap_add_item(keymap, "GRAPH_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
300         RNA_boolean_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_all_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "invert", 1);
301         
302                 /* borderselect */
303         WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, 0, 0);
304         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_ALT, 0);
305                 RNA_boolean_set(kmi->ptr, "axis_range", 1);
306                 
307         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_CTRL, 0);
308                 RNA_boolean_set(kmi->ptr, "include_handles", 1);
309         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
310                 RNA_boolean_set(kmi->ptr, "axis_range", 1);
311                 RNA_boolean_set(kmi->ptr, "include_handles", 1);
312                 
313                 /* column select */
314         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, 0, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_KEYS);
315         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_CTRL, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_CFRA);
316         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_MARKERS_COLUMN);
317         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_ALT, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_MARKERS_BETWEEN);
318         
319                 /* select more/less */
320         WM_keymap_add_item(keymap, "GRAPH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
321         WM_keymap_add_item(keymap, "GRAPH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
322         
323                 /* select linked */
324         WM_keymap_add_item(keymap, "GRAPH_OT_select_linked", LKEY, KM_PRESS, 0, 0);
325         
326         
327         /* graph_edit.c */
328                 /* snap - current frame to selected keys */
329                 // TODO: maybe since this is called jump, we're better to have it on <something>-J?
330         WM_keymap_add_item(keymap, "GRAPH_OT_frame_jump", SKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
331                 
332                 /* menu + single-step transform */
333         WM_keymap_add_item(keymap, "GRAPH_OT_snap", SKEY, KM_PRESS, KM_SHIFT, 0);
334         WM_keymap_add_item(keymap, "GRAPH_OT_mirror", MKEY, KM_PRESS, KM_SHIFT, 0);
335         
336         WM_keymap_add_item(keymap, "GRAPH_OT_handle_type", VKEY, KM_PRESS, 0, 0);
337
338         WM_keymap_add_item(keymap, "GRAPH_OT_interpolation_type", TKEY, KM_PRESS, KM_SHIFT, 0);
339         
340                 /* destructive */
341         WM_keymap_add_item(keymap, "GRAPH_OT_clean", OKEY, KM_PRESS, 0, 0);
342         WM_keymap_add_item(keymap, "GRAPH_OT_smooth", OKEY, KM_PRESS, KM_ALT, 0);
343         WM_keymap_add_item(keymap, "GRAPH_OT_sample", OKEY, KM_PRESS, KM_SHIFT, 0);
344         
345         WM_keymap_add_item(keymap, "GRAPH_OT_bake", CKEY, KM_PRESS, KM_ALT, 0);
346         
347         WM_keymap_add_item(keymap, "GRAPH_OT_delete", XKEY, KM_PRESS, 0, 0);
348         WM_keymap_add_item(keymap, "GRAPH_OT_delete", DELKEY, KM_PRESS, 0, 0);
349         
350         WM_keymap_add_item(keymap, "GRAPH_OT_duplicate", DKEY, KM_PRESS, KM_SHIFT, 0);
351         
352                 /* insertkey */
353         WM_keymap_add_item(keymap, "GRAPH_OT_keyframe_insert", IKEY, KM_PRESS, 0, 0);
354         WM_keymap_add_item(keymap, "GRAPH_OT_click_insert", LEFTMOUSE, KM_CLICK, KM_CTRL, 0);
355         
356                 /* copy/paste */
357         WM_keymap_add_item(keymap, "GRAPH_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
358         WM_keymap_add_item(keymap, "GRAPH_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
359         
360                 /* auto-set range */
361         WM_keymap_add_item(keymap, "GRAPH_OT_previewrange_set", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
362         WM_keymap_add_item(keymap, "GRAPH_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
363         
364                 /* F-Modifiers */
365         RNA_boolean_set(WM_keymap_add_item(keymap, "GRAPH_OT_fmodifier_add", MKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "only_active", 0);
366         
367         /* animation module */
368                 /* channels list 
369                  * NOTE: these operators were originally for the channels list, but are added here too for convenience...
370                  */
371         WM_keymap_add_item(keymap, "ANIM_OT_channels_editable_toggle", TABKEY, KM_PRESS, 0, 0);
372         
373         /* transform system */
374         transform_keymap_for_space(keyconf, keymap, SPACE_IPO);
375 }
376
377 /* --------------- */
378
379 void graphedit_keymap(wmKeyConfig *keyconf)
380 {
381         wmKeyMap *keymap;
382         
383         /* keymap for all regions */
384         keymap= WM_keymap_find(keyconf, "Graph Editor Generic", SPACE_IPO, 0);
385         WM_keymap_add_item(keymap, "GRAPH_OT_properties", NKEY, KM_PRESS, 0, 0);
386                 /* extrapolation works on channels, not keys */
387         WM_keymap_add_item(keymap, "GRAPH_OT_extrapolation_type", EKEY, KM_PRESS, KM_SHIFT, 0);
388
389         /* channels */
390         /* Channels are not directly handled by the Graph Editor module, but are inherited from the Animation module. 
391          * All the relevant operations, keymaps, drawing, etc. can therefore all be found in that module instead, as these
392          * are all used for the Graph Editor too.
393          */
394         
395         /* keyframes */
396         keymap= WM_keymap_find(keyconf, "Graph Editor", SPACE_IPO, 0);
397         graphedit_keymap_keyframes(keyconf, keymap);
398 }
399