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 "piecegroup.h"
16 #include "namedsprite.h"
19 * @class PiecePool <piecepool.h>
20 * @brief The class representing a crazyhouse-like piece pool.
22 * PiecePool is a canvas item attached to a Board that keeps a
23 * stack of pieces and lets you drop them on the board, notifying
26 class PiecePool
: public ClickableCanvas
{
29 /** displayed m_sprites */
30 std::vector
<NamedSprite
> m_sprites
;
35 /** true if flipped */
38 /** size if a square */
41 /** the width of the pool (ie how many pieces) */
44 /** loader class, to load pieces */
45 PixmapLoader m_loader
;
47 /** the piece that is being dragged, if any */
48 NamedSprite m_dragged
;
50 /** the index of the piece being dragged */
53 /** main animation structure */
54 MainAnimation
* m_main_animation
;
56 /** internal function, resizes the grid vector to hold x pieces */
59 /** removes the drag putting it back together with his friends in the pool.
60 if fadeOff is true the current drag will fade off while a new piece will
61 fade in the pool, while if fadeOff is false the fade off is not done.
62 fadeOff will typically be false if the piece sprite has been used in the
63 board and we don't want a clone that is fading off */
64 void cancelDragging(bool fadeOff
= true);
66 /** takes the named sprite */
67 NamedSprite
takeSpriteAt(int i
);
69 /** converts an index to the upper left point of the corresponding square */
72 /** finds to which index corresponds the point p, or -1 if corresponds to none */
73 int toLogical(const QPoint
& p
);
76 friend class GraphicalSystem
;
77 friend class ChessTable
;
79 /** Constructor, requires the board the pool will be attached to */
80 PiecePool(Board
* b
, KGameCanvasAbstract
* parent
);
83 /** returns the sprite loader */
84 PixmapLoader
* loader() { return &m_loader
; }
86 /** returns the sprite loader */
87 const PixmapLoader
* loader() const { return &m_loader
; }
89 /** returns the flipped value */
90 bool flipped() const { return m_flipped
; }
92 /** returns the number of pieces in the pool */
95 /** removes all the pieces */
98 /** adds a sprite to the pool */
99 void insertSprite(int index
, const NamedSprite
& sprite
);
101 /** \return the sprite at the given index. */
102 NamedSprite
getSprite(int index
);
104 /** removes the sprite at the given index from the pool and returns it. */
105 NamedSprite
takeSprite(int index
);
109 /** sets the width of the grid (the fill will stay the same, and the
110 grid height will be recalculated) */
111 void setGridWidth(int w
);
113 /** piecesGroup overload */
114 virtual KGameCanvasAbstract
* piecesGroup();
116 /** the rect that will be covered by the pool */
117 virtual QRect
boardRect() { return QRect(pos(), QSize(m_square_size
*m_width
,
118 (m_flipped
?-1:1)*m_square_size
*((m_sprites
.size()+m_width
-1)/m_width
)) ); }
120 /** flips and moves the pieces in the pool at the same time */
121 void flipAndMoveBy(QPoint p
);
123 /** mouse release event */
124 virtual void onMouseRelease(const QPoint
& pos
, int button
);
126 /** mouse press event */
127 virtual void onMousePress(const QPoint
& pos
, int button
);
129 /** mouse move event */
130 virtual void onMouseMove(const QPoint
& pos
, int button
);
132 /** resize event (new_size is the new size of a SQUARE) */
133 virtual void onResize(int new_size
, bool force_reload
= false);
135 /** changed settings handler */
136 virtual void settingsChanged();