Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Oct 19, 2024
1 parent dc7b700 commit 6d29c7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
29 changes: 29 additions & 0 deletions bin/toucan-view/IItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(_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;
}
}
3 changes: 3 additions & 0 deletions bin/toucan-view/IItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<OTIO_NS::Item> _item;
OTIO_NS::TimeRange _timeRange;
Expand Down
29 changes: 0 additions & 29 deletions bin/toucan-view/TimelineItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(_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);
Expand Down
3 changes: 0 additions & 3 deletions bin/toucan-view/TimelineItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ namespace toucan
void setCurrentTime(const OTIO_NS::RationalTime&);
void setCurrentTimeCallback(const std::function<void(const OTIO_NS::RationalTime&)>&);

OTIO_NS::RationalTime posToTime(double) const;
int timeToPos(const OTIO_NS::RationalTime&) const;

void setGeometry(const dtk::Box2I&) override;
void tickEvent(
bool parentsVisible,
Expand Down
14 changes: 9 additions & 5 deletions bin/toucan-view/TrackItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IItem>(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));
}
}
}

Expand Down

0 comments on commit 6d29c7d

Please sign in to comment.