qskinny/playground/parrots/TextureFilterNode.cpp

100 lines
2.3 KiB
C++
Raw Normal View History

2023-12-07 10:24:47 +00:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
2023-12-18 08:39:17 +00:00
#include "TextureFilterNode.h"
2023-12-19 17:37:34 +00:00
#include "TextureFilterMaterial.h"
2023-12-07 10:24:47 +00:00
#include <qsgtexture.h>
#include <private/qsgnode_p.h>
2023-12-18 08:39:17 +00:00
class TextureFilterNodePrivate final : public QSGGeometryNodePrivate
2023-12-07 10:24:47 +00:00
{
public:
2023-12-18 08:39:17 +00:00
TextureFilterNodePrivate()
2023-12-07 10:24:47 +00:00
: geometry( QSGGeometry::defaultAttributes_TexturedPoint2D(), 4 )
{
}
QSGGeometry geometry;
QRectF rect;
2023-12-19 17:37:34 +00:00
bool ownsTexture = false;
2023-12-07 10:24:47 +00:00
};
2023-12-18 08:39:17 +00:00
TextureFilterNode::TextureFilterNode()
: QSGGeometryNode( *new TextureFilterNodePrivate )
2023-12-07 10:24:47 +00:00
{
2023-12-18 08:39:17 +00:00
Q_D( TextureFilterNode );
2023-12-07 10:24:47 +00:00
setGeometry( &d->geometry );
2023-12-19 17:37:34 +00:00
setFlag( QSGNode::OwnsMaterial, true );
2023-12-07 10:24:47 +00:00
}
2023-12-18 08:39:17 +00:00
TextureFilterNode::~TextureFilterNode()
2023-12-07 10:24:47 +00:00
{
2023-12-19 17:37:34 +00:00
setTexture( nullptr );
}
void TextureFilterNode::setTextureMaterial( TextureFilterMaterial* material )
{
QSGTexture* texture = nullptr;
if ( auto oldMaterial = textureMaterial() )
texture = oldMaterial->texture();
Inherited::setMaterial( material );
if ( material )
material->setTexture( texture );
}
TextureFilterMaterial* TextureFilterNode::textureMaterial() const
{
return dynamic_cast< TextureFilterMaterial* >( material() );
2023-12-07 10:24:47 +00:00
}
2023-12-18 08:39:17 +00:00
void TextureFilterNode::setTexture( QSGTexture* texture )
2023-12-07 10:24:47 +00:00
{
2023-12-19 17:37:34 +00:00
if ( auto mat = textureMaterial() )
{
if ( ownsTexture() && mat->texture() != texture )
delete mat->texture();
mat->setTexture( texture );
markDirty( QSGNode::DirtyMaterial );
}
2023-12-07 10:24:47 +00:00
}
2023-12-19 17:37:34 +00:00
QSGTexture* TextureFilterNode::texture() const
2023-12-07 10:24:47 +00:00
{
2023-12-19 17:37:34 +00:00
auto mat = textureMaterial();
return mat ? mat->texture() : nullptr;
2023-12-07 10:24:47 +00:00
}
2023-12-18 08:39:17 +00:00
void TextureFilterNode::setRect( const QRectF& rect )
2023-12-07 10:24:47 +00:00
{
2023-12-18 08:39:17 +00:00
Q_D( TextureFilterNode );
2023-12-07 10:24:47 +00:00
if ( rect != d->rect )
2023-12-07 10:24:47 +00:00
{
d->rect = rect;
2023-12-07 10:24:47 +00:00
QSGGeometry::updateTexturedRectGeometry(
&d->geometry, rect, QRectF( 0, 0, 1, 1 ) );
2023-12-07 10:24:47 +00:00
d->geometry.markVertexDataDirty();
markDirty( QSGNode::DirtyGeometry );
}
2023-12-07 10:24:47 +00:00
}
2023-12-19 17:37:34 +00:00
void TextureFilterNode::setOwnsTexture( bool on )
{
d_func()->ownsTexture = on;
}
bool TextureFilterNode::ownsTexture() const
{
return d_func()->ownsTexture;
}