Skip to content

Commit

Permalink
Improved support for erasing tiles in scripted tools
Browse files Browse the repository at this point in the history
* The Tool.preview now includes empty-but-modified tiles in the
  highlighted area.

* The TileMap.merge function now also copies empty-but-modified tiles
  from the map getting merged.
  • Loading branch information
bjorn committed Jun 30, 2023
1 parent 23f180f commit d9eb319
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Added support for setting custom properties on the project (#2903)
* Removed Space and Ctrl+Space shortcuts from Layers view to avoid conflict with panning (#3672)
* Scripting: Added API for editing tile layers using terrain sets (with a-morphous, #3758)
* Scripting: Support erasing tiles in Tool.preview and TileMap.merge
* Fixed object preview position with parallax factor on group layer (#3669)
* Fixed hover highlight rendering with active parallax factor (#3669)
* Fixed updating of object selection outlines when changing parallax factor (#3669)
Expand Down
4 changes: 2 additions & 2 deletions src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ QRect Map::tileBoundingRect() const
return mapBounds;
}

QRegion Map::tileRegion() const
QRegion Map::modifiedTileRegion() const
{
QRegion region;
LayerIterator it(this, Layer::TileLayerType);
while (auto tileLayer = static_cast<TileLayer*>(it.next()))
region |= tileLayer->region();
region |= tileLayer->modifiedRegion();
return region;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class TILEDSHARED_EXPORT Map : public Object
MapObject *findObjectById(int objectId) const;

QRect tileBoundingRect() const;
QRegion tileRegion() const;
QRegion modifiedTileRegion() const;

private:
friend class GroupLayer; // so it can call adoptLayer
Expand Down
12 changes: 12 additions & 0 deletions src/libtiled/tilelayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class TILEDSHARED_EXPORT TileLayer : public Layer

QRegion region(std::function<bool (const Cell &)> condition) const;
QRegion region() const;
QRegion modifiedRegion() const;

const Cell &cellAt(int x, int y) const;
const Cell &cellAt(QPoint point) const;
Expand Down Expand Up @@ -626,6 +627,17 @@ inline QRegion TileLayer::region() const
return region([] (const Cell &cell) { return !cell.isEmpty(); });
}

/**
* Calculates the modified region of this layer. This includes both all
* non-empty cells as well as any cells that were marked as "checked".
*/
inline QRegion TileLayer::modifiedRegion() const
{
return region([] (const Cell &cell) {
return !cell.isEmpty() || cell.checked();
});
}

/**
* Returns a read-only reference to the cell at the given coordinates. The
* coordinates have to be within this layer.
Expand Down
13 changes: 3 additions & 10 deletions src/tiled/brushitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ void BrushItem::clear()
*/
void BrushItem::setTileLayer(const SharedTileLayer &tileLayer)
{
mTileLayer = tileLayer;
mRegion = tileLayer ? tileLayer->region() : QRegion();

updateBoundingRect();
update();
setTileLayer(tileLayer,
tileLayer ? tileLayer->modifiedRegion() : QRegion());
}

/**
Expand All @@ -97,11 +94,7 @@ void BrushItem::setTileLayer(const SharedTileLayer &tileLayer,

void BrushItem::setMap(const SharedMap &map)
{
mMap = map;
mRegion = map->tileRegion();

updateBoundingRect();
update();
setMap(map, map->modifiedTileRegion());
}

void BrushItem::setMap(const SharedMap &map, const QRegion &region)
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/brushitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BrushItem : public QGraphicsItem
QWidget *widget = nullptr) override;

protected:
MapDocument *mapDocument() const { return mMapDocument; }
MapDocument *mapDocument() const { return mMapDocument; }

private:
void updateBoundingRect();
Expand Down
1 change: 0 additions & 1 deletion src/tiled/capturestamphelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include "map.h"
#include "mapdocument.h"
#include "tilelayer.h"

#include <memory>

Expand Down
8 changes: 4 additions & 4 deletions src/tiled/mapdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,8 @@ void MapDocument::paintTileLayers(const Map &map, bool mergeable,
auto source = sourceLayers[i];
auto target = targetLayers[i];

const QRegion editedRegion = source->region();
if (editedRegion.isEmpty())
const QRegion paintRegion = source->modifiedRegion();
if (paintRegion.isEmpty())
continue;

std::unique_ptr<TileLayer> newLayer;
Expand All @@ -1018,7 +1018,7 @@ void MapDocument::paintTileLayers(const Map &map, bool mergeable,
source->x(),
source->y(),
source,
editedRegion);
paintRegion);

if (missingTilesets && !missingTilesets->isEmpty()) {
for (const SharedTileset &tileset : std::as_const(*missingTilesets)) {
Expand Down Expand Up @@ -1058,7 +1058,7 @@ void MapDocument::paintTileLayers(const Map &map, bool mergeable,
paintCommand->setMergeable(mergeable);
undoStack()->push(paintCommand);

regions[target] |= editedRegion;
regions[target] |= paintRegion;

mergeable = true; // further paints are always mergeable
}
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/stampbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ void StampBrush::drawPreviewLayer(const QVector<QPoint> &points)
if (regionCache.contains(map)) {
stampRegion = regionCache.value(map);
} else {
stampRegion = map->tileRegion();
stampRegion = map->modifiedTileRegion();
regionCache.insert(map, stampRegion);
}

Expand Down Expand Up @@ -635,7 +635,7 @@ void StampBrush::updatePreview(QPoint tilePos)
}

if (mPreviewMap)
tileRegion = mPreviewMap->tileRegion();
tileRegion = mPreviewMap->modifiedTileRegion();

if (tileRegion.isEmpty())
tileRegion = QRect(tilePos, tilePos);
Expand Down

0 comments on commit d9eb319

Please sign in to comment.