Debian packaging
[impuzzle] / src / puzzleitem.cpp
1 /*
2   Image Puzzle - A set your pieces straight game
3   Copyright (C) 2009  Timo Härkönen
4
5   This program is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "puzzleitem.h"
20 #include "gameview.h"
21
22 #include <QGraphicsSceneMouseEvent>
23 #include <QPropertyAnimation>
24
25 int PuzzleItem::moveCount_ = 0;
26
27 PuzzleItem::PuzzleItem(QGraphicsItem *parent) :
28         QGraphicsPixmapItem(parent)
29 {
30     movable_ = true;
31     moveAnimation_ = new QPropertyAnimation(this, "pos", this);
32 }
33
34 QPointF PuzzleItem::correctPlace() const
35 {
36     return correctPlace_;
37 }
38
39 QPointF PuzzleItem::currentPlace() const
40 {
41     return currentPlace_;
42 }
43
44 void PuzzleItem::setCorrectPlace(const QPointF &place)
45 {
46     correctPlace_ = place;
47 }
48
49 void PuzzleItem::setCurrentPlace(const QPointF &place)
50 {
51     currentPlace_ = place;
52 }
53
54 bool PuzzleItem::movable() const
55 {
56     return movable_;
57 }
58
59 void PuzzleItem::setMovable(bool canMove)
60 {
61     movable_ = canMove;
62 }
63
64 void PuzzleItem::moveMeTo(const QPointF &location)
65 {
66     moveAnimation_->setStartValue(currentPlace());
67     moveAnimation_->setEndValue(location);
68     moveAnimation_->start();
69 }
70
71 void PuzzleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
72 {
73     if(movable_) {
74         moveMeTo(GameView::instance()->emptyPlace());
75         QPointF tmp = currentPlace();
76         setCurrentPlace(GameView::instance()->emptyPlace());
77         GameView::instance()->setEmptyPlace(tmp);
78         event->accept();
79
80         moveCount_++;
81
82         // If piece is in its place check if we won the game
83         bool won = false;
84         if(currentPlace() == correctPlace()) {
85             won = GameView::instance()->areAllPiecesOk();
86         }
87
88         // if we didn't win set pieces that can be moved
89         if(!won) {
90             GameView::instance()->setMovingPieces();
91         }
92     }
93     else {
94         event->ignore();
95     }
96 }
97
98 void PuzzleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
99 {
100     event->ignore();
101 }
102
103 int PuzzleItem::moveCount()
104 {
105     return moveCount_;
106 }
107
108 void PuzzleItem::resetMoveCount()
109 {
110     moveCount_ = 0;
111 }