using boost::weak_ptr;
using boost::dynamic_pointer_cast;
using boost::bind;
+using boost::optional;
+/** Parent class for components of the timeline (e.g. a piece of content or an axis) */
class View : public boost::noncopyable
{
public:
- View (Timeline& t)
+ View (DCPTimeline& t)
: _timeline (t)
{
protected:
virtual void do_paint (wxGraphicsContext *) = 0;
- int time_x (Time t) const
+ int time_x (DCPTime t) const
{
return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
}
- Timeline& _timeline;
+ DCPTimeline& _timeline;
private:
dcpomatic::Rect<int> _last_paint_bbox;
};
+
+/** Parent class for views of pieces of content */
class ContentView : public View
{
public:
- ContentView (Timeline& tl, shared_ptr<Content> c)
+ ContentView (DCPTimeline& tl, shared_ptr<Content> c)
: View (tl)
, _content (c)
, _track (0)
return;
}
- Time const position = cont->position ();
- Time const len = cont->length_after_trim ();
+ DCPTime const position = cont->position ();
+ DCPTime const len = cont->length_after_trim ();
wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
gc->StrokePath (path);
gc->FillPath (path);
- wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path().filename().string()).data(), type().data());
+ wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path_summary()).data(), type().data());
wxDouble name_width;
wxDouble name_height;
wxDouble name_descent;
class AudioContentView : public ContentView
{
public:
- AudioContentView (Timeline& tl, shared_ptr<Content> c)
+ AudioContentView (DCPTimeline& tl, shared_ptr<Content> c)
: ContentView (tl, c)
{}
class VideoContentView : public ContentView
{
public:
- VideoContentView (Timeline& tl, shared_ptr<Content> c)
+ VideoContentView (DCPTimeline& tl, shared_ptr<Content> c)
: ContentView (tl, c)
{}
}
};
-class TimeAxisView : public View
+class DCPTimeAxisView : public View
{
public:
- TimeAxisView (Timeline& tl, int y)
+ DCPTimeAxisView (DCPTimeline& tl, int y)
: View (tl)
, _y (y)
{}
path.AddLineToPoint (_timeline.width(), _y);
gc->StrokePath (path);
- Time t = 0;
+ DCPTime t = 0;
while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
wxGraphicsPath path = gc->CreatePath ();
path.MoveToPoint (time_x (t), _y - 4);
int _y;
};
-Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
+
+DCPTimeline::DCPTimeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
: wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
, _film_editor (ed)
, _film (film)
- , _time_axis_view (new TimeAxisView (*this, 32))
+ , _time_axis_view (new DCPTimeAxisView (*this, 32))
, _tracks (0)
, _pixels_per_time_unit (0)
, _left_down (false)
, _down_view_position (0)
, _first_move (false)
- , _menu (film, this)
+ , _menu (this)
+ , _snap (true)
{
#ifndef __WXOSX__
SetDoubleBuffered (true);
#endif
- Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
- Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
- Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
- Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
- Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
- Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
+ Bind (wxEVT_PAINT, boost::bind (&DCPTimeline::paint, this));
+ Bind (wxEVT_LEFT_DOWN, boost::bind (&DCPTimeline::left_down, this, _1));
+ Bind (wxEVT_LEFT_UP, boost::bind (&DCPTimeline::left_up, this, _1));
+ Bind (wxEVT_RIGHT_DOWN, boost::bind (&DCPTimeline::right_down, this, _1));
+ Bind (wxEVT_MOTION, boost::bind (&DCPTimeline::mouse_moved, this, _1));
+ Bind (wxEVT_SIZE, boost::bind (&DCPTimeline::resized, this));
playlist_changed ();
SetMinSize (wxSize (640, tracks() * track_height() + 96));
- _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
+ _playlist_connection = film->playlist()->Changed.connect (bind (&DCPTimeline::playlist_changed, this));
}
void
-Timeline::paint ()
+DCPTimeline::paint ()
{
wxPaintDC dc (this);
}
void
-Timeline::playlist_changed ()
+DCPTimeline::playlist_changed ()
{
ensure_ui_thread ();
}
void
-Timeline::assign_tracks ()
+DCPTimeline::assign_tracks ()
{
for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
}
int
-Timeline::tracks () const
+DCPTimeline::tracks () const
{
return _tracks;
}
void
-Timeline::setup_pixels_per_time_unit ()
+DCPTimeline::setup_pixels_per_time_unit ()
{
shared_ptr<const Film> film = _film.lock ();
- if (!film) {
+ if (!film || film->length() == 0) {
return;
}
}
shared_ptr<View>
-Timeline::event_to_view (wxMouseEvent& ev)
+DCPTimeline::event_to_view (wxMouseEvent& ev)
{
ViewList::iterator i = _views.begin();
Position<int> const p (ev.GetX(), ev.GetY());
}
void
-Timeline::left_down (wxMouseEvent& ev)
+DCPTimeline::left_down (wxMouseEvent& ev)
{
shared_ptr<View> view = event_to_view (ev);
shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
}
void
-Timeline::left_up (wxMouseEvent& ev)
+DCPTimeline::left_up (wxMouseEvent& ev)
{
_left_down = false;
}
void
-Timeline::mouse_moved (wxMouseEvent& ev)
+DCPTimeline::mouse_moved (wxMouseEvent& ev)
{
if (!_left_down) {
return;
}
void
-Timeline::right_down (wxMouseEvent& ev)
+DCPTimeline::right_down (wxMouseEvent& ev)
{
shared_ptr<View> view = event_to_view (ev);
shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
cv->set_selected (true);
}
- _menu.popup (selected_content (), ev.GetPosition ());
+ _menu.popup (_film, selected_content (), ev.GetPosition ());
}
void
-Timeline::set_position_from_event (wxMouseEvent& ev)
+DCPTimeline::set_position_from_event (wxMouseEvent& ev)
{
wxPoint const p = ev.GetPosition();
if (!_first_move) {
+ /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
+ before the drag is considered to have started.
+ */
int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
if (dist < 8) {
return;
_first_move = true;
}
- Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
- if (_down_view) {
- _down_view->content()->set_position (max (static_cast<Time> (0), _down_view_position + time_diff));
-
- shared_ptr<Film> film = _film.lock ();
- assert (film);
- film->set_sequence_video (false);
+ if (!_down_view) {
+ return;
+ }
+
+ DCPTime new_position = _down_view_position + (p.x - _down_point.x) / _pixels_per_time_unit;
+
+ if (_snap) {
+
+ bool first = true;
+ DCPTime nearest_distance = TIME_MAX;
+ DCPTime nearest_new_position = TIME_MAX;
+
+ /* Find the nearest content edge; this is inefficient */
+ for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
+ shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
+ if (!cv || cv == _down_view) {
+ continue;
+ }
+
+ {
+ /* Snap starts to ends */
+ DCPTime const d = abs (cv->content()->end() - new_position);
+ if (first || d < nearest_distance) {
+ nearest_distance = d;
+ nearest_new_position = cv->content()->end();
+ }
+ }
+
+ {
+ /* Snap ends to starts */
+ DCPTime const d = abs (cv->content()->position() - (new_position + _down_view->content()->length_after_trim()));
+ if (d < nearest_distance) {
+ nearest_distance = d;
+ nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
+ }
+ }
+
+ first = false;
+ }
+
+ if (!first) {
+ /* Snap if it's close; `close' means within a proportion of the time on the timeline */
+ if (nearest_distance < (width() / pixels_per_time_unit()) / 32) {
+ new_position = nearest_new_position;
+ }
+ }
+ }
+
+ if (new_position < 0) {
+ new_position = 0;
}
+
+ _down_view->content()->set_position (new_position);
+
+ shared_ptr<Film> film = _film.lock ();
+ assert (film);
+ film->set_sequence_video (false);
}
void
-Timeline::force_redraw (dcpomatic::Rect<int> const & r)
+DCPTimeline::force_redraw (dcpomatic::Rect<int> const & r)
{
RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
}
shared_ptr<const Film>
-Timeline::film () const
+DCPTimeline::film () const
{
return _film.lock ();
}
void
-Timeline::resized ()
+DCPTimeline::resized ()
{
setup_pixels_per_time_unit ();
}
void
-Timeline::clear_selection ()
+DCPTimeline::clear_selection ()
{
for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
}
}
-Timeline::ContentViewList
-Timeline::selected_views () const
+DCPTimeline::ContentViewList
+DCPTimeline::selected_views () const
{
ContentViewList sel;
}
ContentList
-Timeline::selected_content () const
+DCPTimeline::selected_content () const
{
ContentList sel;
ContentViewList views = selected_views ();