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.
14 #include <boost/shared_ptr.hpp>
15 #include <boost/weak_ptr.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include "kgamecanvas.h"
21 #include "entities/userentity.h"
25 #include "piecegroup.h"
26 #include "namedsprite.h"
33 typedef boost::shared_ptr
<BoardTags
> BoardTagsPtr
;
36 * @class Board <board.h>
37 * @brief Board visualization class
39 * This class is a KGameCanvasItem 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
{
47 friend class GraphicalSystem
;
48 friend class PiecePool
;
49 typedef Grid
<NamedSprite
> PieceGrid
;
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 honor drag threshold
61 InteractionType action
;
63 DragInfo(Point from
, const QPoint
& real
,
64 const boost::shared_ptr
<Sprite
>& sprite
,
65 InteractionType action
)
75 typedef boost::shared_ptr
<DragInfo
> DragInfoPtr
;
78 /** the piece being dragged */
79 DragInfoPtr m_drag_info
;
81 /** displayed m_sprites */
85 /** used by a PiecePool to make available the piece that is being dropped
86 on the board to the GraphicalInfo and the variant-specific animator */
87 NamedSprite m_drop_sprite
;
90 /** the visual move hint */
91 NamedSprite m_hinting
;
94 /** the canvas group that holds the pieces */
95 KGameCanvasGroup
*m_pieces_group
;
97 /** the canvas item that is used as background of the board */
98 KGameCanvasGroup
*m_canvas_background
;
100 /** all the tags on the board */
103 /** the @a PixmapLoader used to create tags */
104 PixmapLoader m_tags_loader
;
106 /** the entity responsible of all the interctions with the board */
107 boost::weak_ptr
<UserEntity
> m_entity
;
109 /** The currently selected square */
112 /** The reviously selected square */
116 Point m_premove_from
;
119 /** size of the border */
122 /** ascent of the font used for the border */
125 /** show the border if true */
128 QColor m_border_color
;
130 QColor m_border_text_color
;
134 QStringList m_border_coords
;
139 /** the rectangles used to draw the border */
140 std::vector
<boost::shared_ptr
<KGameCanvasRectangle
> > m_border_margins
;
142 /** the text items for the border */
143 std::vector
<boost::shared_ptr
<KGameCanvasItem
> > m_border_items
;
145 /** this internal function updates the background after the board has been resized */
146 void updateBackground();
148 /** this internal function updates the border after the board has been resized */
151 /** this internal function creates a new border, if needed */
152 void recreateBorder();
154 void updateHinting(Point pt
, AbstractPiece::Ptr piece
);
156 /** this internal function updates the sprites after the board has been resized */
157 void updateSprites();
159 /** this internal function updates the tags after the board has been resized */
162 void mySettingsChanged();
164 void setPremove(const NormalUserMove
& move
);
165 void setPremove(const DropUserMove
& move
);
166 void setPremove(const class Premove
& move
);
167 void updatePremove();
168 void setSelection(const Point
& p
);
170 QRect
computeRect(Point
) const;
171 QRegion
computeRegion(Point
) const;
172 QRect
squareRect(int x
, int y
) const;
174 /** this function tries to notify the move to the entity */
175 bool doMove(const NormalUserMove
&);
177 /** fetch the sprite */
178 boost::shared_ptr
<Sprite
> spriteAt(const Point
& p
) { return m_sprites
[p
].sprite(); }
181 /** constructor, requires the canvas parent */
182 Board(KGameCanvasAbstract
* parent
);
185 /** set the board flipping */
186 void flip(bool flipped
);
188 /** flips the board */
189 inline void flip() { flip(!m_flipped
); }
191 /** adds a tag with name name, the tag will stay over the pieces if over is true */
192 boost::shared_ptr
<KGameCanvasPixmap
> addTag(const QString
& name
, Point at
, bool over
=false);
194 /** clears the tags with name name */
195 void clearTags(const QString
& name
);
197 /** clears all the tags */
200 /** clear all the tags twith name name, than sets them to the valid given ones */
201 void setTags(const QString
& name
, Point p1
= Point::invalid(), Point p2
= Point::invalid(),
202 Point p3
= Point::invalid(), Point p4
= Point::invalid(),
203 Point p5
= Point::invalid(), Point p6
= Point::invalid() );
205 /** returns a reference to the loader used to load tag pixmaps */
206 PixmapLoader
* tagsLoader() { return &m_tags_loader
; }
209 void cancelPremove();
210 void cancelSelection();
212 /** recreates the board underlying grid */
213 void createGrid(Point p
, const QStringList
& border_coords
);
215 /** returns the size of the border */
216 int marginSize(){ return m_border_size
+1; };
219 /** sets the controlling entity */
220 inline void setEntity(const boost::shared_ptr
<UserEntity
>& entity
) { m_entity
= entity
; }
223 /** Notifies to the board that a certain piece is being dragged over the board */
224 void draggingOn(int pool
, int index
, const QPoint
& p
);
226 /** Executes a drop. This function id typically called by by a PiecePool */
227 bool dropOn(int pool
, int index
, const QPoint
& point
);
229 /** returns the size of the grid */
230 virtual Point
gridSize() const { return m_sprites
.getSize(); }
232 /** return the group that contains the pieces */
233 virtual KGameCanvasAbstract
* piecesGroup() { return m_pieces_group
; }
235 /** mouse release event */
236 virtual void onMouseRelease(const QPoint
& pos
, int button
);
238 /** mouse press event */
239 virtual void onMousePress(const QPoint
& pos
, int button
);
241 /** mouse move event */
242 virtual void onMouseMove(const QPoint
& pos
, int button
);
244 /** mouse leave event */
245 virtual void onMouseLeave();
247 /** resize event (new_size is the new size of a SQUARE) */
248 virtual void onResize(int new_size
, bool force_reload
= false);
250 /** the position changed (will update the move highlighting) */
251 virtual void onPositionChanged();
253 /** changed settings handler */
254 virtual void settingsChanged();
256 /** resets all tags and stops all animations */
260 void error(ErrorCode code
);