6#include "QskFillNode.h"
7#include "QskGradientMaterial.h"
8#include "QskFillNodePrivate.h"
11#include <qsgflatcolormaterial.h>
12#include <qsgvertexcolormaterial.h>
13#include <qglobalstatic.h>
15Q_GLOBAL_STATIC( QSGVertexColorMaterial, qskMaterialColorVertex )
17static inline QskGradient::Type qskGradientType( QskFillNode::Coloring coloring )
19 const auto type = QskGradient::Linear + coloring - QskFillNode::Linear;
20 return static_cast< QskGradient::Type
>( type );
23static inline QskFillNode::Coloring qskColoring( QskGradient::Type type )
25 const auto coloring = QskFillNode::Linear + type - QskGradient::Linear;
26 return static_cast< QskFillNode::Coloring
>( coloring );
29static inline QskFillNode::Hints qskDefaultFillNodeHints()
31 extern bool qskHasEnvironment(
const char* );
33 QskFillNode::Hints hints;
34 if ( !qskHasEnvironment(
"QSK_PREFER_SHADER_COLORS" ) )
35 hints |= QskFillNode::PreferColoredGeometry;
40QskFillNode::QskFillNode()
45QskFillNode::QskFillNode( QskFillNodePrivate& dd )
46 : QSGGeometryNode( dd )
48 static const auto hints = qskDefaultFillNodeHints();
51 setGeometry( &dd.geometry );
53 setMaterial( qskMaterialColorVertex );
54 setFlag( QSGNode::OwnsMaterial,
false );
57QskFillNode::~QskFillNode()
61void QskFillNode::resetGeometry()
63 QskSGNode::resetGeometry(
this );
66void QskFillNode::setColoring( Coloring coloring )
70 if ( coloring == d->coloring )
73 d->coloring = coloring;
79 setMaterial(
new QSGFlatColorMaterial() );
85 setMaterial( qskMaterialColorVertex );
96 const auto gradientType = qskGradientType( coloring );
97 setMaterial( QskGradientMaterial::createMaterial( gradientType ) );
101 if ( material() == qskMaterialColorVertex )
110 setFlag( QSGNode::OwnsMaterial,
false );
112 if ( !isGeometryColored() )
114 const QSGGeometry g( QSGGeometry::defaultAttributes_ColoredPoint2D(), 0 );
115 memcpy( (
void* ) &d->geometry, (
void* ) &g,
sizeof( QSGGeometry ) );
120 setFlag( QSGNode::OwnsMaterial,
true );
122 if ( isGeometryColored() )
124 const QSGGeometry g( QSGGeometry::defaultAttributes_Point2D(), 0 );
125 memcpy( (
void* ) &d->geometry, (
void* ) &g,
sizeof( QSGGeometry ) );
130QskFillNode::Coloring QskFillNode::coloring()
const
132 return static_cast< QskFillNode::Coloring
>( d_func()->coloring );
135void QskFillNode::setColoring(
const QColor& color )
137 setColoring( Monochrome );
139 const auto colorRgb = color.toRgb();
141 auto mat =
static_cast< QSGFlatColorMaterial*
>( material() );
142 if ( mat->color() != colorRgb )
144 mat->setColor( colorRgb );
145 markDirty( QSGNode::DirtyMaterial );
149void QskFillNode::setColoring(
const QRectF& rect,
const QskGradient& gradient )
151 if ( gradient.isMonochrome() )
153 setColoring( gradient.startColor() );
157 if ( gradient.type() == QskGradient::Stops )
159 qWarning() <<
"QskFillNode::setColoring:"
160 <<
"QskGradient::Stops is not supported, using the first color instead.";
162 setColoring( gradient.startColor() );
166 setColoring( qskColoring( gradient.type() ) );
169 if ( mat->updateGradient( rect, gradient ) )
170 markDirty( QSGNode::DirtyMaterial );
175void QskFillNode::setHint( Hint hint,
bool on )
185void QskFillNode::setHints( Hints hints )
187 d_func()->hints = hints;
190QskFillNode::Hints QskFillNode::hints()
const
192 return static_cast< QskFillNode::Hints
>( d_func()->hints );
195bool QskFillNode::hasHint( Hint hint )
const
197 return d_func()->hints & hint;
200bool QskFillNode::isGeometryColored()
const
202 return d_func()->geometry.attributeCount() != 1;
205#include "moc_QskFillNode.cpp"