Motion Tracking: fixed dopesheet left in incorrect state after joining tracks
[blender.git] / source / blender / editors / space_clip / clip_utils.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/clip_utils.c
29  *  \ingroup spclip
30  */
31
32 #include "DNA_scene_types.h"
33 #include "DNA_object_types.h"   /* SELECT */
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_math.h"
39 #include "BLI_listbase.h"
40
41 #include "BKE_context.h"
42 #include "BKE_movieclip.h"
43 #include "BKE_tracking.h"
44 #include "BKE_depsgraph.h"
45
46 #include "BIF_gl.h"
47 #include "BIF_glutil.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "ED_screen.h"
53 #include "ED_clip.h"
54
55 #include "UI_interface.h"
56
57 #include "RNA_access.h"
58 #include "RNA_define.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62 #include "UI_view2d.h"
63
64 #include "clip_intern.h"    // own include
65
66 void clip_graph_tracking_values_iterate_track(SpaceClip *sc, MovieTrackingTrack *track, void *userdata,
67                 void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker, int coord,
68                               int scene_framenr, float val),
69                 void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord),
70                 void (*segment_end)(void *userdata))
71 {
72         MovieClip *clip = ED_space_clip_get_clip(sc);
73         int width, height, coord;
74
75         BKE_movieclip_get_size(clip, &sc->user, &width, &height);
76
77         for (coord = 0; coord < 2; coord++) {
78                 int i, open = FALSE, prevfra = 0;
79                 float prevval = 0.0f;
80
81                 for (i = 0; i < track->markersnr; i++) {
82                         MovieTrackingMarker *marker = &track->markers[i];
83                         float val;
84
85                         if (marker->flag & MARKER_DISABLED) {
86                                 if (open) {
87                                         if (segment_end)
88                                                 segment_end(userdata);
89
90                                         open = FALSE;
91                                 }
92
93                                 continue;
94                         }
95
96                         if (!open) {
97                                 if (segment_start)
98                                         segment_start(userdata, track, coord);
99
100                                 open = TRUE;
101                                 prevval = marker->pos[coord];
102                         }
103
104                         /* value is a pixels per frame speed */
105                         val = (marker->pos[coord] - prevval) * ((coord == 0) ? (width) : (height));
106                         val /= marker->framenr - prevfra;
107
108                         if (func) {
109                                 int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
110
111                                 func(userdata, track, marker, coord, scene_framenr, val);
112                         }
113
114                         prevval = marker->pos[coord];
115                         prevfra = marker->framenr;
116                 }
117
118                 if (open) {
119                         if (segment_end)
120                                 segment_end(userdata);
121                 }
122         }
123 }
124
125 void clip_graph_tracking_values_iterate(SpaceClip *sc, int selected_only, int include_hidden, void *userdata,
126                 void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker,
127                              int coord, int scene_framenr, float val),
128                 void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord),
129                 void (*segment_end)(void *userdata))
130 {
131         MovieClip *clip = ED_space_clip_get_clip(sc);
132         MovieTracking *tracking = &clip->tracking;
133         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
134         MovieTrackingTrack *track;
135
136         for (track = tracksbase->first; track; track = track->next) {
137                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
138                         continue;
139
140                 if (selected_only && !TRACK_SELECTED(track))
141                         continue;
142
143                 clip_graph_tracking_values_iterate_track(sc, track, userdata, func, segment_start, segment_end);
144         }
145 }
146
147 void clip_graph_tracking_iterate(SpaceClip *sc, int selected_only, int include_hidden, void *userdata,
148                                  void (*func)(void *userdata, MovieTrackingMarker *marker))
149 {
150         MovieClip *clip = ED_space_clip_get_clip(sc);
151         MovieTracking *tracking = &clip->tracking;
152         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
153         MovieTrackingTrack *track;
154
155         for (track = tracksbase->first; track; track = track->next) {
156                 int i;
157
158                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
159                         continue;
160
161                 if (selected_only && !TRACK_SELECTED(track))
162                         continue;
163
164                 for (i = 0; i < track->markersnr; i++) {
165                         MovieTrackingMarker *marker = &track->markers[i];
166
167                         if (marker->flag & MARKER_DISABLED)
168                                 continue;
169
170                         if (func)
171                                 func(userdata, marker);
172                 }
173         }
174 }
175
176 void clip_delete_track(bContext *C, MovieClip *clip, ListBase *tracksbase, MovieTrackingTrack *track)
177 {
178         MovieTracking *tracking = &clip->tracking;
179         MovieTrackingStabilization *stab = &tracking->stabilization;
180         MovieTrackingTrack *act_track = BKE_tracking_track_get_active(tracking);
181
182         int has_bundle = FALSE, update_stab = FALSE;
183
184         if (track == act_track)
185                 tracking->act_track = NULL;
186
187         if (track == stab->rot_track) {
188                 stab->rot_track = NULL;
189
190                 update_stab = TRUE;
191         }
192
193         /* handle reconstruction display in 3d viewport */
194         if (track->flag & TRACK_HAS_BUNDLE)
195                 has_bundle = TRUE;
196
197         BKE_tracking_track_free(track);
198         BLI_freelinkN(tracksbase, track);
199
200         WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
201
202         if (update_stab) {
203                 tracking->stabilization.ok = FALSE;
204                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_DISPLAY, clip);
205         }
206
207         DAG_id_tag_update(&clip->id, 0);
208
209         if (has_bundle)
210                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
211 }
212
213 void clip_delete_marker(bContext *C, MovieClip *clip, ListBase *tracksbase,
214                         MovieTrackingTrack *track, MovieTrackingMarker *marker)
215 {
216         if (track->markersnr == 1) {
217                 clip_delete_track(C, clip, tracksbase, track);
218         }
219         else {
220                 BKE_tracking_marker_delete(track, marker->framenr);
221
222                 WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
223         }
224 }
225
226 void clip_view_center_to_point(SpaceClip *sc, float x, float y)
227 {
228         int width, height;
229         float aspx, aspy;
230
231         ED_space_clip_get_size(sc, &width, &height);
232         ED_space_clip_get_aspect(sc, &aspx, &aspy);
233
234         sc->xof = (x - 0.5f) * width * aspx;
235         sc->yof = (y - 0.5f) * height * aspy;
236 }
237
238 void clip_draw_cfra(SpaceClip *sc, ARegion *ar, Scene *scene)
239 {
240         View2D *v2d = &ar->v2d;
241         float xscale, yscale;
242         float vec[2];
243
244         /* Draw a light green line to indicate current frame */
245         vec[0] = (float)(sc->user.framenr * scene->r.framelen);
246
247         UI_ThemeColor(TH_CFRAME);
248         glLineWidth(2.0);
249
250         glBegin(GL_LINE_STRIP);
251         vec[1] = v2d->cur.ymin;
252         glVertex2fv(vec);
253
254         vec[1] = v2d->cur.ymax;
255         glVertex2fv(vec);
256         glEnd();
257
258         glLineWidth(1.0);
259
260         UI_view2d_view_orthoSpecial(ar, v2d, 1);
261
262         /* because the frame number text is subject to the same scaling as the contents of the view */
263         UI_view2d_getscale(v2d, &xscale, &yscale);
264         glScalef(1.0f / xscale, 1.0f, 1.0f);
265
266         clip_draw_curfra_label(sc->user.framenr, (float)sc->user.framenr * xscale, 18);
267
268         /* restore view transform */
269         glScalef(xscale, 1.0, 1.0);
270 }
271
272 void clip_draw_sfra_efra(View2D *v2d, Scene *scene)
273 {
274         UI_view2d_view_ortho(v2d);
275
276         /* currently clip editor supposes that editing clip length is equal to scene frame range */
277         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
278         glEnable(GL_BLEND);
279         glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
280
281         glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
282         glRectf((float)EFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
283         glDisable(GL_BLEND);
284
285         UI_ThemeColorShade(TH_BACK, -60);
286
287         /* thin lines where the actual frames are */
288         fdrawline((float)SFRA, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
289         fdrawline((float)EFRA, v2d->cur.ymin, (float)EFRA, v2d->cur.ymax);
290 }