Initial implementation of the new abstraction architecture.
[tagua.git] / src / piecepool.h
blobe58225c4a85cc75add55e2e2b412a6b194ce913e
1 /*
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.
9 */
11 #ifndef __PIECEPOOL_H__
12 #define __PIECEPOOL_H__
14 #include <boost/shared_ptr.hpp>
15 #include "piecegroup.h"
16 #include "namedsprite.h"
18 /**
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
24 * the board.
26 class PiecePool : public PieceGroup {
27 public:
28 typedef Grid<NamedSprite> PieceGrid;
30 private:
31 /** displayed m_sprites */
32 PieceGrid m_sprites;
34 /** refrence board */
35 class Board* m_board;
37 /** the piece that is being dragged, if any */
38 NamedSprite m_dragged;
40 /** the number of pieces on the pool */
41 int m_fill;
43 /** internal, resizes the grid vector to hold x pieces */
44 void setFill(int x);
46 /** redefinition of PointConverter::flipPoint
47 if the PiecePool is flipped it will be drawn using the position as
48 lower bound. Further, the points are ordered in a snake-like way */
49 virtual Point flipPoint(const Point& p) const; //custom flip function
51 /** removes the drag putting it back together with his friends in the pool.
52 if fadeOff is true the current drag will fade off while a new piece will
53 fade in the pool, while if fadeOff is false the fade off is no done.
54 fadeOff will typically be false if the piece sprite has been used in the
55 board and we don't want a clone that is fading off */
56 void clearDrag(bool fadeOff = true);
58 /** this internal function updates the sprite images after the board has been resized */
59 void updateSprites();
61 /** fetch the sprite */
62 boost::shared_ptr<Sprite> spriteAt(const Point& p) { return m_sprites[p].sprite(); }
64 /** takes the named sprite */
65 NamedSprite takeNamedSprite(const Point& p);
67 public:
68 friend class GraphicalSystem;
69 friend class ChessTable;
71 /** Constructor, requires the board the pool will be attached to */
72 PiecePool(Board* b, Canvas::Abstract* parent);
73 ~PiecePool();
77 /** returns the number of pieces in the pool */
78 int fill();
80 /** removes all the pieces */
81 void clear();
83 /** adds a piece to the pool */
84 void insertSprite(int index, const NamedSprite& sprite);
86 /** \return the piece at the given index. */
87 SpritePtr getSprite(int index);
89 /** removes the piece at the given index from the pool and returns it. */
90 SpritePtr takeSprite(int index);
94 /** sets the width of the grid (the fill will stay the same, and the
95 grid height will be recalculated) */
96 void setGridWidth(int w);
98 /** \return the size of the grid */
99 virtual Point gridSize() const { return m_sprites.getSize(); }
101 /** piecesGroup overload */
102 virtual Canvas::Abstract* piecesGroup();
104 /** the rect that will be covered by the pool */
105 virtual QRect boardRect() { return QRect(pos(), QSize(m_square_size*gridSize().x,
106 (flipped()?-1:1)*m_square_size*gridSize().y)); }
108 /** flips and moves the pool at the same time */
109 void flipAndMoveBy(QPoint p);
111 /** mouse release event */
112 virtual void onMouseRelease(const QPoint& pos, int button);
114 /** mouse press event */
115 virtual void onMousePress(const QPoint& pos, int button);
117 /** mouse move event */
118 virtual void onMouseMove(const QPoint& pos, int button);
120 /** resize event (new_size is the new size of a SQUARE) */
121 virtual void onResize(int new_size, bool force_reload = false);
123 /** changed settings handler */
124 virtual void settingsChanged();
127 #endif //__PIECEPOOL_H__