Initial porting to the new component API.
[tagua/yd.git] / src / piecepool.h
blob2d2a49055e8d0638267d0f73bf0ce611b6eb9613
1 /*
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.
9 */
11 #ifndef PIECEPOOL_H
12 #define PIECEPOOL_H
14 #include <boost/shared_ptr.hpp>
15 #include "clickablecanvas.h"
16 #include "indexconverter.h"
17 #include "mainanimation.h"
18 #include "namedsprite_utils.h"
19 #include "pixmaploader.h"
21 class AnimationSettings;
22 class ChessBoard;
23 class IColor;
25 /**
26 * @class PiecePool <piecepool.h>
27 * @brief The class representing a crazyhouse-like piece pool.
29 * PiecePool is a canvas item attached to a Board that keeps a
30 * stack of pieces and lets you drop them on the board, notifying
31 * the board.
33 class PiecePool : public ClickableCanvas
34 , private IndexConverter {
36 private:
37 const IColor* m_pool;
39 /** displayed m_sprites */
40 std::vector<NamedSprite> m_sprites;
42 /** refrence board */
43 ChessBoard* m_board;
45 /** true if flipped */
46 bool m_flipped;
48 /** size if a square */
49 int m_square_size;
51 /** the width of the pool (ie how many pieces) */
52 int m_width;
54 /** loader class, to load pieces */
55 PixmapLoader m_loader;
57 /** the piece that is being dragged, if any */
58 NamedSprite m_dragged;
60 /** the index of the piece being dragged */
61 int m_dragged_index;
63 /** main animation structure */
64 MainAnimation* m_main_animation;
66 const AnimationSettings& m_anim_settings;
68 /** internal function, resizes the grid vector to hold x pieces */
69 void setFill(int x);
71 /** removes the drag putting it back together with his friends in the pool.
72 if fadeOff is true the current drag will fade off while a new piece will
73 fade in the pool, while if fadeOff is false the fade off is not done.
74 fadeOff will typically be false if the piece sprite has been used in the
75 board and we don't want a clone that is fading off */
76 void cancelDragging(bool fadeOff = true);
78 /** takes the named sprite */
79 NamedSprite takeSpriteAt(int i);
81 /** converts an index to the upper left point of the corresponding square */
82 virtual QPoint toReal(int i) const;
84 /** finds to which index corresponds the point p, or -1 if corresponds to none */
85 virtual int toLogical(const QPoint& p) const;
86 public:
87 friend class GraphicalSystem;
88 friend class ChessTable;
90 /** Constructor, requires the board the pool will be attached to */
91 PiecePool(const IColor* pool, ChessBoard* b, const AnimationSettings&, KGameCanvasAbstract* parent);
92 ~PiecePool();
94 /** returns the sprite loader */
95 PixmapLoader* loader() { return &m_loader; }
97 /** returns the sprite loader */
98 const PixmapLoader* loader() const { return &m_loader; }
100 /** Load a sprite using the sprite loader */
101 QPixmap loadSprite(const QString& id);
103 /** returns the flipped value */
104 bool flipped() const { return m_flipped; }
106 /** returns the number of pieces in the pool */
107 int fill();
109 /** removes all the pieces */
110 void clear();
112 /** adds a sprite to the pool */
113 void insertSprite(int index, const NamedSprite& sprite);
115 /** \return the sprite at the given index. */
116 NamedSprite getSprite(int index);
118 /** removes the sprite at the given index from the pool. */
119 void removeSprite(int index);
121 /** removes the sprite at the given index from the pool and returns it. */
122 NamedSprite takeSprite(int index);
126 /** sets the width of the grid (the fill will stay the same, and the
127 grid height will be recalculated) */
128 void setGridWidth(int w);
130 /** the rect that will be covered by the pool */
131 virtual QRect boardRect();
133 /** flips and moves the pieces in the pool at the same time */
134 void flipAndMoveBy(QPoint p);
136 /** mouse release event */
137 virtual void onMouseRelease(const QPoint& pos, int button);
139 /** mouse press event */
140 virtual void onMousePress(const QPoint& pos, int button);
142 /** mouse move event */
143 virtual void onMouseMove(const QPoint& pos, int button);
145 /** resize event (new_size is the new size of a SQUARE) */
146 virtual void onResize(int new_size, bool force_reload = false);
148 /** changed settings handler */
149 virtual void settingsChanged();
152 #endif //PIECEPOOL_H