VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
ImageRenderer.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef IMAGERENDERER_H
18 #define IMAGERENDERER_H
19 
20 #include <memory>
21 #include "Graphic.h"
22 #include "renderer/RenderNode.h"
23 #include "renderer/Texture.h"
24 
25 class Actor;
26 class Body;
27 class QSGTexture;
28 
38 class ImageRenderer : public Graphic, public RenderableInterface {
39  Q_OBJECT
40 
47  Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
51  Q_PROPERTY(float sizeScale READ getSizeScale WRITE setSizeScale NOTIFY sizeScaleChanged)
55  Q_PROPERTY(float aspectRatio READ getAspectRatio WRITE setAspectRatio NOTIFY aspectRatioChanged)
59  Q_PROPERTY(QString source READ getSource WRITE setSource NOTIFY sourceChanged)
78 public:
83  explicit ImageRenderer(QQuickItem* parent = nullptr);
84  virtual ~ImageRenderer();
85 
89  bool isVisible() const { return mVisible; }
94  void setVisible(bool value);
98  float getSizeScale() const { return mSizeScale; }
103  void setSizeScale(float value);
107  float getAspectRatio() const { return mAspectRatio; }
112  void setAspectRatio(float value);
116  const QString& getSource() const { return mSource; }
121  void setSource(const QString& value);
125  bool getCacheRenderParams() const { return mCacheRenderParams; }
130  void setCacheRenderParams(bool value);
134  bool getCullingControlsBodyActive() const { return mCullingControlsBodyActive; }
139  void setCullingControlsBodyActive(bool value);
140 
144  bool isCulled() const { return mCulled; }
145 
146 signals:
150  void visibleChanged();
154  void sizeScaleChanged();
158  void aspectRatioChanged();
162  void sourceChanged();
171 
172 protected:
173  virtual void synchronizeForRendering(RenderList* renderList) override;
174  virtual void render(RenderNode *node) override;
179  virtual void componentComplete() override;
191  virtual void computeDestTextureSize(QSizeF* textureSize) const;
203  virtual void computeSourceTextureRect(QRectF* textureSubRect) const;
208 
209 private:
210  void reloadTexture();
211  void updateLocalBoundingBox();
212  void setCulled(bool value);
213  void updateVisibility();
214  void checkBody();
215 
216  bool mVisible = true;
217  bool mWasVisible = true;
218  bool mCacheRenderParams = false;
219  bool mCullingControlsBodyActive = false;
220  Actor* mActor = nullptr;
221  Body* mBody = nullptr;
222  float mSizeScale = 1.0f;
223  float mAspectRatio = 1.0f;
224  QRectF mLocalBoundingBox;
225  float mTextureAspectRatio = 1.0f;
226  QString mSource;
227  bool mCulled = false;
228  bool mSourceDirty = false;
229  bool mTransformDirty = true;
230  TexturePtr mTexture;
231  bool mHasCachedRenderParams = false;
232  RenderParameters mCachedRenderParams;
233  // Double buffered data for threaded rendering.
234  RenderNode mRenderNode;
235  TexturePtr mRenderTexture;
236  QPointF mRenderPos;
237  QRectF mRenderTextureSubRect;
238  QSizeF mRenderSize;
239  float mRenderRotation = 0.0f;
240  float mRenderOpacity = 1.0f;
241 };
242 Q_DECLARE_METATYPE(ImageRenderer*)
243 
244 #endif // IMAGERENDERER_H
float aspectRatio
Height of this image, as a ratio of the width.
Definition: ImageRenderer.h:55
virtual void computeSourceTextureRect(QRectF *textureSubRect) const
Computes the subrectangle within the source Texture to render.
void aspectRatioChanged()
Emitted when aspectRatio changes.
void sizeScaleChanged()
Emitted when sizeScale changes.
A QObject container for a Box2D b2Body.
Definition: Body.h:57
Callback set in a RenderNode which can be used to issues draw calls.
Definition: RenderableInterface.h:26
virtual void synchronizeForRendering(RenderList *renderList) override
Synchronize this Graphic for rendering, adding any RenderNodes to renderList, and buffering any data ...
float sizeScale
Width of this image, in world coordinates.
Definition: ImageRenderer.h:51
Base class for visual elements which are part of the game scene, and are rendered through Renderer...
Definition: Graphic.h:34
bool getCullingControlsBodyActive() const
Returns cullingControlsBodyActive.
Definition: ImageRenderer.h:134
bool isCulled() const
Returns whether or not this image has been culled because it is off screen.
Definition: ImageRenderer.h:144
void setCullingControlsBodyActive(bool value)
Sets cullingControlsBodyActive.
void visibleChanged()
Emitted when visible changes.
bool visible
Override of QQuickItem's visible property to set visibility.
Definition: ImageRenderer.h:47
void setSizeScale(float value)
Sets sizeScale.
float getSizeScale() const
Returns sizeScale.
Definition: ImageRenderer.h:98
virtual void render(RenderNode *node) override
Renders the associated node.
void cullingControlsBodyActiveChanged()
Emitted when cullingControlsBodyActive changes.
virtual void componentComplete() override
Post-initialization that checks for the existence of a parent Actor and the parent Actor's Body...
void markSourceTextureRectDirty()
Marks the texture rectangle as dirty, making ImageRenderer update.
virtual void computeDestTextureSize(QSizeF *textureSize) const
Computes the size to treat the source Texture, for purposes of computing destination rectangle size...
void setCacheRenderParams(bool value)
Sets cacheRenderParams.
std::shared_ptr< Texture > TexturePtr
Shared pointer typedef for Texture.
Definition: PointerDeclarations.h:173
const QString & getSource() const
Returns source.
Definition: ImageRenderer.h:116
void setAspectRatio(float value)
Sets aspectRatio.
Node which queues a callback (RenderableInterface) or MeshInstance for rendering. ...
Definition: RenderNode.h:33
bool cacheRenderParams
Controls whether or not the position, rotation, opacity, and z-depth are cached on first render...
Definition: ImageRenderer.h:68
bool isVisible() const
Returns visible.
Definition: ImageRenderer.h:89
bool getCacheRenderParams() const
Returns cacheRenderParams.
Definition: ImageRenderer.h:125
float getAspectRatio() const
Returns aspectRatio.
Definition: ImageRenderer.h:107
Representation of an entity within the Game scene.
Definition: Actor.h:40
void setSource(const QString &value)
Sets source.
List of RenderNodes, sorted by z-depth for rendering.
Definition: RenderList.h:36
void setVisible(bool value)
Sets visible.
Graphic which displays a texture as an image.
Definition: ImageRenderer.h:38
void sourceChanged()
Emitted when source changes.
QObject * parent() const
void cacheRenderParamsChanged()
Emitted when cacheRenderParams changes.
QString source
Path to the source texture.
Definition: ImageRenderer.h:59
bool cullingControlsBodyActive
Controls whether or not the Actor's associated Body's Body::active property is linked to this image's...
Definition: ImageRenderer.h:77