Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / piecepool.h
blobf2b35d259a9311081b92b1b0cdb2c489d7f3b279
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 "animationfactory.h"
16 #include "clickablecanvas.h"
17 #include "indexconverter.h"
18 #include "mainanimation.h"
19 #include "namedsprite.h"
20 #include "pixmaploader.h"
22 /**
23 * @class PiecePool <piecepool.h>
24 * @brief The class representing a crazyhouse-like piece pool.
26 * PiecePool is a canvas item attached to a Board that keeps a
27 * stack of pieces and lets you drop them on the board, notifying
28 * the board.
30 class PiecePool : public ClickableCanvas
31 , private IndexConverter {
33 private:
34 int m_pool_num;
36 /** displayed m_sprites */
37 std::vector<NamedSprite> m_sprites;
39 /** refrence board */
40 class Board* m_board;
42 /** true if flipped */
43 bool m_flipped;
45 /** size if a square */
46 int m_square_size;
48 /** the width of the pool (ie how many pieces) */
49 int m_width;
51 /** loader class, to load pieces */
52 PixmapLoader m_loader;
54 /** the piece that is being dragged, if any */
55 NamedSprite m_dragged;
57 /** the index of the piece being dragged */
58 int m_dragged_index;
60 /** main animation structure */
61 MainAnimation* m_main_animation;
63 const AnimationSettings& m_anim_settings;
65 /** internal function, resizes the grid vector to hold x pieces */
66 void setFill(int x);
68 /** removes the drag putting it back together with his friends in the pool.
69 if fadeOff is true the current drag will fade off while a new piece will
70 fade in the pool, while if fadeOff is false the fade off is not done.
71 fadeOff will typically be false if the piece sprite has been used in the
72 board and we don't want a clone that is fading off */
73 void cancelDragging(bool fadeOff = true);
75 /** takes the named sprite */
76 NamedSprite takeSpriteAt(int i);
78 /** converts an index to the upper left point of the corresponding square */
79 virtual QPoint toReal(int i) const;
81 /** finds to which index corresponds the point p, or -1 if corresponds to none */
82 virtual int toLogical(const QPoint& p) const;
84 void animate(const Animate::Pool::Scheme& scheme, Animate::AnimationType type = Animate::Normal);
85 public:
86 friend class GraphicalSystem;
87 friend class ChessTable;
89 /** Constructor, requires the board the pool will be attached to */
90 PiecePool(int num, Board* b, const AnimationSettings&, KGameCanvasAbstract* parent);
91 ~PiecePool();
93 /** returns the sprite loader */
94 PixmapLoader* loader() { return &m_loader; }
96 /** returns the sprite loader */
97 const PixmapLoader* loader() const { return &m_loader; }
99 /** Load a sprite using the sprite loader */
100 QPixmap loadSprite(const QString& id);
102 /** returns the flipped value */
103 bool flipped() const { return m_flipped; }
105 /** returns the number of pieces in the pool */
106 int fill();
108 /** removes all the pieces */
109 void clear();
111 /** adds a sprite to the pool */
112 void insertSprite(int index, const NamedSprite& sprite);
114 /** \return the sprite at the given index. */
115 NamedSprite getSprite(int index);
117 /** removes the sprite at the given index from the pool. */
118 void removeSprite(int index);
120 /** removes the sprite at the given index from the pool and returns it. */
121 NamedSprite takeSprite(int index);
125 /** sets the width of the grid (the fill will stay the same, and the
126 grid height will be recalculated) */
127 void setGridWidth(int w);
129 /** the rect that will be covered by the pool */
130 virtual QRect boardRect();
132 /** flips and moves the pieces in the pool at the same time */
133 void flipAndMoveBy(QPoint p);
135 /** mouse release event */
136 virtual void onMouseRelease(const QPoint& pos, int button);
138 /** mouse press event */
139 virtual void onMousePress(const QPoint& pos, int button);
141 /** mouse move event */
142 virtual void onMouseMove(const QPoint& pos, int button);
144 /** resize event (new_size is the new size of a SQUARE) */
145 virtual void onResize(int new_size, bool force_reload = false);
147 /** changed settings handler */
148 virtual void settingsChanged();
151 #endif //PIECEPOOL_H