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