Skip to content

Commit

Permalink
Merge pull request #1663 from thabetx/editing-templates
Browse files Browse the repository at this point in the history
Editing templates
  • Loading branch information
bjorn committed Aug 20, 2017
2 parents fafa696 + 084a736 commit 3d7981f
Show file tree
Hide file tree
Showing 26 changed files with 416 additions and 93 deletions.
4 changes: 4 additions & 0 deletions src/libtiled/mapobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class TILEDSHARED_EXPORT MapObject : public Object
void setShape(Shape shape);

bool isPolyShape() const;
bool isTileObject() const;

QRectF bounds() const;
QRectF boundsUseTile() const;
Expand Down Expand Up @@ -402,6 +403,9 @@ inline void MapObject::setShape(MapObject::Shape shape)
inline bool MapObject::isPolyShape() const
{ return mShape == Polygon || mShape == Polyline; }

inline bool MapObject::isTileObject() const
{ return !mCell.isEmpty(); }

/**
* Shortcut to getting a QRectF from position() and size().
*/
Expand Down
5 changes: 3 additions & 2 deletions src/libtiled/objecttemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TILEDSHARED_EXPORT ObjectTemplate : public Object
ObjectTemplate(unsigned id, QString name);

const MapObject *object() const;
void setObject(MapObject *object);
void setObject(const MapObject *object);

unsigned id() const;
void setId(unsigned id);
Expand All @@ -64,8 +64,9 @@ class TILEDSHARED_EXPORT ObjectTemplate : public Object
inline const MapObject *ObjectTemplate::object() const
{ return mObject; }

inline void ObjectTemplate::setObject(MapObject *object)
inline void ObjectTemplate::setObject(const MapObject *object)
{
delete mObject;
mObject = object->clone();
mObject->setId(0);
}
Expand Down
25 changes: 25 additions & 0 deletions src/tiled/abstractobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "abstractobjecttool.h"

#include "addremovetileset.h"
#include "changemapobject.h"
#include "map.h"
#include "mapdocument.h"
#include "mapobject.h"
Expand Down Expand Up @@ -207,6 +209,26 @@ void AbstractObjectTool::saveSelectedObject()
mapDocument()->saveSelectedObject(name, groupIndex);
}

void AbstractObjectTool::changeTile()
{
QList<MapObject*> tileObjects;

MapDocument *currentMapDocument = mapDocument();

for (auto object : currentMapDocument->selectedObjects())
if (object->isTileObject())
tileObjects.append(object);

auto changeMapObjectCommand = new ChangeMapObjectsTile(currentMapDocument, tileObjects, tile());

// Make sure the tileset is part of the document
SharedTileset sharedTileset = tile()->tileset()->sharedPointer();
if (!currentMapDocument->map()->tilesets().contains(sharedTileset))
new AddTileset(currentMapDocument, sharedTileset, changeMapObjectCommand);

currentMapDocument->undoStack()->push(changeMapObjectCommand);
}

