From 4148d42bfb690cdd8ae06c54ef5621f39c4e5826 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Fri, 27 Jun 2025 19:21:50 +0300 Subject: [PATCH] PointProbe and point movement --- gridpreview.cpp | 56 ++++++++++++++++++++++++++++++++++++++++--------- gridpreview.h | 24 +++++++++++++++++++-- line.cpp | 2 +- mainwindow.cpp | 2 ++ 4 files changed, 71 insertions(+), 13 deletions(-) diff --git a/gridpreview.cpp b/gridpreview.cpp index 1257bc9..a4168ec 100644 --- a/gridpreview.cpp +++ b/gridpreview.cpp @@ -13,7 +13,6 @@ void GridPreview::setSize(int newWidth, int newHeight) width = newWidth; height = newHeight; - update(); emit updatedContents(); } @@ -21,7 +20,6 @@ void GridPreview::addLine(Line &line) { lines.push_back(line); - update(); emit updatedContents(); } @@ -29,7 +27,6 @@ void GridPreview::removeLine(int index) { lines.removeAt(index); - update(); emit updatedContents(); } @@ -37,7 +34,6 @@ void GridPreview::setLineColor(int index, QString color) { lines[index].setColor(color); - update(); emit updatedContents(); } @@ -45,7 +41,6 @@ void GridPreview::setLineStartPoint(int index, QPoint point) { lines[index].setStartPoint(point); - update(); emit updatedContents(); } @@ -53,7 +48,6 @@ void GridPreview::setLineEndPoint(int index, QPoint point) { lines[index].setEndPoint(point); - update(); emit updatedContents(); } @@ -72,6 +66,11 @@ const Line &GridPreview::getLine(int index) return lines.at(index); } +const PointProbe &GridPreview::getCapturedProbe() +{ + return capturedProbe; +} + const QString GridPreview::toLuaTable() { QString output(tableTemplate.arg(width).arg(height)); @@ -160,9 +159,25 @@ const QColor GridPreview::colorFromString(const QString color) void GridPreview::mouseMoveEvent(QMouseEvent *event) { - if (!mouseCaptured) { + if (!mouseCaptured) + { return; } + + QPoint gridPos = localMousePosition(event); + + lastProbe = probePoint(gridPos); + if (pointIsFree(lastProbe)) + { + if (capturedProbe.isStart) + { + setLineStartPoint(capturedProbe.ownerIndex, gridPos); + } + else + { + setLineEndPoint(capturedProbe.ownerIndex, gridPos); + } + } } void GridPreview::mousePressEvent(QMouseEvent *event) @@ -173,12 +188,16 @@ void GridPreview::mousePressEvent(QMouseEvent *event) std::cout << gridPos.x() << ',' << gridPos.y() << std::endl; - if (!isPointOccupied(gridPos) && pointInBounds(gridPos)) + lastProbe = probePoint(gridPos); + capturedProbe = lastProbe; + if (pointIsFree(lastProbe)) { Line line; line.setStartPoint(gridPos); line.setEndPoint(gridPos); addLine(line); + + capturedProbe.ownerIndex = lines.size() - 1; } } @@ -211,7 +230,7 @@ const QPoint GridPreview::snapToGrid(const QPoint &global) return QPoint(1 + (global.x() - offsetX) / cellSize, 1 + (global.y() - offsetY) / cellSize); } -double GridPreview::isPointOccupied(const QPoint &point) +bool GridPreview::isPointOccupied(const QPoint &point) { for (auto iter = lines.begin(); iter < lines.end(); iter++) { @@ -223,7 +242,24 @@ double GridPreview::isPointOccupied(const QPoint &point) return false; } -double GridPreview::pointInBounds(const QPoint &point) +bool GridPreview::pointInBounds(const QPoint &point) { return point.x() > 0 && point.y() > 0 && point.x() <= width && point.y() <= height; } + +bool GridPreview::pointIsFree(PointProbe& pp) +{ + return pp.ownerIndex == -1 && pp.inBounds; +} + +const PointProbe GridPreview::probePoint(const QPoint &point) +{ + for (int i = 0; i < lines.size(); i++) + { + if (lines[i].getStart() == point || lines[i].getEnd() == point) + { + return PointProbe(i, lines[i].getStart() == point, pointInBounds(point)); + } + } + return PointProbe(); +} diff --git a/gridpreview.h b/gridpreview.h index 081f75f..52880d9 100644 --- a/gridpreview.h +++ b/gridpreview.h @@ -7,6 +7,20 @@ #include #include "line.h" +struct PointProbe +{ + int ownerIndex = -1; + bool isStart = true; + bool inBounds = true; + + PointProbe() {} + + PointProbe(int owner, bool start = true, bool bounds = true): + ownerIndex(owner), + isStart(start), + inBounds(bounds) {} +}; + class GridPreview : public QWidget { Q_OBJECT @@ -16,6 +30,7 @@ class GridPreview : public QWidget const QString tableTemplate = "return {\n width = %1,\n height = %2,\n lines = {\n%3\n }\n}"; bool mouseCaptured = false; + PointProbe capturedProbe, lastProbe; public: explicit GridPreview(QWidget *parent = nullptr); void setSize(int newWidth, int newHeight); @@ -30,6 +45,7 @@ public: int getWidth(); int getHeight(); const Line& getLine(int index); + const PointProbe& getCapturedProbe(); const QString toLuaTable(); void paintEvent(QPaintEvent* event); @@ -42,8 +58,12 @@ public: const QPoint localMousePosition(QMouseEvent* event); const QPoint snapToGrid(const QPoint& global); - double isPointOccupied(const QPoint& point); - double pointInBounds(const QPoint& point); + bool isPointOccupied(const QPoint& point); + bool pointInBounds(const QPoint& point); + bool pointIsFree(PointProbe &pp); + + int getPointOwner(const QPoint& point); + const PointProbe probePoint(const QPoint& point); signals: void updatedContents(); }; diff --git a/line.cpp b/line.cpp index 6f4c942..5c09429 100644 --- a/line.cpp +++ b/line.cpp @@ -24,7 +24,7 @@ const QPoint &Line::getStart() const QPoint &Line::getEnd() { - return start; + return end; } QString Line::getColor() diff --git a/mainwindow.cpp b/mainwindow.cpp index c85352f..8bb1094 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -18,6 +18,8 @@ MainWindow::~MainWindow() void MainWindow::onGridUpdated() { + ui->gridPreview->repaint(); + ui->textPreview->setPlainText(ui->gridPreview->toLuaTable()); ui->spinGridWidth->setValue(ui->gridPreview->getWidth());