Basics of Atmos content support; can be added to project and appears in timeline.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (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
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "film_editor.h"
21 #include "timeline.h"
22 #include "timeline_time_axis_view.h"
23 #include "timeline_reels_view.h"
24 #include "timeline_labels_view.h"
25 #include "timeline_video_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_subtitle_content_view.h"
28 #include "timeline_atmos_content_view.h"
29 #include "content_panel.h"
30 #include "wx_util.h"
31 #include "lib/film.h"
32 #include "lib/playlist.h"
33 #include "lib/image_content.h"
34 #include "lib/timer.h"
35 #include "lib/audio_content.h"
36 #include "lib/subtitle_content.h"
37 #include "lib/video_content.h"
38 #include "lib/atmos_mxf_content.h"
39 #include <wx/graphics.h>
40 #include <boost/weak_ptr.hpp>
41 #include <boost/foreach.hpp>
42 #include <list>
43 #include <iostream>
44
45 using std::list;
46 using std::cout;
47 using std::max;
48 using boost::shared_ptr;
49 using boost::weak_ptr;
50 using boost::dynamic_pointer_cast;
51 using boost::bind;
52 using boost::optional;
53
54 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
55         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
56         , _content_panel (cp)
57         , _film (film)
58         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
59         , _reels_view (new TimelineReelsView (*this, 32))
60         , _labels_view (new TimelineLabelsView (*this))
61         , _tracks (0)
62         , _left_down (false)
63         , _down_view_position (0)
64         , _first_move (false)
65         , _menu (this)
66         , _snap (true)
67 {
68 #ifndef __WXOSX__
69         SetDoubleBuffered (true);
70 #endif
71
72         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
73         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
74         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
75         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
76         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
77         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
78
79         film_changed (Film::CONTENT);
80
81         SetMinSize (wxSize (640, tracks() * track_height() + 96));
82
83         _tracks_position = Position<int> (_labels_view->bbox().width, 32);
84
85         _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
86         _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
87 }
88
89 void
90 Timeline::paint ()
91 {
92         wxPaintDC dc (this);
93
94         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
95         if (!gc) {
96                 return;
97         }
98
99         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
100
101         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
102
103                 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
104
105                 /* Find areas of overlap */
106                 list<dcpomatic::Rect<int> > overlaps;
107                 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
108                         shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
109                         /* No overlap with non-content views, views no different tracks, audio views or non-active views */
110                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
111                                 continue;
112                         }
113
114                         optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
115                         if (r) {
116                                 overlaps.push_back (r.get ());
117                         }
118                 }
119
120                 i->paint (gc, overlaps);
121         }
122
123         delete gc;
124 }
125
126 void
127 Timeline::film_changed (Film::Property p)
128 {
129         if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
130                 ensure_ui_thread ();
131                 recreate_views ();
132         } else if (p == Film::CONTENT_ORDER) {
133                 Refresh ();
134         }
135 }
136
137 void
138 Timeline::recreate_views ()
139 {
140         shared_ptr<const Film> film = _film.lock ();
141         if (!film) {
142                 return;
143         }
144
145         _views.clear ();
146         _views.push_back (_time_axis_view);
147         _views.push_back (_reels_view);
148         _views.push_back (_labels_view);
149
150         BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
151                 if (i->video) {
152                         _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
153                 }
154
155                 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
156                         _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
157                 }
158
159                 if (i->subtitle) {
160                         _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
161                 }
162
163                 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
164                         _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
165                 }
166         }
167
168         assign_tracks ();
169         setup_pixels_per_second ();
170         Refresh ();
171 }
172
173 void
174 Timeline::film_content_changed (int property, bool frequent)
175 {
176         ensure_ui_thread ();
177
178         if (property == AudioContentProperty::STREAMS) {
179                 recreate_views ();
180         } else if (!frequent) {
181                 setup_pixels_per_second ();
182                 Refresh ();
183         }
184 }
185
186 void
187 Timeline::assign_tracks ()
188 {
189         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
190                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
191                 if (c) {
192                         c->unset_track ();
193                 }
194         }
195
196         /* See if we have any subtitle / atmos / right-eye views */
197         bool have_3d = false;
198         bool have_subtitle = false;
199         bool have_atmos = false;
200         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
201                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
202                 if (!cv) {
203                         continue;
204                 }
205
206                 if (dynamic_pointer_cast<TimelineVideoContentView> (i)) {
207                         if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
208                                 have_3d = true;
209                         }
210                 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (i)) {
211                         have_subtitle = true;
212                 } else if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
213                         have_atmos = true;
214                 }
215         }
216
217         _labels_view->set_3d (have_3d);
218         _labels_view->set_subtitle (have_subtitle);
219         _labels_view->set_atmos (have_atmos);
220
221         /* Hence decide where to start subtitle, atmos and audio tracks */
222         int const subtitle = have_3d ? 2 : 1;
223         int const atmos = have_subtitle ? subtitle + 1 : subtitle;
224         int const audio = have_atmos ? atmos + 1: atmos;
225
226         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
227                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
228                 if (!cv) {
229                         continue;
230                 }
231
232                 if (dynamic_pointer_cast<TimelineVideoContentView> (*i)) {
233                         /* Video on tracks 0 and maybe 1 (left and right eye) */
234                         cv->set_track (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT ? 1 : 0);
235                         _tracks = max (_tracks, have_3d ? 2 : 1);
236                         continue;
237                 } else if (dynamic_pointer_cast<TimelineSubtitleContentView> (*i)) {
238                         cv->set_track (subtitle);
239                         _tracks = max (_tracks, subtitle + 1);
240                         continue;
241                 } else if (dynamic_pointer_cast<TimelineAtmosContentView> (*i)) {
242                         cv->set_track (atmos);
243                         _tracks = max (_tracks, atmos + 1);
244                         continue;
245                 }
246
247                 int t = audio;
248
249                 shared_ptr<Content> content = cv->content();
250                 DCPTimePeriod content_period (content->position(), content->end());
251
252                 while (true) {
253                         TimelineViewList::iterator j = _views.begin();
254                         while (j != _views.end()) {
255                                 shared_ptr<TimelineAudioContentView> test = dynamic_pointer_cast<TimelineAudioContentView> (*j);
256                                 if (!test) {
257                                         ++j;
258                                         continue;
259                                 }
260
261                                 shared_ptr<Content> test_content = test->content();
262
263                                 if (test && test->track() && test->track().get() == t) {
264                                         if (content_period.overlaps (DCPTimePeriod(test_content->position(), test_content->end()))) {
265                                                 /* we have an overlap on track `t' */
266                                                 ++t;
267                                                 break;
268                                         }
269                                 }
270
271                                 ++j;
272                         }
273
274                         if (j == _views.end ()) {
275                                 /* no overlap on `t' */
276                                 break;
277                         }
278                 }
279
280                 cv->set_track (t);
281                 _tracks = max (_tracks, t + 1);
282         }
283
284         _time_axis_view->set_y (tracks() * track_height() + 64);
285         _reels_view->set_y (8);
286 }
287
288 int
289 Timeline::tracks () const
290 {
291         return _tracks;
292 }
293
294 void
295 Timeline::setup_pixels_per_second ()
296 {
297         shared_ptr<const Film> film = _film.lock ();
298         if (!film || film->length() == DCPTime ()) {
299                 return;
300         }
301
302         _pixels_per_second = static_cast<double>(width() - tracks_position().x * 2) / film->length().seconds ();
303 }
304
305 shared_ptr<TimelineView>
306 Timeline::event_to_view (wxMouseEvent& ev)
307 {
308         /* Search backwards through views so that we find the uppermost one first */
309         TimelineViewList::reverse_iterator i = _views.rbegin();
310         Position<int> const p (ev.GetX(), ev.GetY());
311         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
312                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
313                 ++i;
314         }
315
316         if (i == _views.rend ()) {
317                 return shared_ptr<TimelineView> ();
318         }
319
320         return *i;
321 }
322
323 void
324 Timeline::left_down (wxMouseEvent& ev)
325 {
326         shared_ptr<TimelineView> view = event_to_view (ev);
327         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
328
329         _down_view.reset ();
330
331         if (content_view) {
332                 _down_view = content_view;
333                 _down_view_position = content_view->content()->position ();
334         }
335
336         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
337                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
338                 if (!cv) {
339                         continue;
340                 }
341
342                 if (!ev.ShiftDown ()) {
343                         cv->set_selected (view == *i);
344                 }
345         }
346
347         if (content_view && ev.ShiftDown ()) {
348                 content_view->set_selected (!content_view->selected ());
349         }
350
351         _left_down = true;
352         _down_point = ev.GetPosition ();
353         _first_move = false;
354
355         if (_down_view) {
356                 /* Pre-compute the points that we might snap to */
357                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
358                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
359                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
360                                 continue;
361                         }
362
363                         _start_snaps.push_back (cv->content()->position());
364                         _end_snaps.push_back (cv->content()->position());
365                         _start_snaps.push_back (cv->content()->end());
366                         _end_snaps.push_back (cv->content()->end());
367
368                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
369                                 _start_snaps.push_back (i);
370                         }
371                 }
372
373                 /* Tell everyone that things might change frequently during the drag */
374                 _down_view->content()->set_change_signals_frequent (true);
375         }
376 }
377
378 void
379 Timeline::left_up (wxMouseEvent& ev)
380 {
381         _left_down = false;
382
383         if (_down_view) {
384                 _down_view->content()->set_change_signals_frequent (false);
385                 _content_panel->set_selection (_down_view->content ());
386         }
387
388         set_position_from_event (ev);
389
390         /* Clear up up the stuff we don't do during drag */
391         assign_tracks ();
392         setup_pixels_per_second ();
393         Refresh ();
394
395         _start_snaps.clear ();
396         _end_snaps.clear ();
397 }
398
399 void
400 Timeline::mouse_moved (wxMouseEvent& ev)
401 {
402         if (!_left_down) {
403                 return;
404         }
405
406         set_position_from_event (ev);
407 }
408
409 void
410 Timeline::right_down (wxMouseEvent& ev)
411 {
412         shared_ptr<TimelineView> view = event_to_view (ev);
413         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
414         if (!cv) {
415                 return;
416         }
417
418         if (!cv->selected ()) {
419                 clear_selection ();
420                 cv->set_selected (true);
421         }
422
423         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
424 }
425
426 void
427 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
428 {
429         DCPTime const d = a - b;
430         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
431                 nearest_distance = d;
432         }
433 }
434
435 void
436 Timeline::set_position_from_event (wxMouseEvent& ev)
437 {
438         if (!_pixels_per_second) {
439                 return;
440         }
441
442         double const pps = _pixels_per_second.get ();
443
444         wxPoint const p = ev.GetPosition();
445
446         if (!_first_move) {
447                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
448                    before the drag is considered to have started.
449                 */
450                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
451                 if (dist < 8) {
452                         return;
453                 }
454                 _first_move = true;
455         }
456
457         if (!_down_view) {
458                 return;
459         }
460
461         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
462
463         if (_snap) {
464
465                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
466                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
467                    positive is right).
468                 */
469                 optional<DCPTime> nearest_distance;
470
471                 /* Find the nearest snap point */
472
473                 BOOST_FOREACH (DCPTime i, _start_snaps) {
474                         maybe_snap (i, new_position, nearest_distance);
475                 }
476
477                 BOOST_FOREACH (DCPTime i, _end_snaps) {
478                         maybe_snap (i, new_end, nearest_distance);
479                 }
480
481                 if (nearest_distance) {
482                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
483                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
484                                 new_position += nearest_distance.get ();
485                         }
486                 }
487         }
488
489         if (new_position < DCPTime ()) {
490                 new_position = DCPTime ();
491         }
492
493         _down_view->content()->set_position (new_position);
494
495         shared_ptr<Film> film = _film.lock ();
496         DCPOMATIC_ASSERT (film);
497         film->set_sequence (false);
498 }
499
500 void
501 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
502 {
503         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
504 }
505
506 shared_ptr<const Film>
507 Timeline::film () const
508 {
509         return _film.lock ();
510 }
511
512 void
513 Timeline::resized ()
514 {
515         setup_pixels_per_second ();
516 }
517
518 void
519 Timeline::clear_selection ()
520 {
521         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
522                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
523                 if (cv) {
524                         cv->set_selected (false);
525                 }
526         }
527 }
528
529 TimelineContentViewList
530 Timeline::selected_views () const
531 {
532         TimelineContentViewList sel;
533
534         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
535                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
536                 if (cv && cv->selected()) {
537                         sel.push_back (cv);
538                 }
539         }
540
541         return sel;
542 }
543
544 ContentList
545 Timeline::selected_content () const
546 {
547         ContentList sel;
548         TimelineContentViewList views = selected_views ();
549
550         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
551                 sel.push_back ((*i)->content ());
552         }
553
554         return sel;
555 }
556
557 void
558 Timeline::set_selection (ContentList selection)
559 {
560         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
561                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
562                 if (cv) {
563                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
564                 }
565         }
566 }