qskinny/playground/parrots/Overlay.cpp

204 lines
5.5 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-08 14:47:32 +00:00
#include "Overlay.h"
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
2023-12-17 11:33:23 +00:00
#include <QskSkinlet.h>
#include <QskQuick.h>
#include <QskBoxShapeMetrics.h>
#include <QskBoxBorderMetrics.h>
#include <QskBoxBorderColors.h>
#include <QskGradient.h>
Squashed commit of the following: commit 84b7cb31a62edee3ceae702ce52be68cbe222f8a Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 12:30:23 2023 +0100 wip commit 07c4b93e0611298e5e3981017c988fc7e45b6977 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 12:21:55 2023 +0100 wip commit 019448f5e1ea6eed7b767f09ae7fdb0802928a98 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 12:01:01 2023 +0100 wip commit b79f5cba8abc50bd487f08480a62d5c8bcc3c148 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 11:50:55 2023 +0100 wip commit 942291edb6016f29fb2f5c786b84fb7defa10ae2 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 11:23:28 2023 +0100 wip commit 5ae203a03a8413416f723155bf364190937929da Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 11:23:16 2023 +0100 wip commit b5b4453be94c9bdc6655370c949b096061ad1c3a Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 11:04:15 2023 +0100 wip commit cd519b840c6436bdec74a6a92cc5ba69a3f162de Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 10:50:13 2023 +0100 wip commit 9daed04a2c1dde10340764b6ea7769598edeff3d Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 09:46:10 2023 +0100 wip commit c6190b84dde26b4e2790f8154ed3d81db4630b3b Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 09:34:28 2023 +0100 wip commit 837b14e8f688a29aa3294eaaa0c0dbfe66f95dce Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 09:23:01 2023 +0100 wip commit 6486c343a8cfa2a56370f20374939180b576ce2a Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 08:41:40 2023 +0100 wip commit 230aca347b97c0a1bdb75fad6d52153688941aa6 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 08:37:56 2023 +0100 wip commit ec4018de7e8d1940a2e693e9e8183413e02f06c2 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 08:33:22 2023 +0100 wip commit bc3480a01f34d2e92b2445f5e73c0f12262f63a0 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 08:30:41 2023 +0100 wip commit dd896518938f7828f3bc5b5275fccb90d34b08a3 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 07:50:13 2023 +0100 wip commit c534c54ff8af674a45669bc4b688f3a28448a055 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Tue Dec 19 07:07:19 2023 +0100 wip commit bd7af3f28e8a1a79590acb9be41e0ac9046f1fbc Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 17:50:34 2023 +0100 wip commit 639926d3beccdc49aa98db5fe53f5eb70694a763 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 17:48:16 2023 +0100 wip commit b74c790aa15a23cf3d5875f6280407170d2113bb Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 17:35:51 2023 +0100 wip commit 1403da5a05c5782567b8e34e9a6672e041b483ed Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 16:52:45 2023 +0100 wip commit 74d69d44fc8876d95d25c1e5ab96dcf416f93a0e Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 16:04:22 2023 +0100 wip commit 81269cab9a4a2f07b86442c0b5524b0364909124 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 15:50:08 2023 +0100 wip commit a4f0b8c98960155ee8174e0b5253affc73881158 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 15:49:54 2023 +0100 wip commit fc9c5f032671d9b2b8798c97e3803ef8cb98e647 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 15:39:34 2023 +0100 wip commit 2ca8789fed5ee98355662933fed04e8ed9b5e164 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 15:28:53 2023 +0100 wip commit 3a325b6dab6ee558d6d0407c4e7c639575da4f6d Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 15:24:42 2023 +0100 wip commit 102f872db8a158274de674230f23573f941bed23 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 15:22:26 2023 +0100 wip commit 829eb67b0e6351e77f702acab90705f3cb3e9b4a Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 14:39:31 2023 +0100 wip commit 5f89fc9257fdbd169c5f80d6776aa707848dd5c3 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 14:18:18 2023 +0100 wip commit 4b9e2964c3cc3d4b08e55e98ea5b7b06b169b5f2 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 13:21:48 2023 +0100 wip commit 8c94d26ed98d906a010401d7357e45249d9b11e8 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 12:42:51 2023 +0100 wip commit 096180f6034f93725ebc9d97b8934dcca6e4b41d Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 12:15:04 2023 +0100 wip commit f9f794a19e9b9d6455793a6c61c26e5e1d0e12e0 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 11:34:07 2023 +0100 wip commit 8b5a8c859503b3b155737b975fb344b2dc87bf94 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 11:32:03 2023 +0100 wip commit 67f78561f3bae7c4a779d75921cfabbebf802f9b Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 11:29:32 2023 +0100 wip commit ad6515a95ea6803698f76c221c10f4160e82a668 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 11:19:28 2023 +0100 wip commit 2e719e809f29f07dc38ae2f3213e10bbb730c968 Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 11:16:43 2023 +0100 wip commit 2212e09f165130e1621df564aa37bdbefe7377db Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 11:16:00 2023 +0100 wip commit 171aae01c802ad10ad268e35da3dcd33fc6c2b6c Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 10:52:06 2023 +0100 wip commit ab117a43f0a405f4c09f1c395ab215bbcb09a57e Author: Uwe Rathmann <Uwe.Rathmann@tigertal.de> Date: Mon Dec 18 10:47:41 2023 +0100 wip
2023-12-19 11:45:40 +00:00
#include <QskSceneTexture.h>
2023-12-18 08:02:45 +00:00
#include <QskSGNode.h>
#include <QskRgbValue.h>
2023-12-17 11:33:23 +00:00
namespace
2023-12-07 10:24:47 +00:00
{
2023-12-19 17:37:34 +00:00
class Material : public TextureFilterMaterial
{
public:
using TextureFilterMaterial::TextureFilterMaterial;
QSGMaterialType* type() const override
{
static QSGMaterialType staticType;
return &staticType;
}
};
class FilterNode : public TextureFilterNode
{
public:
FilterNode( QSGTexture* texture )
{
QString shaders[] = { ":/shaders/blur.vert", ":/shaders/blur.frag" };
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
shaders[0] += ".qsb";
shaders[1] += ".qsb";
#endif
setFlag( QSGNode::OwnsMaterial, true );
setTextureMaterial( new Material( shaders[0], shaders[1] ) );
setOwnsTexture( true );
setTexture( texture );
}
};
2023-12-17 11:33:23 +00:00
class Skinlet : public QskSkinlet
2023-12-07 10:24:47 +00:00
{
2023-12-17 11:33:23 +00:00
using Inherited = QskSkinlet;
2023-12-07 10:24:47 +00:00
2023-12-17 11:33:23 +00:00
public:
2023-12-18 08:39:17 +00:00
enum NodeRole { FillRole, BorderRole };
2023-12-17 11:33:23 +00:00
Skinlet()
2023-12-07 10:24:47 +00:00
{
2023-12-18 08:39:17 +00:00
setNodeRoles( { FillRole, BorderRole } );
2023-12-17 11:33:23 +00:00
}
2023-12-07 10:24:47 +00:00
2023-12-18 08:02:45 +00:00
QRectF subControlRect( const QskSkinnable*,
const QRectF& contentsRect, QskAspect::Subcontrol ) const override
{
return contentsRect;
}
2023-12-17 11:33:23 +00:00
QSGNode* updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
const auto overlay = static_cast< const Overlay* >( skinnable );
switch ( nodeRole )
2023-12-07 10:24:47 +00:00
{
2023-12-18 08:39:17 +00:00
case FillRole:
return updateFillNode( overlay, node );
2023-12-07 10:24:47 +00:00
2023-12-17 11:33:23 +00:00
case BorderRole:
2023-12-18 08:02:45 +00:00
return updateBoxNode( skinnable, node, Overlay::Panel );
2023-12-17 11:33:23 +00:00
break;
};
2023-12-07 10:24:47 +00:00
2023-12-17 11:33:23 +00:00
return nullptr;
2023-12-12 09:55:30 +00:00
}
2023-12-17 11:33:23 +00:00
private:
2023-12-18 08:39:17 +00:00
QSGNode* updateFillNode( const Overlay* overlay, QSGNode* node ) const
2023-12-18 08:02:45 +00:00
{
/*
There should be a way to avoid the clip node by passing the
2023-12-18 08:39:17 +00:00
vertex list directly to the texture node. TODO ...
2023-12-18 08:02:45 +00:00
*/
using Q = Overlay;
QSGNode* clipNode = nullptr;
2023-12-18 08:39:17 +00:00
QSGNode* textureNode = nullptr;
2023-12-18 08:02:45 +00:00
if ( node )
{
if ( node->firstChild() )
{
clipNode = node;
2023-12-18 08:39:17 +00:00
textureNode = node->firstChild();
2023-12-18 08:02:45 +00:00
}
else
{
2023-12-18 08:39:17 +00:00
textureNode = node;
2023-12-18 08:02:45 +00:00
}
}
2023-12-18 08:39:17 +00:00
textureNode = updateTextureNode( overlay, textureNode );
2023-12-18 08:02:45 +00:00
if ( overlay->boxShapeHint( Q::Panel ).isRectangle() )
{
delete clipNode;
clipNode = nullptr;
}
else
{
clipNode = updateBoxClipNode( overlay, clipNode, Q::Panel );
}
2023-12-18 08:39:17 +00:00
QskSGNode::setParentNode( textureNode, clipNode );
2023-12-18 08:02:45 +00:00
2023-12-18 08:39:17 +00:00
return clipNode ? clipNode : textureNode;
2023-12-18 08:02:45 +00:00
}
2023-12-18 08:39:17 +00:00
QSGNode* updateTextureNode( const Overlay* overlay, QSGNode* node ) const
2023-12-17 11:33:23 +00:00
{
const auto window = overlay->window();
2023-12-07 10:24:47 +00:00
2023-12-17 11:33:23 +00:00
if ( overlay->size().isEmpty() )
return nullptr;
2023-12-17 11:33:23 +00:00
auto rootNode = qskScenegraphAnchorNode( window );
if ( rootNode == nullptr )
return nullptr;
2023-12-07 10:24:47 +00:00
2023-12-19 17:37:34 +00:00
auto textureNode = static_cast< FilterNode* >( node );
2023-12-07 10:24:47 +00:00
2023-12-18 08:39:17 +00:00
if ( textureNode == nullptr )
2023-12-17 11:33:23 +00:00
{
auto texture = new QskSceneTexture( window );
QObject::connect( texture, &QskSceneTexture::updateRequested,
overlay, &QQuickItem::update );
2023-12-07 10:24:47 +00:00
2023-12-19 17:37:34 +00:00
textureNode = new FilterNode( texture );
2023-12-17 11:33:23 +00:00
}
2023-12-18 08:02:45 +00:00
{
2023-12-18 08:39:17 +00:00
auto texture = qobject_cast< QskSceneTexture* >( textureNode->texture() );
2023-12-18 08:02:45 +00:00
Q_ASSERT( texture );
2023-12-17 11:33:23 +00:00
2023-12-18 08:02:45 +00:00
texture->setFiltering(
overlay->smooth() ? QSGTexture::Linear : QSGTexture::Nearest );
2023-12-17 11:33:23 +00:00
2023-12-18 08:02:45 +00:00
auto finalNode = const_cast< QSGTransformNode* >( qskItemNode( overlay ) );
texture->render( rootNode, finalNode, overlay->geometry() );
}
2023-12-17 11:33:23 +00:00
2023-12-18 08:02:45 +00:00
{
const auto rect = overlay->subControlRect( Overlay::Panel );
2023-12-18 08:39:17 +00:00
textureNode->setRect( rect );
2023-12-18 08:02:45 +00:00
}
2023-12-17 11:33:23 +00:00
2023-12-18 08:39:17 +00:00
return textureNode;
2023-12-17 11:33:23 +00:00
}
};
2023-12-07 10:24:47 +00:00
}
2023-12-18 08:02:45 +00:00
QSK_SUBCONTROL( Overlay, Panel )
2023-12-17 11:33:23 +00:00
Overlay::Overlay( QQuickItem* parent )
: Inherited( parent )
2023-12-07 10:24:47 +00:00
{
2023-12-17 11:33:23 +00:00
setSkinlet( new Skinlet() );
2023-12-18 08:02:45 +00:00
setBoxBorderMetricsHint( Panel, 1 );
setBoxBorderColorsHint( Panel, QskRgb::toTransparent( QskRgb::DarkGrey, 100 ) );
setBoxShapeHint( Panel, QskBoxShapeMetrics( 25, Qt::RelativeSize ) );
2023-12-07 10:24:47 +00:00
}
2023-12-17 11:33:23 +00:00
Overlay::~Overlay()
2023-12-07 10:24:47 +00:00
{
}
2023-12-18 08:02:45 +00:00
QRectF Overlay::layoutRectForSize( const QSizeF& size ) const
{
return subControlContentsRect( size, Panel );
}
2023-12-08 14:47:32 +00:00
void Overlay::geometryChange(
2023-12-07 10:24:47 +00:00
const QRectF& newGeometry, const QRectF& oldGeometry )
{
Inherited::geometryChange( newGeometry, oldGeometry );
2023-12-12 09:55:30 +00:00
update();
2023-12-07 10:24:47 +00:00
}
2023-12-08 14:47:32 +00:00
#include "moc_Overlay.cpp"