Skip to content

Commit fe78c43

Browse files
committed
add card animations during moves
1 parent c2c35d6 commit fe78c43

File tree

6 files changed

+82
-16
lines changed

6 files changed

+82
-16
lines changed

abstractcardholder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
#ifndef ABSTRACTCARDHOLDER_H
1919
#define ABSTRACTCARDHOLDER_H
2020

21+
#include <QObject>
22+
2123
class QPoint;
2224
class Card;
2325

24-
class AbstractCardHolder
26+
class AbstractCardHolder : public QObject
2527
{
2628
public:
2729
AbstractCardHolder();

board.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void Board::automaticMove(Card* card)
164164
bottomSpot = bottomSpot->getChild();
165165
}
166166
if (bottomSpot->canStackCard(card)) {
167-
card->setParent(bottomSpot);
167+
card->setParent(bottomSpot, true);
168168
return;
169169
}
170170
}
@@ -173,7 +173,7 @@ void Board::automaticMove(Card* card)
173173
std::vector<Freecell*>::iterator itFreecell;
174174
for (itFreecell = mFreeCells.begin(); itFreecell < mFreeCells.end(); itFreecell++) {
175175
if ((*itFreecell)->isEmpty()) {
176-
card->setParent(*itFreecell);
176+
card->setParent(*itFreecell, true);
177177
return;
178178
}
179179
}
@@ -230,8 +230,9 @@ void Board::selectCard(Card* card)
230230
if (card->isOnAceSpot()) {
231231
mSelectedCard->setOnAceSpot(true);
232232
}
233-
mSelectedCard->setParent(card);
233+
mSelectedCard->setParent(card, true);
234234
} else {
235+
if (mSelectedCard) mSelectedCard->updatePosition(true);
235236
setSelectedCard(card);
236237
}
237238
}

card.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "cardproxy.h"
2121
#include "board.h"
2222

23+
#include <QPropertyAnimation>
24+
2325
#include <iostream>
2426
#include <sstream>
2527

@@ -30,6 +32,7 @@ Card::Card(cardcolor color, cardvalue value, Board* board) : AbstractCardHolder(
3032
mValue = value;
3133
mBoard = board;
3234
mIsOnAceSpot = false;
35+
mSelected = false;
3336

3437
mWidget = new CardWidget();
3538
mWidget->setText(getLabel());
@@ -56,15 +59,15 @@ QPixmap Card::getWidgetPixmap()
5659
return mWidget->grab();
5760
}
5861

59-
void Card::setParent(AbstractCardHolder* parent)
62+
void Card::setParent(AbstractCardHolder* parent, bool animate)
6063
{
6164
if (mParent) {
6265
mParent->setChild(0);
6366
}
6467
mParent = parent;
6568
if (mParent) {
6669
mParent->setChild(this);
67-
updatePosition();
70+
updatePosition(animate);
6871
mBoard->unselectCard();
6972
}
7073
}
@@ -203,6 +206,24 @@ QPoint Card::getPosition()
203206
return mPosition;
204207
}
205208

209+
void Card::animatePosition(QPoint pos)
210+
{
211+
mPosition = pos;
212+
setZIndex(100);
213+
214+
QPropertyAnimation *animation = new QPropertyAnimation(mWidget, "pos");
215+
animation->setDuration(100);
216+
animation->setStartValue(mWidget->pos());
217+
animation->setEndValue(mPosition);
218+
animation->start(QAbstractAnimation::DeleteWhenStopped);
219+
220+
QObject::connect(animation, SIGNAL(finished()), this, SLOT(resetZIndex()));
221+
222+
if (mChild) {
223+
mChild->updatePosition(true);
224+
}
225+
}
226+
206227
void Card::setPosition(QPoint pos)
207228
{
208229
mPosition = pos;
@@ -212,10 +233,14 @@ void Card::setPosition(QPoint pos)
212233
}
213234
}
214235

215-
void Card::updatePosition()
236+
void Card::updatePosition(bool animate)
216237
{
217-
setPosition(mParent->getChildPosition());
218-
setZIndex(mParent->getZIndex() + 1);
238+
if (animate) {
239+
animatePosition(mParent->getChildPosition());
240+
} else {
241+
setPosition(mParent->getChildPosition());
242+
setZIndex(mParent->getZIndex() + 1);
243+
}
219244
}
220245

221246
int Card::getTopZIndex()
@@ -239,6 +264,11 @@ void Card::setZIndex(int index, bool cascade)
239264
}
240265
}
241266

