2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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.
14 #include <boost/shared_ptr.hpp>
15 #include <boost/weak_ptr.hpp>
18 #include <KGameCanvas>
19 #include <boost/shared_ptr.hpp>
21 #include <core/move.h>
22 #include <core/namedsprite.h>
23 #include <core/piece.h>
25 #include "entities/userentity.h"
29 #include "pointconverter.h"
30 #include "pixmaploader.h"
31 #include "clickablecanvas.h"
34 class AnimationManager
;
37 class ConstrainedText
;
43 typedef boost::shared_ptr
<BoardTags
> BoardTagsPtr
;
46 * @brief Board visualization class
48 * This class is a KGameCanvasItem that displaying a board filled with pieces.
49 * You can set custom tags for each square.
50 * @sa Table, @sa PiecePool, @sa Sprite
52 class ChessBoard
: public QObject
, public ClickableCanvas
, protected PointConverter
{
56 friend class GraphicalSystem
;
57 friend class PiecePool
;
58 typedef Grid
<NamedSprite
> PieceGrid
;
63 static const int DRAG_THRESHOLD
= 100; // pixels ^ 2
64 boost::shared_ptr
<Sprite
> sprite
;
65 Point from
; // logical coordinates
66 QPoint real
; /// real starting point, used to honor drag threshold
70 InteractionType action
;
72 DragInfo(Point from
, const QPoint
& real
,
73 const boost::shared_ptr
<Sprite
>& sprite
,
74 InteractionType action
)
84 typedef boost::shared_ptr
<DragInfo
> DragInfoPtr
;
86 /** true if the board is flipped */
89 /** size of a square in points */
92 /** size of the border */
95 /** margin for text */
96 int m_border_text_near
;
98 /** far margin for text */
99 int m_border_text_far
;
101 /** loader class, to load pieces */
102 PixmapLoader m_loader
;
104 /** the piece being dragged */
105 DragInfoPtr m_drag_info
;
107 /** displayed m_sprites */
110 /** the visual move hint */
111 NamedSprite m_hinting
;
114 /** the canvas group that holds the pieces */
115 KGameCanvasGroup
*m_pieces_group
;
117 /** the canvas item that holds the background of the board */
118 KGameCanvasGroup
*m_canvas_background
;
120 /** the canvas item that holds board border */
121 KGameCanvasGroup
*m_canvas_border
;
123 /** the canvas item that holds board border text */
124 KGameCanvasGroup
*m_canvas_border_text
;
126 /** all the tags on the board */
129 /** the @a PixmapLoader used to create tags */
130 PixmapLoader m_tags_loader
;
132 /** the @a PixmapLoader used for controls */
133 PixmapLoader m_controls_loader
;
135 /** the entity responsible of all the interctions with the board */
136 boost::weak_ptr
<UserEntity
> m_entity
;
138 /** The currently selected square */
141 /** The reviously selected square */
145 Point m_premove_from
;
148 QColor m_border_text_color
;
152 QStringList m_border_coords
;
154 const IColor
* m_dropped_pool
;
157 AnimationManager
* m_anim_manager
;
161 /** the text items for the border */
162 std::vector
<ConstrainedText
*> m_border_text
;
164 /** this internal function updates the background after the board has been resized */
165 void updateBackground();
167 /** this internal function updates the border after the board has been resized */
170 /** this internal function creates a new border, if needed */
171 void recreateBorder();
173 void updateHinting(Point pt
, Piece piece
);
175 /** this internal function updates the sprites after the board has been resized */
176 void updateSprites();
178 /** this internal function updates the tags after the board has been resized */
181 void setPremove(const Move
& move
);
182 void updatePremove();
183 void setSelection(const Point
& p
);
185 QRect
computeRect(Point
) const;
186 QRegion
computeRegion(Point
) const;
187 QRect
squareRect(int x
, int y
) const;
189 /** this function tries to notify the move to the entity */
193 /** constructor, requires the canvas parent */
194 ChessBoard(Components
* components
,
195 AnimationManager
* animManager
,
196 KGameCanvasAbstract
* parent
);
199 /** set the board flipping */
200 void flip(bool flipped
);
202 /** flips the board */
203 void flip() { flip(!m_flipped
); }
205 /** returns the flipped value */
206 bool flipped() const { return m_flipped
; }
208 /** returns the size of a square */
209 int squareSize() const { return m_square_size
; }
211 /** returns the area covered by the piece grid */
212 QRect
boardRect() { return QRect(pos(), QSize(m_square_size
*gridSize().x
,
213 m_square_size
*gridSize().y
)); }
215 /** returns the size of the grid */
216 Point
gridSize() const { return m_sprites
.getSize(); }
218 /** recreates the board underlying grid */
219 void createGrid(Point p
, const QStringList
& border_coords
);
221 /** return the group that contains the pieces */
222 KGameCanvasAbstract
* piecesGroup() { return m_pieces_group
; }
224 /** returns the point converter class */
225 PointConverter
* converter() { return static_cast<PointConverter
*>(this); }
227 /** returns the point converter class */
228 const PointConverter
* converter() const { return static_cast<const PointConverter
*>(this); }
230 /** returns the sprite loader */
231 PixmapLoader
* loader() { return &m_loader
; }
233 /** Load a sprite using the sprite loader */
234 QPixmap
loadSprite(const QString
& id
);
236 /** returns the sprite loader */
237 const PixmapLoader
* loader() const { return &m_loader
; }
240 /** adds a tag with name name, the tag will stay over the pieces if over is true */
241 boost::shared_ptr
<KGameCanvasPixmap
> addTag(const QString
& name
, Point at
, bool over
=false);
243 /** clears the tags with name name */
244 void clearTags(const QString
& name
);
246 /** clears all the tags */
249 /** clear all the tags twith name name, than sets them to the valid given ones */
250 void setTags(const QString
& name
, Point p1
= Point::invalid(), Point p2
= Point::invalid(),
251 Point p3
= Point::invalid(), Point p4
= Point::invalid(),
252 Point p5
= Point::invalid(), Point p6
= Point::invalid() );
254 /** returns a reference to the loader used to load tag pixmaps */
255 PixmapLoader
* tagsLoader() { return &m_tags_loader
; }
257 /** returns a reference to the loader used to load controls pixmaps */
258 PixmapLoader
* controlsLoader() { return &m_controls_loader
; }
261 void cancelPremove();
263 void cancelSelection();
265 /** Brings the sprite to its correct position */
266 void adjustSprite(const Point
& p
, bool immediate
= false);
268 /** sets the controlling entity */
269 inline void setEntity(const boost::shared_ptr
<UserEntity
>& entity
) { m_entity
= entity
; }
272 /** Notifies to the board that a certain piece is being dragged over the board */
273 void draggingOn(const IColor
* pool
, int index
, const QPoint
& p
);
275 /** Executes a drop. This function id typically called by by a PiecePool */
276 bool dropOn(const IColor
* pool
, int index
, const QPoint
& point
);
278 /** mouse release event */
279 virtual void onMouseRelease(const QPoint
& pos
, int button
);
281 /** mouse press event */
282 virtual void onMousePress(const QPoint
& pos
, int button
);
284 /** mouse move event */
285 virtual void onMouseMove(const QPoint
& pos
, int button
);
287 /** mouse leave event */
288 virtual void onMouseLeave();
290 /** resize event (new_size is the new size of a SQUARE) */
291 virtual void onResize(int new_size
, int border_size
, int border_text_near
,
292 int border_text_far
, bool force_reload
= false);
294 /** the position changed (will update the move highlighting) */
295 virtual void onPositionChanged();
297 /** changed settings handler */
298 virtual void settingsChanged();
300 /** resets all tags and stops all animations */
304 void error(ErrorCode code
);
307 #endif // CHESSBOARD_H