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
{
31 /** displayed m_sprites */
32 std::vector
<NamedSprite
> m_sprites
;
37 /** true if flipped */
40 /** size if a square */
43 /** the width of the pool (ie how many pieces) */
46 /** loader class, to load pieces */
47 PixmapLoader m_loader
;
49 /** the piece that is being dragged, if any */
50 NamedSprite m_dragged
;
52 /** the index of the piece being dragged */
55 /** main animation structure */
56 MainAnimation
* m_main_animation
;
58 /** internal function, resizes the grid vector to hold x pieces */
61 /** removes the drag putting it back together with his friends in the pool.
62 if fadeOff is true the current drag will fade off while a new piece will
63 fade in the pool, while if fadeOff is false the fade off is not done.
64 fadeOff will typically be false if the piece sprite has been used in the
65 board and we don't want a clone that is fading off */
66 void cancelDragging(bool fadeOff
= true);
68 /** takes the named sprite */
69 NamedSprite
takeSpriteAt(int i
);
71 /** converts an index to the upper left point of the corresponding square */
74 /** finds to which index corresponds the point p, or -1 if corresponds to none */
75 int toLogical(const QPoint
& p
);
78 friend class GraphicalSystem
;
79 friend class ChessTable
;
81 /** Constructor, requires the board the pool will be attached to */
82 PiecePool(int num
, Board
* b
, KGameCanvasAbstract
* parent
);
85 /** returns the sprite loader */
86 PixmapLoader
* loader() { return &m_loader
; }
88 /** returns the sprite loader */
89 const PixmapLoader
* loader() const { return &m_loader
; }
91 /** returns the flipped value */
92 bool flipped() const { return m_flipped
; }
94 /** returns the number of pieces in the pool */
97 /** removes all the pieces */
100 /** adds a sprite to the pool */
101 void insertSprite(int index
, const NamedSprite
& sprite
);
103 /** \return the sprite at the given index. */
104 NamedSprite
getSprite(int index
);
106 /** removes the sprite at the given index from the pool and returns it. */
107 NamedSprite
takeSprite(int index
);
111 /** sets the width of the grid (the fill will stay the same, and the
112 grid height will be recalculated) */
113 void setGridWidth(int w
);
115 /** piecesGroup overload */
116 virtual KGameCanvasAbstract
* piecesGroup();
118 /** the rect that will be covered by the pool */
119 virtual QRect
boardRect() { return QRect(pos(), QSize(m_square_size
*m_width
,
120 (m_flipped
?-1:1)*m_square_size
*((m_sprites
.size()+m_width
-1)/m_width
)) ); }
122 /** flips and moves the pieces in the pool at the same time */
123 void flipAndMoveBy(QPoint p
);
125 /** mouse release event */
126 virtual void onMouseRelease(const QPoint
& pos
, int button
);
128 /** mouse press event */
129 virtual void onMousePress(const QPoint
& pos
, int button
);
131 /** mouse move event */
132 virtual void onMouseMove(const QPoint
& pos
, int button
);
134 /** resize event (new_size is the new size of a SQUARE) */
135 virtual void onResize(int new_size
, bool force_reload
= false);
137 /** changed settings handler */
138 virtual void settingsChanged();