PointProbe and point movement
This commit is contained in:
parent
4ae8031e88
commit
4148d42bfb
4 changed files with 71 additions and 13 deletions
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue