-
Notifications
You must be signed in to change notification settings - Fork 7
/
animpoint.cpp
140 lines (115 loc) · 2.2 KB
/
animpoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "animpoint.h"
#include <math.h>
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() );
}