Skip to content

Commit

Permalink
Dedupe apply function between tilelayeredit and tilelayerwangedit
Browse files Browse the repository at this point in the history
  • Loading branch information
a-morphous committed Jun 2, 2023
1 parent cc75f09 commit 532edce
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 97 deletions.
42 changes: 42 additions & 0 deletions src/tiled/editabletilelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

#include "editabletilelayer.h"

#include "addremovetileset.h"
#include "changelayer.h"
#include "editablemanager.h"
#include "editablemap.h"
#include "painttilelayer.h"
#include "resizetilelayer.h"
#include "tilelayeredit.h"
#include "tilelayerwangedit.h"
Expand Down Expand Up @@ -109,6 +111,46 @@ TileLayerWangEdit *EditableTileLayer::wangEdit(EditableWangSet *wangSet)
return new TileLayerWangEdit(this, wangSet);
}

void EditableTileLayer::applyChangesFrom(TileLayer *changes, bool mergeable)
{
// Determine painted region and normalize the changes layer
auto paintedRegion = changes->region([] (const Cell &cell) { return cell.checked(); });

// If the painted region is empty there's nothing else to do
if (paintedRegion.isEmpty())
return;

auto rect = paintedRegion.boundingRect();
changes->resize(rect.size(), -rect.topLeft());
const auto tilesets = changes->usedTilesets();

if (mapDocument()) {
// Apply the change using an undo command
auto mapDocument = map()->mapDocument();
auto paint = new PaintTileLayer(mapDocument,
tileLayer(),
rect.x(), rect.y(),
changes,
paintedRegion);
paint->setMergeable(mergeable);

// Add any used tilesets that aren't yet part of the target map
const auto existingTilesets = mapDocument->map()->tilesets();
for (const SharedTileset &tileset : tilesets)
if (!existingTilesets.contains(tileset))
new AddTileset(mapDocument, tileset, paint);

map()->push(paint);
} else {
// Add any used tilesets that aren't yet part of the target map
if (auto map = tileLayer()->map())
map->addTilesets(tilesets);

// Apply the change directly
tileLayer()->setCells(rect.x(), rect.y(), changes, paintedRegion);
}
}

} // namespace Tiled

#include "moc_editabletilelayer.cpp"
2 changes: 2 additions & 0 deletions src/tiled/editabletilelayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class EditableTileLayer : public EditableLayer

QList<TileLayerEdit*> mActiveEdits;
QList<TileLayerWangEdit*> mActiveWangEdits;

void applyChangesFrom(TileLayer *changes, bool mergeable);
};


Expand Down
39 changes: 1 addition & 38 deletions src/tiled/tilelayeredit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,7 @@ void TileLayerEdit::apply()
// Applying an edit automatically makes it mergeable, so that further
// changes made through the same edit are merged by default.
bool mergeable = std::exchange(mMergeable, true);

// Determine painted region and normalize the changes layer
auto paintedRegion = mChanges.region([] (const Cell &cell) { return cell.checked(); });

// If the painted region is empty there's nothing else to do
if (paintedRegion.isEmpty())
return;

auto rect = paintedRegion.boundingRect();
mChanges.resize(rect.size(), -rect.topLeft());
const auto tilesets = mChanges.usedTilesets();

if (mTargetLayer->mapDocument()) {
// Apply the change using an undo command
auto mapDocument = mTargetLayer->map()->mapDocument();
auto paint = new PaintTileLayer(mapDocument,
mTargetLayer->tileLayer(),
rect.x(), rect.y(),
&mChanges,
paintedRegion);
paint->setMergeable(mergeable);

// Add any used tilesets that aren't yet part of the target map
const auto existingTilesets = mapDocument->map()->tilesets();
for (const SharedTileset &tileset : tilesets)
if (!existingTilesets.contains(tileset))
new AddTileset(mapDocument, tileset, paint);

mTargetLayer->map()->push(paint);
} else {
// Add any used tilesets that aren't yet part of the target map
if (auto map = mTargetLayer->tileLayer()->map())
map->addTilesets(tilesets);

// Apply the change directly
mTargetLayer->tileLayer()->setCells(rect.x(), rect.y(), &mChanges, paintedRegion);
}

mTargetLayer->applyChangesFrom(&mChanges, mergeable);
mChanges.clear();
}

Expand Down
40 changes: 1 addition & 39 deletions src/tiled/tilelayerwangedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void TileLayerWangEdit::setTerrain(int x, int y, int color, WangId::Index direct
mWangPainter->setTerrain(mTargetLayer->mapDocument(), color, QPoint(x, y), direction);
}

// currently copied from tilelayeredit.cpp
void TileLayerWangEdit::apply()
{
// apply terrain changes
Expand All @@ -36,44 +35,7 @@ void TileLayerWangEdit::apply()
// Applying an edit automatically makes it mergeable, so that further
// changes made through the same edit are merged by default.
bool mergeable = std::exchange(mMergeable, true);

// Determine painted region and normalize the changes layer
auto paintedRegion = mChanges.region([] (const Cell &cell) { return cell.checked(); });

// If the painted region is empty there's nothing else to do
if (paintedRegion.isEmpty())
return;

auto rect = paintedRegion.boundingRect();
mChanges.resize(rect.size(), -rect.topLeft());
const auto tilesets = mChanges.usedTilesets();

if (mTargetLayer->mapDocument()) {
// Apply the change using an undo command
auto mapDocument = mTargetLayer->map()->mapDocument();
auto paint = new PaintTileLayer(mapDocument,
mTargetLayer->tileLayer(),
rect.x(), rect.y(),
&mChanges,
paintedRegion);
paint->setMergeable(mergeable);

// Add any used tilesets that aren't yet part of the target map
const auto existingTilesets = mapDocument->map()->tilesets();
for (const SharedTileset &tileset : tilesets)
if (!existingTilesets.contains(tileset))
new AddTileset(mapDocument, tileset, paint);

mTargetLayer->map()->push(paint);
} else {
// Add any used tilesets that aren't yet part of the target map
if (auto map = mTargetLayer->tileLayer()->map())
map->addTilesets(tilesets);

// Apply the change directly
mTargetLayer->tileLayer()->setCells(rect.x(), rect.y(), &mChanges, paintedRegion);
}

mTargetLayer->applyChangesFrom(&mChanges, mergeable);
mChanges.clear();
}

Expand Down
20 changes: 0 additions & 20 deletions src/tiled/tilelayerwangedit.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
/*
* tilelayeredit.h
* Copyright 2019, Thorbjørn Lindeijer <[email protected]>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "editablewangset.h"
Expand Down

0 comments on commit 532edce

Please sign in to comment.