From 6d29c7d64fd2eeb73b7a3158dba2784b07c2cc08 Mon Sep 17 00:00:00 2001 From: Darby Johnston Date: Fri, 18 Oct 2024 18:26:35 -0700 Subject: [PATCH] Refactoring --- bin/toucan-view/IItem.cpp | 29 +++++++++++++++++++++++++++++ bin/toucan-view/IItem.h | 3 +++ bin/toucan-view/TimelineItem.cpp | 29 ----------------------------- bin/toucan-view/TimelineItem.h | 3 --- bin/toucan-view/TrackItem.cpp | 14 +++++++++----- 5 files changed, 41 insertions(+), 37 deletions(-) diff --git a/bin/toucan-view/IItem.cpp b/bin/toucan-view/IItem.cpp index 6a0874e..c374ab4 100644 --- a/bin/toucan-view/IItem.cpp +++ b/bin/toucan-view/IItem.cpp @@ -59,4 +59,33 @@ namespace toucan _selected = value; _setDrawUpdate(); } + + OTIO_NS::RationalTime IItem::posToTime(double value) const + { + OTIO_NS::RationalTime out; + const dtk::Box2I& g = getGeometry(); + if (g.w() > 0) + { + const double normalized = (value - g.min.x) / + static_cast(_timeRange.duration().rescaled_to(1.0).value() * _scale); + out = OTIO_NS::RationalTime( + _timeRange.start_time() + + OTIO_NS::RationalTime( + _timeRange.duration().value() * normalized, + _timeRange.duration().rate())). + round(); + out = dtk::clamp( + out, + _timeRange.start_time(), + _timeRange.end_time_inclusive()); + } + return out; + } + + int IItem::timeToPos(const OTIO_NS::RationalTime& value) const + { + const dtk::Box2I& g = getGeometry(); + const OTIO_NS::RationalTime t = value - _timeRange.start_time(); + return g.min.x + t.rescaled_to(1.0).value() * _scale; + } } diff --git a/bin/toucan-view/IItem.h b/bin/toucan-view/IItem.h index b15a61b..55cad6f 100644 --- a/bin/toucan-view/IItem.h +++ b/bin/toucan-view/IItem.h @@ -35,6 +35,9 @@ namespace toucan bool isSelected() const; void setSelected(bool); + OTIO_NS::RationalTime posToTime(double) const; + int timeToPos(const OTIO_NS::RationalTime&) const; + protected: OTIO_NS::SerializableObject::Retainer _item; OTIO_NS::TimeRange _timeRange; diff --git a/bin/toucan-view/TimelineItem.cpp b/bin/toucan-view/TimelineItem.cpp index 288499a..be43628 100644 --- a/bin/toucan-view/TimelineItem.cpp +++ b/bin/toucan-view/TimelineItem.cpp @@ -96,35 +96,6 @@ namespace toucan _currentTimeCallback = value; } - OTIO_NS::RationalTime TimelineItem::posToTime(double value) const - { - OTIO_NS::RationalTime out; - const dtk::Box2I& g = getGeometry(); - if (g.w() > 0) - { - const double normalized = (value - g.min.x) / - static_cast(_timeRange.duration().rescaled_to(1.0).value() * _scale); - out = OTIO_NS::RationalTime( - _timeRange.start_time() + - OTIO_NS::RationalTime( - _timeRange.duration().value() * normalized, - _timeRange.duration().rate())). - round(); - out = dtk::clamp( - out, - _timeRange.start_time(), - _timeRange.end_time_inclusive()); - } - return out; - } - - int TimelineItem::timeToPos(const OTIO_NS::RationalTime& value) const - { - const dtk::Box2I& g = getGeometry(); - const OTIO_NS::RationalTime t = value - _timeRange.start_time(); - return g.min.x + t.rescaled_to(1.0).value() * _scale; - } - void TimelineItem::setGeometry(const dtk::Box2I& value) { IItem::setGeometry(value); diff --git a/bin/toucan-view/TimelineItem.h b/bin/toucan-view/TimelineItem.h index 9c85041..db072a9 100644 --- a/bin/toucan-view/TimelineItem.h +++ b/bin/toucan-view/TimelineItem.h @@ -37,9 +37,6 @@ namespace toucan void setCurrentTime(const OTIO_NS::RationalTime&); void setCurrentTimeCallback(const std::function&); - OTIO_NS::RationalTime posToTime(double) const; - int timeToPos(const OTIO_NS::RationalTime&) const; - void setGeometry(const dtk::Box2I&) override; void tickEvent( bool parentsVisible, diff --git a/bin/toucan-view/TrackItem.cpp b/bin/toucan-view/TrackItem.cpp index 66b97c5..d0280f1 100644 --- a/bin/toucan-view/TrackItem.cpp +++ b/bin/toucan-view/TrackItem.cpp @@ -70,13 +70,17 @@ namespace toucan { IItem::setGeometry(value); const dtk::Box2I& g = getGeometry(); - dtk::V2I pos = g.min; - pos.y += _size.fontMetrics.lineHeight + _size.margin * 2 + _size.border * 2; + const int y = g.min.y + _size.fontMetrics.lineHeight + _size.margin * 2 + _size.border * 2; for (const auto& child : getChildren()) { - const dtk::Size2I& sizeHint = child->getSizeHint(); - child->setGeometry(dtk::Box2I(pos.x, pos.y, sizeHint.w, sizeHint.h)); - pos.x += sizeHint.w; + if (auto item = std::dynamic_pointer_cast(child)) + { + const OTIO_NS::TimeRange& timeRange = item->getTimeRange(); + const int x0 = timeToPos(timeRange.start_time()); + const int x1 = timeToPos(timeRange.end_time_exclusive()); + const dtk::Size2I& sizeHint = child->getSizeHint(); + child->setGeometry(dtk::Box2I(x0, y, x1 - x0 + 1, sizeHint.h)); + } } }