267+
void Card::resetZIndex()
268+
{
269+
setZIndex(mParent->getZIndex() + 1);
270+
}
271+
242272
void Card::show()
243273
{
244274
mWidget->show();
@@ -256,13 +286,19 @@ void Card::select()
256286

257287
void Card::setSelected(bool selected)
258288
{
259-
if (selected) {
289+
mSelected = selected;
290+
if (mSelected) {
260291
mWidget->setStyleSheet("CardWidget {background-color:white;border: 2px solid yellow;border-radius:5px;}");
261292
} else {
262293
mWidget->setStyleSheet("CardWidget {background-color:white;border: 2px solid black;border-radius:5px;}");
263294
}
264295
}
265296

297+
bool Card::isSelected()
298+
{
299+
return mSelected;
300+
}
301+
266302
void Card::automaticMove()
267303
{
268304
mBoard->automaticMove(this);

card.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ class QPixmap;
3232

3333
class Card : public AbstractCardHolder
3434
{
35+
Q_OBJECT
3536
public:
3637
Card();
3738
Card(cardcolor color, cardvalue value, Board* board);
3839

39-
void setParent(AbstractCardHolder*);
40+
void setParent(AbstractCardHolder*, bool = false);
4041
int countChildren();
4142

4243
bool canStackCard(Card*);
@@ -58,18 +59,23 @@ class Card : public AbstractCardHolder
5859

5960
QPoint getPosition();
6061
QPoint getChildPosition();
62+
void animatePosition(QPoint pos);
6163
void setPosition(QPoint pos);
62-
void updatePosition();
64+
void updatePosition(bool animate = false);
6365
int getTopZIndex();
6466
int getZIndex();
6567
void setZIndex(int index, bool cascade = true);
6668
void hide();
6769
void show();
6870

6971
void select();
72+
bool isSelected();
7073
void setSelected(bool selected);
7174
void automaticMove();
7275

76+
public slots:
77+
void resetZIndex();
78+
7379
protected:
7480
cardcolor mColor;
7581
cardvalue mValue;
@@ -79,6 +85,7 @@ class Card : public AbstractCardHolder
7985
CardProxy* mProxy;
8086

8187
QPoint mPosition;
88+
bool mSelected;
8289

8390
bool mIsOnAceSpot;
8491

cardproxy.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ CardProxy::CardProxy(Card* card) :
4141
void CardProxy::mousePressEvent(QGraphicsSceneMouseEvent *event)
4242
{
4343
if (event->button() == Qt::LeftButton) {
44-
mCard->select();
45-
mCard->setZIndex(100);
44+
if (!mCard->isSelected()) {
45+
mCard->select();
46+
mCard->setZIndex(100);
47+
}
4648
} else if (event->button() == Qt::RightButton) {
4749
mCard->setZIndex(mCard->getTopZIndex(), false);
4850
}
@@ -66,25 +68,39 @@ void CardProxy::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
6668
}
6769
} else if (event->button() == Qt::LeftButton) {
6870
QList<QGraphicsItem*> items = this->scene()->items(event->scenePos());
71+
bool selection = false;
72+
73+
// if the card is not moved enough, replace it
74+
if ((event->buttonDownScenePos(Qt::LeftButton) - event->scenePos()).manhattanLength() < 10) {
75+
mCard->updatePosition(true);
76+
return;
77+
}
6978

79+
// browse the items underneath
7080
for (int i = 0; i < items.size(); i++) {
7181
if (items[i] != this && items[i]->data(0) == QVariant("card")) {
7282
CardProxy* proxy = dynamic_cast<CardProxy*>(items[i]);
7383
if (proxy->mCard->getChild() == 0) {
7484
proxy->mCard->select();
85+
selection = true;
7586
}
7687
break;
7788
} else if (items[i] != this && items[i]->data(0) == QVariant("acespot")) {
7889
CardSpotProxy* proxy = dynamic_cast<CardSpotProxy*>(items[i]);
7990
proxy->select();
91+
selection = true;
8092
break;
8193
} else if (items[i] != this && items[i]->data(0) == QVariant("freecell")) {
8294
CardSpotProxy* proxy = dynamic_cast<CardSpotProxy*>(items[i]);
8395
proxy->select();
96+
selection = true;
8497
break;
8598
}
8699
}
87-
mCard->updatePosition();
100+
// if not item matches, replace the card at it's original position
101+
if (!selection) {
102+
mCard->updatePosition(true);
103+
}
88104
}
89105
}
90106

@@ -95,6 +111,7 @@ void CardProxy::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
95111
void CardProxy::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
96112
{
97113
if (event->buttons() & Qt::LeftButton) {
114+
mCard->setZIndex(100);
98115
mCard->setPosition(event->scenePos().toPoint() - event->buttonDownPos(Qt::LeftButton).toPoint());
99116
}
100117
}

cardspot.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ void CardSpot::select()
8484
{
8585
Card* card = mBoard->getSelectedCard();
8686
if (card && canStackCard(card)) {
87-
card->setParent(this);
87+
card->setParent(this, true);
88+
} else if (card) {
89+
card->updatePosition(true);
90+
mBoard->unselectCard();
8891
}
8992
}

0 commit comments

Comments
 (0)