forked from mapeditor/tiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform changes to the world through the undo commands
This also makes sure the UI adjusts immediately to the changes, since the WorldManager::worldsChanged signal is emitted. Also added some checks on the function parameters.
- Loading branch information
Showing
8 changed files
with
236 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* changeworld.cpp | ||
* Copyright 2019, Nils Kuebler <[email protected]> | ||
* Copyright 2024, Thorbjørn Lindeijer <[email protected]> | ||
* | ||
* 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 "changeworld.h" | ||
|
||
#include "documentmanager.h" | ||
#include "worldmanager.h" | ||
|
||
#include <QCoreApplication> | ||
|
||
namespace Tiled { | ||
|
||
AddMapCommand::AddMapCommand(const QString &worldName, const QString &mapName, const QRect &rect) | ||
: QUndoCommand(QCoreApplication::translate("Undo Commands", "Add Map to World")) | ||
, mWorldName(worldName) | ||
, mMapName(mapName) | ||
, mRect(rect) | ||
{ | ||
} | ||
|
||
void AddMapCommand::undo() | ||
{ | ||
WorldManager::instance().removeMap(mMapName); | ||
} | ||
|
||
void AddMapCommand::redo() | ||
{ | ||
qDebug() << Q_FUNC_INFO; | ||
WorldManager::instance().addMap(mWorldName, mMapName, mRect); | ||
} | ||
|
||
|
||
RemoveMapCommand::RemoveMapCommand(const QString &mapName) | ||
: QUndoCommand(QCoreApplication::translate("Undo Commands", "Remove Map from World")) | ||
, mMapName(mapName) | ||
{ | ||
const WorldManager &manager = WorldManager::instance(); | ||
const World *world = manager.worldForMap(mMapName); | ||
mPreviousRect = world->mapRect(mMapName); | ||
mWorldName = world->fileName; | ||
} | ||
|
||
void RemoveMapCommand::undo() | ||
{ | ||
WorldManager::instance().addMap(mWorldName, mMapName, mPreviousRect); | ||
} | ||
|
||
void RemoveMapCommand::redo() | ||
{ | ||
// ensure we're switching to a different map in case the current map is removed | ||
DocumentManager *manager = DocumentManager::instance(); | ||
if (manager->currentDocument() && manager->currentDocument()->fileName() == mMapName) { | ||
const World *world = WorldManager::instance().worldForMap(mMapName); | ||
for (const World::MapEntry &entry : world->allMaps()) { | ||
if (entry.fileName != mMapName) { | ||
manager->switchToDocument(entry.fileName); | ||
break; | ||
} | ||
} | ||
} | ||
WorldManager::instance().removeMap(mMapName); | ||
} | ||
|
||
|
||
SetMapRectCommand::SetMapRectCommand(const QString &mapName, QRect rect) | ||
: QUndoCommand(QCoreApplication::translate("Undo Commands", "Move Map")) | ||
, mMapName(mapName) | ||
, mRect(rect) | ||
{ | ||
const WorldManager &manager = WorldManager::instance(); | ||
mPreviousRect = manager.worldForMap(mMapName)->mapRect(mMapName); | ||
} | ||
|
||
void SetMapRectCommand::undo() | ||
{ | ||
WorldManager::instance().setMapRect(mMapName, mPreviousRect); | ||
} | ||
|
||
void SetMapRectCommand::redo() | ||
{ | ||
WorldManager::instance().setMapRect(mMapName, mRect); | ||
} | ||
|
||
} // namespace Tiled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* changeworld.h | ||
* Copyright 2019, Nils Kuebler <[email protected]> | ||
* Copyright 2024, Thorbjørn Lindeijer <[email protected]> | ||
* | ||
* 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/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QRect> | ||
#include <QUndoCommand> | ||
|
||
namespace Tiled { | ||
|
||
class AddMapCommand : public QUndoCommand | ||
{ | ||
public: | ||
AddMapCommand(const QString &worldName, const QString &mapName, const QRect &rect); | ||
|
||
void undo() override; | ||
void redo() override; | ||
|
||
private: | ||
QString mWorldName; | ||
QString mMapName; | ||
QRect mRect; | ||
}; | ||
|
||
class RemoveMapCommand : public QUndoCommand | ||
{ | ||
public: | ||
RemoveMapCommand(const QString &mapName); | ||
|
||
void undo() override; | ||
void redo() override; | ||
|
||
private: | ||
QString mWorldName; | ||
QString mMapName; | ||
QRect mPreviousRect; | ||
}; | ||
|
||
class SetMapRectCommand : public QUndoCommand | ||
{ | ||
public: | ||
SetMapRectCommand(const QString &mapName, QRect rect); | ||
|
||
void undo() override; | ||
void redo() override; | ||
|
||
private: | ||
QString mMapName; | ||
QRect mRect; | ||
QRect mPreviousRect; | ||
}; | ||
|
||
} // namespace Tiled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.