Blender V2.61 - r43446

graph_ops.c

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