Starting to make things work again: dragging
[tagua.git] / src / piecepool.h
blob92cf803fe21f2e66aad9c94fbed7bcbd80089bb9
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;
39 int m_dragged_index;
41 /** the number of pieces on the pool */
42 int m_fill;
44 /** internal, resizes the grid vector to hold x pieces */
45 void setFill(int x);
47 /** redefinition of PointConverter::flipPoint
48 if the PiecePool is flipped it will be drawn using the position as
49 lower bound. Further, the points are ordered in a snake-like way */
50 virtual Point flipPoint(const Point& p) const; //custom flip function
52 /** removes the drag putting it back together with his friends in the pool.
53 if fadeOff is true the current drag will fade off while a new piece will
54 fade in the pool, while if fadeOff is false the fade off is no done.
55 fadeOff will typically be false if the piece sprite has been used in the
56 board and we don't want a clone that is fading off */
57 void clearDrag(bool fadeOff = true);
59 /** this internal function updates the sprite images after the board has been resized */
60 void updateSprites();
62 /** fetch the sprite */
63 boost::shared_ptr<Sprite> spriteAt(const Point& p) { return m_sprites[p].sprite(); }
65 /** takes the named sprite */
66 NamedSprite takeNamedSprite(const Point& p);
68 public:
69 friend class GraphicalSystem;
70 friend class ChessTable;
72 /** Constructor, requires the board the pool will be attached to */
73 PiecePool(Board* b, Canvas::Abstract* parent);
74 ~PiecePool();
78 /** returns the number of pieces in the pool */
79 int fill();
81 /** removes all the pieces */
82 void clear();
84 /** adds a piece to the pool */
85 void insertSprite(int index, const NamedSprite& sprite);
87 /** \return the piece at the given index. */
88 SpritePtr getSprite(int index);
90 /** removes the piece at the given index from the pool and returns it. */
91 SpritePtr takeSprite(int index);
95 /** sets the width of the grid (the fill will stay the same, and the
96 grid height will be recalculated) */
97 void setGridWidth(int w);
99 /** \return the size of the grid */
100 virtual Point gridSize() const { return m_sprites.getSize(); }
102 /** piecesGroup overload */
103 virtual Canvas::Abstract* piecesGroup();
105 /** the rect that will be covered by the pool */
106 virtual QRect boardRect() { return QRect(pos(), QSize(m_square_size*gridSize().x,
107 (flipped()?-1:1)*m_square_size*gridSize().y)); }
109 /** flips and moves the pool at the same time */
110 void flipAndMoveBy(QPoint p);
112 /** mouse release event */
113 virtual void onMouseRelease(const QPoint& pos, int button);
115 /** mouse press event */
116 virtual void onMousePress(const QPoint& pos, int button);
118 /** mouse move event */
119 virtual void onMouseMove(const QPoint& pos, int button);
121 /** resize event (new_size is the new size of a SQUARE) */
122 virtual void onResize(int new_size, bool force_reload = false);
124 /** changed settings handler */
125 virtual void settingsChanged();
128 #endif //__PIECEPOOL_H__