1
0
Fork 0

WIP on clipping working

This commit is contained in:
James Turner 2017-04-24 12:24:58 +01:00
parent 045e6fb29a
commit 19fd8e6e26
4 changed files with 47 additions and 1 deletions

View file

@ -18,6 +18,7 @@
#include "canvasitem.h" #include "canvasitem.h"
#include <QMatrix4x4> #include <QMatrix4x4>
#include <QSGClipNode>
class LocalTransform : public QQuickTransform class LocalTransform : public QQuickTransform
{ {
@ -48,4 +49,35 @@ void CanvasItem::setTransform(const QMatrix4x4 &mat)
m_localTransform->setTransform(mat); m_localTransform->setTransform(mat);
} }
void CanvasItem::setGlobalClip(const QRectF &clip)
{
m_hasClip = true;
update();
}
void CanvasItem::setScaleToFit(const QSizeF &windowSize)
{
const double verticalScaling = windowSize.height() / implicitHeight();
const double horizontalScaling = windowSize.width() / implicitWidth();
const double usedScale = std::min(verticalScaling, horizontalScaling);
setScale(usedScale);
}
QSGClipNode *CanvasItem::getClipNode()
{
if (!m_hasClip) {
return nullptr;
}
if (!m_clipNode) {
m_clipNode = new QSGClipNode();
}
// transform global rect to local
QRectF localRect(mapFromGlobal(m_globalClipRect.topLeft()),
mapFromGlobal(m_globalClipRect.bottomRight()));
m_clipNode->setClipRect(localRect);
return m_clipNode;
}
#include "canvasitem.moc" #include "canvasitem.moc"

View file

@ -21,6 +21,7 @@
#include <QQuickItem> #include <QQuickItem>
class LocalTransform; class LocalTransform;
class QSGClipNode;
class CanvasItem : public QQuickItem class CanvasItem : public QQuickItem
{ {
@ -33,8 +34,14 @@ signals:
public slots: public slots:
protected:
QSGClipNode* getClipNode();
private: private:
LocalTransform* m_localTransform; LocalTransform* m_localTransform;
QRectF m_globalClipRect;
bool m_hasClip = false;
QSGClipNode* m_clipNode = nullptr;
}; };
#endif // CANVASITEM_H #endif // CANVASITEM_H

View file

@ -148,7 +148,7 @@ void FGCanvasElement::paint(FGCanvasPaintContext *context) const
if (_hasClip) if (_hasClip)
{ {
// clip is definef in the global coordinate system // clip is defined in the global coordinate system
#if 0 #if 0
p->save(); p->save();
p->setTransform(context->globalCoordinateTransform()); p->setTransform(context->globalCoordinateTransform());

View file

@ -168,6 +168,13 @@ public:
strokeGeom->setFlag(QSGNode::OwnsMaterial); strokeGeom->setFlag(QSGNode::OwnsMaterial);
} }
QSGClipNode* clip = getClipNode();
if (clip) {
if (fillGeom) clip->appendChildNode(fillGeom);
if (strokeGeom) clip->appendChildNode(strokeGeom);
return clip;
}
if (fillGeom && strokeGeom) { if (fillGeom && strokeGeom) {
QSGNode* groupNode = new QSGNode; QSGNode* groupNode = new QSGNode;
groupNode->appendChildNode(fillGeom); groupNode->appendChildNode(fillGeom);