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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
32 #include "MEM_guardedalloc.h"
34 #include "DNA_action_types.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_space_types.h"
38 #include "DNA_windowmanager_types.h"
40 #include "BLI_blenlib.h"
42 #include "BKE_context.h"
43 #include "BKE_utildefines.h"
45 #include "UI_interface.h"
46 #include "UI_view2d.h"
48 #include "RNA_access.h"
49 #include "RNA_define.h"
54 #include "ED_anim_api.h"
55 #include "ED_keyframing.h" // XXX remove?
56 #include "ED_markers.h"
57 #include "ED_screen.h"
59 /* ********************** frame change operator ***************************/
61 /* Set any flags that are necessary to indicate modal time-changing operation */
62 static int change_frame_init(bContext *C, wmOperator *op)
64 ScrArea *curarea= CTX_wm_area(C);
69 if (curarea->spacetype == SPACE_TIME) {
70 SpaceTime *stime= (SpaceTime*)CTX_wm_space_data(C);
72 /* timeline displays frame number only when dragging indicator */
73 // XXX make this more in line with other anim editors?
74 stime->flag |= TIME_CFRA_NUM;
80 /* Set the new frame number */
81 static void change_frame_apply(bContext *C, wmOperator *op)
83 Scene *scene= CTX_data_scene(C);
86 /* get frame, and clamp to MINFRAME */
87 cfra= RNA_int_get(op->ptr, "frame");
89 if (cfra < MINFRAME) cfra= MINFRAME;
92 WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
95 /* Clear any temp flags */
96 static void change_frame_exit(bContext *C, wmOperator *op)
98 ScrArea *curarea= CTX_wm_area(C);
103 if (curarea->spacetype == SPACE_TIME) {
104 SpaceTime *stime= (SpaceTime*)CTX_wm_space_data(C);
106 /* timeline displays frame number only when dragging indicator */
107 // XXX make this more in line with other anim editors?
108 stime->flag &= ~TIME_CFRA_NUM;
114 /* Non-modal callback for running operator without user input */
115 static int change_frame_exec(bContext *C, wmOperator *op)
117 if (!change_frame_init(C, op))
118 return OPERATOR_CANCELLED;
120 change_frame_apply(C, op);
121 change_frame_exit(C, op);
122 return OPERATOR_FINISHED;
127 /* Get frame from mouse coordinates */
128 static int frame_from_event(bContext *C, wmEvent *event)
130 ARegion *region= CTX_wm_region(C);
134 /* convert screen coordinates to region coordinates */
135 x= event->x - region->winrct.xmin;
136 y= event->y - region->winrct.ymin;
138 /* convert from region coordinates to View2D 'tot' space */
139 UI_view2d_region_to_view(®ion->v2d, x, y, &viewx, NULL);
141 /* round result to nearest int (frames are ints!) */
142 return (int)floor(viewx+0.5f);
145 /* Modal Operator init */
146 static int change_frame_invoke(bContext *C, wmOperator *op, wmEvent *event)
148 /* Change to frame that mouse is over before adding modal handler,
149 * as user could click on a single frame (jump to frame) as well as
150 * click-dragging over a range (modal scrubbing).
152 RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
154 change_frame_init(C, op);
155 change_frame_apply(C, op);
157 /* add temp handler */
158 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
160 return OPERATOR_RUNNING_MODAL;
163 /* In case modal operator is cancelled */
164 static int change_frame_cancel(bContext *C, wmOperator *op)
166 change_frame_exit(C, op);
167 return OPERATOR_CANCELLED;
170 /* Modal event handling of frame changing */
171 static int change_frame_modal(bContext *C, wmOperator *op, wmEvent *event)
173 /* execute the events */
174 switch (event->type) {
176 change_frame_exit(C, op);
177 return OPERATOR_FINISHED;
180 RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
181 change_frame_apply(C, op);
186 /* we check for either mouse-button to end, as checking for ACTIONMOUSE (which is used to init
187 * the modal op) doesn't work for some reason
190 change_frame_exit(C, op);
191 return OPERATOR_FINISHED;
196 return OPERATOR_RUNNING_MODAL;
199 void ANIM_OT_change_frame(wmOperatorType *ot)
202 ot->name= "Change frame";
203 ot->idname= "ANIM_OT_change_frame";
206 ot->exec= change_frame_exec;
207 ot->invoke= change_frame_invoke;
208 ot->cancel= change_frame_cancel;
209 ot->modal= change_frame_modal;
212 RNA_def_int(ot->srna, "frame", 0, 1, MAXFRAME, "Frame", "", 1, MAXFRAME);
215 /* ****************** set preview range operator ****************************/
217 static int previewrange_define_exec(bContext *C, wmOperator *op)
219 Scene *scene= CTX_data_scene(C);
220 ARegion *ar= CTX_wm_region(C);
224 /* get min/max values from border select rect (already in region coordinates, not screen) */
225 xmin= RNA_int_get(op->ptr, "xmin");
226 xmax= RNA_int_get(op->ptr, "xmax");
228 /* convert min/max values to frames (i.e. region to 'tot' rect) */
229 UI_view2d_region_to_view(&ar->v2d, xmin, 0, &sfra, NULL);
230 UI_view2d_region_to_view(&ar->v2d, xmax, 0, &efra, NULL);
232 /* set start/end frames for preview-range
233 * - must clamp within allowable limits
234 * - end must not be before start (though this won't occur most of the time)
236 if (sfra < 1) sfra = 1.0f;
237 if (efra < 1) efra = 1.0f;
238 if (efra < sfra) efra= sfra;
240 scene->r.psfra= (int)floor(sfra + 0.5f);
241 scene->r.pefra= (int)floor(efra + 0.5f);
243 return OPERATOR_FINISHED;
246 void ANIM_OT_previewrange_define(wmOperatorType *ot)
249 ot->name= "Set Preview Range";
250 ot->idname= "ANIM_OT_previewrange_define";
253 ot->invoke= WM_border_select_invoke;
254 ot->exec= previewrange_define_exec;
255 ot->modal= WM_border_select_modal;
257 ot->poll= ED_operator_areaactive;
260 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
263 /* used to define frame range */
264 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
265 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
266 /* these are not used, but are needed by borderselect gesture operator stuff */
267 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
268 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
271 /* ****************** clear preview range operator ****************************/
273 static int previewrange_clear_exec(bContext *C, wmOperator *op)
275 Scene *scene= CTX_data_scene(C);
276 ScrArea *curarea= CTX_wm_area(C);
279 if (ELEM(NULL, scene, curarea))
280 return OPERATOR_CANCELLED;
282 /* simply clear values */
286 ED_area_tag_redraw(curarea);
288 return OPERATOR_FINISHED;
291 void ANIM_OT_previewrange_clear(wmOperatorType *ot)
294 ot->name= "Clear Preview Range";
295 ot->idname= "ANIM_OT_previewrange_clear";
298 ot->exec= previewrange_clear_exec;
300 ot->poll= ED_operator_areaactive;
303 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
306 /* ****************** time display toggle operator ****************************/
308 static int toggle_time_exec(bContext *C, wmOperator *op)
310 ScrArea *curarea= CTX_wm_area(C);
313 return OPERATOR_CANCELLED;
315 /* simply toggle draw frames flag in applicable spaces */
316 // XXX or should relevant spaces define their own version of this?
317 switch (curarea->spacetype) {
318 case SPACE_TIME: /* TimeLine */
320 SpaceTime *stime= (SpaceTime *)CTX_wm_space_data(C);
321 stime->flag ^= TIME_DRAWFRAMES;
324 case SPACE_ACTION: /* Action Editor */
326 SpaceAction *saction= (SpaceAction *)CTX_wm_space_data(C);
327 saction->flag ^= SACTION_DRAWTIME;
330 case SPACE_IPO: /* IPO Editor */
332 SpaceIpo *sipo= (SpaceIpo *)CTX_wm_space_data(C);
333 sipo->flag ^= SIPO_DRAWTIME;
336 case SPACE_NLA: /* NLA Editor */
338 SpaceNla *snla= (SpaceNla *)CTX_wm_space_data(C);
339 snla->flag ^= SNLA_DRAWTIME;
342 case SPACE_SEQ: /* Sequencer */
344 SpaceSeq *sseq= (SpaceSeq *)CTX_wm_space_data(C);
345 sseq->flag ^= SEQ_DRAWFRAMES;
349 default: /* editor doesn't show frames */
350 return OPERATOR_CANCELLED; // XXX or should we pass through instead?
353 ED_area_tag_redraw(curarea);
355 return OPERATOR_FINISHED;
358 void ANIM_OT_time_toggle(wmOperatorType *ot)
361 ot->name= "Toggle Frames/Seconds";
362 ot->idname= "ANIM_OT_time_toggle";
365 ot->exec= toggle_time_exec;
367 ot->poll= ED_operator_areaactive;
370 /* ************************** registration **********************************/
372 void ED_operatortypes_anim(void)
374 WM_operatortype_append(ANIM_OT_change_frame);
375 WM_operatortype_append(ANIM_OT_time_toggle);
377 WM_operatortype_append(ANIM_OT_previewrange_define);
378 WM_operatortype_append(ANIM_OT_previewrange_clear);
380 // XXX this is used all over... maybe for screen instead?
381 WM_operatortype_append(ANIM_OT_insert_keyframe);
382 WM_operatortype_append(ANIM_OT_delete_keyframe);
383 WM_operatortype_append(ANIM_OT_insert_keyframe_old);
384 WM_operatortype_append(ANIM_OT_delete_keyframe_old);
387 void ED_keymap_anim(wmWindowManager *wm)
389 ListBase *keymap= WM_keymap_listbase(wm, "Animation", 0, 0);
391 /* frame management */
392 /* NOTE: 'ACTIONMOUSE' not 'LEFTMOUSE', as user may have swapped mouse-buttons */
393 WM_keymap_verify_item(keymap, "ANIM_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
394 WM_keymap_verify_item(keymap, "ANIM_OT_time_toggle", TKEY, KM_PRESS, KM_CTRL, 0);
397 WM_keymap_verify_item(keymap, "ANIM_OT_previewrange_define", PKEY, KM_PRESS, KM_CTRL, 0);
398 WM_keymap_verify_item(keymap, "ANIM_OT_previewrange_clear", PKEY, KM_PRESS, KM_ALT, 0);