Skip to content

Commit

Permalink
Added context menu entry for tileset collision masks to copy to all s…
Browse files Browse the repository at this point in the history
…elected tiles

Issue mapeditor#1322
  • Loading branch information
robinmacharg authored and bjorn committed Jul 21, 2020
1 parent 390f383 commit 7b5b013
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/tiled/abstractobjecttool.cpp
Expand Up @@ -253,6 +253,37 @@ void AbstractObjectTool::removeObjects()
mapDocument()->removeObjects(mapDocument()->selectedObjects());
}

/**
* @brief AbstractObjectTool::applyCollisionsToSelection
*
* Add the selected collision masks for the currently selected tile - the last selected tile - to
* all selected tiles.
*/
void AbstractObjectTool::applyCollisionsToSelection()
{
QList<MapObject*> selectedObjects = mapDocument()->selectedObjects();

if (auto document = DocumentManager::instance()->currentDocument()) {
if (auto tilesetDocument = qobject_cast<TilesetDocument*>(document)) {
QList<Tile *>selectedTiles = tilesetDocument->selectedTiles();

// Add each collision object to each selected tile, as long as the tile is not the current one
for (Tile* tile : selectedTiles) {

// Create a group for collision objects if none exists
if (!tile->objectGroup())
tile->setObjectGroup(std::make_unique<ObjectGroup>());

for (MapObject* object : selectedObjects) {
MapObject* newObject = new MapObject;
newObject->copyPropertiesFrom(object);
tile->objectGroup()->addObject(newObject);
}
}
}
}
}

void AbstractObjectTool::resetTileSize()
{
QList<QUndoCommand*> commands;
Expand Down Expand Up @@ -463,6 +494,11 @@ void AbstractObjectTool::showContextMenu(MapObject *clickedObject,
QAction *removeAction = menu.addAction(tr("Remove %n Object(s)", "", selectedObjects.size()),
this, &AbstractObjectTool::removeObjects);

// Allow the currently selected collision mask to be applied to all selected tiles in the tileset editor
if (auto document = DocumentManager::instance()->currentDocument())
if (document->type() == Document::TilesetDocumentType)
menu.addAction(tr("Apply Collision(s) to Selection"), this, &AbstractObjectTool::applyCollisionsToSelection);

duplicateAction->setIcon(QIcon(QLatin1String(":/images/16/stock-duplicate-16.png")));
removeAction->setIcon(QIcon(QLatin1String(":/images/16/edit-delete.png")));

Expand Down
1 change: 1 addition & 0 deletions src/tiled/abstractobjecttool.h
Expand Up @@ -77,6 +77,7 @@ class AbstractObjectTool : public AbstractTool
private:
void duplicateObjects();
void removeObjects();
void applyCollisionsToSelection();
void resetTileSize();
void saveSelectedObject();
void detachSelectedObjects();
Expand Down

0 comments on commit 7b5b013

Please sign in to comment.