Skip to content

Commit

Permalink
Changed StampBrush to do rectangle-erase on right-click
Browse files Browse the repository at this point in the history
Feature requested by elvisish, but can't be merged like this. Instead,
the mouse interaction in general would need to be made more
configurable.

This change currently leaves no way to capture a stamp from the map.
  • Loading branch information
bjorn committed Sep 19, 2023
1 parent fb6c1ff commit 2a7e7bc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
55 changes: 49 additions & 6 deletions src/tiled/stampbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
#include "map.h"
#include "mapdocument.h"
#include "mapscene.h"
#include "painttilelayer.h"
#include "stampactions.h"
#include "tile.h"
#include "tilestamp.h"
#include "wangset.h"
#include "wangfiller.h"

#include <QAction>
#include <QCoreApplication>
#include <QToolBar>
#include <QVector>

Expand Down Expand Up @@ -143,7 +145,8 @@ void StampBrush::mousePressed(QGraphicsSceneMouseEvent *event)
break;
}
return;
} else if (event->button() == Qt::RightButton && event->modifiers() == Qt::NoModifier) {
} else if (event->button() == Qt::RightButton && (event->modifiers() == Qt::NoModifier ||
event->modifiers() == Qt::ShiftModifier)) {
beginCapture();
return;
}
Expand Down Expand Up @@ -341,11 +344,51 @@ void StampBrush::endCapture()

mBrushBehavior = Free;

TileStamp stamp = mCaptureStampHelper.endCapture(*mapDocument(), tilePosition());
if (!stamp.isEmpty())
emit stampChanged(TileStamp(stamp));
else
updatePreview();
doErase(mCaptureStampHelper.capturedArea(tilePosition()));
updatePreview();
}

void StampBrush::doErase(QRect area)
{
QList<QPair<QRegion, TileLayer*>> erasedRegions;

auto *eraseCommand = new PaintTileLayer(mapDocument());
eraseCommand->setText(QCoreApplication::translate("Undo Commands", "Erase"));

auto eraseOnLayer = [&] (TileLayer *tileLayer) {
if (!tileLayer->isUnlocked())
return;

QRect eraseRegion = area.intersected(tileLayer->bounds());
if (eraseRegion.isEmpty())
return;

eraseCommand->erase(tileLayer, eraseRegion);

erasedRegions.append({ eraseRegion, tileLayer });
};

const bool allLayers = mModifiers & Qt::ShiftModifier;
if (allLayers) {
for (Layer *layer : mapDocument()->map()->tileLayers())
eraseOnLayer(static_cast<TileLayer*>(layer));
} else if (!mapDocument()->selectedLayers().isEmpty()) {
for (Layer *layer : mapDocument()->selectedLayers())
if (TileLayer *tileLayer = layer->asTileLayer())
eraseOnLayer(tileLayer);
} else if (auto tileLayer = currentTileLayer()) {
eraseOnLayer(tileLayer);
}

if (!erasedRegions.isEmpty())
mapDocument()->undoStack()->push(eraseCommand);

for (auto &[region, tileLayer] : std::as_const(erasedRegions)) {
if (tileLayer->map() != mapDocument()->map())
continue;

emit mapDocument()->regionEdited(region, tileLayer);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/tiled/stampbrush.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public slots:

void beginCapture();
void endCapture();
void doErase(QRect area);

void updateBrushBehavior();
void updatePreview();
Expand Down

0 comments on commit 2a7e7bc

Please sign in to comment.