QskRoundedRectRenderer reorganized

This commit is contained in:
Uwe Rathmann 2023-01-11 17:40:25 +01:00
parent e66520294c
commit c670d23582
1 changed files with 129 additions and 115 deletions

View File

@ -27,10 +27,16 @@ namespace
BottomRight = Qt::BottomRightCorner BottomRight = Qt::BottomRightCorner
}; };
int additionalGradientStops( const QskGradient& gradient ) inline int additionalGradientStops( const QskGradient& gradient )
{ {
return qMax( 0, gradient.stepCount() - 1 ); return qMax( 0, gradient.stepCount() - 1 );
} }
template< class BorderMap >
inline int extraStops( const BorderMap& borderMap )
{
return additionalGradientStops( borderMap.gradient() );
}
} }
namespace namespace
@ -439,54 +445,126 @@ namespace
{ {
} }
inline void setAdditionalLine( inline void setGradientLine(
float x11, float y11, float x12, float y12, const QskVertex::Line& l, float dx1, float dy1, float dx2, float dy2,
float dx1, float dy1, float dx2, float dy2, const QskGradientStop& stop, Line* line ) const
const QskGradientStop& stop, Line* line )
{ {
const auto pos = 1.0 - stop.position(); const auto pos = stop.position();
const float x1 = x11 + pos * dx1; const float x1 = l.p1.x + pos * dx1;
const float y1 = y11 + pos * dy1; const float y1 = l.p1.y + pos * dy1;
const float x2 = x12 + pos * dx2; const float x2 = l.p2.x + pos * dx2;
const float y2 = y12 + pos * dy2; const float y2 = l.p2.y + pos * dy2;
line->setLine( x1, y1, x2, y2, stop.rgb() ); line->setLine( x1, y1, x2, y2, stop.rgb() );
} }
void addAdditionalLines( inline void addGradientLines( const BorderValues& v, int c1,
float x11, float y11, float x12, float y12, // start line const QskGradient& gradient, Line* lines ) const
float x21, float y21, float x22, float y22, // end line
const QskGradient& gradient, Line* lines )
{ {
const float dx1 = x21 - x11; QskVertex::Line from, to;
const float dy1 = y21 - y11;
const float dx2 = x22 - x12; const auto& c = m_metrics.corner;
const float dy2 = y22 - y12;
switch( c1 )
{
case TopLeft:
{
const auto c2 = BottomLeft;
to.p1.x = c[ c1 ].centerX - v.dx1( c1 );
to.p1.y = c[ c1 ].centerY - v.dy1( c1 );
to.p2.x = c[ c1 ].centerX - v.dx2( c1 );
to.p2.y = c[ c1 ].centerY - v.dy2( c1 );
from.p1.x = c[ c2 ].centerX - v.dx1( c2 );
from.p1.y = c[ c2 ].centerY + v.dy1( c2 );
from.p2.x = c[ c2 ].centerX - v.dx2( c2 );
from.p2.y = c[ c2 ].centerY + v.dy2( c2 );
break;
}
case TopRight:
{
const auto c2 = TopLeft;
to.p1.x = c[ c1 ].centerX + v.dx1( c1 );
to.p1.y = c[ c1 ].centerY - v.dy1( c1 );
to.p2.x = c[ c1 ].centerX + v.dx2( c1 );
to.p2.y = c[ c1 ].centerY - v.dy2( c1 );
from.p1.x = c[ c2 ].centerX - v.dx1( c2 );
from.p1.y = c[ c2 ].centerY - v.dy1( c2 );
from.p2.x = c[ c2 ].centerX - v.dx2( c2 );
from.p2.y = c[ c2 ].centerY - v.dy2( c2 );
break;
}
case BottomLeft:
{
const auto c2 = BottomRight;
to.p1.x = c[ c1 ].centerX - v.dx1( c1 );
to.p1.y = c[ c1 ].centerY + v.dy1( c1 );
to.p2.x = c[ c1 ].centerX - v.dx2( c1 );
to.p2.y = c[ c1 ].centerY + v.dy2( c1 );
from.p1.x = c[ c2 ].centerX + v.dx1( c2 );
from.p1.y = c[ c2 ].centerY + v.dy1( c2 );
from.p2.x = c[ c2 ].centerX + v.dx2( c2 );
from.p2.y = c[ c2 ].centerY + v.dy2( c2 );
break;
}
case BottomRight:
{
const auto c2 = TopRight;
to.p1.x = c[ c1 ].centerX + v.dx1( c1 );
to.p1.y = c[ c1 ].centerY + v.dy1( c1 );
to.p2.x = c[ c1 ].centerX + v.dx2( c1 );
to.p2.y = c[ c1 ].centerY + v.dy2( c1 );
from.p1.x = c[ c2 ].centerX + v.dx1( c2 );
from.p1.y = c[ c2 ].centerY - v.dy1( c2 );
from.p2.x = c[ c2 ].centerX + v.dx2( c2 );
from.p2.y = c[ c2 ].centerY - v.dy2( c2 );
break;
}
}
addGradientLines( from, to, gradient, lines );
}
void addGradientLines( QskVertex::Line& from, QskVertex::Line& to,
const QskGradient& gradient, Line* lines ) const
{
Q_ASSERT( gradient.stepCount() > 0 );
const float dx1 = to.p1.x - from.p1.x;
const float dy1 = to.p1.y - from.p1.y;
const float dx2 = to.p2.x - from.p2.x;
const float dy2 = to.p2.y - from.p2.y;
const auto stops = gradient.stops(); const auto stops = gradient.stops();
auto line = lines + additionalGradientStops( gradient ); auto line = lines;
{
const auto& stop = stops.last();
if ( stop.position() < 1.0 )
setGradientLine( from, dx1, dy1, dx2, dy2, stop, ++line );
}
for( int i = stops.count() - 2; i >= 1; i-- )
setGradientLine( from, dx1, dy1, dx2, dy2, stops[i], ++line );
{ {
const auto& stop = stops.first(); const auto& stop = stops.first();
if ( stop.position() > 0.0 ) if ( stop.position() > 0.0 )
setAdditionalLine( x11, y11, x12, y12, dx1, dy1, dx2, dy2, stop, line-- ); setGradientLine( from, dx1, dy1, dx2, dy2, stop, ++line );
}
for( int i = 1; i < stops.count() - 1; i++ )
{
setAdditionalLine( x11, y11, x12, y12, dx1, dy1, dx2, dy2, stops[i], line-- );
}
{
const auto& stop = stops.last();
if ( stop.position() < 1.0 )
{
setAdditionalLine( x11, y11, x12, y12, dx1, dy1, dx2, dy2, stop, line-- );
}
} }
} }
@ -513,15 +591,9 @@ namespace
if ( borderLines ) if ( borderLines )
{ {
linesBR = borderLines; linesBR = borderLines;
linesTR = linesBR + numCornerLines + extraStops( borderMapBR );
linesTR = linesBR + numCornerLines linesTL = linesTR + numCornerLines + extraStops( borderMapTR );
+ additionalGradientStops( borderMapBR.gradient() ); linesBL = linesTL + numCornerLines + extraStops( borderMapTL );
linesTL = linesTR + numCornerLines
+ additionalGradientStops( borderMapTR.gradient() );
linesBL = linesTL + numCornerLines
+ additionalGradientStops( borderMapTL.gradient() );
} }
if ( fillLines ) if ( fillLines )
@ -535,15 +607,9 @@ namespace
if ( borderLines ) if ( borderLines )
{ {
linesTR = borderLines + 1; linesTR = borderLines + 1;
linesTL = linesTR + numCornerLines + extraStops( borderMapTR );
linesTL = linesTR + numCornerLines linesBL = linesTL + numCornerLines + extraStops( borderMapTL );
+ additionalGradientStops( borderMapTR.gradient() ); linesBR = linesBL + numCornerLines + extraStops( borderMapBL );
linesBL = linesTL + numCornerLines
+ additionalGradientStops( borderMapTL.gradient() );
linesBR = linesBL + numCornerLines
+ additionalGradientStops( borderMapBL.gradient() );
} }
if ( fillLines ) if ( fillLines )
@ -617,78 +683,30 @@ namespace
if( j == 0 ) if( j == 0 )
{ {
if( additionalGradientStops( borderMapTR.gradient() ) > 0 ) if( extraStops( borderMapTR ) > 0 )
{ {
float x1TR = c[ TopRight ].centerX + v.dx1( TopRight ), addGradientLines( v, TopRight,
y1TR = c[ TopRight ].centerY - v.dy1( TopRight ),
x2TR = c[ TopRight ].centerX + v.dx2( TopRight ),
y2TR = c[ TopRight ].centerY - v.dy2( TopRight ),
x1TL = c[ TopLeft ].centerX - v.dx1( TopLeft ),
y1TL = c[ TopLeft ].centerY - v.dy1( TopLeft ),
x2TL = c[ TopLeft ].centerX - v.dx2( TopLeft ),
y2TL = c[ TopLeft ].centerY - v.dy2( TopLeft );
addAdditionalLines(
x1TR, y1TR, x2TR, y2TR,
x1TL, y1TL, x2TL, y2TL,
borderMapTR.gradient(), linesTR + k ); borderMapTR.gradient(), linesTR + k );
} }
if( additionalGradientStops( borderMapBL.gradient() ) > 0 ) if( extraStops( borderMapBL ) > 0 )
{ {
float x1BL = c[ BottomLeft ].centerX - v.dx1( BottomLeft ), addGradientLines( v, BottomLeft,
y1BL = c[ BottomLeft ].centerY + v.dy1( BottomLeft ),
x2BL = c[ BottomLeft ].centerX - v.dx2( BottomLeft ),
y2BL = c[ BottomLeft ].centerY + v.dy2( BottomLeft ),
x1BR = c[ BottomRight ].centerX + v.dx1( BottomRight ),
y1BR = c[ BottomRight ].centerY + v.dy1( BottomRight ),
x2BR = c[ BottomRight ].centerX + v.dx2( BottomRight ),
y2BR = c[ BottomRight ].centerY + v.dy2( BottomRight );
addAdditionalLines(
x1BL, y1BL, x2BL, y2BL,
x1BR, y1BR, x2BR, y2BR,
borderMapBL.gradient(), linesBL + k ); borderMapBL.gradient(), linesBL + k );
} }
} }
if( j == numCornerLines - 1 ) if( j == numCornerLines - 1 )
{ {
if( additionalGradientStops( borderMapTL.gradient() ) > 0 ) if( extraStops( borderMapTL ) > 0 )
{ {
float x1TL = c[ TopLeft ].centerX - v.dx1( TopLeft ), addGradientLines( v, TopLeft,
y1TL = c[ TopLeft ].centerY - v.dy1( TopLeft ),
x2TL = c[ TopLeft ].centerX - v.dx2( TopLeft ),
y2TL = c[ TopLeft ].centerY - v.dy2( TopLeft ),
x1BL = c[ BottomLeft ].centerX - v.dx1( BottomLeft ),
y1BL = c[ BottomLeft ].centerY + v.dy1( BottomLeft ),
x2BL = c[ BottomLeft ].centerX - v.dx2( BottomLeft ),
y2BL = c[ BottomLeft ].centerY + v.dy2( BottomLeft );
addAdditionalLines(
x1TL, y1TL, x2TL, y2TL,
x1BL, y1BL, x2BL, y2BL,
borderMapTL.gradient(), linesTL + j ); borderMapTL.gradient(), linesTL + j );
} }
if( additionalGradientStops( borderMapBR.gradient() ) > 0 ) if( extraStops( borderMapBR ) > 0 )
{ {
float x1BR = c[ BottomRight ].centerX + v.dx1( BottomRight ), addGradientLines( v, BottomRight,
y1BR = c[ BottomRight ].centerY + v.dy1( BottomRight ),
x2BR = c[ BottomRight ].centerX + v.dx2( BottomRight ),
y2BR = c[ BottomRight ].centerY + v.dy2( BottomRight ),
x1TR = c[ TopRight ].centerX + v.dx1( TopRight ),
y1TR = c[ TopRight ].centerY - v.dy1( TopRight ),
x2TR = c[ TopRight ].centerX + v.dx2( TopRight ),
y2TR = c[ TopRight ].centerY - v.dy2( TopRight );
addAdditionalLines(
x1BR, y1BR, x2BR, y2BR,
x1TR, y1TR, x2TR, y2TR,
borderMapBR.gradient(), linesBR + j ); borderMapBR.gradient(), linesBR + j );
} }
} }
@ -740,13 +758,9 @@ namespace
#if 1 #if 1
if ( borderLines ) if ( borderLines )
{ {
const int additionalStops = const int k = 4 * numCornerLines
additionalGradientStops( borderMapBR.gradient() ) + extraStops( borderMapBR ) + extraStops( borderMapTR )
+ additionalGradientStops( borderMapTR.gradient() ) + extraStops( borderMapTL ) + extraStops( borderMapBL );
+ additionalGradientStops( borderMapTL.gradient() )
+ additionalGradientStops( borderMapBL.gradient() );
const int k = 4 * numCornerLines + additionalStops;
if ( orientation == Qt::Vertical ) if ( orientation == Qt::Vertical )
borderLines[ k ] = borderLines[ 0 ]; borderLines[ k ] = borderLines[ 0 ];