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
26 * ***** END GPL LICENSE BLOCK *****
32 #include "MEM_guardedalloc.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_windowmanager_types.h"
39 #include "BLI_blenlib.h"
41 #include "BKE_global.h"
42 #include "BKE_utildefines.h"
44 #include "UI_interface.h"
45 #include "UI_view2d.h"
47 #include "RNA_access.h"
48 #include "RNA_define.h"
53 #include "ED_markers.h"
55 /* ********************** frame change operator ***************************/
57 static int change_frame_init(bContext *C, wmOperator *op)
59 SpaceTime *stime= C->area->spacedata.first;
61 stime->flag |= TIME_CFRA_NUM;
66 static void change_frame_apply(bContext *C, wmOperator *op)
70 cfra= RNA_int_get(op->ptr, "frame");
76 if( cfra!=CFRA || first )
80 update_for_newframe_nodraw(0); // 1= nosound
81 timeline_force_draw(stime->redraws);
83 else PIL_sleep_ms(30);
89 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
90 /* XXX: add WM_NOTE_TIME_CHANGED? */
93 static void change_frame_exit(bContext *C, wmOperator *op)
95 SpaceTime *stime= C->area->spacedata.first;
97 stime->flag &= ~TIME_CFRA_NUM;
100 static int change_frame_exec(bContext *C, wmOperator *op)
102 if(!change_frame_init(C, op))
103 return OPERATOR_CANCELLED;
105 change_frame_apply(C, op);
106 change_frame_exit(C, op);
107 return OPERATOR_FINISHED;
110 static int frame_from_event(bContext *C, wmEvent *event)
112 ARegion *region= C->region;
116 x= event->x - region->winrct.xmin;
117 y= event->y - region->winrct.ymin;
118 UI_view2d_region_to_view(®ion->v2d, x, y, &viewx, NULL);
120 return (int)floor(viewx+0.5f);
123 static int change_frame_invoke(bContext *C, wmOperator *op, wmEvent *event)
125 RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
126 change_frame_init(C, op);
127 change_frame_apply(C, op);
129 /* add temp handler */
130 WM_event_add_modal_handler(C, &C->window->handlers, op);
132 return OPERATOR_RUNNING_MODAL;
135 static int change_frame_cancel(bContext *C, wmOperator *op)
137 change_frame_exit(C, op);
138 return OPERATOR_CANCELLED;
141 static int change_frame_modal(bContext *C, wmOperator *op, wmEvent *event)
143 /* execute the events */
144 switch(event->type) {
146 RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
147 change_frame_apply(C, op);
152 change_frame_exit(C, op);
153 return OPERATOR_FINISHED;
158 return OPERATOR_RUNNING_MODAL;
161 void ED_TIME_OT_change_frame(wmOperatorType *ot)
166 ot->name= "Change frame";
167 ot->idname= "ED_TIME_OT_change_frame";
170 ot->exec= change_frame_exec;
171 ot->invoke= change_frame_invoke;
172 ot->cancel= change_frame_cancel;
173 ot->modal= change_frame_modal;
176 prop= RNA_def_property(ot->srna, "frame", PROP_INT, PROP_NONE);
179 /* ****************** time display toggle operator ****************************/
181 static int toggle_time_exec(bContext *C, wmOperator *op)
185 if (ELEM(NULL, C->area, C->area->spacedata.first))
186 return OPERATOR_CANCELLED;
188 /* simply toggle draw frames flag for now */
189 // XXX in past, this displayed menu to choose... (for later!)
190 stime= C->area->spacedata.first;
191 stime->flag ^= TIME_DRAWFRAMES;
193 return OPERATOR_FINISHED;
196 void ED_TIME_OT_toggle_time(wmOperatorType *ot)
199 ot->name= "Toggle Frames/Seconds";
200 ot->idname= "ED_TIME_OT_toggle_time";
203 ot->exec= toggle_time_exec;
206 /* ************************** registration **********************************/
208 void time_operatortypes(void)
210 WM_operatortype_append(ED_TIME_OT_change_frame);
211 WM_operatortype_append(ED_TIME_OT_toggle_time);
214 void time_keymap(wmWindowManager *wm)
216 WM_keymap_verify_item(&wm->timekeymap, "ED_TIME_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
217 WM_keymap_verify_item(&wm->timekeymap, "ED_TIME_OT_toggle_time", TKEY, KM_PRESS, 0, 0);
220 /* markers (XXX move to function?) */
221 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_add", MKEY, KM_PRESS, 0, 0);
222 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_move", EVT_TWEAK_R, KM_ANY, 0, 0);
223 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_duplicate", DKEY, KM_PRESS, KM_SHIFT, 0);
224 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_mouseselect", RIGHTMOUSE, KM_PRESS, 0, 0);
225 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_mouseselect_extend", RIGHTMOUSE, KM_PRESS, KM_SHIFT, 0);
226 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_border_select", BKEY, KM_PRESS, 0, 0);
227 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_select_all", AKEY, KM_PRESS, 0, 0);
228 WM_keymap_verify_item(&wm->timekeymap, "ED_MARKER_OT_delete", XKEY, KM_PRESS, 0, 0);
230 WM_keymap_add_item(&wm->timekeymap, "ED_MARKER_OT_move", GKEY, KM_PRESS, 0, 0);
232 /* generates event, in end to make select work */
233 WM_keymap_verify_item(&wm->timekeymap, "WM_OT_tweak_gesture", RIGHTMOUSE, KM_PRESS, 0, 0);