enve/examples/pathEffects/eLinearize/elinearize.cpp

121 lines
3.7 KiB
C++
Raw Permalink Normal View History

2019-08-24 15:07:28 +00:00
// enve - 2D animations software
2020-01-02 19:46:10 +00:00
// Copyright (C) 2016-2020 Maurycy Liebner
2019-08-24 15:07:28 +00:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-08-28 16:30:12 +00:00
#include "elinearize.h"
2019-09-05 19:08:59 +00:00
#include "enveCore/Animators/qrealanimator.h"
2019-06-02 16:53:04 +00:00
2020-03-31 14:18:42 +00:00
void eCreateNewestVersion(qsptr<CustomPathEffect> &result) {
2019-07-06 12:57:14 +00:00
// Use default, most up to date, version
2020-03-31 14:18:42 +00:00
result = enve::make_shared<eLinearize000>();
2019-06-02 16:53:04 +00:00
}
2020-03-31 14:18:42 +00:00
void eCreate(const CustomIdentifier &identifier,
qsptr<CustomPathEffect> &result) {
2019-12-23 13:02:55 +00:00
Q_UNUSED(identifier)
2019-07-06 12:57:14 +00:00
// Choose version based on identifier
2019-07-06 13:57:28 +00:00
// if(identifier.fVersion == CustomIdentifier::Version{0, 0, 0})
2020-03-31 14:18:42 +00:00
result = enve::make_shared<eLinearize000>();
2019-07-06 12:57:14 +00:00
}
// Returned value must be unique, lets enve distinguish effects
QString effectId() {
return "waer9yv11r3gl10x1qtm";
}
2020-03-31 14:18:42 +00:00
#define eLName QStringLiteral("eLinearize")
2019-07-06 12:57:14 +00:00
// Name of your effect used in UI
2020-03-31 14:18:42 +00:00
void eName(QString &result) {
result = eLName;
2019-06-02 16:53:04 +00:00
}
2019-07-06 13:57:28 +00:00
// here specify your effect's most up to date version
CustomIdentifier::Version effectVersion() {
return { 0, 0, 0 };
2019-07-06 12:57:14 +00:00
}
2020-03-31 14:18:42 +00:00
void eIdentifier(CustomIdentifier& result) {
result = { effectId(), eLName, effectVersion() };
2019-06-02 16:53:04 +00:00
}
2019-08-08 19:45:22 +00:00
bool eSupports(const CustomIdentifier &identifier) {
2019-07-06 12:57:14 +00:00
if(identifier.fEffectId != effectId()) return false;
2020-03-31 14:18:42 +00:00
if(identifier.fEffectName != eLName) return false;
2019-07-06 12:57:14 +00:00
return identifier.fVersion == effectVersion();
2019-06-02 16:53:04 +00:00
}
2019-07-06 12:57:14 +00:00
2019-08-28 16:30:12 +00:00
eLinearize000::eLinearize000() :
2020-03-31 14:18:42 +00:00
CustomPathEffect(eLName.toLower()) {
2019-08-07 08:51:45 +00:00
mInfluence = enve::make_shared<QrealAnimator>(0, 0, 1, 0.1, "influence");
2019-07-30 16:55:43 +00:00
ca_addChild(mInfluence);
2019-06-02 16:53:04 +00:00
}
2019-08-28 16:30:12 +00:00
CustomIdentifier eLinearize000::getIdentifier() const {
2020-03-31 14:18:42 +00:00
return { effectId(), eLName, { 0, 0, 0 } };
2019-06-02 16:53:04 +00:00
}
2019-09-05 19:08:59 +00:00
class eLinearize000EffectCaller : public PathEffectCaller {
public:
eLinearize000EffectCaller(const qreal infl) :
mInfl(toSkScalar(infl)) {}
void apply(SkPath& path);
private:
const float mInfl;
};
void eLinearize000EffectCaller::apply(SkPath &path) {
const float invInf = 1 - mInfl;
SkPath src;
path.swap(src);
path.setFillType(src.getFillType());
2019-06-02 16:53:04 +00:00
SkPath::Iter iter(src, false);
SkPoint pts[4];
for(;;) {
switch(iter.next(pts)) {
2019-06-02 16:53:04 +00:00
case SkPath::kLine_Verb: {
2019-09-05 19:08:59 +00:00
path.lineTo(pts[1]);
2019-06-02 16:53:04 +00:00
} break;
case SkPath::kQuad_Verb: {
2019-09-05 19:08:59 +00:00
path.quadTo(pts[1]*invInf + (pts[0] + pts[2])*0.5f*mInfl, pts[2]);
2019-06-02 16:53:04 +00:00
} break;
case SkPath::kConic_Verb: {
2019-09-05 19:08:59 +00:00
path.conicTo(pts[1]*invInf + (pts[0] + pts[2])*0.5f*mInfl,
2019-06-03 12:24:00 +00:00
pts[2], iter.conicWeight());
2019-06-02 16:53:04 +00:00
} break;
case SkPath::kCubic_Verb: {
2019-09-05 19:08:59 +00:00
path.cubicTo(pts[0]*mInfl + pts[1]*invInf,
pts[3]*mInfl + pts[2]*invInf, pts[3]);
2019-06-02 16:53:04 +00:00
} break;
case SkPath::kClose_Verb: {
2019-09-05 19:08:59 +00:00
path.close();
2019-06-02 16:53:04 +00:00
} break;
case SkPath::kMove_Verb: {
2019-09-05 19:08:59 +00:00
path.moveTo(pts[0]);
2019-06-02 16:53:04 +00:00
} break;
case SkPath::kDone_Verb:
return;
}
}
}
2019-09-05 19:08:59 +00:00
2019-12-31 15:38:32 +00:00
stdsptr<PathEffectCaller> eLinearize000::getEffectCaller(
const qreal relFrame, const qreal influence) const {
const qreal infl = mInfluence->getEffectiveValue(relFrame)*influence;
2019-09-05 19:08:59 +00:00
return enve::make_shared<eLinearize000EffectCaller>(infl);
}