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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
bjorn committed Jul 6, 2023
commit 415e0165b994dbe4210c5f4963b43a4e45b0c679
6 changes: 6 additions & 0 deletions docs/scripting-doc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/tiled/stampbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ void StampBrush::drawPreviewLayer(const QVector<QPoint> &points)
};

WangFiller wangFiller(*mWangSet, *tileLayer, mapDocument()->renderer());
wangFiller.setErasingEnabled(false);
wangFiller.setRegion(paintedRegion);
wangFiller.apply(*previewLayer);

Expand Down
11 changes: 11 additions & 0 deletions src/tiled/tilelayerwangedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "maprenderer.h"
#include "scriptmanager.h"
#include "tilelayer.h"
#include "wangfiller.h"

#include <QCoreApplication>

Expand Down Expand Up @@ -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<WangId::Index>(index), color);
Expand Down
9 changes: 7 additions & 2 deletions src/tiled/tilelayerwangedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "editablewangset.h"
#include "map.h"
#include "wangfiller.h"

#include <QObject>

Expand All @@ -32,6 +31,8 @@
namespace Tiled {

class EditableTileLayer;
class MapRenderer;
class WangFiller;

// Copy of WangId::Index, for exposing the enum to JS
namespace WangIndex
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/tiled/wangfiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/wangfiller.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 &region);
Expand Down Expand Up @@ -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;
Expand Down