Skip to content

Commit

Permalink
Improved positioning when adding maps to world via context menu
Browse files Browse the repository at this point in the history
Previously the map would get added to the world at its origin, where it
would likely overlap with existing maps in the world. Now the map is
positioned to the right of the last map that was added to the world,
where it is less likely to overlap.

Also improved the calculation of the map size by using the renderer
instead of only handling the orthogonal map case.
  • Loading branch information
bjorn committed Jul 14, 2020
1 parent a41b332 commit 1e308cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/tiled/abstractworldtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void AbstractWorldTool::showContextMenu(QGraphicsSceneMouseEvent *event)
menu.addAction(tr("Add \"%1\" to World \"%2\"")
.arg(currentDocument->displayName())
.arg(world->displayName()),
this, [this, fileName = world->fileName] { addToWorld(fileName); });
this, [=] { addToWorld(world); });
}
}

Expand Down Expand Up @@ -353,15 +353,19 @@ void AbstractWorldTool::removeFromWorld(const QString &mapFileName)
undoStack()->push(new RemoveMapCommand(mapFileName));
}

void AbstractWorldTool::addToWorld(const QString &worldFileName)
void AbstractWorldTool::addToWorld(const World *world)
{
MapDocument *document = mapDocument();
QSize size = document->map()->size();
size.setWidth(size.width() * document->map()->tileWidth());
size.setHeight(size.height() * document->map()->tileHeight());
const QRect rect = QRect(QPoint(0, 0), size);
QUndoStack *undoStack = DocumentManager::instance()->ensureWorldDocument(worldFileName)->undoStack();
undoStack->push(new AddMapCommand(worldFileName, document->fileName(), rect));
QRect rect = document->renderer()->mapBoundingRect();

// Position the map alongside the last map by default
if (!world->maps.isEmpty()) {
const QRect &lastWorldRect = world->maps.last().rect;
rect.moveTo(lastWorldRect.topRight());
}

QUndoStack *undoStack = DocumentManager::instance()->ensureWorldDocument(world->fileName)->undoStack();
undoStack->push(new AddMapCommand(world->fileName, document->fileName(), rect));
}

QUndoStack *AbstractWorldTool::undoStack()
Expand Down Expand Up @@ -391,7 +395,7 @@ void AbstractWorldTool::populateToolBar(QToolBar *toolBar)
addToWorldMenu->addAction(tr("Add \"%1\" to World \"%2\"")
.arg(mapDocument()->displayName())
.arg(world->displayName()),
this, [this, fileName = world->fileName] { addToWorld(fileName); });
this, [=] { addToWorld(world); });
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/tiled/abstractworldtool.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AbstractWorldTool : public AbstractTool
void addAnotherMapToWorld(QPoint insertPos);
void removeCurrentMapFromWorld();
void removeFromWorld(const QString &mapFileName);
void addToWorld(const QString &worldFileName);
void addToWorld(const World *world);

QPoint snapPoint(QPoint point, MapDocument *document) const;

Expand Down

0 comments on commit 1e308cc

Please sign in to comment.