Cute Chess  0.1
boardscene.h
1 /*
2  This file is part of Cute Chess.
3 
4  Cute Chess is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Cute Chess is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef BOARDSCENE_H
19 #define BOARDSCENE_H
20 
21 #include <QGraphicsScene>
22 #include <QMultiMap>
23 #include <QPointer>
24 #include <board/square.h>
25 #include <board/genericmove.h>
26 #include <board/boardtransition.h>
27 #include <board/result.h>
28 namespace Chess
29 {
30  class Board;
31  class Move;
32  class Side;
33  class Piece;
34 }
35 class ChessGame;
36 class QSvgRenderer;
37 class QAbstractAnimation;
38 class QPropertyAnimation;
39 class GraphicsBoard;
41 class GraphicsPiece;
42 class PieceChooser;
43 
44 
56 class BoardScene : public QGraphicsScene
57 {
58  Q_OBJECT
59 
60  public:
62  explicit BoardScene(QObject* parent = nullptr);
64  virtual ~BoardScene();
65 
67  Chess::Board* board() const;
75 
76  public slots:
85  void populate();
87  void setFenString(const QString& fenString);
89  void makeMove(const Chess::Move& move);
91  void makeMove(const Chess::GenericMove& move);
93  void undoMove();
95  void flip();
100  void onGameFinished(ChessGame* game, Chess::Result result);
101 
102  signals:
109  void humanMove(const Chess::GenericMove& move,
110  const Chess::Side& side);
111 
112  protected:
113  // Inherited from QGraphicsScene
114  virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
115  virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
116  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
117 
118  private slots:
119  void onTransitionFinished();
120  void onPromotionChosen(const Chess::Piece& promotion);
121 
122  private:
123  void cancelUserMove();
124 
125  enum MoveDirection
126  {
127  Forward,
128  Backward
129  };
130 
131  QPointF squarePos(const Chess::Square& square) const;
132  GraphicsPiece* pieceAt(const QPointF& pos) const;
133  GraphicsPiece* createPiece(const Chess::Piece& piece);
134  QPropertyAnimation* pieceAnimation(GraphicsPiece* piece,
135  const QPointF& endPoint) const;
136  void stopAnimation();
137  void tryMove(GraphicsPiece* piece, const QPointF& targetPos);
138  void selectPiece(const QList<Chess::Piece>& types,
139  const char* member);
140  void addMoveArrow(const QPointF& sourcePos,
141  const QPointF& targetPos);
142  void applyTransition(const Chess::BoardTransition& transition,
143  MoveDirection direction);
144  void updateMoves();
145 
146  Chess::Board* m_board;
147  MoveDirection m_direction;
148  Chess::BoardTransition m_transition;
150  QPointF m_sourcePos;
151  GraphicsBoard* m_squares;
152  GraphicsPieceReserve* m_reserve;
153  QPointer<PieceChooser> m_chooser;
154  QPointer<QAbstractAnimation> m_anim;
155  QSvgRenderer* m_renderer;
158  Chess::GenericMove m_promotionMove;
159  GraphicsPiece* m_highlightPiece;
160  QGraphicsItemGroup* m_moveArrows;
161 };
162 
163 #endif // BOARDSCENE_H
A light-weight QGraphicsObject dialog for selecting a chess piece.
Definition: piecechooser.h:34
A graphical reserve for captured chess pieces.
Definition: graphicspiecereserve.h:34
BoardScene(QObject *parent=nullptr)
Definition: boardscene.cpp:40
Chess::Board * board() const
Definition: boardscene.cpp:60
void undoMove()
Definition: boardscene.cpp:165
void onGameFinished(ChessGame *game, Chess::Result result)
Definition: boardscene.cpp:349
An internal chessboard class.
Definition: board.h:56
A graphical surface for displaying a chessgame.
Definition: boardscene.h:56
void populate()
Definition: boardscene.cpp:82
A graphical representation of a chess piece.
Definition: graphicspiece.h:36
A graphical chessboard.
Definition: graphicsboard.h:37
Definition: chessgame.h:38
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:33
Definition: boardscene.h:28
The side or color of a chess player.
Definition: side.h:34
void flip()
Definition: boardscene.cpp:192
A chess piece.
Definition: piece.h:39
void humanMove(const Chess::GenericMove &move, const Chess::Side &side)
The result of a chess game.
Definition: result.h:33
Details of a board transition caused by a move.
Definition: boardtransition.h:39
virtual ~BoardScene()
Definition: boardscene.cpp:54
A small and efficient chessmove class.
Definition: move.h:41
void makeMove(const Chess::Move &move)
Definition: boardscene.cpp:144
void setBoard(Chess::Board *board)
Definition: boardscene.cpp:65
A generic chess square type consisting of a file and a rank.
Definition: square.h:32
void setFenString(const QString &fenString)
Definition: boardscene.cpp:134