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 "mainanimation.h"
16 #include "pixmaploader.h"
17 #include "clickablecanvas.h"
18 #include "namedsprite.h"
21 * @class PiecePool <piecepool.h>
22 * @brief The class representing a crazyhouse-like piece pool.
24 * PiecePool is a canvas item attached to a Board that keeps a
25 * stack of pieces and lets you drop them on the board, notifying
28 class PiecePool
: public ClickableCanvas
{
33 /** displayed m_sprites */
34 std::vector
<NamedSprite
> m_sprites
;
39 /** true if flipped */
42 /** size if a square */
45 /** the width of the pool (ie how many pieces) */
48 /** loader class, to load pieces */
49 PixmapLoader m_loader
;
51 /** the piece that is being dragged, if any */
52 NamedSprite m_dragged
;
54 /** the index of the piece being dragged */
57 /** main animation structure */
58 MainAnimation
* m_main_animation
;
61 /** internal function, resizes the grid vector to hold x pieces */
64 /** removes the drag putting it back together with his friends in the pool.
65 if fadeOff is true the current drag will fade off while a new piece will
66 fade in the pool, while if fadeOff is false the fade off is not done.
67 fadeOff will typically be false if the piece sprite has been used in the
68 board and we don't want a clone that is fading off */
69 void cancelDragging(bool fadeOff
= true);
71 /** takes the named sprite */
72 NamedSprite
takeSpriteAt(int i
);
74 /** converts an index to the upper left point of the corresponding square */
77 /** finds to which index corresponds the point p, or -1 if corresponds to none */
78 int toLogical(const QPoint
& p
);
81 friend class GraphicalSystem
;
82 friend class ChessTable
;
84 /** Constructor, requires the board the pool will be attached to */
85 PiecePool(int num
, Board
* b
, KGameCanvasAbstract
* parent
);
88 /** returns the sprite loader */
89 PixmapLoader
* loader() { return &m_loader
; }
91 /** returns the sprite loader */
92 const PixmapLoader
* loader() const { return &m_loader
; }
94 /** returns the flipped value */
95 bool flipped() const { return m_flipped
; }
97 /** returns the number of pieces in the pool */
100 /** removes all the pieces */
103 /** adds a sprite to the pool */
104 void insertSprite(int index
, const NamedSprite
& sprite
);
106 /** \return the sprite at the given index. */
107 NamedSprite
getSprite(int index
);
109 /** removes the sprite at the given index from the pool. */
110 void removeSprite(int index
);
112 /** removes the sprite at the given index from the pool and returns it. */
113 NamedSprite
takeSprite(int index
);
117 /** sets the width of the grid (the fill will stay the same, and the
118 grid height will be recalculated) */
119 void setGridWidth(int w
);
121 /** the rect that will be covered by the pool */
122 virtual QRect
boardRect() { return QRect(pos(), QSize(m_square_size
*m_width
,
123 (m_flipped
?-1:1)*m_square_size
*((m_sprites
.size()+m_width
-1)/m_width
)) ); }
125 /** flips and moves the pieces in the pool at the same time */
126 void flipAndMoveBy(QPoint p
);
128 /** mouse release event */
129 virtual void onMouseRelease(const QPoint
& pos
, int button
);
131 /** mouse press event */
132 virtual void onMousePress(const QPoint
& pos
, int button
);
134 /** mouse move event */
135 virtual void onMouseMove(const QPoint
& pos
, int button
);
137 /** resize event (new_size is the new size of a SQUARE) */
138 virtual void onResize(int new_size
, bool force_reload
= false);
140 /** changed settings handler */
141 virtual void settingsChanged();