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