]> git.sven.stormbind.net Git - sven/vym.git/blob - src/animpoint.cpp
New upstream version 2.9.22
[sven/vym.git] / src / animpoint.cpp
1 #include "animpoint.h"
2
3 #include <math.h>
4
5 AnimPoint::AnimPoint() { init(); }
6
7 void AnimPoint::operator=(const AnimPoint &other) { copy(other); }
8
9 void AnimPoint::operator=(const QPointF &other)
10 {
11     init();
12     setX(other.x());
13     setY(other.x());
14 }
15
16 bool AnimPoint::operator==(const QPointF &other)
17 {
18     QPointF p(x(), y());
19     return p == other;
20 }
21
22 bool AnimPoint::operator==(AnimPoint other)
23 {
24     if (rx() != other.rx())
25         return false;
26     if (ry() != other.ry())
27         return false;
28     if (startPos != other.startPos)
29         return false;
30     if (destPos != other.destPos)
31         return false;
32     if (animated != other.animated)
33         return false;
34
35     return true;
36 }
37
38 void AnimPoint::init()
39 {
40     animated = false;
41     n = 0;
42     startPos = QPointF(0, 0);
43     destPos = QPointF(0, 0);
44     vector = QPointF(0, 0);
45     animTicks = 10;
46 }
47
48 void AnimPoint::copy(AnimPoint other)
49 {
50     setX(other.x());
51     setY(other.x());
52     startPos = other.startPos;
53     destPos = other.destPos;
54     vector = other.vector;
55     animated = other.animated;
56     n = other.n;
57     animTicks = other.animTicks;
58 }
59
60 void AnimPoint::setStart(const QPointF &p)
61 {
62     startPos = p;
63     initVector();
64 }
65
66 QPointF AnimPoint::getStart() { return startPos; }
67
68 void AnimPoint::setDest(const QPointF &p)
69 {
70     destPos = p;
71     initVector();
72 }
73
74 QPointF AnimPoint::getDest() { return destPos; }
75
76 void AnimPoint::setTicks(const uint &t) { animTicks = t; }
77
78 uint AnimPoint::getTicks() { return (uint)animTicks; }
79
80 void AnimPoint::setAnimated(bool b)
81 {
82     animated = b;
83     if (b)
84         n = 0;
85 }
86
87 bool AnimPoint::isAnimated() { return animated; }
88
89 bool AnimPoint::animate()
90 {
91     if (!animated)
92         return false;
93     n++;
94     if (n > animTicks) {
95         vector = QPointF(0, 0);
96         animated = false;
97         setX(destPos.x());
98         setY(destPos.y());
99         return false;
100     }
101
102     // Some math to slow down the movement in the end
103     qreal f = 1 - n / (qreal)animTicks;
104     qreal ff = 1 - f * f * f;
105     setX(startPos.x() + vector.x() * ff);
106     setY(startPos.y() + vector.y() * ff);
107
108     return animated;
109 }
110
111 void AnimPoint::stop()
112 {
113     animated = false;
114     setX(destPos.x());
115     setY(destPos.y());
116 }
117
118 void AnimPoint::initVector()
119 {
120     vector.setX(destPos.x() - startPos.x());
121     vector.setY(destPos.y() - startPos.y());
122 }