2.5
[blender.git] / source / blender / editors / space_time / time_ops.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_windowmanager_types.h"
38
39 #include "BLI_blenlib.h"
40
41 #include "BKE_global.h"
42 #include "BKE_utildefines.h"
43
44 #include "UI_interface.h"
45 #include "UI_view2d.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "ED_markers.h"
54
55 /* ********************** frame change operator ***************************/
56
57 static int change_frame_init(bContext *C, wmOperator *op)
58 {
59         SpaceTime *stime= C->area->spacedata.first;
60
61         stime->flag |= TIME_CFRA_NUM;
62         
63         return 1;
64 }
65
66 static void change_frame_apply(bContext *C, wmOperator *op)
67 {
68         int cfra;
69
70         cfra= RNA_int_get(op->ptr, "frame");
71
72         if(cfra < MINFRAME)
73                 cfra= MINFRAME;
74
75 #if 0
76         if( cfra!=CFRA || first )
77         {
78                 first= 0;
79                 CFRA= cfra;
80                 update_for_newframe_nodraw(0);  // 1= nosound
81                 timeline_force_draw(stime->redraws);
82         }
83         else PIL_sleep_ms(30);
84 #endif
85
86         if(cfra!=CFRA)
87                 CFRA= cfra;
88         
89         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
90         /* XXX: add WM_NOTE_TIME_CHANGED? */
91 }
92
93 static void change_frame_exit(bContext *C, wmOperator *op)
94 {
95         SpaceTime *stime= C->area->spacedata.first;
96
97         stime->flag &= ~TIME_CFRA_NUM;
98 }
99
100 static int change_frame_exec(bContext *C, wmOperator *op)
101 {
102         if(!change_frame_init(C, op))
103                 return OPERATOR_CANCELLED;
104         
105         change_frame_apply(C, op);
106         change_frame_exit(C, op);
107         return OPERATOR_FINISHED;
108 }
109
110 static int frame_from_event(bContext *C, wmEvent *event)
111 {
112         ARegion *region= C->region;
113         int x, y;
114         float viewx;
115         
116         x= event->x - region->winrct.xmin;
117         y= event->y - region->winrct.ymin;
118         UI_view2d_region_to_view(&region->v2d, x, y, &viewx, NULL);
119
120         return (int)floor(viewx+0.5f);
121 }
122
123 static int change_frame_invoke(bContext *C, wmOperator *op, wmEvent *event)
124 {
125         RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
126         change_frame_init(C, op);
127         change_frame_apply(C, op);
128
129         /* add temp handler */
130         WM_event_add_modal_handler(C, &C->window->handlers, op);
131
132         return OPERATOR_RUNNING_MODAL;
133 }
134
135 static int change_frame_cancel(bContext *C, wmOperator *op)
136 {
137         change_frame_exit(C, op);
138         return OPERATOR_CANCELLED;
139 }
140
141 static int change_frame_modal(bContext *C, wmOperator *op, wmEvent *event)
142 {
143         /* execute the events */
144         switch(event->type) {
145                 case MOUSEMOVE:
146                         RNA_int_set(op->ptr, "frame", frame_from_event(C, event));
147                         change_frame_apply(C, op);
148                         break;
149                         
150                 case LEFTMOUSE:
151                         if(event->val==0) {
152                                 change_frame_exit(C, op);
153                                 return OPERATOR_FINISHED;
154                         }
155                         break;
156         }
157
158         return OPERATOR_RUNNING_MODAL;
159 }
160
161 void ED_TIME_OT_change_frame(wmOperatorType *ot)
162 {
163         PropertyRNA *prop;
164
165         /* identifiers */
166         ot->name= "Change frame";
167         ot->idname= "ED_TIME_OT_change_frame";
168         
169         /* api callbacks */
170         ot->exec= change_frame_exec;
171         ot->invoke= change_frame_invoke;
172         ot->cancel= change_frame_cancel;
173         ot->modal= change_frame_modal;
174
175         /* rna */
176         prop= RNA_def_property(ot->srna, "frame", PROP_INT, PROP_NONE);
177 }
178
179 /* ****************** time display toggle operator ****************************/
180
181 static int toggle_time_exec(bContext *C, wmOperator *op)
182 {
183         SpaceTime *stime;
184         
185         if (ELEM(NULL, C->area, C->area->spacedata.first))
186                 return OPERATOR_CANCELLED;
187         
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;
192         
193         return OPERATOR_FINISHED;
194 }
195
196 void ED_TIME_OT_toggle_time(wmOperatorType *ot)
197 {
198         /* identifiers */
199         ot->name= "Toggle Frames/Seconds";
200         ot->idname= "ED_TIME_OT_toggle_time";
201         
202         /* api callbacks */
203         ot->exec= toggle_time_exec;
204 }
205
206 /* ************************** registration **********************************/
207
208 void time_operatortypes(void)
209 {
210         WM_operatortype_append(ED_TIME_OT_change_frame);
211         WM_operatortype_append(ED_TIME_OT_toggle_time);
212 }
213
214 void time_keymap(wmWindowManager *wm)
215 {
216         ListBase *keymap= WM_keymap_listbase(wm, "TimeLine", SPACE_TIME, 0);
217         
218         WM_keymap_verify_item(keymap, "ED_TIME_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
219         WM_keymap_verify_item(keymap, "ED_TIME_OT_toggle_time", TKEY, KM_PRESS, 0, 0);
220 }
221