PointProbe and point movement

This commit is contained in:
Alexey 2025-06-27 19:21:50 +03:00
commit 4148d42bfb
4 changed files with 71 additions and 13 deletions

View file

@ -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();
}

View file

@ -7,6 +7,20 @@
#include <iostream>
#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();
};

View file

@ -24,7 +24,7 @@ const QPoint &Line::getStart()
const QPoint &Line::getEnd()
{
return start;
return end;
}
QString Line::getColor()

View file

@ -18,6 +18,8 @@ MainWindow::~MainWindow()
void MainWindow::onGridUpdated()
{
ui->gridPreview->repaint();
ui->textPreview->setPlainText(ui->gridPreview->toLuaTable());
ui->spinGridWidth->setValue(ui->gridPreview->getWidth());