Blender  V2.59
graph_ops.c
Go to the documentation of this file.
00001 /*
00002  * $Id: graph_ops.c 36790 2011-05-20 07:40:05Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2008 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * 
00024  * Contributor(s): Blender Foundation
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <stdlib.h>
00035 #include <math.h>
00036 
00037 #include "DNA_scene_types.h"
00038 #include "DNA_anim_types.h"
00039 
00040 #include "BLI_blenlib.h"
00041 #include "BLI_utildefines.h"
00042 
00043 #include "BKE_context.h"
00044 #include "BKE_sound.h"
00045 
00046 #include "UI_view2d.h"
00047 
00048 #include "ED_anim_api.h"
00049 #include "ED_screen.h"
00050 #include "ED_transform.h"
00051 
00052 #include "graph_intern.h"
00053 
00054 #include "RNA_access.h"
00055 #include "RNA_define.h"
00056 
00057 #include "WM_api.h"
00058 #include "WM_types.h"
00059 
00060 /* ************************** view-based operators **********************************/
00061 // XXX should these really be here?
00062 
00063 /* Set Cursor --------------------------------------------------------------------- */
00064 /* The 'cursor' in the Graph Editor consists of two parts:
00065  *      1) Current Frame Indicator (as per ANIM_OT_change_frame)
00066  *      2) Value Indicator (stored per Graph Editor instance)
00067  */
00068 
00069 /* Set the new frame number */
00070 static void graphview_cursor_apply(bContext *C, wmOperator *op)
00071 {
00072         Scene *scene= CTX_data_scene(C);
00073         SpaceIpo *sipo= CTX_wm_space_graph(C);
00074         
00075         /* adjust the frame 
00076          * NOTE: sync this part of the code with ANIM_OT_change_frame
00077          */
00078         CFRA= RNA_int_get(op->ptr, "frame");
00079         SUBFRA=0.f;
00080         sound_seek_scene(C);
00081         
00082         /* set the cursor value */
00083         sipo->cursorVal= RNA_float_get(op->ptr, "value");
00084         
00085         /* send notifiers - notifiers for frame should force an update for both vars ok... */
00086         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
00087 }
00088 
00089 /* ... */
00090 
00091 /* Non-modal callback for running operator without user input */
00092 static int graphview_cursor_exec(bContext *C, wmOperator *op)
00093 {
00094         graphview_cursor_apply(C, op);
00095         return OPERATOR_FINISHED;
00096 }
00097 
00098 /* ... */
00099 
00100 /* set the operator properties from the initial event */
00101 static void graphview_cursor_setprops(bContext *C, wmOperator *op, wmEvent *event)
00102 {
00103         ARegion *ar= CTX_wm_region(C);
00104         float viewx, viewy;
00105 
00106         /* abort if not active region (should not really be possible) */
00107         if (ar == NULL)
00108                 return;
00109 
00110         /* convert from region coordinates to View2D 'tot' space */
00111         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &viewx, &viewy);
00112         
00113         /* store the values in the operator properties */
00114                 /* frame is rounded to the nearest int, since frames are ints */
00115         RNA_int_set(op->ptr, "frame", (int)floor(viewx+0.5f));
00116         RNA_float_set(op->ptr, "value", viewy);
00117 }
00118 
00119 /* Modal Operator init */
00120 static int graphview_cursor_invoke(bContext *C, wmOperator *op, wmEvent *event)
00121 {
00122         /* Change to frame that mouse is over before adding modal handler,
00123          * as user could click on a single frame (jump to frame) as well as
00124          * click-dragging over a range (modal scrubbing).
00125          */
00126         graphview_cursor_setprops(C, op, event);
00127         
00128         /* apply these changes first */
00129         graphview_cursor_apply(C, op);
00130         
00131         /* add temp handler */
00132         WM_event_add_modal_handler(C, op);
00133         return OPERATOR_RUNNING_MODAL;
00134 }
00135 
00136 /* Modal event handling of cursor changing */
00137 static int graphview_cursor_modal(bContext *C, wmOperator *op, wmEvent *event)
00138 {
00139         /* execute the events */
00140         switch (event->type) {
00141                 case ESCKEY:
00142                         return OPERATOR_FINISHED;
00143                 
00144                 case MOUSEMOVE:
00145                         /* set the new values */
00146                         graphview_cursor_setprops(C, op, event);
00147                         graphview_cursor_apply(C, op);
00148                         break;
00149                 
00150                 case LEFTMOUSE: 
00151                 case RIGHTMOUSE:
00152                         /* we check for either mouse-button to end, as checking for ACTIONMOUSE (which is used to init 
00153                          * the modal op) doesn't work for some reason
00154                          */
00155                         if (event->val==KM_RELEASE)
00156                                 return OPERATOR_FINISHED;
00157                         break;
00158         }
00159 
00160         return OPERATOR_RUNNING_MODAL;
00161 }
00162 
00163 static void GRAPH_OT_cursor_set(wmOperatorType *ot)
00164 {
00165         /* identifiers */
00166         ot->name= "Set Cursor";
00167         ot->idname= "GRAPH_OT_cursor_set";
00168         ot->description= "Interactively set the current frame number and value cursor";
00169         
00170         /* api callbacks */
00171         ot->exec= graphview_cursor_exec;
00172         ot->invoke= graphview_cursor_invoke;
00173         ot->modal= graphview_cursor_modal;
00174         ot->poll= ED_operator_graphedit_active;
00175         
00176         /* flags */
00177         ot->flag= OPTYPE_BLOCKING|OPTYPE_UNDO;
00178 
00179         /* rna */
00180         RNA_def_int(ot->srna, "frame", 0, MINAFRAME, MAXFRAME, "Frame", "", MINAFRAME, MAXFRAME);
00181         RNA_def_float(ot->srna, "value", 0, FLT_MIN, FLT_MAX, "Value", "", -100.0f, 100.0f);
00182 }
00183 
00184 /* Toggle Handles ----------------------------------------------------------------- */
00185 
00186 static int view_toggle_handles_exec (bContext *C, wmOperator *UNUSED(op))
00187 {
00188         SpaceIpo *sipo= CTX_wm_space_graph(C);
00189         ARegion *ar= CTX_wm_region(C);
00190         
00191         if (sipo == NULL)
00192                 return OPERATOR_CANCELLED;
00193         
00194         /* toggle flag to hide handles */
00195         sipo->flag ^= SIPO_NOHANDLES;
00196         
00197         /* request refresh of keys area */
00198         ED_region_tag_redraw(ar);
00199         
00200         return OPERATOR_FINISHED;
00201 }
00202 
00203 static void GRAPH_OT_view_togglehandles (wmOperatorType *ot)
00204 {
00205         /* identification */
00206         ot->name= "Show/Hide All Handles";
00207         ot->idname= "GRAPH_OT_handles_view_toggle";
00208         ot->description= "Toggle whether handles are drawn on all keyframes that need them";
00209         
00210         /* callbacks */
00211         ot->exec= view_toggle_handles_exec;
00212         ot->poll= ED_operator_graphedit_active;
00213 }
00214 
00215 /* ************************** registration - operator types **********************************/
00216 
00217 void graphedit_operatortypes(void)
00218 {
00219         /* view */
00220         WM_operatortype_append(GRAPH_OT_view_togglehandles);
00221         WM_operatortype_append(GRAPH_OT_cursor_set);
00222         
00223         WM_operatortype_append(GRAPH_OT_previewrange_set);
00224         WM_operatortype_append(GRAPH_OT_view_all);
00225         WM_operatortype_append(GRAPH_OT_view_selected);
00226         WM_operatortype_append(GRAPH_OT_properties);
00227         
00228         WM_operatortype_append(GRAPH_OT_ghost_curves_create);
00229         WM_operatortype_append(GRAPH_OT_ghost_curves_clear);
00230         
00231         /* keyframes */
00232                 /* selection */
00233         WM_operatortype_append(GRAPH_OT_clickselect);
00234         WM_operatortype_append(GRAPH_OT_select_all_toggle);
00235         WM_operatortype_append(GRAPH_OT_select_border);
00236         WM_operatortype_append(GRAPH_OT_select_column);
00237         WM_operatortype_append(GRAPH_OT_select_linked);
00238         WM_operatortype_append(GRAPH_OT_select_more);
00239         WM_operatortype_append(GRAPH_OT_select_less);
00240         WM_operatortype_append(GRAPH_OT_select_leftright);
00241         
00242                 /* editing */
00243         WM_operatortype_append(GRAPH_OT_snap);
00244         WM_operatortype_append(GRAPH_OT_mirror);
00245         WM_operatortype_append(GRAPH_OT_frame_jump);
00246         WM_operatortype_append(GRAPH_OT_handle_type);
00247         WM_operatortype_append(GRAPH_OT_interpolation_type);
00248         WM_operatortype_append(GRAPH_OT_extrapolation_type);
00249         WM_operatortype_append(GRAPH_OT_sample);
00250         WM_operatortype_append(GRAPH_OT_bake);
00251         WM_operatortype_append(GRAPH_OT_sound_bake);
00252         WM_operatortype_append(GRAPH_OT_smooth);
00253         WM_operatortype_append(GRAPH_OT_clean);
00254         WM_operatortype_append(GRAPH_OT_euler_filter);
00255         WM_operatortype_append(GRAPH_OT_delete);
00256         WM_operatortype_append(GRAPH_OT_duplicate);
00257         
00258         WM_operatortype_append(GRAPH_OT_copy);
00259         WM_operatortype_append(GRAPH_OT_paste);
00260         
00261         WM_operatortype_append(GRAPH_OT_keyframe_insert);
00262         WM_operatortype_append(GRAPH_OT_click_insert);
00263         
00264         /* F-Curve Modifiers */
00265         WM_operatortype_append(GRAPH_OT_fmodifier_add);
00266         WM_operatortype_append(GRAPH_OT_fmodifier_copy);
00267         WM_operatortype_append(GRAPH_OT_fmodifier_paste);
00268 }
00269 
00270 void ED_operatormacros_graph(void)
00271 {
00272         wmOperatorType *ot;
00273         wmOperatorTypeMacro *otmacro;
00274         
00275         ot= WM_operatortype_append_macro("GRAPH_OT_duplicate_move", "Duplicate", OPTYPE_UNDO|OPTYPE_REGISTER);
00276         if (ot) {
00277                 WM_operatortype_macro_define(ot, "GRAPH_OT_duplicate");
00278                 otmacro= WM_operatortype_macro_define(ot, "TRANSFORM_OT_transform");
00279                 RNA_enum_set(otmacro->ptr, "mode", TFM_TIME_DUPLICATE);
00280         }
00281 }
00282 
00283 
00284 /* ************************** registration - keymaps **********************************/
00285 
00286 static void graphedit_keymap_keyframes (wmKeyConfig *keyconf, wmKeyMap *keymap)
00287 {
00288         wmKeyMapItem *kmi;
00289         
00290         /* view */
00291         WM_keymap_add_item(keymap, "GRAPH_OT_handles_view_toggle", HKEY, KM_PRESS, KM_CTRL, 0);
00292                 /* NOTE: 'ACTIONMOUSE' not 'LEFTMOUSE', as user may have swapped mouse-buttons
00293                  * This keymap is supposed to override ANIM_OT_change_frame, which does the same except it doesn't do y-values
00294                  */
00295         WM_keymap_add_item(keymap, "GRAPH_OT_cursor_set", ACTIONMOUSE, KM_PRESS, 0, 0);
00296         
00297         
00298         /* graph_select.c - selection tools */
00299                 /* click-select */
00300         WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, 0, 0);
00301         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_ALT, 0);
00302                 RNA_boolean_set(kmi->ptr, "column", 1);
00303         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
00304                 RNA_boolean_set(kmi->ptr, "extend", 1);
00305         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_ALT|KM_SHIFT, 0);
00306                 RNA_boolean_set(kmi->ptr, "extend", 1);
00307                 RNA_boolean_set(kmi->ptr, "column", 1);
00308         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT, 0);
00309                 RNA_boolean_set(kmi->ptr, "curves", 1);
00310         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_clickselect", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_ALT|KM_SHIFT, 0);
00311                 RNA_boolean_set(kmi->ptr, "curves", 1);
00312                 RNA_boolean_set(kmi->ptr, "extend", 1);
00313         
00314         /* select left/right */
00315         WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", SELECTMOUSE, KM_PRESS, KM_CTRL, 0);
00316         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", SELECTMOUSE, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
00317                 RNA_boolean_set(kmi->ptr, "extend", 1);
00318         
00319         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", LEFTBRACKETKEY, KM_PRESS, 0, 0);
00320                 RNA_enum_set(kmi->ptr, "mode", GRAPHKEYS_LRSEL_LEFT);
00321         kmi= WM_keymap_add_item(keymap, "GRAPH_OT_select_leftright", RIGHTBRACKETKEY, KM_PRESS, 0, 0);
00322                 RNA_enum_set(kmi->ptr, "mode", GRAPHKEYS_LRSEL_RIGHT);
00323         
00324                 /* deselect all */
00325         WM_keymap_add_item(keymap, "GRAPH_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
00326         RNA_boolean_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_all_toggle", IKEY, KM_PRESS, KM_CTRL, 0)->ptr, "invert", 1);
00327         
00328                 /* borderselect */
00329         WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, 0, 0);
00330         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_ALT, 0);
00331                 RNA_boolean_set(kmi->ptr, "axis_range", 1);
00332                 
00333         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_CTRL, 0);
00334                 RNA_boolean_set(kmi->ptr, "include_handles", 1);
00335         kmi = WM_keymap_add_item(keymap, "GRAPH_OT_select_border", BKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
00336                 RNA_boolean_set(kmi->ptr, "axis_range", 1);
00337                 RNA_boolean_set(kmi->ptr, "include_handles", 1);
00338                 
00339                 /* column select */
00340         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, 0, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_KEYS);
00341         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_CTRL, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_CFRA);
00342         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_MARKERS_COLUMN);
00343         RNA_enum_set(WM_keymap_add_item(keymap, "GRAPH_OT_select_column", KKEY, KM_PRESS, KM_ALT, 0)->ptr, "mode", GRAPHKEYS_COLUMNSEL_MARKERS_BETWEEN);
00344         
00345                 /* select more/less */
00346         WM_keymap_add_item(keymap, "GRAPH_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
00347         WM_keymap_add_item(keymap, "GRAPH_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0);
00348         
00349                 /* select linked */
00350         WM_keymap_add_item(keymap, "GRAPH_OT_select_linked", LKEY, KM_PRESS, 0, 0);
00351         
00352         
00353         /* graph_edit.c */
00354                 /* snap - current frame to selected keys */
00355                 // TODO: maybe since this is called jump, we're better to have it on <something>-J?
00356         WM_keymap_add_item(keymap, "GRAPH_OT_frame_jump", SKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
00357                 
00358                 /* menu + single-step transform */
00359         WM_keymap_add_item(keymap, "GRAPH_OT_snap", SKEY, KM_PRESS, KM_SHIFT, 0);
00360         WM_keymap_add_item(keymap, "GRAPH_OT_mirror", MKEY, KM_PRESS, KM_SHIFT, 0);
00361         
00362         WM_keymap_add_item(keymap, "GRAPH_OT_handle_type", VKEY, KM_PRESS, 0, 0);
00363 
00364         WM_keymap_add_item(keymap, "GRAPH_OT_interpolation_type", TKEY, KM_PRESS, KM_SHIFT, 0);
00365         
00366                 /* destructive */
00367         WM_keymap_add_item(keymap, "GRAPH_OT_clean", OKEY, KM_PRESS, 0, 0);
00368         WM_keymap_add_item(keymap, "GRAPH_OT_smooth", OKEY, KM_PRESS, KM_ALT, 0);
00369         WM_keymap_add_item(keymap, "GRAPH_OT_sample", OKEY, KM_PRESS, KM_SHIFT, 0);
00370         
00371         WM_keymap_add_item(keymap, "GRAPH_OT_bake", CKEY, KM_PRESS, KM_ALT, 0);
00372         
00373         WM_keymap_add_item(keymap, "GRAPH_OT_delete", XKEY, KM_PRESS, 0, 0);
00374         WM_keymap_add_item(keymap, "GRAPH_OT_delete", DELKEY, KM_PRESS, 0, 0);
00375         
00376         WM_keymap_add_item(keymap, "GRAPH_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0);
00377         
00378                 /* insertkey */
00379         WM_keymap_add_item(keymap, "GRAPH_OT_keyframe_insert", IKEY, KM_PRESS, 0, 0);
00380         WM_keymap_add_item(keymap, "GRAPH_OT_click_insert", LEFTMOUSE, KM_CLICK, KM_CTRL, 0);
00381         
00382                 /* copy/paste */
00383         WM_keymap_add_item(keymap, "GRAPH_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
00384         WM_keymap_add_item(keymap, "GRAPH_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
00385         
00386                 /* auto-set range */
00387         WM_keymap_add_item(keymap, "GRAPH_OT_previewrange_set", PKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
00388         WM_keymap_add_item(keymap, "GRAPH_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
00389         WM_keymap_add_item(keymap, "GRAPH_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
00390         
00391                 /* F-Modifiers */
00392         RNA_boolean_set(WM_keymap_add_item(keymap, "GRAPH_OT_fmodifier_add", MKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "only_active", 0);
00393         
00394         /* animation module */
00395                 /* channels list 
00396                  * NOTE: these operators were originally for the channels list, but are added here too for convenience...
00397                  */
00398         WM_keymap_add_item(keymap, "ANIM_OT_channels_editable_toggle", TABKEY, KM_PRESS, 0, 0);
00399         
00400         /* transform system */
00401         transform_keymap_for_space(keyconf, keymap, SPACE_IPO);
00402 }
00403 
00404 /* --------------- */
00405 
00406 void graphedit_keymap(wmKeyConfig *keyconf)
00407 {
00408         wmKeyMap *keymap;
00409         
00410         /* keymap for all regions */
00411         keymap= WM_keymap_find(keyconf, "Graph Editor Generic", SPACE_IPO, 0);
00412         WM_keymap_add_item(keymap, "GRAPH_OT_properties", NKEY, KM_PRESS, 0, 0);
00413                 /* extrapolation works on channels, not keys */
00414         WM_keymap_add_item(keymap, "GRAPH_OT_extrapolation_type", EKEY, KM_PRESS, KM_SHIFT, 0);
00415 
00416         /* channels */
00417         /* Channels are not directly handled by the Graph Editor module, but are inherited from the Animation module. 
00418          * All the relevant operations, keymaps, drawing, etc. can therefore all be found in that module instead, as these
00419          * are all used for the Graph Editor too.
00420          */
00421         
00422         /* keyframes */
00423         keymap= WM_keymap_find(keyconf, "Graph Editor", SPACE_IPO, 0);
00424         graphedit_keymap_keyframes(keyconf, keymap);
00425 }
00426