Allow building as monolithic app (no plugins) for easier developement.
[tagua/yd.git] / src / piecepool.h
blob7f48f15dcbe70a82303d85fecdfde34c76bbb6aa
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 "namedsprite_utils.h"
18 #include "pixmaploader.h"
20 class AnimationManager;
21 class ChessBoard;
22 class IColor;
24 /**
25 * @class PiecePool <piecepool.h>
26 * @brief The class representing a crazyhouse-like piece pool.
28 * PiecePool is a canvas item attached to a Board that keeps a
29 * stack of pieces and lets you drop them on the board, notifying
30 * the board.
32 class PiecePool : public ClickableCanvas
33 , private IndexConverter {
35 private:
36 const IColor* m_pool;
38 /** displayed m_sprites */
39 std::vector<NamedSprite> m_sprites;
41 /** refrence board */
42 ChessBoard* m_board;
44 /** true if flipped */
45 bool m_flipped;
47 /** size if a square */
48 int m_square_size;
50 /** the width of the pool (ie how many pieces) */
51 int m_width;
53 /** loader class, to load pieces */
54 PixmapLoader m_loader;
56 /** the piece that is being dragged, if any */
57 NamedSprite m_dragged;
59 /** the index of the piece being dragged */
60 int m_dragged_index;
62 AnimationManager* m_anim_manager;
64 /** internal function, resizes the grid vector to hold x pieces */
65 void setFill(int x);
67 /** removes the drag putting it back together with his friends in the pool.
68 if fadeOff is true the current drag will fade off while a new piece will
69 fade in the pool, while if fadeOff is false the fade off is not done.
70 fadeOff will typically be false if the piece sprite has been used in the
71 board and we don't want a clone that is fading off */
72 void cancelDragging(bool fadeOff = true);
74 /** takes the named sprite */
75 NamedSprite takeSpriteAt(int i);
77 /** converts an index to the upper left point of the corresponding square */
78 virtual QPoint toReal(int i) const;
80 /** finds to which index corresponds the point p, or -1 if corresponds to none */
81 virtual int toLogical(const QPoint& p) const;
82 public:
83 friend class GraphicalSystem;
84 friend class ChessTable;
86 /** Constructor, requires the board the pool will be attached to */
87 PiecePool(const IColor* pool, ChessBoard* b, AnimationManager*, KGameCanvasAbstract* parent);
88 ~PiecePool();
90 /** returns the sprite loader */
91 PixmapLoader* loader() { return &m_loader; }
93 /** returns the sprite loader */
94 const PixmapLoader* loader() const { return &m_loader; }
96 /** Load a sprite using the sprite loader */
97 QPixmap loadSprite(const QString& id);
99 /** returns the flipped value */
100 bool flipped() const { return m_flipped; }
102 /** returns the number of pieces in the pool */
103 int fill();
105 /** removes all the pieces */
106 void clear();
108 /** adds a sprite to the pool */
109 void insertSprite(int index, const NamedSprite& sprite);
111 /** \return the sprite at the given index. */
112 NamedSprite getSprite(int index);
114 /** removes the sprite at the given index from the pool. */
115 void removeSprite(int index);
117 /** removes the sprite at the given index from the pool and returns it. */
118 NamedSprite takeSprite(int index);
122 /** sets the width of the grid (the fill will stay the same, and the
123 grid height will be recalculated) */
124 void setGridWidth(int w);
126 /** the rect that will be covered by the pool */
127 virtual QRect boardRect();
129 /** flips and moves the pieces in the pool at the same time */
130 void flipAndMoveBy(QPoint p);
132 /** mouse release event */
133 virtual void onMouseRelease(const QPoint& pos, int button);
135 /** mouse press event */
136 virtual void onMousePress(const QPoint& pos, int button);
138 /** mouse move event */
139 virtual void onMouseMove(const QPoint& pos, int button);
141 /** resize event (new_size is the new size of a SQUARE) */
142 virtual void onResize(int new_size, bool force_reload = false);
144 /** changed settings handler */
145 virtual void settingsChanged();
147 virtual const IndexConverter* converter() const;
150 #endif //PIECEPOOL_H