KDEGames

kgamerenderedgraphicsobject.h
1/*
2 SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#ifndef KGAMERENDEREDGRAPHICSOBJECT_H
8#define KGAMERENDEREDGRAPHICSOBJECT_H
9
10// own
11#include "kdegames_export.h"
12#include "kgamerendererclient.h"
13// Qt
14#include <QGraphicsObject>
15// Std
16#include <memory>
17
18class QGraphicsView;
19
20class KGameGraphicsViewRenderer;
21class KGameRenderedGraphicsObjectPrivate;
22
23/**
24 * @class KGameRenderedGraphicsObject kgamerenderedgraphicsobject.h <KGameRenderedGraphicsObject>
25 * @short A QGraphicsObject which displays pixmaps from a KGameRenderer.
26 *
27 * This item displays a pixmap which is retrieved from a KGameRenderer, and is
28 * updated automatically when the KGameRenderer changes the theme.
29 *
30 * The item has built-in handling for animated sprites (i.e. those with multiple
31 * frames). It is a QGraphicsObject and exposes a "frame" property, so you can
32 * easily run the animation by plugging in a QPropertyAnimation.
33 *
34 * @section operationalmodes Modes of operation
35 *
36 * By default, this item behaves just like a QGraphicsPixmapItem. The size of
37 * its bounding rect is equal to the size of the pixmap, i.e. the renderSize().
38 *
39 * However, the KGameRenderedGraphicsObject has a second mode of operation, which is
40 * enabled by setting a "primary view". (This can be done automatically via
41 * KGameGraphicsViewRenderer::setDefaultPrimaryView.)
42 *
43 * If such a primary view is set, the following happens:
44 * \li The renderSize of the pixmap is automatically determined from the
45 * painting requests received from the primary view (manual calls to
46 * setRenderSize() are unnecessary and need to be avoided).
47 * \li The size of the item's boundingRect() is independent of the renderSize().
48 * The default fixedSize() is 1x1, which means that the item's bounding rect
49 * is the unit square (moved by the configured offset()).
50 * @since 4.6
51 */
53{
54 Q_OBJECT
55 Q_PROPERTY(int frame READ frame WRITE setFrame)
56
57public:
58 /// Creates a new KGameRenderedGraphicsObject which renders the sprite with
59 /// the given @a spriteKey as provided by the given @a renderer.
60 KGameRenderedGraphicsObject(KGameGraphicsViewRenderer *renderer, const QString &spriteKey, QGraphicsItem *parent = nullptr);
62
63 /// @return the item's offset, which defines the point of the top-left
64 /// corner of the bounding rect, in local coordinates.
65 QPointF offset() const;
66 /// Sets the item's offset, which defines the point of the top-left
67 /// corner of the bounding rect, in local coordinates.
68 void setOffset(QPointF offset);
69 /// @overload
70 void setOffset(qreal x, qreal y);
71 /// @return the fixed size of this item (or (-1, -1) if this item has no
72 /// primary view)
73 QSizeF fixedSize() const;
74 /// Sets the fixed size of this item, i.e. the guaranteed size of the
75 /// item. This works only when a primary view has been set.
76 void setFixedSize(QSizeF size);
77
78 /// Returns a pointer to the current primary view, or 0 if no primary
79 /// view has been set (which is the default).
80 /// @see setPrimaryView()
81 QGraphicsView *primaryView() const;
82 /// Sets the primary view of this item. (See class documentation for what
83 /// the primary view does.) Pass a null pointer to just disconnect from
84 /// the current primary view. The fixed size is then reset to (-1, -1).
85 /// If a primary view is set, the fixed size is initialized to (1, 1).
86 /// @warning While a primary view is set, avoid any manual calls to
87 /// setRenderSize().
88 /// @see {Modes of operation}
89 void setPrimaryView(QGraphicsView *view);
90
91 // QGraphicsItem reimplementations (see comment in source file for why we need all of this)
92 QRectF boundingRect() const override;
93 bool contains(const QPointF &point) const override;
94 bool isObscuredBy(const QGraphicsItem *item) const override;
95 QPainterPath opaqueArea() const override;
96 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
97 QPainterPath shape() const override;
98
99protected:
100 void receivePixmap(const QPixmap &pixmap) override;
101
102private:
103 friend class KGameRenderedGraphicsObjectPrivate;
104 std::unique_ptr<KGameRenderedGraphicsObjectPrivate> const d_ptr;
105 Q_DECLARE_PRIVATE(KGameRenderedGraphicsObject)
106};
107
108#endif // KGAMERENDEREDGRAPHICSOBJECT_H
A QGraphicsObject which displays pixmaps from a KGameRenderer.
An object that receives pixmaps from a KGameRenderer.
virtual void receivePixmap(const QPixmap &pixmap)=0
This method is called when the KGameRenderer has provided a new pixmap for this client (esp.
virtual QRectF boundingRect() const const=0
virtual bool contains(const QPointF &point) const const
virtual bool isObscuredBy(const QGraphicsItem *item) const const
virtual QPainterPath opaqueArea() const const
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)=0
virtual QPainterPath shape() const const
Q_PROPERTY(...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:06:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.