Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WangTiles now supports infinite maps #1688

Merged
merged 2 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tiled/bucketfilltool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ void BucketFillTool::wangFill(TileLayer &tileLayerToFill,
return;

WangFiller wangFiller(mWangSet,
mapDocument()->map()->infinite(),
dynamic_cast<StaggeredRenderer *>(mapDocument()->renderer()),
mapDocument()->map()->staggerAxis());

Expand Down
1 change: 1 addition & 0 deletions src/tiled/stampbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ void StampBrush::drawPreviewLayer(const QVector<QPoint> &list)
bounds.width(), bounds.height()));

WangFiller wangFiller(mWangSet,
mapDocument()->map()->infinite(),
dynamic_cast<StaggeredRenderer *>(mapDocument()->renderer()),
mapDocument()->map()->staggerAxis());

Expand Down
61 changes: 25 additions & 36 deletions src/tiled/wangbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,6 @@ void WangBrush::captureHoverColor()

QPoint mousePoint = mPaintPoint - tileLayer->position();

if (!tileLayer->contains(mousePoint))
return;

const Cell &cell = tileLayer->cellAt(mousePoint);

if (WangId wangId = mWangSet->wangIdOfCell(cell)) {
Expand Down Expand Up @@ -511,7 +508,7 @@ void WangBrush::updateBrush()
adjacentPositions[i] = mPaintPoint + aroundTilePoints[i];
}

