Initial implementation of the new abstraction architecture.
[tagua.git] / src / board.h
blob69569c7d522e3f2176d14e0c295e1044a7e2eb4a
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
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 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef BOARD_H
12 #define BOARD_H
14 #include <boost/shared_ptr.hpp>
15 #include <boost/weak_ptr.hpp>
17 #include <QObject>
18 #include <QFont>
19 #include <boost/shared_ptr.hpp>
20 #include "canvas/canvas.h"
21 #include "entities/userentity.h"
22 #include "common.h"
23 #include "grid.h"
24 #include "usermove.h"
25 #include "piecegroup.h"
26 #include "namedsprite.h"
28 class DragInfo;
29 class Animation;
30 class UserEntity;
32 class BoardTags;
33 typedef boost::shared_ptr<BoardTags> BoardTagsPtr;
35 /**
36 * @class Board <board.h>
37 * @brief Board visualization class
39 * This class is a Canvas::Item that displaying a board filled with pieces.
40 * You can set custom tags for each square.
41 * @sa Table, @sa PiecePool, @sa Sprite
43 class Board : public QObject, public PieceGroup {
44 Q_OBJECT
46 public:
47 friend class GraphicalSystem;
48 friend class PiecePool;
49 typedef Grid<NamedSprite> PieceGrid;
51 private:
52 class DragInfo {
53 public:
54 static const int DRAG_THRESHOLD = 100; // pixels ^ 2
55 boost::shared_ptr<Sprite> sprite;
56 Point from; // logical coordinates
57 QPoint real; /// real starting point, used to honour drag threshold
58 bool dragging;
59 bool dragStarted;
60 bool droppedOut;
61 UserEntity::Action action;
63 DragInfo(Point from, const QPoint& real,
64 const boost::shared_ptr<Sprite>& sprite,
65 UserEntity::Action action)
66 : sprite(sprite)
67 , from(from)
68 , real(real)
69 , dragging(false)
70 , dragStarted(false)
71 , droppedOut(false)
72 , action(action) {}
75 typedef boost::shared_ptr<DragInfo> DragInfoPtr;
78 /** the piece being dragged */
79 DragInfoPtr m_drag_info;
81 /** displayed m_sprites */
82 PieceGrid m_sprites;
84 /** used by a PiecePool to make available the piece that is being dropped
85 on the board to the GraphicalInfo and the variant-specific animator */
86 NamedSprite m_drop_sprite;
88 /** the visual move hint */
89 NamedSprite m_hinting;
90 Point m_hinting_pos;
92 /** the canvas group that holds the pieces */
93 Canvas::Group *m_pieces_group;
95 /** the canvas item that is used as background of the board */
96 Canvas::Group *m_canvas_background;
98 /** all the tags on the board */
99 BoardTagsPtr m_tags;
101 /** the @a SpriteLoader used to create tags */
102 SpriteLoader m_tags_loader;
104 /** the entity responsible of all the interctions with the board */
105 boost::weak_ptr<UserEntity> m_entity;
107 /** The currently selected square */
108 Point selection;
110 /** The reviously selected square */
111 Point lastSelection;
113 /** premove */
114 Point m_premove_from;
115 Point m_premove_to;
117 /** size of the border */
118 int m_border_size;
120 /** ascent of the font used for the border */
121 int m_border_asc;
123 /** show the border if true */
124 bool m_show_border;
126 QColor m_border_color;
128 QColor m_border_text_color;
130 QFont m_border_font;
132 QStringList m_border_coords;
134 /** the rectangles used to draw the border */
135 std::vector<boost::shared_ptr<Canvas::Rectangle> > m_border_margins;
137 /** the text items for the border */
138 std::vector<boost::shared_ptr<Canvas::Item> > m_border_items;
140 /** this internal function updates the background after the board has been resized */
141 void updateBackground();
143 /** this internal function updates the border after the board has been resized */
144 void updateBorder();
146 /** this internal function creates a new border, if needed */
147 void recreateBorder();
149 void updateHinting(Point pt, AbstractPiece::Ptr piece);
151 /** this internal function updates the sprites after the board has been resized */
152 void updateSprites();
154 /** this internal function updates the tags after the board has been resized */
155 void updateTags();
157 void mySettingsChanged();
159 void setPremove(const NormalUserMove& move);
160 void setPremove(const DropUserMove& move);
161 void setPremove(const class Premove& move);
162 void updatePremove();
163 void setSelection(const Point& p);
165 QRect computeRect(Point) const;
166 QRegion computeRegion(Point) const;
167 QRect squareRect(int x, int y) const;
169 /** this function tries to notify the move to the entity */
170 bool doMove(const NormalUserMove&);
172 /** fetch the sprite */
173 boost::shared_ptr<Sprite> spriteAt(const Point& p) { return m_sprites[p].sprite(); }
175 public:
176 /** constructor, requires the canvas parent */
177 Board(Canvas::Abstract* parent);
178 ~Board();
180 /** set the board flipping */
181 void flip(bool flipped);
183 /** flips the board */
184 inline void flip() { flip(!m_flipped); }
186 /** adds a tag with name name, the tag will stay over the pieces if over is true */
187 boost::shared_ptr<Canvas::Pixmap> addTag(const QString& name, Point at, bool over=false);
189 /** clears the tags with name name */
190 void clearTags(const QString& name);
192 /** clears all the tags */
193 void clearTags();
195 /** clear all the tags twith name name, than sets them to the valid given ones */
196 void setTags(const QString& name, Point p1 = Point::invalid(), Point p2 = Point::invalid(),
197 Point p3 = Point::invalid(), Point p4 = Point::invalid(),
198 Point p5 = Point::invalid(), Point p6 = Point::invalid() );
200 /** returns a reference to the loader used to load tag pixmaps */
201 SpriteLoader* tagsLoader() { return &m_tags_loader; }
204 void cancelPremove();
205 void cancelSelection();
207 /** recreates the board underlying grid */
208 void createGrid(Point p, const QStringList& border_coords);
210 /** returns the size of the border */
211 int marginSize(){ return m_border_size+1; };
214 /** sets the controlling entity */
215 inline void setEntity(const boost::shared_ptr<UserEntity>& entity) { m_entity = entity; }
218 /** Notifies to the board that a certain piece is being dragged over the board */
219 void draggingOn(AbstractPiece::Ptr piece, const QPoint& p);
221 /** Executes a drop. This function id typically called by by a PiecePool */
222 bool dropOn(AbstractPiece::Ptr piece, const QPoint& point);
224 /** returns the size of the grid */
225 virtual Point gridSize() const { return m_sprites.getSize(); }
227 /** return the group that contains the pieces */
228 virtual Canvas::Abstract* piecesGroup() { return m_pieces_group; }
230 /** mouse release event */
231 virtual void onMouseRelease(const QPoint& pos, int button);
233 /** mouse press event */
234 virtual void onMousePress(const QPoint& pos, int button);
236 /** mouse move event */
237 virtual void onMouseMove(const QPoint& pos, int button);
239 /** mouse leave event */
240 virtual void onMouseLeave();
242 /** resize event (new_size is the new size of a SQUARE) */
243 virtual void onResize(int new_size, bool force_reload = false);
245 /** the position changed (will update the move highlighting) */
246 virtual void onPositionChanged();
248 /** changed settings handler */
249 virtual void settingsChanged();
251 /** resets all tags and stops all animations */
252 void reset();
254 signals:
255 void error(ErrorCode code);
258 #endif //BOARD_H