scene: Make top left corner of CursorItem match the hotspot

It's more convenient if one wants to paint cursor at the specified
location. For example, when drawing the cursor on your own.
This commit is contained in:
Vlad Zahorodnii 2023-12-09 14:37:12 +02:00
parent f6b605daf2
commit c153fc719a
3 changed files with 11 additions and 7 deletions

View file

@ -31,13 +31,13 @@ void CursorItem::refresh()
{
const CursorSource *source = Cursors::self()->currentCursor()->source();
if (auto surfaceSource = qobject_cast<const SurfaceCursorSource *>(source)) {
setSurface(surfaceSource->surface());
setSurface(surfaceSource->surface(), surfaceSource->hotspot());
} else if (auto shapeSource = qobject_cast<const ShapeCursorSource *>(source)) {
setImage(shapeSource->image());
setImage(shapeSource->image(), shapeSource->hotspot());
}
}
void CursorItem::setSurface(SurfaceInterface *surface)
void CursorItem::setSurface(SurfaceInterface *surface, const QPointF &hotspot)
{
m_imageItem.reset();
@ -48,9 +48,12 @@ void CursorItem::setSurface(SurfaceInterface *surface)
m_surfaceItem.reset();
}
}
if (m_surfaceItem) {
m_surfaceItem->setPosition(-hotspot);
}
}
void CursorItem::setImage(const QImage &image)
void CursorItem::setImage(const QImage &image, const QPointF &hotspot)
{
m_surfaceItem.reset();
@ -58,6 +61,7 @@ void CursorItem::setImage(const QImage &image)
m_imageItem = scene()->renderer()->createImageItem(scene(), this);
}
m_imageItem->setImage(image);
m_imageItem->setPosition(-hotspot);
m_imageItem->setSize(image.size() / image.devicePixelRatio());
}

View file

@ -25,8 +25,8 @@ public:
private:
void refresh();
void setSurface(SurfaceInterface *surface);
void setImage(const QImage &image);
void setSurface(SurfaceInterface *surface, const QPointF &hotspot);
void setImage(const QImage &image, const QPointF &hotspot);
std::unique_ptr<ImageItem> m_imageItem;
std::unique_ptr<SurfaceItemWayland> m_surfaceItem;

View file

@ -55,7 +55,7 @@ void CursorScene::postPaint()
void CursorScene::paint(const RenderTarget &renderTarget, const QRegion &region)
{
RenderViewport viewport(QRectF(QPointF(), QSizeF(renderTarget.size()) / m_paintedOutput->scale()), m_paintedOutput->scale(), renderTarget);
RenderViewport viewport(QRectF(geometry().topLeft(), QSizeF(renderTarget.size()) / m_paintedOutput->scale()), m_paintedOutput->scale(), renderTarget);
m_renderer->beginFrame(renderTarget, viewport);
m_renderer->renderBackground(renderTarget, viewport, region);
m_renderer->renderItem(renderTarget, viewport, m_rootItem.get(), 0, region, WindowPaintData{});