4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation, Joshua Leung
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/animation/anim_ops.c
30 * \ingroup edanimation
37 #include "BLO_sys_types.h"
39 #include "BLI_utildefines.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_scene_types.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_sound.h"
48 #include "UI_view2d.h"
50 #include "RNA_access.h"
51 #include "RNA_define.h"
56 #include "ED_anim_api.h"
57 #include "ED_screen.h"
59 #include "anim_intern.h"
61 /* ********************** frame change operator ***************************/
63 /* Check if the operator can be run from the current context */
64 static int change_frame_poll(bContext *C)
66 ScrArea *curarea= CTX_wm_area(C);
68 /* XXX temp? prevent changes during render */
69 if(G.rendering) return 0;
71 /* as long as there is an active area, and it isn't a Graph Editor
72 * (since the Graph Editor has its own version which does extra stuff),
75 return ((curarea) && (curarea->spacetype != SPACE_IPO));
78 /* Set the new frame number */
79 static void change_frame_apply(bContext *C, wmOperator *op)
81 Scene *scene= CTX_data_scene(C);
83 /* set the new frame number */
84 CFRA= RNA_int_get(op->ptr, "frame");
85 FRAMENUMBER_MIN_CLAMP(CFRA);
90 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
95 /* Non-modal callback for running operator without user input */
96 static int change_frame_exec(bContext *C, wmOperator *op)
98 change_frame_apply(C, op);
100 return OPERATOR_FINISHED;
105 /* Get frame from mouse coordinates */
106 static int frame_from_event(bContext *C, wmEvent *event)
108 ARegion *region= CTX_wm_region(C);
112 /* convert screen coordinates to region coordinates */
113 x= event->x - region->winrct.xmin;
114 y= event->y - region->winrct.ymin;
116 /* convert from region coordinates to View2D 'tot' space */
117 UI_view2d_region_to_view(®ion->v2d, x, y, &viewx, NULL);
119 /* round result to nearest int (frames are ints!) */
120 return (int)floor(viewx+0.5f);
123 /* Modal Operator init */
124 static int change_frame_invoke(bContext *C, wmOperator *op, wmEvent *event)
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).
130 RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
132 change_frame_apply(C, op);
134 /* add temp handler */
135 WM_event_add_modal_handler(C, op);
137 return OPERATOR_RUNNING_MODAL;
140 /* Modal event handling of frame changing */
141 static int change_frame_modal(bContext *C, wmOperator *op, wmEvent *event)
143 /* execute the events */
144 switch (event->type) {
146 return OPERATOR_FINISHED;
149 RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
150 change_frame_apply(C, op);
155 /* we check for either mouse-button to end, as checking for ACTIONMOUSE (which is used to init
156 * the modal op) doesn't work for some reason
158 if (event->val==KM_RELEASE)
159 return OPERATOR_FINISHED;
163 return OPERATOR_RUNNING_MODAL;
166 static void ANIM_OT_change_frame(wmOperatorType *ot)
169 ot->name= "Change frame";
170 ot->idname= "ANIM_OT_change_frame";
171 ot->description= "Interactively change the current frame number";
174 ot->exec= change_frame_exec;
175 ot->invoke= change_frame_invoke;
176 ot->modal= change_frame_modal;
177 ot->poll= change_frame_poll;
180 ot->flag= OPTYPE_BLOCKING|OPTYPE_UNDO;
183 RNA_def_int(ot->srna, "frame", 0, MINAFRAME, MAXFRAME, "Frame", "", MINAFRAME, MAXFRAME);
186 /* ****************** set preview range operator ****************************/
188 static int previewrange_define_exec(bContext *C, wmOperator *op)
190 Scene *scene= CTX_data_scene(C);
191 ARegion *ar= CTX_wm_region(C);
195 /* get min/max values from border select rect (already in region coordinates, not screen) */
196 xmin= RNA_int_get(op->ptr, "xmin");
197 xmax= RNA_int_get(op->ptr, "xmax");
199 /* convert min/max values to frames (i.e. region to 'tot' rect) */
200 UI_view2d_region_to_view(&ar->v2d, xmin, 0, &sfra, NULL);
201 UI_view2d_region_to_view(&ar->v2d, xmax, 0, &efra, NULL);
203 /* set start/end frames for preview-range
204 * - must clamp within allowable limits
205 * - end must not be before start (though this won't occur most of the time)
207 if (sfra < 1) sfra = 1.0f;
208 if (efra < 1) efra = 1.0f;
209 if (efra < sfra) efra= sfra;
211 scene->r.flag |= SCER_PRV_RANGE;
212 scene->r.psfra= (int)floor(sfra + 0.5f);
213 scene->r.pefra= (int)floor(efra + 0.5f);
216 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
218 return OPERATOR_FINISHED;
221 static void ANIM_OT_previewrange_set(wmOperatorType *ot)
224 ot->name= "Set Preview Range";
225 ot->idname= "ANIM_OT_previewrange_set";
226 ot->description= "Interactively define frame range used for playback";
229 ot->invoke= WM_border_select_invoke;
230 ot->exec= previewrange_define_exec;
231 ot->modal= WM_border_select_modal;
233 ot->poll= ED_operator_animview_active;
236 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
239 /* used to define frame range */
240 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
241 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
242 /* these are not used, but are needed by borderselect gesture operator stuff */
243 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
244 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
247 /* ****************** clear preview range operator ****************************/
249 static int previewrange_clear_exec(bContext *C, wmOperator *UNUSED(op))
251 Scene *scene= CTX_data_scene(C);
252 ScrArea *curarea= CTX_wm_area(C);
255 if (ELEM(NULL, scene, curarea))
256 return OPERATOR_CANCELLED;
258 /* simply clear values */
259 scene->r.flag &= ~SCER_PRV_RANGE;
263 ED_area_tag_redraw(curarea);
265 return OPERATOR_FINISHED;
268 static void ANIM_OT_previewrange_clear(wmOperatorType *ot)
271 ot->name= "Clear Preview Range";
272 ot->idname= "ANIM_OT_previewrange_clear";
273 ot->description= "Clear Preview Range";
276 ot->exec= previewrange_clear_exec;
278 ot->poll= ED_operator_animview_active;
281 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
284 /* ****************** time display toggle operator ****************************/
286 static int toggle_time_exec(bContext *C, wmOperator *UNUSED(op))
288 ScrArea *curarea= CTX_wm_area(C);
291 return OPERATOR_CANCELLED;
293 /* simply toggle draw frames flag in applicable spaces */
294 // XXX or should relevant spaces define their own version of this?
295 switch (curarea->spacetype) {
296 case SPACE_TIME: /* TimeLine */
298 SpaceTime *stime= CTX_wm_space_time(C);
299 stime->flag ^= TIME_DRAWFRAMES;
302 case SPACE_ACTION: /* Action Editor */
304 SpaceAction *saction= CTX_wm_space_action(C);
305 saction->flag ^= SACTION_DRAWTIME;
308 case SPACE_IPO: /* Graph Editor */
310 SpaceIpo *sipo= CTX_wm_space_graph(C);
311 sipo->flag ^= SIPO_DRAWTIME;
314 case SPACE_NLA: /* NLA Editor */
316 SpaceNla *snla= CTX_wm_space_nla(C);
317 snla->flag ^= SNLA_DRAWTIME;
320 case SPACE_SEQ: /* Sequencer */
322 SpaceSeq *sseq= CTX_wm_space_seq(C);
323 sseq->flag ^= SEQ_DRAWFRAMES;
327 default: /* editor doesn't show frames */
328 return OPERATOR_CANCELLED; // XXX or should we pass through instead?
331 ED_area_tag_redraw(curarea);
333 return OPERATOR_FINISHED;
336 static void ANIM_OT_time_toggle(wmOperatorType *ot)
339 ot->name= "Toggle Frames/Seconds";
340 ot->idname= "ANIM_OT_time_toggle";
341 ot->description= "Toggle whether timing is displayed in frames or seconds for active timeline view";
344 ot->exec= toggle_time_exec;
346 ot->poll= ED_operator_animview_active;
349 /* ************************** registration **********************************/
351 void ED_operatortypes_anim(void)
353 /* Animation Editors only -------------------------- */
354 WM_operatortype_append(ANIM_OT_change_frame);
355 WM_operatortype_append(ANIM_OT_time_toggle);
357 WM_operatortype_append(ANIM_OT_previewrange_set);
358 WM_operatortype_append(ANIM_OT_previewrange_clear);
360 /* Entire UI --------------------------------------- */
361 WM_operatortype_append(ANIM_OT_keyframe_insert);
362 WM_operatortype_append(ANIM_OT_keyframe_delete);
363 WM_operatortype_append(ANIM_OT_keyframe_insert_menu);
364 WM_operatortype_append(ANIM_OT_keyframe_delete_v3d);
365 WM_operatortype_append(ANIM_OT_keyframe_insert_button);
366 WM_operatortype_append(ANIM_OT_keyframe_delete_button);
369 WM_operatortype_append(ANIM_OT_driver_button_add);
370 WM_operatortype_append(ANIM_OT_driver_button_remove);
371 WM_operatortype_append(ANIM_OT_copy_driver_button);
372 WM_operatortype_append(ANIM_OT_paste_driver_button);
375 WM_operatortype_append(ANIM_OT_keyingset_button_add);
376 WM_operatortype_append(ANIM_OT_keyingset_button_remove);
378 WM_operatortype_append(ANIM_OT_keying_set_add);
379 WM_operatortype_append(ANIM_OT_keying_set_remove);
380 WM_operatortype_append(ANIM_OT_keying_set_path_add);
381 WM_operatortype_append(ANIM_OT_keying_set_path_remove);
383 WM_operatortype_append(ANIM_OT_keying_set_active_set);
386 void ED_keymap_anim(wmKeyConfig *keyconf)
388 wmKeyMap *keymap= WM_keymap_find(keyconf, "Animation", 0, 0);
390 /* frame management */
391 /* NOTE: 'ACTIONMOUSE' not 'LEFTMOUSE', as user may have swapped mouse-buttons */
392 WM_keymap_add_item(keymap, "ANIM_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
393 WM_keymap_verify_item(keymap, "ANIM_OT_time_toggle", TKEY, KM_PRESS, KM_CTRL, 0);
396 WM_keymap_verify_item(keymap, "ANIM_OT_previewrange_set", PKEY, KM_PRESS, 0, 0);
397 WM_keymap_verify_item(keymap, "ANIM_OT_previewrange_clear", PKEY, KM_PRESS, KM_ALT, 0);