File indexing completed on 2024-12-22 03:51:25

0001 /***************************************************************************
0002  *   Copyright 2010 Stefan Majewsky <majewsky@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU Library General Public License          *
0006  *   version 2 as published by the Free Software Foundation                *
0007  *                                                                         *
0008  *   This program is distributed in the hope that it will be useful,       *
0009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0011  *   GNU Library General Public License for more details.                  *
0012  *                                                                         *
0013  *   You should have received a copy of the GNU Library General Public     *
0014  *   License along with this program; if not, write to the                 *
0015  *   Free Software Foundation, Inc.,                                       *
0016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0017  ***************************************************************************/
0018 
0019 #ifndef TAGARO_SPRITEOBJECTITEM_H
0020 #define TAGARO_SPRITEOBJECTITEM_H
0021 
0022 #include <QGraphicsItem>
0023 
0024 #include <KGameRendererClient>
0025 
0026 namespace Tagaro {
0027 
0028 /**
0029  * @class Tagaro::SpriteObjectItem spriteobjectitem.h <Tagaro/SpriteObjectItem>
0030  * @short A QGraphicsObject which displays pixmaps from a Tagaro::Renderer.
0031  *
0032  * This item displays a pixmap which is retrieved from a Tagaro::Renderer, and
0033  * is updated automatically when the Tagaro::Renderer changes the theme.
0034  *
0035  * The item has built-in handling for animated sprites (i.e. those with multiple
0036  * frames). It is a QGraphicsObject and exposes a "frame" property, so you can
0037  * easily run the animation by plugging in a QPropertyAnimation.
0038  *
0039  * The most important difference to Tagaro::SpriteItem, which is a classic
0040  * QGraphicsPixmapItem, is that the bounding rectangle of this item is fixed
0041  * to QRectF(offset(), size()). The rendered pixmap is scaled to fit into this
0042  * bounding rectangle, regardless of its render size.
0043  *
0044  * To automatically adjust the render size to the physical coordinate system of
0045  * a QGraphicsView, insert the Tagaro::SpriteObjectItem into a Tagaro::Board.
0046  */
0047 class SpriteObjectItem : public QGraphicsObject, public KGameRendererClient
0048 {
0049     Q_OBJECT
0050     Q_PROPERTY(int frame READ frame WRITE setFrame)
0051     Q_PROPERTY(QPointF offset READ offset WRITE setOffset)
0052     Q_PROPERTY(QSize renderSize READ renderSize WRITE setRenderSize)
0053     Q_PROPERTY(QSizeF size READ size WRITE setSize NOTIFY sizeChanged)
0054     Q_PROPERTY(QString spriteKey READ spriteKey WRITE setSpriteKey)
0055     public:
0056         ///Creates a new Tagaro::SpriteObjectItem which renders the sprite with
0057         ///the given @a spriteKey as provided by the given @a renderer.
0058         SpriteObjectItem(KGameRenderer* renderer, const QString& spriteKey, QGraphicsItem* parent = nullptr);
0059         ~SpriteObjectItem() override;
0060 
0061         ///@return the item's offset, which defines the point of the top-left
0062         ///corner of the bounding rect, in local coordinates.
0063         QPointF offset() const;
0064         ///Sets the item's offset, which defines the point of the top-left
0065         ///corner of the bounding rect, in local coordinates.
0066         void setOffset(const QPointF& offset);
0067         ///@overload
0068         inline void setOffset(qreal x, qreal y);
0069         ///@return the size of the item's bounding rect
0070         QSizeF size() const;
0071         ///Sets the size of the item's bounding rect. The rendered pixmap is
0072         ///scaled to fit in this size, regardless of the render size.
0073         void setSize(const QSizeF& size);
0074         ///@overload
0075         inline void setSize(qreal width, qreal height);
0076 
0077         //QGraphicsItem reimplementations (see comment in source file for why we need all of this)
0078         QRectF boundingRect() const override;
0079         bool contains(const QPointF& point) const override;
0080         bool isObscuredBy(const QGraphicsItem* item) const override;
0081         QPainterPath opaqueArea() const override;
0082         void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
0083         QPainterPath shape() const override;
0084     Q_SIGNALS:
0085         ///This signal is emitted when the size of the item's bounding rect
0086         ///changes.
0087         void sizeChanged(const QSizeF& size);
0088     protected:
0089         void receivePixmap(const QPixmap& pixmap) override;
0090     private:
0091         class Private;
0092         Private* const d;
0093 };
0094 
0095 } //namespace Tagaro
0096 
0097 void Tagaro::SpriteObjectItem::setOffset(qreal x, qreal y)
0098 {
0099     setOffset(QPointF(x, y));
0100 }
0101 
0102 void Tagaro::SpriteObjectItem::setSize(qreal w, qreal h)
0103 {
0104     setSize(QSizeF(w, h));
0105 }
0106 
0107 #endif // TAGARO_SPRITEOBJECTITEM_H