2022-09-23 17:49:49 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
2023-04-06 09:23:37 +02:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2022-09-23 17:49:49 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "QskStrokeNode.h"
|
2023-05-02 18:04:23 +02:00
|
|
|
#include "QskVertex.h"
|
|
|
|
|
2022-09-23 17:49:49 +02:00
|
|
|
#include <qsgflatcolormaterial.h>
|
2023-05-02 18:04:23 +02:00
|
|
|
#include <qsgvertexcolormaterial.h>
|
|
|
|
#include <qpainterpath.h>
|
|
|
|
#include <qpolygon.h>
|
2022-09-23 17:49:49 +02:00
|
|
|
|
|
|
|
QSK_QT_PRIVATE_BEGIN
|
|
|
|
#include <private/qsgnode_p.h>
|
|
|
|
#include <private/qtriangulatingstroker_p.h>
|
|
|
|
QSK_QT_PRIVATE_END
|
|
|
|
|
2023-05-02 18:04:23 +02:00
|
|
|
Q_GLOBAL_STATIC( QSGVertexColorMaterial, qskMaterialColorVertex )
|
|
|
|
|
|
|
|
static inline void qskMapPolygon( const QPolygonF& polygon,
|
|
|
|
const QTransform& transform, const QColor& color, QSGGeometry& geometry )
|
|
|
|
{
|
|
|
|
const QskVertex::Color c( color );
|
|
|
|
|
|
|
|
auto p = geometry.vertexDataAsColoredPoint2D();
|
|
|
|
|
|
|
|
if ( transform.isIdentity() )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < polygon.count(); i++ )
|
|
|
|
{
|
|
|
|
const auto& pos = polygon[i];
|
|
|
|
p[i].set( pos.x(), pos.y(), c.r, c.g, c.b, c.a );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < polygon.count(); i++ )
|
|
|
|
{
|
|
|
|
const auto pos = transform.map( polygon[i] );
|
|
|
|
p[i].set( pos.x(), pos.y(), c.r, c.g, c.b, c.a );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void qskMapPolygon( const QPolygonF& polygon,
|
|
|
|
const QTransform& transform, QSGGeometry& geometry )
|
|
|
|
{
|
|
|
|
auto p = geometry.vertexDataAsPoint2D();
|
|
|
|
|
|
|
|
if ( transform.isIdentity() )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < polygon.count(); i++ )
|
|
|
|
{
|
|
|
|
const auto& pos = polygon[i];
|
|
|
|
p[i].set( pos.x(), pos.y() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < polygon.count(); i++ )
|
|
|
|
{
|
|
|
|
const auto pos = transform.map( polygon[i] );
|
|
|
|
p[i].set( pos.x(), pos.y() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:49:49 +02:00
|
|
|
class QskStrokeNodePrivate final : public QSGGeometryNodePrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QskStrokeNodePrivate()
|
2023-05-02 18:04:23 +02:00
|
|
|
: geometry( QSGGeometry::defaultAttributes_ColoredPoint2D(), 0 )
|
2022-09-23 17:49:49 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QSGGeometry geometry;
|
|
|
|
};
|
|
|
|
|
|
|
|
QskStrokeNode::QskStrokeNode()
|
|
|
|
: QSGGeometryNode( *new QskStrokeNodePrivate )
|
|
|
|
{
|
|
|
|
Q_D( QskStrokeNode );
|
|
|
|
|
|
|
|
setGeometry( &d->geometry );
|
2023-05-02 18:04:23 +02:00
|
|
|
setMaterial( qskMaterialColorVertex );
|
|
|
|
}
|
|
|
|
|
2023-05-17 14:21:40 +02:00
|
|
|
QskStrokeNode::~QskStrokeNode() = default;
|
|
|
|
|
2023-05-02 18:04:23 +02:00
|
|
|
void QskStrokeNode::setRenderHint( RenderHint renderHint )
|
|
|
|
{
|
|
|
|
Q_D( QskStrokeNode );
|
|
|
|
|
|
|
|
const auto material = this->material();
|
|
|
|
|
|
|
|
if ( renderHint == Colored )
|
|
|
|
{
|
|
|
|
if ( material != qskMaterialColorVertex )
|
|
|
|
{
|
|
|
|
setMaterial( qskMaterialColorVertex );
|
|
|
|
delete material;
|
|
|
|
|
|
|
|
const QSGGeometry g( QSGGeometry::defaultAttributes_ColoredPoint2D(), 0 );
|
|
|
|
memcpy( ( void* ) &d->geometry, ( void* ) &g, sizeof( QSGGeometry ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( material == qskMaterialColorVertex )
|
|
|
|
{
|
|
|
|
setMaterial( new QSGFlatColorMaterial() );
|
|
|
|
|
|
|
|
const QSGGeometry g( QSGGeometry::defaultAttributes_Point2D(), 0 );
|
|
|
|
memcpy( ( void* ) &d->geometry, ( void* ) &g, sizeof( QSGGeometry ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QskStrokeNode::RenderHint QskStrokeNode::renderHint() const
|
|
|
|
{
|
|
|
|
return ( material() == qskMaterialColorVertex ) ? Colored : Flat;
|
2022-09-23 17:49:49 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 09:37:43 +02:00
|
|
|
void QskStrokeNode::updateNode( const QPainterPath& path, const QPen& pen )
|
|
|
|
{
|
|
|
|
updateNode( path, QTransform(), pen );
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:26:15 +02:00
|
|
|
void QskStrokeNode::updateNode(
|
|
|
|
const QPainterPath& path, const QTransform& transform, const QPen& pen )
|
2022-09-23 17:49:49 +02:00
|
|
|
{
|
|
|
|
Q_D( QskStrokeNode );
|
|
|
|
|
2022-09-29 17:26:15 +02:00
|
|
|
if ( path.isEmpty() || ( pen.style() == Qt::NoPen ) ||
|
2023-05-02 18:04:23 +02:00
|
|
|
!pen.color().isValid() || ( pen.color().alpha() == 0 ) )
|
2022-09-23 17:49:49 +02:00
|
|
|
{
|
|
|
|
if ( d->geometry.vertexCount() > 0 )
|
|
|
|
{
|
|
|
|
d->geometry.allocate( 0 );
|
|
|
|
markDirty( QSGNode::DirtyGeometry );
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( true ) // For the moment we always update the geometry. TODO ...
|
|
|
|
{
|
2022-09-29 17:26:15 +02:00
|
|
|
/*
|
|
|
|
Unfortunately QTriangulatingStroker does not offer on the fly
|
|
|
|
transformations - like with qTriangulate. TODO ...
|
|
|
|
*/
|
|
|
|
const auto scaledPath = transform.map( path );
|
|
|
|
|
2022-09-30 15:09:52 +02:00
|
|
|
auto effectivePen = pen;
|
|
|
|
|
|
|
|
if ( !effectivePen.isCosmetic() )
|
|
|
|
{
|
|
|
|
const auto scaleFactor = qMin( transform.m11(), transform.m22() );
|
|
|
|
if ( scaleFactor != 1.0 )
|
|
|
|
{
|
|
|
|
effectivePen.setWidth( effectivePen.widthF() * scaleFactor );
|
|
|
|
effectivePen.setCosmetic( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 17:49:49 +02:00
|
|
|
QTriangulatingStroker stroker;
|
|
|
|
|
|
|
|
if ( pen.style() == Qt::SolidLine )
|
|
|
|
{
|
|
|
|
// clipRect, renderHint are ignored in QTriangulatingStroker::process
|
2022-09-30 15:09:52 +02:00
|
|
|
stroker.process( qtVectorPathForPath( scaledPath ), effectivePen, {}, {} );
|
2022-09-23 17:49:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-29 17:26:15 +02:00
|
|
|
constexpr QRectF clipRect; // empty rect: no clipping
|
|
|
|
|
2022-09-23 17:49:49 +02:00
|
|
|
QDashedStrokeProcessor dashStroker;
|
2022-09-30 15:09:52 +02:00
|
|
|
dashStroker.process( qtVectorPathForPath( scaledPath ), effectivePen, clipRect, {} );
|
2022-09-23 17:49:49 +02:00
|
|
|
|
|
|
|
const QVectorPath dashedVectorPath( dashStroker.points(),
|
|
|
|
dashStroker.elementCount(), dashStroker.elementTypes(), 0 );
|
|
|
|
|
2022-09-30 15:09:52 +02:00
|
|
|
stroker.process( dashedVectorPath, effectivePen, {}, {} );
|
2022-09-23 17:49:49 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 17:36:55 +02:00
|
|
|
// 2 vertices for each point
|
2023-05-02 18:04:23 +02:00
|
|
|
d->geometry.setDrawingMode( QSGGeometry::DrawTriangleStrip );
|
2022-10-18 17:36:55 +02:00
|
|
|
d->geometry.allocate( stroker.vertexCount() / 2 );
|
2022-09-23 17:49:49 +02:00
|
|
|
|
2023-05-02 18:04:23 +02:00
|
|
|
if ( material() == qskMaterialColorVertex )
|
|
|
|
{
|
|
|
|
const QskVertex::Color c( pen.color() );
|
|
|
|
|
|
|
|
const auto v = stroker.vertices();
|
|
|
|
auto points = d->geometry.vertexDataAsColoredPoint2D();
|
|
|
|
|
|
|
|
for ( int i = 0; i < d->geometry.vertexCount(); i++ )
|
|
|
|
{
|
|
|
|
const auto j = 2 * i;
|
|
|
|
points[i].set( v[j], v[j + 1], c.r, c.g, c.b, c.a );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( d->geometry.vertexData(), stroker.vertices(),
|
|
|
|
stroker.vertexCount() * sizeof( float ) );
|
|
|
|
}
|
2022-09-23 17:49:49 +02:00
|
|
|
|
|
|
|
markDirty( QSGNode::DirtyGeometry );
|
|
|
|
}
|
|
|
|
|
2023-05-02 18:04:23 +02:00
|
|
|
if ( material() != qskMaterialColorVertex )
|
|
|
|
{
|
|
|
|
const auto color = pen.color().toRgb();
|
|
|
|
|
|
|
|
auto flatMaterial = static_cast< QSGFlatColorMaterial* >( material() );
|
|
|
|
if ( flatMaterial->color() != color )
|
|
|
|
{
|
|
|
|
flatMaterial->setColor( color );
|
|
|
|
markDirty( QSGNode::DirtyMaterial );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:37:43 +02:00
|
|
|
void QskStrokeNode::updateNode( const QPolygonF& polygon,
|
|
|
|
qreal lineWidth, const QColor& color )
|
|
|
|
{
|
|
|
|
updateNode( polygon, QTransform(), lineWidth, color );
|
|
|
|
}
|
|
|
|
|
2023-05-02 18:04:23 +02:00
|
|
|
/*
|
|
|
|
For polygons with a small lineWidth ( < 2 ) or a line without
|
|
|
|
connections we might get away with a simple and fast implementation
|
|
|
|
using DrawLineStrip/DrawLineLoop
|
|
|
|
*/
|
|
|
|
void QskStrokeNode::updateNode( const QPolygonF& polygon,
|
|
|
|
const QTransform& transform, qreal lineWidth, const QColor& color )
|
|
|
|
{
|
|
|
|
Q_D( QskStrokeNode );
|
|
|
|
|
|
|
|
if ( polygon.isEmpty() || !color.isValid() || ( color.alpha() == 0 ) )
|
|
|
|
{
|
|
|
|
if ( d->geometry.vertexCount() > 0 )
|
|
|
|
{
|
|
|
|
d->geometry.allocate( 0 );
|
|
|
|
markDirty( QSGNode::DirtyGeometry );
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->geometry.setDrawingMode( QSGGeometry::DrawLineStrip );
|
|
|
|
|
|
|
|
const float lineWidthF = lineWidth;
|
|
|
|
|
|
|
|
if( lineWidthF != d->geometry.lineWidth() )
|
|
|
|
d->geometry.setLineWidth( lineWidthF );
|
2022-09-23 17:49:49 +02:00
|
|
|
|
2023-05-02 18:04:23 +02:00
|
|
|
if ( true ) // TODO
|
2022-09-23 17:49:49 +02:00
|
|
|
{
|
2023-05-02 18:04:23 +02:00
|
|
|
d->geometry.allocate( polygon.count() );
|
|
|
|
|
|
|
|
if ( material() == qskMaterialColorVertex )
|
|
|
|
{
|
|
|
|
qskMapPolygon( polygon, transform, color, d->geometry );
|
|
|
|
markDirty( QSGNode::DirtyGeometry );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qskMapPolygon( polygon, transform, d->geometry );
|
|
|
|
markDirty( QSGNode::DirtyGeometry );
|
|
|
|
|
|
|
|
auto flatMaterial = static_cast< QSGFlatColorMaterial* >( material() );
|
|
|
|
if ( flatMaterial->color() != color )
|
|
|
|
{
|
|
|
|
flatMaterial->setColor( color );
|
|
|
|
markDirty( QSGNode::DirtyMaterial );
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 17:49:49 +02:00
|
|
|
}
|
|
|
|
}
|