Skip to content

Commit

Permalink
Enable replacing an object with a template
Browse files Browse the repository at this point in the history
  • Loading branch information
thabetx committed Oct 22, 2017
1 parent 7b17bbe commit f19a5cc
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/tiled/abstractobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ void AbstractObjectTool::detachSelectedObjects()
currentMapDocument->undoStack()->push(changeMapObjectCommand);
}

void AbstractObjectTool::replaceObjectsWithTemplate()
{
mapDocument()->undoStack()->push(new ReplaceObjectsWithTemplate(mapDocument(),
mapDocument()->selectedObjects(),
{objectTemplate()->templateGroup(), objectTemplate()->id()}));
}

void AbstractObjectTool::resetInstances()
{
QList<MapObject *> templateInstances;
Expand Down Expand Up @@ -351,6 +358,9 @@ void AbstractObjectTool::showContextMenu(MapObjectItem *clickedObjectItem,

auto changeTileAction = menu.addAction(tr("Replace Tile"), this, SLOT(changeTile()));
changeTileAction->setEnabled(tile());

auto replaceTemplateAction = menu.addAction(tr("Replace With Template"), this, SLOT(replaceObjectsWithTemplate()));
replaceTemplateAction->setEnabled(objectTemplate());
}

if (selectedObjects.size() == 1) {
Expand Down
1 change: 1 addition & 0 deletions src/tiled/abstractobjecttool.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private slots:
void resetTileSize();
void saveSelectedObject();
void detachSelectedObjects();
void replaceObjectsWithTemplate();
void resetInstances();
void changeTile();

Expand Down
2 changes: 2 additions & 0 deletions src/tiled/abstracttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "abstracttool.h"

#include "mapdocument.h"
#include "templatesdock.h"

#include <QKeyEvent>

Expand All @@ -36,6 +37,7 @@ AbstractTool::AbstractTool(const QString &name, const QIcon &icon,
, mShortcut(shortcut)
, mEnabled(false)
, mTile(nullptr)
, mObjectTemplate(nullptr)
, mMapDocument(nullptr)
{
}
Expand Down
15 changes: 15 additions & 0 deletions src/tiled/abstracttool.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Tiled {

class Layer;
class Tile;
class ObjectTemplate;

namespace Internal {

Expand Down Expand Up @@ -94,6 +95,8 @@ class AbstractTool : public QObject

Tile *tile() const;

ObjectTemplate *objectTemplate() const;

/**
* Activates this tool. If the tool plans to add any items to the scene, it
* probably wants to do it here.
Expand Down Expand Up @@ -152,6 +155,7 @@ class AbstractTool : public QObject
public slots:
void setMapDocument(MapDocument *mapDocument);
void setTile(Tile *tile);
void setObjectTemplate(ObjectTemplate *objectTemplate);

protected:
/**
Expand Down Expand Up @@ -191,6 +195,7 @@ protected slots:
QCursor mCursor;
bool mEnabled;
Tile *mTile;
ObjectTemplate *mObjectTemplate;

MapDocument *mMapDocument;
};
Expand Down Expand Up @@ -251,6 +256,16 @@ inline void AbstractTool::setTile(Tile *tile)
mTile = tile;
}

inline ObjectTemplate *AbstractTool::objectTemplate() const
{
return mObjectTemplate;
}

inline void AbstractTool::setObjectTemplate(ObjectTemplate *objectTemplate)
{
mObjectTemplate = objectTemplate;
}

} // namespace Internal
} // namespace Tiled

Expand Down
72 changes: 61 additions & 11 deletions src/tiled/changemapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ void DetachObjects::undo()
emit mMapDocument->mapObjectModel()->objectsChanged(mMapObjects);
}

void copyProperties(const MapObject *source, MapObject *target) {
target->setName(source->name());
target->setSize(source->size());
target->setType(source->type());
target->setTextData(source->textData());
target->setPolygon(source->polygon());
target->setShape(source->shape());
target->setCell(source->cell());
target->setRotation(source->rotation());
target->setVisible(source->VisibleProperty);
target->setProperties(source->properties());
target->setChangedProperties(source->changedProperties());
target->setTemplateRef(source->templateRef());
}

ResetInstances::ResetInstances(MapDocument *mapDocument,
const QList<MapObject *> &mapObjects,
Expand Down Expand Up @@ -245,19 +259,55 @@ void ResetInstances::undo()
for (int i = 0; i < mMapObjects.size(); ++i) {
MapObject *current = mMapObjects.at(i);
MapObject *old = mOldMapObjects.at(i);
current->setName(old->name());
current->setSize(old->size());
current->setType(old->type());
current->setTextData(old->textData());
current->setPolygon(old->polygon());
current->setShape(old->shape());
current->setCell(old->cell());
current->setRotation(old->rotation());
current->setVisible(old->VisibleProperty);
current->setProperties(old->properties());
current->setChangedProperties(old->changedProperties());
copyProperties(old, current);
}

emit mMapDocument->objectsChanged(mMapObjects);
emit mMapDocument->selectedObjectsChanged();
}


ReplaceObjectsWithTemplate::ReplaceObjectsWithTemplate(MapDocument *mapDocument,
const QList<MapObject *> &mapObjects,
TemplateRef templateRef,
QUndoCommand *parent)
: QUndoCommand(QCoreApplication::translate("Undo Commands",
"Replace %n object(s) with template",
nullptr, mapObjects.size()), parent)
, mMapDocument(mapDocument)
, mMapObjects(mapObjects)
, mTemplateRef(templateRef)
{
for (const MapObject *object : mapObjects)
mOldMapObjects.append(object->clone());
}

void ReplaceObjectsWithTemplate::redo()
{
for (auto object : mMapObjects) {
object->clearProperties();
object->setChangedProperties(0);
object->setTemplateRef(mTemplateRef);
object->syncWithTemplate();
}

emit mMapDocument->objectsChanged(mMapObjects);
emit mMapDocument->selectedObjectsChanged();
}

void ReplaceObjectsWithTemplate::undo()
{
for (int i = 0; i < mMapObjects.size(); ++i) {
MapObject *current = mMapObjects.at(i);
MapObject *old = mOldMapObjects.at(i);
copyProperties(old, current);;
}

emit mMapDocument->objectsChanged(mMapObjects);
emit mMapDocument->selectedObjectsChanged();
}

ReplaceObjectsWithTemplate::~ReplaceObjectsWithTemplate()
{
qDeleteAll(mOldMapObjects);
}
23 changes: 23 additions & 0 deletions src/tiled/changemapobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,28 @@ class ResetInstances : public QUndoCommand
QList<MapObject*> mOldMapObjects;
};

class ReplaceObjectsWithTemplate : public QUndoCommand
{
public:
/**
* Creates an undo command that replaces the given objects with a template
*/
ReplaceObjectsWithTemplate(MapDocument *mapDocument,
const QList<MapObject *> &mapObjects,
TemplateRef templateRef,
QUndoCommand *parent = nullptr);

~ReplaceObjectsWithTemplate();

void redo() override;
void undo() override;

private:
MapDocument *mMapDocument;
const QList<MapObject*> mMapObjects;
QList<MapObject*> mOldMapObjects;
TemplateRef mTemplateRef;
};

} // namespace Internal
} // namespace Tiled
7 changes: 4 additions & 3 deletions src/tiled/createtemplatetool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ using namespace Tiled::Internal;

CreateTemplateTool::CreateTemplateTool(QObject *parent)
: CreateObjectTool(parent)
, mObjectTemplate(nullptr)
{
QIcon icon(QLatin1String(":images/24x24/insert-template.png"));
icon.addFile(QLatin1String(":images/48x48/insert-template.png"));
Expand Down Expand Up @@ -86,11 +85,13 @@ void CreateTemplateTool::languageChanged()

MapObject *CreateTemplateTool::createNewMapObject()
{
if (!mObjectTemplate)
auto newObjectTemplate = objectTemplate();

if (!newObjectTemplate)
return nullptr;

MapObject *newMapObject = new MapObject();
newMapObject->setTemplateRef(mObjectTemplate->templateRef());
newMapObject->setTemplateRef(newObjectTemplate->templateRef());
newMapObject->syncWithTemplate();
return newMapObject;
}
6 changes: 0 additions & 6 deletions src/tiled/createtemplatetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class CreateTemplateTool : public CreateObjectTool

void languageChanged() override;

public slots:
void setTemplate(ObjectTemplate *objectTemplate) { mObjectTemplate = objectTemplate; }

protected:
void mouseMovedWhileCreatingObject(const QPointF &pos,
Qt::KeyboardModifiers modifiers) override;
Expand All @@ -49,9 +46,6 @@ public slots:

bool startNewMapObject(const QPointF &pos, ObjectGroup *objectGroup) override;
MapObject *createNewMapObject() override;

private:
ObjectTemplate *mObjectTemplate;
};

} // namespace Internal
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ MapEditor::MapEditor(QObject *parent)
connect(mTilesetDock, &TilesetDock::currentTileChanged, mTemplatesDock, &TemplatesDock::setTile);
connect(mTilesetDock, &TilesetDock::stampCaptured, this, &MapEditor::setStamp);
connect(mTilesetDock, &TilesetDock::localFilesDropped, this, &MapEditor::filesDroppedOnTilesetDock);
connect(mTemplatesDock, &TemplatesDock::currentTemplateChanged, templatesTool, &CreateTemplateTool::setTemplate);
connect(mTemplatesDock, &TemplatesDock::currentTemplateChanged, mToolManager, &ToolManager::setObjectTemplate);

connect(mStampBrush, &StampBrush::stampChanged, this, &MapEditor::setStamp);
connect(mBucketFillTool, &BucketFillTool::stampChanged, this, &MapEditor::setStamp);
Expand Down
9 changes: 9 additions & 0 deletions src/tiled/toolmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ToolManager::ToolManager(QObject *parent)
, mPreviouslyDisabledTool(nullptr)
, mMapDocument(nullptr)
, mTile(nullptr)
, mObjectTemplate(nullptr)
, mSelectEnabledToolPending(false)
{
mActionGroup->setExclusive(true);
Expand Down Expand Up @@ -186,6 +187,13 @@ void ToolManager::setTile(Tile *tile)
mSelectedTool->setTile(mTile);
}

void ToolManager::setObjectTemplate(ObjectTemplate *objectTemplate)
{
mObjectTemplate = objectTemplate;
if (mSelectedTool)
mSelectedTool->setObjectTemplate(mObjectTemplate);
}

void ToolManager::toolEnabledChanged(bool enabled)
{
AbstractTool *tool = qobject_cast<AbstractTool*>(sender());
Expand Down Expand Up @@ -261,5 +269,6 @@ void ToolManager::setSelectedTool(AbstractTool *tool)
connect(mSelectedTool, SIGNAL(statusInfoChanged(QString)),
this, SIGNAL(statusInfoChanged(QString)));
tool->setTile(mTile);
tool->setObjectTemplate(mObjectTemplate);
}
}
3 changes: 3 additions & 0 deletions src/tiled/toolmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class QActionGroup;
namespace Tiled {

class Tile;
class ObjectTemplate;

namespace Internal {

Expand Down Expand Up @@ -66,6 +67,7 @@ public slots:
* CreateTileObjects or when replacing a tile of a tile object.
*/
void setTile(Tile *tile);
void setObjectTemplate(ObjectTemplate *objectTemplate);

signals:
void selectedToolChanged(AbstractTool *tool);
Expand Down Expand Up @@ -93,6 +95,7 @@ private slots:
AbstractTool *mPreviouslyDisabledTool;
MapDocument *mMapDocument;
Tile *mTile;
ObjectTemplate *mObjectTemplate;

bool mSelectEnabledToolPending;
};
Expand Down

0 comments on commit f19a5cc

Please sign in to comment.