void AbstractObjectTool::flipHorizontally()
{
mapDocument()->flipSelectedObjects(FlipHorizontally);
Expand Down Expand Up @@ -274,6 +296,9 @@ void AbstractObjectTool::showContextMenu(MapObjectItem *clickedObjectItem,
resetTileSizeAction->setEnabled(std::any_of(selectedObjects.begin(),
selectedObjects.end(),
isResizedTileObject));

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

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 @@ -73,6 +73,7 @@ private slots:
void removeObjects();
void resetTileSize();
void saveSelectedObject();
void changeTile();

void flipHorizontally();
void flipVertically();
Expand Down
1 change: 1 addition & 0 deletions src/tiled/abstracttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ AbstractTool::AbstractTool(const QString &name, const QIcon &icon,
, mIcon(icon)
, mShortcut(shortcut)
, mEnabled(false)
, mTile(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 @@ -36,6 +36,7 @@ class QToolBar;
namespace Tiled {

class Layer;
class Tile;

namespace Internal {

Expand Down Expand Up @@ -91,6 +92,8 @@ class AbstractTool : public QObject
bool isEnabled() const;
void setEnabled(bool enabled);

Tile *tile() 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 @@ -148,6 +151,7 @@ class AbstractTool : public QObject

public slots:
void setMapDocument(MapDocument *mapDocument);
void setTile(Tile *tile);

protected:
/**
Expand Down Expand Up @@ -186,6 +190,7 @@ protected slots:
QString mStatusInfo;
QCursor mCursor;
bool mEnabled;
Tile *mTile;

MapDocument *mMapDocument;
};
Expand Down Expand Up @@ -236,6 +241,16 @@ inline bool AbstractTool::isEnabled() const
return mEnabled;
}

inline Tile *AbstractTool::tile() const
{
return mTile;
}

inline void AbstractTool::setTile(Tile *tile)
{
mTile = tile;
}

} // namespace Internal
} // namespace Tiled

Expand Down
1 change: 0 additions & 1 deletion src/tiled/createobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ CreateObjectTool::CreateObjectTool(QObject *parent)
, mObjectGroupItem(new ObjectGroupItem(mNewMapObjectGroup))
, mNewMapObjectItem(nullptr)
, mOverlayPolygonItem(nullptr)
, mTile(nullptr)
{
mObjectGroupItem->setZValue(10000); // same as the BrushItem
}
Expand Down
8 changes: 0 additions & 8 deletions src/tiled/createobjecttool.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ class CreateObjectTool : public AbstractObjectTool
void mousePressed(QGraphicsSceneMouseEvent *event) override;
void mouseReleased(QGraphicsSceneMouseEvent *event) override;

public slots:
/**
* Sets the tile that will be used when the creation mode is
* CreateTileObjects.
*/
void setTile(Tile *tile) { mTile = tile; }

protected:
virtual void mouseMovedWhileCreatingObject(const QPointF &pos,
Qt::KeyboardModifiers modifiers);
Expand All @@ -73,7 +66,6 @@ public slots:
ObjectGroupItem *mObjectGroupItem;
MapObjectItem *mNewMapObjectItem;
MapObjectItem *mOverlayPolygonItem;
Tile *mTile;
};

} // namespace Internal
Expand Down
6 changes: 3 additions & 3 deletions src/tiled/createtileobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ void CreateTileObjectTool::languageChanged()

MapObject *CreateTileObjectTool::createNewMapObject()
{
if (!mTile)
if (!tile())
return nullptr;

MapObject *newMapObject = new MapObject;
newMapObject->setShape(MapObject::Rectangle);
newMapObject->setCell(Cell(mTile));
newMapObject->setSize(mTile->size());
newMapObject->setCell(Cell(tile()));
newMapObject->setSize(tile()->size());
return newMapObject;
}
14 changes: 14 additions & 0 deletions src/tiled/mapdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,20 @@ void MapDocument::onLayerRemoved(Layer *layer)
emit layerRemoved(layer);
}

void MapDocument::updateTemplateInstances(const MapObject *mapObject)
{
QList<MapObject*> objectList;
for (ObjectGroup *group : mMap->objectGroups()) {
for (auto object : group->objects()) {
if (object->isTemplateInstance() && object->templateObject() == mapObject) {
object->syncWithTemplate();
objectList.append(object);
}
}
}
emit objectsChanged(objectList);
}

void MapDocument::deselectObjects(const QList<MapObject *> &objects)
{
// Unset the current object when it was part of this list of objects
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/mapdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ private slots:
void onLayerAboutToBeRemoved(GroupLayer *groupLayer, int index);
void onLayerRemoved(Layer *layer);

public slots:
void updateTemplateInstances(const MapObject *mapObject);

private:
void deselectObjects(const QList<MapObject*> &objects);
void moveObjectIndex(const MapObject *object, int count);
Expand Down
22 changes: 21 additions & 1 deletion src/tiled/mapeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ MapEditor::MapEditor(QObject *parent)
mMainWindow->addToolBar(mToolSpecificToolBar);

mPropertiesDock = new PropertiesDock(mMainWindow);
mTemplatesDock->setPropertiesDock(mPropertiesDock);
mTileStampsDock = new TileStampsDock(mTileStampManager, mMainWindow);

mMainWindow->addDockWidget(Qt::RightDockWidgetArea, mLayerDock);
Expand Down Expand Up @@ -242,7 +243,8 @@ MapEditor::MapEditor(QObject *parent)

connect(mWidgetStack, &QStackedWidget::currentChanged, this, &MapEditor::currentWidgetChanged);
connect(mToolManager, &ToolManager::statusInfoChanged, this, &MapEditor::updateStatusInfoLabel);
connect(mTilesetDock, &TilesetDock::currentTileChanged, tileObjectsTool, &CreateObjectTool::setTile);
connect(mTilesetDock, &TilesetDock::currentTileChanged, mToolManager, &ToolManager::setTile);
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);
Expand Down Expand Up @@ -281,6 +283,9 @@ MapEditor::MapEditor(QObject *parent)
connect(mToolManager, &ToolManager::selectedToolChanged,
this, &MapEditor::setSelectedTool);

connect(mTemplatesDock, &TemplatesDock::templateEdited,
this, &MapEditor::updateTemplateInstances);

setupQuickStamps();
retranslateUi();
connect(Preferences::instance(), &Preferences::languageChanged, this, &MapEditor::retranslateUi);
Expand Down Expand Up @@ -411,6 +416,12 @@ void MapEditor::setCurrentDocument(Document *document)
mZoomable->setComboBox(mZoomComboBox);
}

connect(mCurrentMapDocument, &MapDocument::currentObjectChanged,
this, [this, mapDocument](){ mPropertiesDock->setDocument(mapDocument); });

connect(mapView, &MapView::focused,
this, [this, mapDocument](){ mPropertiesDock->setDocument(mapDocument); });

mReversingProxyModel->setSourceModel(mapDocument->layerModel());
} else {
mReversingProxyModel->setSourceModel(nullptr);
Expand Down Expand Up @@ -761,6 +772,15 @@ void MapEditor::filesDroppedOnTilesetDock(const QStringList &fileNames)
handleExternalTilesetsAndImages(fileNames, true);
}

void MapEditor::updateTemplateInstances(const MapObject *mapObject)
{
QHashIterator<MapDocument*, MapView*> mapDocumentIterator(mWidgetForMap);
while (mapDocumentIterator.hasNext()) {
mapDocumentIterator.next();
mapDocumentIterator.key()->updateTemplateInstances(mapObject);
}
}

void MapEditor::handleExternalTilesetsAndImages(const QStringList &fileNames,
bool handleImages)
{
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/mapeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class QToolButton;

namespace Tiled {

class MapObject;
class Terrain;

namespace Internal {
Expand Down Expand Up @@ -123,6 +124,8 @@ public slots:
void addExternalTilesets(const QStringList &fileNames);
void filesDroppedOnTilesetDock(const QStringList &fileNames);

void updateTemplateInstances(const MapObject *mapObject);

private slots:
void currentWidgetChanged();

Expand Down
6 changes: 6 additions & 0 deletions src/tiled/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
QGraphicsView::mouseReleaseEvent(event);
}

void MapView::focusInEvent(QFocusEvent *event)
{
Q_UNUSED(event);
emit focused();
}

/**
* Moves the view with the mouse while hand scrolling.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/tiled/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ class MapView : public QGraphicsView
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;

void focusInEvent(QFocusEvent *event) override;

void handlePinchGesture(QPinchGesture *pinch);

void adjustCenterFromMousePosition(QPoint &mousePos);

signals:
void focused();

private slots:
void adjustScale(qreal scale);
void setUseOpenGL(bool useOpenGL);
Expand Down
10 changes: 10 additions & 0 deletions src/tiled/objecttemplatemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ ObjectTemplate *ObjectTemplateModel::toObjectTemplate(const QModelIndex &index)
return nullptr;
}

void ObjectTemplateModel::save(const TemplateGroup *templateGroup) const
{
for (auto document : mTemplateDocuments) {
if (document->templateGroup() == templateGroup) {
document->save(document->fileName());
break;
}
}
}

TemplateGroup *ObjectTemplateModel::toTemplateGroup(const QModelIndex &index) const
{
if (!index.isValid())
Expand Down
1 change: 1 addition & 0 deletions src/tiled/objecttemplatemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ObjectTemplateModel : public QAbstractItemModel
bool addTemplateGroup(TemplateGroup *templateGroup);
bool saveObjectToDocument(MapObject *object, QString name, int documentIndex);
ObjectTemplate *toObjectTemplate(const QModelIndex &index) const;
void save(const TemplateGroup *templateGroup) const;

Qt::ItemFlags flags(const QModelIndex &index) const override;
QStringList mimeTypes() const override;
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/propertiesdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ PropertiesDock::PropertiesDock(QWidget *parent)

void PropertiesDock::setDocument(Document *document)
{
if (mDocument == document)
return;

if (mDocument)
mDocument->disconnect(this);

Expand Down

0 comments on commit 3d7981f

Please sign in to comment.