private void pour(TileLayer layer, int x, int y, Tile newTile, Tile oldTile) {
if (newTile == oldTile || !layer.canEdit())
return;
Rectangle area;
TileLayer before = (TileLayer) createLayerCopy(layer);
TileLayer after;
// Check that the copy was succesfully created
if (before == null) {
return;
}
if (marqueeSelection == null) {
area = new Rectangle(x, y, 0, 0);
Stack<Point> stack = new Stack<Point>();
stack.push(new Point(x, y));
while (!stack.empty()) {
// Remove the next tile from the stack
Point p = stack.pop();
// If the tile it meets the requirements, set it and push its
// neighbouring tiles on the stack.
if (layer.contains(p.x, p.y)
&& layer.getTileAt(p.x, p.y) == oldTile) {
layer.setTileAt(p.x, p.y, newTile);
Converter.add(area, p.x, p.y);
stack.push(new Point(p.x, p.y - 1));
stack.push(new Point(p.x, p.y + 1));
stack.push(new Point(p.x + 1, p.y));
stack.push(new Point(p.x - 1, p.y));
}
}
} else {
if (marqueeSelection.getSelectedArea().contains(x, y)) {
area = marqueeSelection.getSelectedAreaBounds();
for (int i = area.y; i < area.height + area.y; i++) {
for (int j = area.x; j < area.width + area.x; j++) {
if (marqueeSelection.getSelectedArea().contains(j, i)) {
layer.setTileAt(j, i, newTile);
}
}
}
} else {
return;
}
}
Rectangle bounds = new Rectangle(area.x, area.y, area.width + 1,
area.height + 1);
after = new TileLayer(bounds);
after.copyFrom(layer);
MapLayerEdit mle = new MapLayerEdit(layer, before, after);
mle.setPresentationName(TOOL_FILL);
addEdit(mle);
}