Skip to content

Improve WangBrush and TileLayerWangEdit behavior with empty color #3774

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

Merged
merged 8 commits into from
Jul 6, 2023
Prev Previous commit
Next Next commit
WangFiller: Restored old behavior for correctionsEnabled = false
  • Loading branch information
bjorn committed Jul 6, 2023
commit 09aaeffc383f79406654b0e410f5ad9a5622763b
73 changes: 36 additions & 37 deletions src/tiled/wangfiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,6 @@ WangFiller::WangFiller(const WangSet &wangSet,
void WangFiller::setRegion(const QRegion &region)
{
mFillRegion.region = region;
auto &grid = mFillRegion.grid;

// Set the Wang IDs at the border of the region to make sure the tiles in
// the filled region connect with those outside of it.
auto setDesiredWangId = [&] (int x, int y, WangId::Index edge) {
const WangId source = wangIdFromSurroundings(QPoint(x, y));
CellInfo &info = grid.add(x, y);

auto setIndex = [&](int i) {
if (!info.mask.indexColor(i)) {
const int color = source.indexColor(i);
if (color != WangId::INDEX_MASK) {
info.desired.setIndexColor(i, color);
info.mask.setIndexColor(i, WangId::INDEX_MASK);
}
}
};

setIndex(WangId::previousIndex(edge));
setIndex(edge);
setIndex(WangId::nextIndex(edge));
};

for (const QRect &rect : region) {
for (int x = rect.left(); x <= rect.right(); ++x) {
setDesiredWangId(x, rect.top(), WangId::Top);
setDesiredWangId(x, rect.bottom(), WangId::Bottom);
}
for (int y = rect.top(); y <= rect.bottom(); ++y) {
setDesiredWangId(rect.left(), y, WangId::Left);
setDesiredWangId(rect.right(), y, WangId::Right);
}
}
}

WangFiller::CellInfo &WangFiller::changePosition(QPoint pos)
Expand All @@ -97,7 +64,7 @@ WangFiller::CellInfo &WangFiller::changePosition(QPoint pos)

// Initialize the desired WangId when necessary, and make sure the location
// is part of the to be processed region.
if (info.desired == WangId::FULL_MASK) {
if (info == CellInfo()) {
info.desired = mWangSet.wangIdOfCell(mBack.cellAt(pos));
mFillRegion.region += QRect(pos, pos);
}
Expand Down Expand Up @@ -219,6 +186,40 @@ void WangFiller::apply(TileLayer &target)
auto &grid = mFillRegion.grid;
auto &region = mFillRegion.region;

if (!mCorrectionsEnabled) {
// Set the Wang IDs at the border of the region to make sure the tiles in
// the filled region connect with those outside of it.
auto setDesiredWangId = [&] (int x, int y, WangId::Index edge) {
const WangId source = wangIdFromSurroundings(QPoint(x, y));
CellInfo &info = grid.add(x, y);

auto setIndex = [&](int i) {
if (!info.mask.indexColor(i)) {
const int color = source.indexColor(i);
if (color != WangId::INDEX_MASK) {
info.desired.setIndexColor(i, color);
info.mask.setIndexColor(i, WangId::INDEX_MASK);
}
}
};

setIndex(WangId::previousIndex(edge));
setIndex(edge);
setIndex(WangId::nextIndex(edge));
};

for (const QRect &rect : region) {
for (int x = rect.left(); x <= rect.right(); ++x) {
setDesiredWangId(x, rect.top(), WangId::Top);
setDesiredWangId(x, rect.bottom(), WangId::Bottom);
}
for (int y = rect.top(); y <= rect.bottom(); ++y) {
setDesiredWangId(rect.left(), y, WangId::Left);
setDesiredWangId(rect.right(), y, WangId::Right);
}
}
}

// Determine the bounds of the affected area
QRect bounds = region.boundingRect();
int margin = mWangSet.maximumColorDistance() + (mHexagonalRenderer != nullptr);
Expand Down Expand Up @@ -258,7 +259,7 @@ void WangFiller::apply(TileLayer &target)
if (target.cellAt(p - target.position()).checked())
continue;

CellInfo adjacentInfo = grid.get(p);
CellInfo &adjacentInfo = grid.add(p);
updateToAdjacent(adjacentInfo, cellWangId, WangId::oppositeIndex(i));

// Check if we may need to reconsider a tile outside of our starting region
Expand All @@ -274,8 +275,6 @@ void WangFiller::apply(TileLayer &target)
adjacentInfo.desired.setIndexColor(i, currentWangId.indexColor(i));
}
}

grid.set(p, adjacentInfo);
}
};

Expand Down