2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-06-18 15:12:21 +02:00
|
|
|
|
|
|
|
|
#include "animationcurve.h"
|
2019-06-28 10:45:28 +02:00
|
|
|
#include "curvesegment.h"
|
2021-06-16 13:10:42 +02:00
|
|
|
#include "detail/curveeditorutils.h"
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
#include <QEasingCurve>
|
2019-06-18 15:12:21 +02:00
|
|
|
#include <QLineF>
|
2019-06-28 10:45:28 +02:00
|
|
|
#include <QPainterPath>
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-08-27 14:02:48 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
namespace QmlDesigner {
|
2019-06-18 15:12:21 +02:00
|
|
|
|
|
|
|
|
AnimationCurve::AnimationCurve()
|
2022-07-01 13:22:32 +02:00
|
|
|
: m_type(AnimationCurve::ValueType::Undefined)
|
|
|
|
|
, m_fromData(false)
|
2019-06-28 10:45:28 +02:00
|
|
|
, m_minY(std::numeric_limits<double>::max())
|
|
|
|
|
, m_maxY(std::numeric_limits<double>::lowest())
|
|
|
|
|
, m_frames()
|
2019-06-18 15:12:21 +02:00
|
|
|
{}
|
|
|
|
|
|
2022-07-01 13:22:32 +02:00
|
|
|
AnimationCurve::AnimationCurve(AnimationCurve::ValueType type, const std::vector<Keyframe> &frames)
|
|
|
|
|
: m_type(type)
|
|
|
|
|
, m_fromData(false)
|
2019-06-18 15:12:21 +02:00
|
|
|
, m_minY(std::numeric_limits<double>::max())
|
|
|
|
|
, m_maxY(std::numeric_limits<double>::lowest())
|
2019-06-28 10:45:28 +02:00
|
|
|
, m_frames(frames)
|
2019-06-18 15:12:21 +02:00
|
|
|
{
|
2019-06-28 10:45:28 +02:00
|
|
|
analyze();
|
|
|
|
|
}
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2022-07-01 13:22:32 +02:00
|
|
|
AnimationCurve::AnimationCurve(
|
|
|
|
|
AnimationCurve::ValueType type,
|
|
|
|
|
const QEasingCurve &easing,
|
|
|
|
|
const QPointF &start,
|
|
|
|
|
const QPointF &end)
|
|
|
|
|
: m_type(type)
|
|
|
|
|
, m_fromData(true)
|
2019-06-28 10:45:28 +02:00
|
|
|
, 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());
|
|
|
|
|
};
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2023-06-26 15:57:45 +02:00
|
|
|
QList<QPointF> points = easing.toCubicSpline();
|
2019-08-26 15:12:58 +02:00
|
|
|
int numSegments = points.size() / 3;
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
Keyframe current;
|
|
|
|
|
Keyframe tmp(start);
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-08-26 15:12:58 +02:00
|
|
|
current.setInterpolation(Keyframe::Interpolation::Linear);
|
2019-06-28 10:45:28 +02:00
|
|
|
tmp.setInterpolation(Keyframe::Interpolation::Bezier);
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
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);
|
|
|
|
|
|
2019-08-26 15:12:58 +02:00
|
|
|
current.setInterpolation(tmp.interpolation());
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
tmp.setLeftHandle(p2);
|
|
|
|
|
tmp.setPosition(p3);
|
2019-06-18 15:12:21 +02:00
|
|
|
}
|
2019-06-28 10:45:28 +02:00
|
|
|
|
|
|
|
|
m_frames.push_back(tmp);
|
|
|
|
|
|
|
|
|
|
analyze();
|
2019-06-18 15:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
bool AnimationCurve::isEmpty() const
|
|
|
|
|
{
|
|
|
|
|
return m_frames.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 15:12:21 +02:00
|
|
|
bool AnimationCurve::isValid() const
|
|
|
|
|
{
|
|
|
|
|
return m_frames.size() >= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
bool AnimationCurve::isFromData() const
|
|
|
|
|
{
|
|
|
|
|
return m_fromData;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 15:05:28 +01:00
|
|
|
bool AnimationCurve::hasUnified() const
|
|
|
|
|
{
|
|
|
|
|
for (auto &&frame : m_frames) {
|
|
|
|
|
if (frame.isUnified())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:22:32 +02:00
|
|
|
AnimationCurve::ValueType AnimationCurve::valueType() const
|
|
|
|
|
{
|
|
|
|
|
return m_type;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 15:12:21 +02:00
|
|
|
double AnimationCurve::minimumTime() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_frames.empty())
|
|
|
|
|
return m_frames.front().position().x();
|
|
|
|
|
|
|
|
|
|
return std::numeric_limits<double>::max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double AnimationCurve::maximumTime() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_frames.empty())
|
|
|
|
|
return m_frames.back().position().x();
|
|
|
|
|
|
|
|
|
|
return std::numeric_limits<double>::lowest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double AnimationCurve::minimumValue() const
|
|
|
|
|
{
|
|
|
|
|
return m_minY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double AnimationCurve::maximumValue() const
|
|
|
|
|
{
|
|
|
|
|
return m_maxY;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 14:02:48 +02:00
|
|
|
std::string AnimationCurve::string() const
|
|
|
|
|
{
|
|
|
|
|
std::stringstream sstream;
|
|
|
|
|
sstream << "{ ";
|
|
|
|
|
for (size_t i = 0; i < m_frames.size(); ++i) {
|
|
|
|
|
if (i == m_frames.size() - 1)
|
|
|
|
|
sstream << m_frames[i].string();
|
|
|
|
|
else
|
|
|
|
|
sstream << m_frames[i].string() << ", ";
|
|
|
|
|
}
|
|
|
|
|
sstream << " }";
|
|
|
|
|
|
|
|
|
|
return sstream.str();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 15:05:28 +01:00
|
|
|
QString AnimationCurve::unifyString() const
|
|
|
|
|
{
|
|
|
|
|
QString out;
|
|
|
|
|
for (auto &&frame : m_frames) {
|
|
|
|
|
if (frame.isUnified())
|
|
|
|
|
out.append("1");
|
|
|
|
|
else
|
|
|
|
|
out.append("0");
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
CurveSegment AnimationCurve::segment(double time) const
|
2019-06-18 15:12:21 +02:00
|
|
|
{
|
2019-06-28 10:45:28 +02:00
|
|
|
CurveSegment seg;
|
|
|
|
|
for (auto &frame : m_frames) {
|
|
|
|
|
if (frame.position().x() > time) {
|
|
|
|
|
seg.setRight(frame);
|
|
|
|
|
return seg;
|
|
|
|
|
}
|
|
|
|
|
seg.setLeft(frame);
|
|
|
|
|
}
|
|
|
|
|
return CurveSegment();
|
2019-06-18 15:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
std::vector<CurveSegment> AnimationCurve::segments() const
|
2019-06-18 15:12:21 +02:00
|
|
|
{
|
2019-06-28 10:45:28 +02:00
|
|
|
if (m_frames.empty())
|
|
|
|
|
return {};
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
std::vector<CurveSegment> out;
|
|
|
|
|
|
|
|
|
|
CurveSegment current;
|
|
|
|
|
current.setLeft(m_frames.at(0));
|
2019-06-18 15:12:21 +02:00
|
|
|
|
|
|
|
|
for (size_t i = 1; i < m_frames.size(); ++i) {
|
2019-06-28 10:45:28 +02:00
|
|
|
current.setRight(m_frames[i]);
|
|
|
|
|
out.push_back(current);
|
|
|
|
|
current.setLeft(m_frames[i]);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
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());
|
|
|
|
|
}
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
QPainterPath AnimationCurve::simplePath() const
|
|
|
|
|
{
|
|
|
|
|
if (m_frames.empty())
|
|
|
|
|
return QPainterPath();
|
|
|
|
|
|
|
|
|
|
QPainterPath path(m_frames.front().position());
|
|
|
|
|
|
|
|
|
|
CurveSegment segment;
|
|
|
|
|
segment.setLeft(m_frames.front());
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
for (size_t i = 1; i < m_frames.size(); ++i) {
|
|
|
|
|
segment.setRight(m_frames[i]);
|
|
|
|
|
segment.extend(path);
|
2019-06-18 15:12:21 +02:00
|
|
|
segment.setLeft(m_frames[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPainterPath AnimationCurve::intersectionPath() const
|
|
|
|
|
{
|
|
|
|
|
QPainterPath path = simplePath();
|
|
|
|
|
QPainterPath reversed = path.toReversed();
|
|
|
|
|
path.connectPath(reversed);
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 15:12:58 +02:00
|
|
|
Keyframe AnimationCurve::keyframeAt(size_t id) const
|
|
|
|
|
{
|
|
|
|
|
if (id >= m_frames.size())
|
|
|
|
|
return Keyframe();
|
|
|
|
|
|
|
|
|
|
return m_frames.at(id);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
std::vector<Keyframe> AnimationCurve::keyframes() const
|
|
|
|
|
{
|
|
|
|
|
return m_frames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<QPointF> AnimationCurve::extrema() const
|
|
|
|
|
{
|
|
|
|
|
std::vector<QPointF> out;
|
|
|
|
|
|
|
|
|
|
for (auto &&segment : segments()) {
|
|
|
|
|
const auto es = segment.extrema();
|
|
|
|
|
out.insert(std::end(out), std::begin(es), std::end(es));
|
|
|
|
|
}
|
2019-06-18 15:12:21 +02:00
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<double> AnimationCurve::yForX(double x) const
|
|
|
|
|
{
|
|
|
|
|
if (m_frames.front().position().x() > x)
|
|
|
|
|
return std::vector<double>();
|
|
|
|
|
|
|
|
|
|
CurveSegment segment;
|
|
|
|
|
for (auto &frame : m_frames) {
|
|
|
|
|
if (frame.position().x() > x) {
|
|
|
|
|
segment.setRight(frame);
|
|
|
|
|
return segment.yForX(x);
|
|
|
|
|
}
|
|
|
|
|
segment.setLeft(frame);
|
|
|
|
|
}
|
|
|
|
|
return std::vector<double>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<double> AnimationCurve::xForY(double y, uint segment) const
|
|
|
|
|
{
|
|
|
|
|
if (m_frames.size() > segment + 1) {
|
|
|
|
|
CurveSegment seg(m_frames[segment], m_frames[segment + 1]);
|
|
|
|
|
return seg.xForY(y);
|
|
|
|
|
}
|
|
|
|
|
return std::vector<double>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
bool AnimationCurve::intersects(const QPointF &coord, double radiusX, double radiusY) const
|
2019-06-18 15:12:21 +02:00
|
|
|
{
|
|
|
|
|
if (m_frames.size() < 2)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
std::vector<CurveSegment> influencer;
|
|
|
|
|
|
|
|
|
|
CurveSegment current;
|
|
|
|
|
current.setLeft(m_frames.at(0));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 1; i < m_frames.size(); ++i) {
|
2019-06-28 10:45:28 +02:00
|
|
|
const Keyframe &frame = m_frames.at(i);
|
2019-06-18 15:12:21 +02:00
|
|
|
|
|
|
|
|
current.setRight(frame);
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
if (current.containsX(coord.x() - radiusX) || current.containsX(coord.x())
|
|
|
|
|
|| current.containsX(coord.x() + radiusX)) {
|
2019-06-18 15:12:21 +02:00
|
|
|
influencer.push_back(current);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 10:45:28 +02:00
|
|
|
if (frame.position().x() > coord.x() + radiusX)
|
2019-06-18 15:12:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
current.setLeft(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto &segment : influencer) {
|
2019-06-28 10:45:28 +02:00
|
|
|
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()
|
|
|
|
|
{
|
2021-04-20 15:11:14 +02:00
|
|
|
m_minY = std::numeric_limits<double>::max();
|
|
|
|
|
m_maxY = std::numeric_limits<double>::lowest();
|
2019-06-28 10:45:28 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
auto byTime = [](const auto &a, const auto &b) {
|
|
|
|
|
return a.position().x() < b.position().x();
|
|
|
|
|
};
|
|
|
|
|
std::sort(m_frames.begin(), m_frames.end(), byTime);
|
2019-06-28 10:45:28 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
for (auto e : extrema()) {
|
|
|
|
|
if (m_minY > e.y())
|
|
|
|
|
m_minY = e.y();
|
2019-06-28 10:45:28 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
if (m_maxY < e.y())
|
|
|
|
|
m_maxY = e.y();
|
|
|
|
|
}
|
2019-06-18 15:12:21 +02:00
|
|
|
|
2022-10-07 14:46:06 +02:00
|
|
|
for (auto &frame : std::as_const(m_frames)) {
|
2021-04-20 15:11:14 +02:00
|
|
|
if (frame.position().y() < m_minY)
|
|
|
|
|
m_minY = frame.position().y();
|
2019-06-28 10:45:28 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
if (frame.position().y() > m_maxY)
|
|
|
|
|
m_maxY = frame.position().y();
|
2019-08-14 13:11:38 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
if (frame.hasLeftHandle()) {
|
|
|
|
|
if (frame.leftHandle().y() < m_minY)
|
|
|
|
|
m_minY = frame.leftHandle().y();
|
2019-08-14 13:11:38 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
if (frame.leftHandle().y() > m_maxY)
|
|
|
|
|
m_maxY = frame.leftHandle().y();
|
|
|
|
|
}
|
2019-08-14 13:11:38 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
if (frame.hasRightHandle()) {
|
|
|
|
|
if (frame.rightHandle().y() < m_minY)
|
|
|
|
|
m_minY = frame.rightHandle().y();
|
2019-08-14 13:11:38 +02:00
|
|
|
|
2021-04-20 15:11:14 +02:00
|
|
|
if (frame.rightHandle().y() > m_maxY)
|
|
|
|
|
m_maxY = frame.rightHandle().y();
|
2019-06-18 15:12:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:26:17 +01:00
|
|
|
} // End namespace QmlDesigner.
|