qmlfrontend/renderer/tiled_image_item.h
author unC0Rr
Mon, 17 Feb 2025 16:37:59 +0100
branchqmlrenderer
changeset 16089 02304ad06381
permissions -rw-r--r--
Add TiledImageItem and a scene that represents hedgewars map

#pragma once

#include <QImage>
#include <QQuickItem>
#include <QQuickWindow>

class TiledImageItem : public QQuickItem {
  Q_OBJECT
  QML_ELEMENT

  Q_PROPERTY(
      int tileSize READ tileSize WRITE setTileSize NOTIFY tileSizeChanged FINAL)

 public:
  explicit TiledImageItem(QQuickItem *parent = nullptr);
  ~TiledImageItem();

  void setImageData(int width, int height, uchar *pixels,
                    QImage::Format format);

  int tileSize() const;
  void setTileSize(int newTileSize);

  Q_INVOKABLE void test(const QString &fileName);

 protected:
  QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) override;

 Q_SIGNALS:

  void tileSizeChanged();

 private:
  Q_DISABLE_COPY_MOVE(TiledImageItem)

  QImage m_texture;
  bool m_dirty;

  struct Tile {
    Tile() = default;
    Tile(Tile &&other) noexcept;
    ~Tile() = default;
    QRectF outRect;
    QImage image;
    bool dirty = false;
    std::unique_ptr<QSGImageNode> node;
    std::unique_ptr<QSGTexture, QScopedPointerDeleteLater> texture;
  };

  void buildTiles();
  int m_tileSize{512};
  std::vector<Tile> m_tiles;
};