File indexing completed on 2024-04-28 04:03:10

0001 /***************************************************************************
0002     File                 : board.h
0003     Project              : Knights
0004     Description          : Game board (scene)
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016 Alexander Semke (alexander.semke@web.de)
0007     SPDX-FileCopyrightText: 2009-2011 Miha Čančula (miha@noughmad.eu)
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  SPDX-License-Identifier: GPL-2.0-or-later
0014  *                                                                         *
0015  *  This program is distributed in the hope that it will be useful,        *
0016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0018  *  GNU General Public License for more details.                           *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the Free Software           *
0022  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0023  *   Boston, MA  02110-1301  USA                                           *
0024  *                                                                         *
0025  ***************************************************************************/
0026 
0027 #ifndef KNIGHTSBOARD_H
0028 #define KNIGHTSBOARD_H
0029 
0030 #include "core/piece.h"
0031 #include <KGameThemeProvider>
0032 
0033 #include <QGraphicsScene>
0034 #include <QMap>
0035 #include <QPointer>
0036 
0037 class QDrag;
0038 
0039 namespace Knights {
0040 
0041 class Move;
0042 class Item;
0043 class Renderer;
0044 class Pos;
0045 class Rules;
0046 
0047 class Board : public QGraphicsScene {
0048     Q_OBJECT
0049     Q_ENUMS(MarkerType)
0050 
0051 public:
0052     enum MarkerType {
0053         LegalMove,
0054         Danger,
0055         Motion
0056     };
0057 
0058     explicit Board(KGameThemeProvider*, QObject* parent = nullptr);
0059     ~Board() override;
0060 
0061     void populate();
0062     static bool isInBoard(const Pos&);
0063 
0064 private:
0065     Rules* m_rules;
0066     Grid m_grid;
0067     QMap<Pos, Item*> m_tiles;
0068     QList<Item*> m_borders;
0069     QList<Item*> m_notations;
0070     Item* m_background;
0071     bool m_displayBorders;
0072     bool m_displayNotations;
0073     KGameGraphicsViewRenderer* renderer;
0074     KGameThemeProvider* m_themeProvider;
0075 
0076     QPointer<QDrag> drag;
0077     bool m_dragActive;
0078     Piece* draggedPiece;
0079     Piece* selectedPiece;
0080     QPoint dragStartPoint;
0081 
0082     bool m_paused;
0083     int m_tileSize;
0084     QRectF m_boardRect;
0085     bool m_animated;
0086     QPointF m_draggedPos;
0087     QPointF m_dragStartPos;
0088     Color m_currentPlayer;
0089     Color m_displayedPlayer;
0090     Colors m_playerColors;
0091     QMap<Pos, Item*> markers;
0092     bool m_drawFrame;
0093 
0094     void addPiece(PieceType type, Color color, const Pos& pos);
0095     void addMarker(const Pos& pos, MarkerType type );
0096     void addMarker(const Pos& pos, const QString& spriteKey);
0097     void addTiles();
0098 
0099     Piece* pieceAt(const QPointF&);
0100     Pos mapFromScene(const QPointF&);
0101     QPointF mapToScene(Pos);
0102     void centerOnPos(Item* item, const Pos& pos, bool animated = true);
0103     void centerOnPos(Item* item, bool animated = true);
0104     void centerAndResize(Item* item, QSize size, bool animated = true);
0105     PieceType getPromotedType();
0106 
0107 protected:
0108     void mousePressEvent(QGraphicsSceneMouseEvent*) override;
0109     void mouseMoveEvent(QGraphicsSceneMouseEvent*) override;
0110     void mouseReleaseEvent(QGraphicsSceneMouseEvent*) override;
0111     void dropEvent(QGraphicsSceneDragDropEvent*) override;
0112     void dragEnterEvent(QGraphicsSceneDragDropEvent*) override;
0113     void dragMoveEvent(QGraphicsSceneDragDropEvent*) override;
0114     void dragLeaveEvent(QGraphicsSceneDragDropEvent*) override;
0115 
0116 public Q_SLOTS:
0117     void movePiece(const Move&);
0118     void updateTheme();
0119     void updateGraphics();
0120     void changeDisplayedPlayer();
0121     void setCurrentColor(Color);
0122     void setPlayerColors(Colors);
0123 
0124 Q_SIGNALS:
0125     void pieceMoved(const Move&);
0126     void activePlayerChanged(Color);
0127     void displayedPlayerChanged(Color);
0128     void centerChanged(const QPointF&);
0129 };
0130 
0131 }
0132 
0133 #endif // KNIGHTSBOARD_H