X-Git-Url: https://git.sven.stormbind.net/?a=blobdiff_plain;f=src%2Fanimpoint.cpp;fp=src%2Fanimpoint.cpp;h=66dd0fac9cdae711b61f9386988133d9e5cc814f;hb=d483bd8e6523c23c6f1d8908a2e0611c2bc9ff4f;hp=0000000000000000000000000000000000000000;hpb=7dfa3fe589d1722d49681f42cdb0bf1e6efb5223;p=sven%2Fvym.git diff --git a/src/animpoint.cpp b/src/animpoint.cpp new file mode 100644 index 0000000..66dd0fa --- /dev/null +++ b/src/animpoint.cpp @@ -0,0 +1,122 @@ +#include "animpoint.h" + +#include + +AnimPoint::AnimPoint() { init(); } + +void AnimPoint::operator=(const AnimPoint &other) { copy(other); } + +void AnimPoint::operator=(const QPointF &other) +{ + init(); + setX(other.x()); + setY(other.x()); +} + +bool AnimPoint::operator==(const QPointF &other) +{ + QPointF p(x(), y()); + return p == other; +} + +bool AnimPoint::operator==(AnimPoint other) +{ + if (rx() != other.rx()) + return false; + if (ry() != other.ry()) + return false; + if (startPos != other.startPos) + return false; + if (destPos != other.destPos) + return false; + if (animated != other.animated) + return false; + + return true; +} + +void AnimPoint::init() +{ + animated = false; + n = 0; + startPos = QPointF(0, 0); + destPos = QPointF(0, 0); + vector = QPointF(0, 0); + animTicks = 10; +} + +void AnimPoint::copy(AnimPoint other) +{ + setX(other.x()); + setY(other.x()); + startPos = other.startPos; + destPos = other.destPos; + vector = other.vector; + animated = other.animated; + n = other.n; + animTicks = other.animTicks; +} + +void AnimPoint::setStart(const QPointF &p) +{ + startPos = p; + initVector(); +} + +QPointF AnimPoint::getStart() { return startPos; } + +void AnimPoint::setDest(const QPointF &p) +{ + destPos = p; + initVector(); +} + +QPointF AnimPoint::getDest() { return destPos; } + +void AnimPoint::setTicks(const uint &t) { animTicks = t; } + +uint AnimPoint::getTicks() { return (uint)animTicks; } + +void AnimPoint::setAnimated(bool b) +{ + animated = b; + if (b) + n = 0; +} + +bool AnimPoint::isAnimated() { return animated; } + +bool AnimPoint::animate() +{ + if (!animated) + return false; + n++; + if (n > animTicks) { + vector = QPointF(0, 0); + animated = false; + setX(destPos.x()); + setY(destPos.y()); + return false; + } + + // Some math to slow down the movement in the end + qreal f = 1 - n / (qreal)animTicks; + qreal ff = 1 - f * f * f; + setX(startPos.x() + vector.x() * ff); + setY(startPos.y() + vector.y() * ff); + + return animated; +} + +void AnimPoint::stop() +{ + animated = false; + setX(destPos.x()); + setY(destPos.y()); +} + +void AnimPoint::initVector() +{ + vector.setX(destPos.x() - startPos.x()); + vector.setY(destPos.y() - startPos.y()); +}