Skip to content

Commit

Permalink
Implemented a Magic Wand tool
Browse files Browse the repository at this point in the history
Closes #128
  • Loading branch information
HenryJia authored and bjorn committed Feb 21, 2015
1 parent cef4129 commit 57c27c1
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions src/tiled/magicwandtool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* magicwandtool.cpp
* Copyright 2009-2010, Jeff Bland <jksb@member.fsf.org>
* Copyright 2010, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
* Copyright 2010, Jared Adams <jaxad0127@gmail.com>
* Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
*
* 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/>.
*/

#include "magicwandtool.h"

#include "brushitem.h"
#include "filltiles.h"
#include "tilepainter.h"
#include "tile.h"
#include "mapscene.h"
#include "mapdocument.h"
#include "changeselectedarea.h"

#include <QApplication>

using namespace Tiled;
using namespace Tiled::Internal;

MagicWandTool::MagicWandTool(QObject *parent)
: AbstractTileTool(tr("Magic Wand"),
QIcon(QLatin1String(
":images/22x22/stock-tool-fuzzy-select-22.png")),
QKeySequence(tr("W")),
parent)
{
}

void MagicWandTool::tilePositionChanged(const QPoint &tilePos)
{
// Make sure that a tile layer is selected
TileLayer *tileLayer = currentTileLayer();
if (!tileLayer)
return;

TilePainter regionComputer(mapDocument(), tileLayer);
mSelectedRegion = regionComputer.computeFillRegion(tilePos);
brushItem()->setTileRegion(mSelectedRegion);
}

void MagicWandTool::mousePressed(QGraphicsSceneMouseEvent *event)
{
if (event->button() != Qt::LeftButton)
return;

const Qt::KeyboardModifiers modifiers = event->modifiers();

MapDocument *document = mapDocument();

QRegion selection = document->selectedArea();

if (modifiers == Qt::ShiftModifier)
selection += mSelectedRegion;
else if (modifiers == Qt::ControlModifier)
selection -= mSelectedRegion;
else if (modifiers == (Qt::ControlModifier | Qt::ShiftModifier))
selection &= mSelectedRegion;
else
selection = mSelectedRegion;

if (selection != document->selectedArea()) {
QUndoCommand *cmd = new ChangeSelectedArea(document, selection);
document->undoStack()->push(cmd);
}
}

void MagicWandTool::mouseReleased(QGraphicsSceneMouseEvent *)
{
}

void MagicWandTool::languageChanged()
{
setName(tr("Magic Wand"));
setShortcut(QKeySequence(tr("W")));
}
64 changes: 64 additions & 0 deletions src/tiled/magicwandtool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* magicwandtool.h
* Copyright 2009-2010, Jeff Bland <jksb@member.fsf.org>
* Copyright 2010, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
* Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
*
* 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/>.
*/


#ifndef MAGICWANDTOOL_H
#define MAGICWANDTOOL_H


#include "abstracttiletool.h"

#include "tilelayer.h"

namespace Tiled {
namespace Internal {

class MapDocument;

/**
* Implements a tool that bucket fills (flood fills) a region with a repeatable
* stamp.
*/
class MagicWandTool : public AbstractTileTool
{
Q_OBJECT

public:
MagicWandTool(QObject *parent = 0);

void mousePressed(QGraphicsSceneMouseEvent *event);
void mouseReleased(QGraphicsSceneMouseEvent *event);

void languageChanged();

protected:
void tilePositionChanged(const QPoint &tilePos);

private:

QRegion mSelectedRegion;
};

} // namespace Internal
} // namespace Tiled

#endif // MAGICWANDTOOL_H
2 changes: 2 additions & 0 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "tileanimationeditor.h"
#include "tilecollisioneditor.h"
#include "imagemovementtool.h"
#include "magicwandtool.h"

#ifdef Q_OS_MAC
#include "macsupport.h"
Expand Down Expand Up @@ -407,6 +408,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
toolBar->addAction(mToolManager->registerTool(mBucketFillTool));
toolBar->addAction(mToolManager->registerTool(new Eraser(this)));
toolBar->addAction(mToolManager->registerTool(new TileSelectionTool(this)));
toolBar->addAction(mToolManager->registerTool(new MagicWandTool(this)));
toolBar->addSeparator();
toolBar->addAction(mToolManager->registerTool(new ObjectSelectionTool(this)));
toolBar->addAction(mToolManager->registerTool(new EditPolygonTool(this)));
Expand Down
6 changes: 4 additions & 2 deletions src/tiled/tiled.pro
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ SOURCES += aboutdialog.cpp \
utils.cpp \
varianteditorfactory.cpp \
variantpropertymanager.cpp \
zoomable.cpp
zoomable.cpp \
magicwandtool.cpp

HEADERS += aboutdialog.h \
abstractimagetool.h \
Expand Down Expand Up @@ -301,7 +302,8 @@ HEADERS += aboutdialog.h \
utils.h \
varianteditorfactory.h \
variantpropertymanager.h \
zoomable.h
zoomable.h \
magicwandtool.h

macx {
OBJECTIVE_SOURCES += macsupport.mm
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/tiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ QtGuiApplication {
"layerdock.h",
"layermodel.cpp",
"layermodel.h",
"magicwandtool.h",
"magicwandtool.cpp",
"main.cpp",
"mainwindow.cpp",
"mainwindow.h",
Expand Down
1 change: 1 addition & 0 deletions src/tiled/tiled.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@
<file>images/32x32/tiled.png</file>
<file>images/24x24/move-image-layer.png</file>
<file>images/16x16/rename.png</file>
<file>images/22x22/stock-tool-fuzzy-select-22.png</file>
</qresource>
</RCC>

0 comments on commit 57c27c1

Please sign in to comment.