-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyWidget.cpp
More file actions
64 lines (59 loc) · 1.93 KB
/
MyWidget.cpp
File metadata and controls
64 lines (59 loc) · 1.93 KB
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
#include <MyWidget.h>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
// ball's parameters init and ball creation
startPosX = 30;
startPosY = 130;
ball = new Ball(this);
ball->move(startPosX, startPosY);
ball->show();
// button's parameters init and button creation
clicked = false;
startButton = new QPushButton("&Start", this);
int posX = this->width() / 2 - startButton->width() / 2;
int posY = this->height() - 50;
startButton->move(posX, posY);
startButton->show();
connect(startButton, SIGNAL(clicked()), this, SLOT(buttonClick()));
// timer init
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerTick()));
}
void MyWidget::resizeEvent(QResizeEvent*) {
// button repositioning
int posX = this->width() / 2 - startButton->width() / 2;
int posY = this->height() - 50;
startButton->move(posX, posY);
}
void MyWidget::timerTick() {
int duration = 2500; // animation duration: 2,5 sec
timeElapsed += 20; // timer ticks every 20 msec
int currY = ball->y();
inteX = quarticEaseOut(timeElapsed, startX, finalX, duration); // ball position interpolation
ball->move(inteX, currY);
if(timeElapsed >= duration)
timer->stop(); // stop the timer if elapsed time is >= 2,5
}
void MyWidget::buttonClick() {
if (clicked) {
// repositioning the ball
ball->move(startPosX, startPosY);
clicked = false;
}
timeElapsed = 0;
timer->start(20); // start the timer (and the animation)
// set start position of the ball
startX = ball->x();
startY = ball->y();
// set final position of the ball
finalX = this->width() - 30*2 - ball->width();
finalY = startY;
clicked = true;
}
float MyWidget::quarticEaseOut(float t, float b, float c, float d){
// t: elapsed time
// b: initial position
// c: final position
// d: animation duration
t /= d;
return c*t*t*t*t + b;
}