forked from qt-creator/qt-creator
Connect CurveEditor edits to the timeline module
Change-Id: Ic00e0840da34bdbb8627b2fe2d8546a867b24966 Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -24,40 +24,69 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "animationcurve.h"
|
||||
#include "detail/curvesegment.h"
|
||||
#include "curvesegment.h"
|
||||
#include "detail/utils.h"
|
||||
|
||||
#include <QEasingCurve>
|
||||
#include <QLineF>
|
||||
#include <QPainterPath>
|
||||
|
||||
namespace DesignTools {
|
||||
|
||||
AnimationCurve::AnimationCurve()
|
||||
: m_frames()
|
||||
: m_fromData(false)
|
||||
, m_minY(std::numeric_limits<double>::max())
|
||||
, m_maxY(std::numeric_limits<double>::lowest())
|
||||
, m_frames()
|
||||
{}
|
||||
|
||||
AnimationCurve::AnimationCurve(const std::vector<Keyframe> &frames)
|
||||
: m_frames(frames)
|
||||
: m_fromData(false)
|
||||
, m_minY(std::numeric_limits<double>::max())
|
||||
, m_maxY(std::numeric_limits<double>::lowest())
|
||||
, m_frames(frames)
|
||||
{
|
||||
if (isValid()) {
|
||||
analyze();
|
||||
}
|
||||
|
||||
for (auto e : extrema()) {
|
||||
AnimationCurve::AnimationCurve(const QEasingCurve &easing, const QPointF &start, const QPointF &end)
|
||||
: m_fromData(true)
|
||||
, m_minY(std::numeric_limits<double>::max())
|
||||
, m_maxY(std::numeric_limits<double>::lowest())
|
||||
, m_frames()
|
||||
{
|
||||
auto mapPosition = [start, end](const QPointF &pos) {
|
||||
QPointF slope(end.x() - start.x(), end.y() - start.y());
|
||||
return QPointF(start.x() + slope.x() * pos.x(), start.y() + slope.y() * pos.y());
|
||||
};
|
||||
|
||||
if (m_minY > e.y())
|
||||
m_minY = e.y();
|
||||
QVector<QPointF> points = easing.toCubicSpline();
|
||||
int numSegments = points.count() / 3;
|
||||
|
||||
if (m_maxY < e.y())
|
||||
m_maxY = e.y();
|
||||
}
|
||||
Keyframe current;
|
||||
Keyframe tmp(start);
|
||||
|
||||
for (auto &frame : qAsConst(m_frames)) {
|
||||
if (frame.position().y() < m_minY)
|
||||
m_minY = frame.position().y();
|
||||
current.setInterpolation(Keyframe::Interpolation::Bezier);
|
||||
tmp.setInterpolation(Keyframe::Interpolation::Bezier);
|
||||
|
||||
if (frame.position().y() > m_maxY)
|
||||
m_maxY = frame.position().y();
|
||||
}
|
||||
for (int i = 0; i < numSegments; i++) {
|
||||
QPointF p1 = mapPosition(points.at(i * 3));
|
||||
QPointF p2 = mapPosition(points.at(i * 3 + 1));
|
||||
QPointF p3 = mapPosition(points.at(i * 3 + 2));
|
||||
|
||||
current.setPosition(tmp.position());
|
||||
current.setLeftHandle(tmp.leftHandle());
|
||||
current.setRightHandle(p1);
|
||||
|
||||
m_frames.push_back(current);
|
||||
|
||||
tmp.setLeftHandle(p2);
|
||||
tmp.setPosition(p3);
|
||||
}
|
||||
|
||||
m_frames.push_back(tmp);
|
||||
|
||||
analyze();
|
||||
}
|
||||
|
||||
bool AnimationCurve::isValid() const
|
||||
@@ -65,6 +94,11 @@ bool AnimationCurve::isValid() const
|
||||
return m_frames.size() >= 2;
|
||||
}
|
||||
|
||||
bool AnimationCurve::isFromData() const
|
||||
{
|
||||
return m_fromData;
|
||||
}
|
||||
|
||||
double AnimationCurve::minimumTime() const
|
||||
{
|
||||
if (!m_frames.empty())
|
||||
@@ -91,6 +125,70 @@ double AnimationCurve::maximumValue() const
|
||||
return m_maxY;
|
||||
}
|
||||
|
||||
CurveSegment AnimationCurve::segment(double time) const
|
||||
{
|
||||
CurveSegment seg;
|
||||
for (auto &frame : m_frames) {
|
||||
if (frame.position().x() > time) {
|
||||
seg.setRight(frame);
|
||||
return seg;
|
||||
}
|
||||
seg.setLeft(frame);
|
||||
}
|
||||
return CurveSegment();
|
||||
}
|
||||
|
||||
std::vector<CurveSegment> AnimationCurve::segments() const
|
||||
{
|
||||
if (m_frames.empty())
|
||||
return {};
|
||||
|
||||
std::vector<CurveSegment> out;
|
||||
|
||||
CurveSegment current;
|
||||
current.setLeft(m_frames.at(0));
|
||||
|
||||
for (size_t i = 1; i < m_frames.size(); ++i) {
|
||||
current.setRight(m_frames[i]);
|
||||
out.push_back(current);
|
||||
current.setLeft(m_frames[i]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
QPointF mapEasing(const QPointF &start, const QPointF &end, const QPointF &pos)
|
||||
{
|
||||
QPointF slope(end.x() - start.x(), end.y() - start.y());
|
||||
return QPointF(start.x() + slope.x() * pos.x(), start.y() + slope.y() * pos.y());
|
||||
}
|
||||
|
||||
QPainterPath AnimationCurve::simplePath() const
|
||||
{
|
||||
if (m_frames.empty())
|
||||
return QPainterPath();
|
||||
|
||||
QPainterPath path(m_frames.front().position());
|
||||
|
||||
CurveSegment segment;
|
||||
segment.setLeft(m_frames.front());
|
||||
|
||||
for (size_t i = 1; i < m_frames.size(); ++i) {
|
||||
segment.setRight(m_frames[i]);
|
||||
segment.extend(path);
|
||||
segment.setLeft(m_frames[i]);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
QPainterPath AnimationCurve::intersectionPath() const
|
||||
{
|
||||
QPainterPath path = simplePath();
|
||||
QPainterPath reversed = path.toReversed();
|
||||
path.connectPath(reversed);
|
||||
return path;
|
||||
}
|
||||
|
||||
std::vector<Keyframe> AnimationCurve::keyframes() const
|
||||
{
|
||||
return m_frames;
|
||||
@@ -100,19 +198,10 @@ std::vector<QPointF> AnimationCurve::extrema() const
|
||||
{
|
||||
std::vector<QPointF> out;
|
||||
|
||||
CurveSegment segment;
|
||||
segment.setLeft(m_frames.at(0));
|
||||
|
||||
for (size_t i = 1; i < m_frames.size(); ++i) {
|
||||
|
||||
segment.setRight(m_frames[i]);
|
||||
|
||||
for (auto &&segment : segments()) {
|
||||
const auto es = segment.extrema();
|
||||
out.insert(std::end(out), std::begin(es), std::end(es));
|
||||
|
||||
segment.setLeft(m_frames[i]);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -141,7 +230,7 @@ std::vector<double> AnimationCurve::xForY(double y, uint segment) const
|
||||
return std::vector<double>();
|
||||
}
|
||||
|
||||
bool AnimationCurve::intersects(const QPointF &coord, double radius)
|
||||
bool AnimationCurve::intersects(const QPointF &coord, double radiusX, double radiusY) const
|
||||
{
|
||||
if (m_frames.size() < 2)
|
||||
return false;
|
||||
@@ -152,36 +241,94 @@ bool AnimationCurve::intersects(const QPointF &coord, double radius)
|
||||
current.setLeft(m_frames.at(0));
|
||||
|
||||
for (size_t i = 1; i < m_frames.size(); ++i) {
|
||||
Keyframe &frame = m_frames.at(i);
|
||||
const Keyframe &frame = m_frames.at(i);
|
||||
|
||||
current.setRight(frame);
|
||||
|
||||
if (current.containsX(coord.x() - radius) ||
|
||||
current.containsX(coord.x()) ||
|
||||
current.containsX(coord.x() + radius)) {
|
||||
if (current.containsX(coord.x() - radiusX) || current.containsX(coord.x())
|
||||
|| current.containsX(coord.x() + radiusX)) {
|
||||
influencer.push_back(current);
|
||||
}
|
||||
|
||||
if (frame.position().x() > coord.x() + radius)
|
||||
if (frame.position().x() > coord.x() + radiusX)
|
||||
break;
|
||||
|
||||
current.setLeft(frame);
|
||||
}
|
||||
|
||||
for (auto &segment : influencer) {
|
||||
for (auto &y : segment.yForX(coord.x())) {
|
||||
QLineF line(coord.x(), y, coord.x(), coord.y());
|
||||
if (line.length() < radius)
|
||||
return true;
|
||||
}
|
||||
|
||||
for (auto &x : segment.xForY(coord.y())) {
|
||||
QLineF line(x, coord.y(), coord.x(), coord.y());
|
||||
if (line.length() < radius)
|
||||
return true;
|
||||
}
|
||||
if (segment.intersects(coord, radiusX, radiusY))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AnimationCurve::append(const AnimationCurve &other)
|
||||
{
|
||||
if (!other.isValid())
|
||||
return;
|
||||
|
||||
if (!isValid()) {
|
||||
m_frames = other.keyframes();
|
||||
analyze();
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<Keyframe> otherFrames = other.keyframes();
|
||||
m_frames.back().setRightHandle(otherFrames.front().rightHandle());
|
||||
m_frames.insert(std::end(m_frames), std::begin(otherFrames) + 1, std::end(otherFrames));
|
||||
analyze();
|
||||
}
|
||||
|
||||
void AnimationCurve::insert(double time)
|
||||
{
|
||||
CurveSegment seg = segment(time);
|
||||
|
||||
if (!seg.isValid())
|
||||
return;
|
||||
|
||||
auto insertFrames = [this](std::array<Keyframe, 3> &&frames) {
|
||||
auto samePosition = [frames](const Keyframe &frame) {
|
||||
return frame.position() == frames[0].position();
|
||||
};
|
||||
|
||||
auto iter = std::find_if(m_frames.begin(), m_frames.end(), samePosition);
|
||||
if (iter != m_frames.end()) {
|
||||
auto erased = m_frames.erase(iter, iter + 2);
|
||||
m_frames.insert(erased, frames.begin(), frames.end());
|
||||
}
|
||||
};
|
||||
|
||||
insertFrames(seg.splitAt(time));
|
||||
}
|
||||
|
||||
void AnimationCurve::analyze()
|
||||
{
|
||||
if (isValid()) {
|
||||
m_minY = std::numeric_limits<double>::max();
|
||||
m_maxY = std::numeric_limits<double>::lowest();
|
||||
|
||||
auto byTime = [](const auto &a, const auto &b) {
|
||||
return a.position().x() < b.position().x();
|
||||
};
|
||||
std::sort(m_frames.begin(), m_frames.end(), byTime);
|
||||
|
||||
for (auto e : extrema()) {
|
||||
if (m_minY > e.y())
|
||||
m_minY = e.y();
|
||||
|
||||
if (m_maxY < e.y())
|
||||
m_maxY = e.y();
|
||||
}
|
||||
|
||||
for (auto &frame : qAsConst(m_frames)) {
|
||||
if (frame.position().y() < m_minY)
|
||||
m_minY = frame.position().y();
|
||||
|
||||
if (frame.position().y() > m_maxY)
|
||||
m_maxY = frame.position().y();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // End namespace DesignTools.
|
||||
|
||||
Reference in New Issue
Block a user