Fix resize layout grid changing with viewport

This commit is contained in:
GriffinR 2025-07-17 13:02:03 -04:00
parent 5fa5638277
commit 25e8bdee49
2 changed files with 29 additions and 20 deletions

View File

@ -24,11 +24,21 @@ class CheckeredBgScene : public QGraphicsScene {
Q_OBJECT
public:
CheckeredBgScene(QObject *parent = nullptr);
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, y * this->gridSize, width * this->gridSize, height * this->gridSize);
this->validRect = QRect(x * this->gridSize.width(),
y * this->gridSize.height(),
width * this->gridSize.width(),
height * this->gridSize.height());
}
void setValidRect(QRect rect) {
void setValidRect(const QRect &rect) {
this->validRect = rect;
}
QRect getValidRect() { return this->validRect; }
@ -37,8 +47,8 @@ protected:
void drawBackground(QPainter *painter, const QRectF &rect) override;
private:
int gridSize = 16; // virtual pixels
QRect validRect = QRect();
QSize gridSize;
QRect validRect;
};

View File

@ -7,32 +7,31 @@
#include "ui_resizelayoutpopup.h"
CheckeredBgScene::CheckeredBgScene(QObject *parent) : QGraphicsScene(parent) { }
void CheckeredBgScene::drawBackground(QPainter *painter, const QRectF &rect) {
QRect r = rect.toRect();
int xMin = r.left() - r.left() % this->gridSize - this->gridSize;
int yMin = r.top() - r.top() % this->gridSize - this->gridSize;
int xMax = r.right() - r.right() % this->gridSize + this->gridSize;
int yMax = r.bottom() - r.bottom() % this->gridSize + this->gridSize;
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 16x16 from top to bottom of scene
QColor paintColor(0x00ff00);
for (int x = xMin, xTile = 0; x <= xMax; x += this->gridSize, xTile++) {
for (int y = yMin, yTile = 0; y <= yMax; y += this->gridSize, yTile++) {
if (!((xTile ^ yTile) & 1)) { // tile numbers have same parity (evenness)
// 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 {
} 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, this->gridSize, this->gridSize), paintColor);
painter->fillRect(QRect(x, y, w, h), paintColor);
}
}
}
@ -85,7 +84,7 @@ ResizeLayoutPopup::ResizeLayoutPopup(QWidget *parent, Layout *layout, Project *p
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
this->setWindowModality(Qt::ApplicationModal);
this->scene = new CheckeredBgScene(this);
this->scene = new CheckeredBgScene(Metatile::pixelSize(), this);
this->ui->graphicsView->setScene(this->scene);
this->ui->graphicsView->setRenderHints(QPainter::Antialiasing);
this->ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);