File indexing completed on 2024-04-28 04:05:03

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KGAMERENDEREDGRAPHICSOBJECT_H
0008 #define KGAMERENDEREDGRAPHICSOBJECT_H
0009 
0010 // own
0011 #include "kdegames_export.h"
0012 #include "kgamerendererclient.h"
0013 // Qt
0014 #include <QGraphicsObject>
0015 // Std
0016 #include <memory>
0017 
0018 class QGraphicsView;
0019 
0020 class KGameGraphicsViewRenderer;
0021 class KGameRenderedGraphicsObjectPrivate;
0022 
0023 /**
0024  * @class KGameRenderedGraphicsObject kgamerenderedgraphicsobject.h <KGameRenderedGraphicsObject>
0025  * @short A QGraphicsObject which displays pixmaps from a KGameRenderer.
0026  *
0027  * This item displays a pixmap which is retrieved from a KGameRenderer, and is
0028  * updated automatically when the KGameRenderer changes the theme.
0029  *
0030  * The item has built-in handling for animated sprites (i.e. those with multiple
0031  * frames). It is a QGraphicsObject and exposes a "frame" property, so you can
0032  * easily run the animation by plugging in a QPropertyAnimation.
0033  *
0034  * @section operationalmodes Modes of operation
0035  *
0036  * By default, this item behaves just like a QGraphicsPixmapItem. The size of
0037  * its bounding rect is equal to the size of the pixmap, i.e. the renderSize().
0038  *
0039  * However, the KGameRenderedGraphicsObject has a second mode of operation, which is
0040  * enabled by setting a "primary view". (This can be done automatically via
0041  * KGameGraphicsViewRenderer::setDefaultPrimaryView.)
0042  *
0043  * If such a primary view is set, the following happens:
0044  * \li The renderSize of the pixmap is automatically determined from the
0045  *     painting requests received from the primary view (manual calls to
0046  *     setRenderSize() are unnecessary and need to be avoided).
0047  * \li The size of the item's boundingRect() is independent of the renderSize().
0048  *     The default fixedSize() is 1x1, which means that the item's bounding rect
0049  *     is the unit square (moved by the configured offset()).
0050  * @since 4.6
0051  */
0052 class KDEGAMES_EXPORT KGameRenderedGraphicsObject : public QGraphicsObject, public KGameRendererClient
0053 {
0054     Q_OBJECT
0055     Q_PROPERTY(int frame READ frame WRITE setFrame)
0056 
0057 public:
0058     /// Creates a new KGameRenderedGraphicsObject which renders the sprite with
0059     /// the given @a spriteKey as provided by the given @a renderer.
0060     KGameRenderedGraphicsObject(KGameGraphicsViewRenderer *renderer, const QString &spriteKey, QGraphicsItem *parent = nullptr);
0061     ~KGameRenderedGraphicsObject() override;
0062 
0063     /// @return the item's offset, which defines the point of the top-left
0064     /// corner of the bounding rect, in local coordinates.
0065     QPointF offset() const;
0066     /// Sets the item's offset, which defines the point of the top-left
0067     /// corner of the bounding rect, in local coordinates.
0068     void setOffset(QPointF offset);
0069     /// @overload
0070     void setOffset(qreal x, qreal y);
0071     /// @return the fixed size of this item (or (-1, -1) if this item has no
0072     /// primary view)
0073     QSizeF fixedSize() const;
0074     /// Sets the fixed size of this item, i.e. the guaranteed size of the
0075     /// item. This works only when a primary view has been set.
0076     void setFixedSize(QSizeF size);
0077 
0078     /// Returns a pointer to the current primary view, or 0 if no primary
0079     /// view has been set (which is the default).
0080     /// @see setPrimaryView()
0081     QGraphicsView *primaryView() const;
0082     /// Sets the primary view of this item. (See class documentation for what
0083     /// the primary view does.) Pass a null pointer to just disconnect from
0084     /// the current primary view. The fixed size is then reset to (-1, -1).
0085     /// If a primary view is set, the fixed size is initialized to (1, 1).
0086     /// @warning While a primary view is set, avoid any manual calls to
0087     /// setRenderSize().
0088     /// @see {Modes of operation}
0089     void setPrimaryView(QGraphicsView *view);
0090 
0091     // QGraphicsItem reimplementations (see comment in source file for why we need all of this)
0092     QRectF boundingRect() const override;
0093     bool contains(const QPointF &point) const override;
0094     bool isObscuredBy(const QGraphicsItem *item) const override;
0095     QPainterPath opaqueArea() const override;
0096     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
0097     QPainterPath shape() const override;
0098 
0099 protected:
0100     void receivePixmap(const QPixmap &pixmap) override;
0101 
0102 private:
0103     friend class KGameRenderedGraphicsObjectPrivate;
0104     std::unique_ptr<KGameRenderedGraphicsObjectPrivate> const d_ptr;
0105     Q_DECLARE_PRIVATE(KGameRenderedGraphicsObject)
0106 };
0107 
0108 #endif // KGAMERENDEREDGRAPHICSOBJECT_H