if (currentLayer->contains(mPaintPoint)) {
if (mapDocument()->map()->infinite() || currentLayer->contains(mPaintPoint)) {
WangId centerWangId = mWangSet->wangIdOfCell(currentLayer->cellAt(mPaintPoint));

for (int i = 0; i < 4; ++i) {
Expand Down Expand Up @@ -544,7 +541,7 @@ void WangBrush::updateBrush()
continue;

QPoint p = adjacentPositions[i];
if (!currentLayer->contains(p) || currentLayer->cellAt(p).isEmpty())
if (currentLayer->cellAt(p).isEmpty())
continue;

WangId wangId = mWangSet->wangIdOfCell(currentLayer->cellAt(p));
Expand Down Expand Up @@ -622,7 +619,7 @@ void WangBrush::updateBrush()
for (int i = 0; i < 4; ++i) {
QPoint p = adjacentPoints[i];

if (!currentLayer->contains(p) || currentLayer->cellAt(p).isEmpty())
if (currentLayer->cellAt(p).isEmpty())
continue;

WangId wangId = mWangSet->wangIdOfCell(currentLayer->cellAt(p));
Expand Down Expand Up @@ -681,44 +678,36 @@ void WangBrush::updateBrush()
dirPoint = mPaintPoint + aroundTilePoints[mEdgeDir*2];
}

if (currentLayer->contains(mPaintPoint)) {
WangId wangId = mWangSet->wangIdOfCell(currentLayer->cellAt(mPaintPoint));

if (wangId && !currentLayer->cellAt(mPaintPoint).isEmpty()) {
wangId.setEdgeColor(mEdgeDir, mCurrentColor);
if (WangId wangId = mWangSet->wangIdOfCell(currentLayer->cellAt(mPaintPoint))) {
wangId.setEdgeColor(mEdgeDir, mCurrentColor);

const Cell &cell = mWangSet->findMatchingWangTile(wangId).makeCell();

if (cell.isEmpty()) {
QRegion r = QRect(mPaintPoint, QSize(1, 1));
r += QRect(dirPoint, QSize(1, 1));
static_cast<WangBrushItem*>(brushItem())->setInvalidTiles(r);
return;
}
const Cell &cell = mWangSet->findMatchingWangTile(wangId).makeCell();

QPoint p = mPaintPoint - stamp->position();
stamp->setCell(p.x(), p.y(), cell);
if (cell.isEmpty()) {
QRegion r = QRect(mPaintPoint, QSize(1, 1));
r += QRect(dirPoint, QSize(1, 1));
static_cast<WangBrushItem*>(brushItem())->setInvalidTiles(r);
return;
}
}

if (currentLayer->contains(dirPoint)) {
WangId wangId = mWangSet->wangIdOfCell(currentLayer->cellAt(dirPoint));

if (wangId && !currentLayer->cellAt(dirPoint).isEmpty()) {
wangId.setEdgeColor((mEdgeDir + 2) % 4, mCurrentColor);
QPoint p = mPaintPoint - stamp->position();
stamp->setCell(p.x(), p.y(), cell);
}

const Cell &cell = mWangSet->findMatchingWangTile(wangId).makeCell();
if (WangId wangId = mWangSet->wangIdOfCell(currentLayer->cellAt(dirPoint))) {
wangId.setEdgeColor((mEdgeDir + 2) % 4, mCurrentColor);

if (cell.isEmpty()) {
QRegion r = QRect(mPaintPoint, QSize(1, 1));
r += QRect(dirPoint, QSize(1, 1));
static_cast<WangBrushItem*>(brushItem())->setInvalidTiles(r);
return;
}
const Cell &cell = mWangSet->findMatchingWangTile(wangId).makeCell();

dirPoint -= stamp->position();
stamp->setCell(dirPoint.x(), dirPoint.y(), cell);
if (cell.isEmpty()) {
QRegion r = QRect(mPaintPoint, QSize(1, 1));
r += QRect(dirPoint, QSize(1, 1));
static_cast<WangBrushItem*>(brushItem())->setInvalidTiles(r);
return;
}

dirPoint -= stamp->position();
stamp->setCell(dirPoint.x(), dirPoint.y(), cell);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/tiled/wangfiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ static const QPoint aroundTilePoints[] = {
};

WangFiller::WangFiller(WangSet *wangSet,
bool isInfinite,
StaggeredRenderer *staggeredRenderer,
Map::StaggerAxis staggerAxis)
: mWangSet(wangSet)
, mIsInfinite(isInfinite)
, mStaggeredRenderer(staggeredRenderer)
, mStaggerAxis(staggerAxis)
{
Expand Down Expand Up @@ -248,14 +250,10 @@ const Cell &WangFiller::getCell(const TileLayer &back,
const QRegion &fillRegion,
QPoint point) const
{
if (!fillRegion.contains(point) && back.contains(point)) {
if (!fillRegion.contains(point))
return back.cellAt(point);
} else if (front.contains(point.x() - front.x(), point.y() - front.y())) {
else
return front.cellAt(point.x() - front.x(), point.y() - front.y());
} else {
static const Cell cell;
return cell;
}
}


Expand Down Expand Up @@ -285,7 +283,7 @@ WangId WangFiller::wangIdFromSurroundings(const TileLayer &back,
getSurroundingPoints(point, mStaggeredRenderer, mStaggerAxis, adjacentPoints);

for (int i = 0; i < 8; ++i) {
if (!fillRegion.contains(adjacentPoints[i]) && back.contains(adjacentPoints[i]))
if (!fillRegion.contains(adjacentPoints[i]))
surroundingCells[i] = back.cellAt(adjacentPoints[i]);
}

Expand Down
4 changes: 3 additions & 1 deletion src/tiled/wangfiller.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace Internal {
class WangFiller
{
public:
explicit WangFiller(WangSet *wangSet = nullptr,
explicit WangFiller(WangSet *wangSet,
bool isInfinite, //the map we are filling to is infinite.
StaggeredRenderer *staggeredRenderer = nullptr,
Map::StaggerAxis staggerAxis = Map::StaggerX);

Expand Down Expand Up @@ -91,6 +92,7 @@ class WangFiller
QPoint point) const;

WangSet *mWangSet;
bool mIsInfinite;
StaggeredRenderer *mStaggeredRenderer;
Map::StaggerAxis mStaggerAxis;
};
Expand Down
6 changes: 4 additions & 2 deletions tests/staggeredrenderer/test_staggeredrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ void test_StaggeredRenderer::screenToTileCoords()
QFETCH(QPointF, tileCoords);

StaggeredRenderer renderer(mMap);
QCOMPARE(renderer.screenToTileCoords(screenCoords).toPoint(), QPoint(qFloor(tileCoords.x()),
qFloor(tileCoords.y())));

QPointF point = renderer.screenToTileCoords(screenCoords);

QCOMPARE(QPoint(qFloor(point.x()), qFloor(point.y())), tileCoords.toPoint());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, should have noticed that. Thanks for fixing!

}

void test_StaggeredRenderer::tileToScreenCoords_data()
Expand Down