From 823d428546d3c691fd4b0277687ead7ae1e843e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 6 Jul 2023 13:03:45 +0200 Subject: [PATCH] Disabled placement of empty tiles by Stamp Brush in Terrain Fill mode Also exposed the new TileLayerWangEdit.erasingEnabled property in the scripting API. Another change is that empty tiles no longer contribute to the desired WangId for the Terrain Fill mode (when corrections are disabled). Previously, only tiles outside of the map would not contribute to the desired WangId. This change yields nicer behavior when using the Wang Fill mode with the Stamp Brush, but it may need to be reconsidered for the Shape Fill tool or a later Terrain Fill tool. --- docs/scripting-doc/index.d.ts | 6 ++++++ src/tiled/stampbrush.cpp | 1 + src/tiled/tilelayerwangedit.cpp | 11 +++++++++++ src/tiled/tilelayerwangedit.h | 9 +++++++-- src/tiled/wangfiller.cpp | 8 +++++--- src/tiled/wangfiller.h | 4 ++++ 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/docs/scripting-doc/index.d.ts b/docs/scripting-doc/index.d.ts index 72f9cc52a9b..f7aec4aa0f7 100644 --- a/docs/scripting-doc/index.d.ts +++ b/docs/scripting-doc/index.d.ts @@ -2965,6 +2965,12 @@ interface TileLayerWangEdit { */ correctionsEnabled : boolean + /** + * Whether the empty tile is considered when looking for matching tiles. + * Defaults to `true`. + */ + erasingEnabled : boolean + /** * Sets the desired color for the given Wang index at the given location. * diff --git a/src/tiled/stampbrush.cpp b/src/tiled/stampbrush.cpp index bcaf5fa1c54..5305397e60f 100644 --- a/src/tiled/stampbrush.cpp +++ b/src/tiled/stampbrush.cpp @@ -462,6 +462,7 @@ void StampBrush::drawPreviewLayer(const QVector &points) }; WangFiller wangFiller(*mWangSet, *tileLayer, mapDocument()->renderer()); + wangFiller.setErasingEnabled(false); wangFiller.setRegion(paintedRegion); wangFiller.apply(*previewLayer); diff --git a/src/tiled/tilelayerwangedit.cpp b/src/tiled/tilelayerwangedit.cpp index 9bf5ddefc9a..19f2beef075 100644 --- a/src/tiled/tilelayerwangedit.cpp +++ b/src/tiled/tilelayerwangedit.cpp @@ -26,6 +26,7 @@ #include "maprenderer.h" #include "scriptmanager.h" #include "tilelayer.h" +#include "wangfiller.h" #include @@ -63,6 +64,16 @@ void TileLayerWangEdit::setCorrectionsEnabled(bool correctionsEnabled) mWangFiller->setCorrectionsEnabled(correctionsEnabled); } +bool TileLayerWangEdit::erasingEnabled() const +{ + return mWangFiller->erasingEnabled(); +} + +void TileLayerWangEdit::setErasingEnabled(bool erasingEnabled) +{ + mWangFiller->setErasingEnabled(erasingEnabled); +} + void TileLayerWangEdit::setWangIndex(QPoint pos, WangIndex::Value index, int color) { mWangFiller->setWangIndex(pos, static_cast(index), color); diff --git a/src/tiled/tilelayerwangedit.h b/src/tiled/tilelayerwangedit.h index 19a774f772e..5b1507400ed 100644 --- a/src/tiled/tilelayerwangedit.h +++ b/src/tiled/tilelayerwangedit.h @@ -23,7 +23,6 @@ #include "editablewangset.h" #include "map.h" -#include "wangfiller.h" #include @@ -32,6 +31,8 @@ namespace Tiled { class EditableTileLayer; +class MapRenderer; +class WangFiller; // Copy of WangId::Index, for exposing the enum to JS namespace WangIndex @@ -63,6 +64,7 @@ class TileLayerWangEdit : public QObject Q_PROPERTY(Tiled::EditableWangSet *wangSet READ wangSet CONSTANT) Q_PROPERTY(bool mergeable READ isMergeable WRITE setMergeable) Q_PROPERTY(bool correctionsEnabled READ correctionsEnabled WRITE setCorrectionsEnabled) + Q_PROPERTY(bool erasingEnabled READ erasingEnabled WRITE setErasingEnabled) public: explicit TileLayerWangEdit(EditableTileLayer *tileLayer, @@ -80,7 +82,10 @@ class TileLayerWangEdit : public QObject bool isMergeable() const; bool correctionsEnabled() const; - void setCorrectionsEnabled(bool newCorrectionsEnabled); + void setCorrectionsEnabled(bool correctionsEnabled); + + bool erasingEnabled() const; + void setErasingEnabled(bool erasingEnabled); EditableTileLayer *target() const; EditableWangSet *wangSet() const; diff --git a/src/tiled/wangfiller.cpp b/src/tiled/wangfiller.cpp index 87f4c6f42a9..b9060046d76 100644 --- a/src/tiled/wangfiller.cpp +++ b/src/tiled/wangfiller.cpp @@ -336,13 +336,14 @@ WangId WangFiller::wangIdFromSurroundings(QPoint point) const for (int i = 0; i < WangId::NumIndexes; ++i) { wangIds[i] = WangId::FULL_MASK; - if (!mMapRenderer->map()->infinite() && !mBack.rect().contains(adjacentPoints[i])) + const auto &cell = mBack.cellAt(adjacentPoints[i]); + if (cell.isEmpty()) continue; if (mFillRegion.region.contains(adjacentPoints[i])) continue; - wangIds[i] = mWangSet.wangIdOfCell(mBack.cellAt(adjacentPoints[i])); + wangIds[i] = mWangSet.wangIdOfCell(cell); } return wangIdFromSurrounding(wangIds); @@ -410,7 +411,8 @@ bool WangFiller::findBestMatch(const TileLayer &target, for (int i = 0, i_end = wangIdsAndCells.size(); i < i_end; ++i) processCandidate(wangIdsAndCells[i].wangId, wangIdsAndCells[i].cell); - processCandidate(WangId(), Cell()); + if (mErasingEnabled) + processCandidate(WangId(), Cell()); // Choose a candidate at random, with consideration for probability while (!matches.isEmpty()) { diff --git a/src/tiled/wangfiller.h b/src/tiled/wangfiller.h index 19cdeab72bd..fb364f586f0 100644 --- a/src/tiled/wangfiller.h +++ b/src/tiled/wangfiller.h @@ -73,6 +73,9 @@ class WangFiller bool correctionsEnabled() const { return mCorrectionsEnabled; } void setCorrectionsEnabled(bool enabled) { mCorrectionsEnabled = enabled; } + bool erasingEnabled() const { return mErasingEnabled; } + void setErasingEnabled(bool enabled) { mErasingEnabled = enabled; } + void setDebugPainter(QPainter *painter) { mDebugPainter = painter; } void setRegion(const QRegion ®ion); @@ -104,6 +107,7 @@ class WangFiller const MapRenderer * const mMapRenderer; const HexagonalRenderer * const mHexagonalRenderer; bool mCorrectionsEnabled = false; + bool mErasingEnabled = true; FillRegion mFillRegion; QPainter *mDebugPainter = nullptr;