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.
14 #include <boost/shared_ptr.hpp>
15 #include "kgamecanvas.h"
18 #include "animation.h"
20 #include "mainanimation.h"
21 #include "pointconverter.h"
22 #include "pixmaploader.h"
23 #include "clickablecanvas.h"
32 * @class PieceGroup <piecegroup.h>
33 * @brief The abstract base class for all widgets that draw pieces.
35 * PieceGroup is a general abstract widget that can be subclassed to
36 * implement a rectangular widget with piece m_sprites on it.
37 * It uses the method template pattern to provide a flexible yet comfortable
38 * api for subclassing.
40 class PieceGroup
: public ClickableCanvas
, protected PointConverter
{
44 /** fading animations enabled */
47 /** movement animations enabled */
50 /** true if the board is flipped */
53 /** size of a square in points */
56 /** loader class, to load pieces */
57 PixmapLoader m_loader
;
59 /** main animation structure */
60 MainAnimation
* m_main_animation
;
62 /** enqueue an animation */
63 void enqueue(const boost::shared_ptr
<Animation
>&);
65 /** stops all animations */
66 void stopAnimations();
68 /** finalizes an animaiton group */
69 void finalizeAnimation(AnimationGroup
*);
71 /** ajdusts the sprite's position */
72 void adjustSprite(const Point
&, bool smooth
= false);
74 /** enqueue a movement animation (convenience function) */
75 void animatePiece(const boost::shared_ptr
<Sprite
>& piece
,
76 const Point
& to
, double speed
);
78 /** fades in the sprite at a given point */
79 void fadeIn(const Point
&);
81 /** returns the sprite at p */
82 virtual boost::shared_ptr
<Sprite
> spriteAt(const Point
&) = 0;
85 PieceGroup(KGameCanvasAbstract
* parent
);
86 virtual ~PieceGroup();
88 /** returns the point converter class */
89 PointConverter
* converter() { return static_cast<PointConverter
*>(this); }
91 /** returns the point converter class */
92 const PointConverter
* converter() const { return static_cast<const PointConverter
*>(this); }
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 /** returns the flipped value */
101 virtual bool flipped() const { return m_flipped
; }
103 /** returns the size of a square */
104 virtual int squareSize() const { return m_square_size
; }
106 /** returns the size of the grid */
107 virtual Point
gridSize() const = 0;
109 /** returns the center of a square */
110 QPoint
squareCenter(const QPoint
& point
) {
111 return point
- QPoint(m_square_size
, m_square_size
) / 2;
114 /** x size (deprecated, use boardRect instead) */
115 int boardSizeX() { return m_square_size
*gridSize().x
; }
117 /** y size (deprecated, use boardRect instead) */
118 int boardSizeY() { return m_square_size
*gridSize().y
; }
120 /** returns the area covered by the piece grid */
121 virtual QRect
boardRect() { return QRect(pos(), QSize(m_square_size
*gridSize().x
,
122 m_square_size
*gridSize().y
)); }
124 /** returns the group that contains the pieces (override this) */
125 virtual KGameCanvasAbstract
* piecesGroup() = 0;
127 /** resize event handler (updates the sprites and the square size) */
128 virtual void onResize(int new_size
, bool force_reload
= false);
130 /** changed settings handler */
131 virtual void settingsChanged();
133 /** create a sprite from a pixmap */
134 virtual boost::shared_ptr
<Sprite
> createSprite(const QPixmap
& pix
, const Point
& pos
);
139 #endif //PIECEGROUP_H