diff --git a/include/ui/checkeredbgscene.h b/include/ui/checkeredbgscene.h new file mode 100644 index 00000000..bf0452a7 --- /dev/null +++ b/include/ui/checkeredbgscene.h @@ -0,0 +1,39 @@ +#ifndef CHECKEREDBGSCENE_H +#define CHECKEREDBGSCENE_H + +#include + +// Custom scene that paints its background a gray checkered pattern. +// Additionally there is a definable "valid" area which will paint the checkerboard green inside. +class CheckeredBgScene : public QGraphicsScene { + Q_OBJECT + +public: + CheckeredBgScene(const QSize &gridSize, QObject *parent = nullptr) + : QGraphicsScene(parent), + gridSize(gridSize) + {}; + CheckeredBgScene(int width, int height, QObject *parent = nullptr) + : CheckeredBgScene(QSize(width, height), parent) + {}; + + void setValidRect(int x, int y, int width, int height) { + this->validRect = QRect(x * this->gridSize.width(), + y * this->gridSize.height(), + width * this->gridSize.width(), + height * this->gridSize.height()); + } + void setValidRect(const QRect &rect) { + this->validRect = rect; + } + QRect getValidRect() { return this->validRect; } + +protected: + void drawBackground(QPainter *painter, const QRectF &rect) override; + +private: + QSize gridSize; + QRect validRect; +}; + +#endif // CHECKEREDBGSCENE_H diff --git a/include/ui/mapimageexporter.h b/include/ui/mapimageexporter.h index 51c1afe3..789cf9b9 100644 --- a/include/ui/mapimageexporter.h +++ b/include/ui/mapimageexporter.h @@ -2,6 +2,7 @@ #define MAPIMAGEEXPORTER_H #include "project.h" +#include "checkeredbgscene.h" class QGifImage; @@ -52,7 +53,7 @@ private: Project *m_project = nullptr; Map *m_map = nullptr; Layout *m_layout = nullptr; - QGraphicsScene *m_scene = nullptr; + CheckeredBgScene *m_scene = nullptr; QGifImage *m_timelapseGifImage = nullptr; QBuffer *m_timelapseBuffer = nullptr; QMovie *m_timelapseMovie = nullptr; diff --git a/include/ui/metatileimageexporter.h b/include/ui/metatileimageexporter.h index a6b8f6cd..9e9b0bf2 100644 --- a/include/ui/metatileimageexporter.h +++ b/include/ui/metatileimageexporter.h @@ -2,7 +2,6 @@ #define METATILEIMAGEEXPORTER_H #include -#include #include #include #include @@ -10,6 +9,7 @@ #include #include "config.h" +#include "checkeredbgscene.h" class Tileset; @@ -73,7 +73,7 @@ private: Tileset *m_secondaryTileset; Settings *m_savedSettings; - QGraphicsScene *m_scene = nullptr; + CheckeredBgScene *m_scene = nullptr; QGraphicsPixmapItem *m_preview = nullptr; bool m_previewUpdateQueued = false; QList m_layerOrder; diff --git a/include/ui/resizelayoutpopup.h b/include/ui/resizelayoutpopup.h index 917cda44..b187044f 100644 --- a/include/ui/resizelayoutpopup.h +++ b/include/ui/resizelayoutpopup.h @@ -3,10 +3,10 @@ #include "maplayout.h" #include "project.h" +#include "checkeredbgscene.h" #include #include -#include #include #include #include @@ -16,43 +16,6 @@ namespace Ui { class ResizeLayoutPopup; } - - -/// Custom scene that paints its background a gray checkered pattern. -/// Additionally there is a definable "valid" area which will paint the checkerboard green inside. -class CheckeredBgScene : public QGraphicsScene { - Q_OBJECT - -public: - CheckeredBgScene(const QSize &gridSize, QObject *parent = nullptr) - : QGraphicsScene(parent), - gridSize(gridSize) - {}; - CheckeredBgScene(int width, int height, QObject *parent = nullptr) - : CheckeredBgScene(QSize(width, height), parent) - {}; - - void setValidRect(int x, int y, int width, int height) { - this->validRect = QRect(x * this->gridSize.width(), - y * this->gridSize.height(), - width * this->gridSize.width(), - height * this->gridSize.height()); - } - void setValidRect(const QRect &rect) { - this->validRect = rect; - } - QRect getValidRect() { return this->validRect; } - -protected: - void drawBackground(QPainter *painter, const QRectF &rect) override; - -private: - QSize gridSize; - QRect validRect; -}; - - - /// PixmapItem subclass which allows for creating a boundary which determine whether /// the pixmap paints normally or with a black tint. /// This item is movable and snaps on a 'cellSize' grid. diff --git a/porymap.pro b/porymap.pro index 982fbd37..9c6327b7 100644 --- a/porymap.pro +++ b/porymap.pro @@ -66,6 +66,7 @@ SOURCES += src/core/advancemapparser.cpp \ src/scriptapi/apiutility.cpp \ src/scriptapi/scripting.cpp \ src/ui/aboutporymap.cpp \ + src/ui/checkeredbgscene.cpp \ src/ui/colorinputwidget.cpp \ src/ui/connectionslistitem.cpp \ src/ui/customattributesdialog.cpp \ @@ -181,6 +182,7 @@ HEADERS += include/core/advancemapparser.h \ include/lib/orderedmap.h \ include/lib/orderedjson.h \ include/ui/aboutporymap.h \ + include/ui/checkeredbgscene.h \ include/ui/connectionslistitem.h \ include/ui/customattributesdialog.h \ include/ui/customattributestable.h \ diff --git a/src/ui/checkeredbgscene.cpp b/src/ui/checkeredbgscene.cpp new file mode 100644 index 00000000..9a32dff7 --- /dev/null +++ b/src/ui/checkeredbgscene.cpp @@ -0,0 +1,32 @@ +#include "checkeredbgscene.h" + +#include + +void CheckeredBgScene::drawBackground(QPainter *painter, const QRectF &rect) { + QRect r = rect.toRect(); + int w = this->gridSize.width(); + int h = this->gridSize.height(); + int xMin = r.left() - (r.left() % w) - w; + int yMin = r.top() - (r.top() % h) - h; + int xMax = r.right() - (r.right() % w) + w; + int yMax = r.bottom() - (r.bottom() % h) + h; + + // draw grid from top to bottom of scene + QColor paintColor; + for (int x = xMin; x <= xMax; x += w) { + for (int y = yMin; y <= yMax; y += h) { + if ((x/w + y/h) % 2) { + if (this->validRect.contains(x, y)) + paintColor = QColor(132, 217, 165); // green light color + else + paintColor = 0xbcbcbc; // normal light color + } else { + if (this->validRect.contains(x, y)) + paintColor = QColor(76, 178, 121); // green dark color + else + paintColor = 0x969696; // normal dark color + } + painter->fillRect(QRect(x, y, w, h), paintColor); + } + } +} diff --git a/src/ui/mapimageexporter.cpp b/src/ui/mapimageexporter.cpp index 450ad9d0..30c6ff72 100644 --- a/src/ui/mapimageexporter.cpp +++ b/src/ui/mapimageexporter.cpp @@ -46,7 +46,7 @@ MapImageExporter::MapImageExporter(QWidget *parent, Project *project, Map *map, setAttribute(Qt::WA_DeleteOnClose); ui->setupUi(this); - m_scene = new QGraphicsScene(this); + m_scene = new CheckeredBgScene(QSize(8,8), this); m_preview = m_scene->addPixmap(QPixmap()); ui->graphicsView_Preview->setScene(m_scene); diff --git a/src/ui/metatileimageexporter.cpp b/src/ui/metatileimageexporter.cpp index 52a55694..5ffbf8d7 100644 --- a/src/ui/metatileimageexporter.cpp +++ b/src/ui/metatileimageexporter.cpp @@ -23,7 +23,7 @@ MetatileImageExporter::MetatileImageExporter(QWidget *parent, Tileset *primaryTi ui->radioButton_TransparencyFirst, }; - m_scene = new QGraphicsScene(this); + m_scene = new CheckeredBgScene(QSize(8,8), this); m_preview = m_scene->addPixmap(QPixmap()); ui->graphicsView_Preview->setScene(m_scene); diff --git a/src/ui/resizelayoutpopup.cpp b/src/ui/resizelayoutpopup.cpp index 48d9cebd..679e9ee2 100644 --- a/src/ui/resizelayoutpopup.cpp +++ b/src/ui/resizelayoutpopup.cpp @@ -7,39 +7,6 @@ #include "ui_resizelayoutpopup.h" -void CheckeredBgScene::drawBackground(QPainter *painter, const QRectF &rect) { - QRect r = rect.toRect(); - int w = this->gridSize.width(); - int h = this->gridSize.height(); - int xMin = r.left() - (r.left() % w) - w; - int yMin = r.top() - (r.top() % h) - h; - int xMax = r.right() - (r.right() % w) + w; - int yMax = r.bottom() - (r.bottom() % h) + h; - - // draw grid from top to bottom of scene - QColor paintColor; - for (int x = xMin; x <= xMax; x += w) { - for (int y = yMin; y <= yMax; y += h) { - if ((x/w + y/h) % 2) { - if (this->validRect.contains(x, y)) - paintColor = QColor(132, 217, 165); // green light color - else - paintColor = 0xbcbcbc; // normal light color - } else { - if (this->validRect.contains(x, y)) - paintColor = QColor(76, 178, 121); // green dark color - else - paintColor = 0x969696; // normal dark color - } - painter->fillRect(QRect(x, y, w, h), paintColor); - } - } -} - -/****************************************************************************** - ************************************************************************ - ******************************************************************************/ - BoundedPixmapItem::BoundedPixmapItem(const QPixmap &pixmap, const QSize &cellSize, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent) { setFlags(this->flags() | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsSelectable); this->cellSize = cellSize;