2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CMapEditManager.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-01-06 22:30:12 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CMapEditManager.h"
|
|
|
|
|
2022-09-06 18:47:41 +02:00
|
|
|
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "../mapObjects/CObjectClassesHandler.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2013-08-17 15:46:48 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
2015-01-03 01:29:42 +02:00
|
|
|
#include "CDrawRoadsOperation.h"
|
2015-12-02 21:05:10 +02:00
|
|
|
#include "../mapping/CMap.h"
|
2022-09-06 18:47:41 +02:00
|
|
|
#include "CMapOperation.h"
|
2015-01-03 04:03:44 +02:00
|
|
|
|
2022-09-05 18:27:09 +02:00
|
|
|
CMapUndoManager::CMapUndoManager() :
|
|
|
|
undoRedoLimit(10),
|
|
|
|
undoCallback([](bool, bool) {})
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2022-09-05 18:27:09 +02:00
|
|
|
//TODO: unlimited undo
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::undo()
|
|
|
|
{
|
|
|
|
doOperation(undoStack, redoStack, true);
|
2022-09-05 18:27:09 +02:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::redo()
|
|
|
|
{
|
|
|
|
doOperation(redoStack, undoStack, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::clearAll()
|
|
|
|
{
|
|
|
|
undoStack.clear();
|
|
|
|
redoStack.clear();
|
2022-09-05 18:27:09 +02:00
|
|
|
onUndoRedo();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CMapUndoManager::getUndoRedoLimit() const
|
|
|
|
{
|
|
|
|
return undoRedoLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::setUndoRedoLimit(int value)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(value >= 0);
|
2013-04-19 14:43:11 +03:00
|
|
|
undoStack.resize(std::min(undoStack.size(), static_cast<TStack::size_type>(value)));
|
|
|
|
redoStack.resize(std::min(redoStack.size(), static_cast<TStack::size_type>(value)));
|
2022-09-06 08:05:34 +02:00
|
|
|
onUndoRedo();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CMapOperation * CMapUndoManager::peekRedo() const
|
|
|
|
{
|
|
|
|
return peek(redoStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CMapOperation * CMapUndoManager::peekUndo() const
|
|
|
|
{
|
|
|
|
return peek(undoStack);
|
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void CMapUndoManager::addOperation(std::unique_ptr<CMapOperation> && operation)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
undoStack.push_front(std::move(operation));
|
|
|
|
if(undoStack.size() > undoRedoLimit) undoStack.pop_back();
|
|
|
|
redoStack.clear();
|
2022-09-06 08:05:34 +02:00
|
|
|
onUndoRedo();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::doOperation(TStack & fromStack, TStack & toStack, bool doUndo)
|
|
|
|
{
|
|
|
|
if(fromStack.empty()) return;
|
|
|
|
auto & operation = fromStack.front();
|
|
|
|
if(doUndo)
|
|
|
|
{
|
|
|
|
operation->undo();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
operation->redo();
|
|
|
|
}
|
|
|
|
toStack.push_front(std::move(operation));
|
|
|
|
fromStack.pop_front();
|
2022-09-05 18:27:09 +02:00
|
|
|
onUndoRedo();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CMapOperation * CMapUndoManager::peek(const TStack & stack) const
|
|
|
|
{
|
|
|
|
if(stack.empty()) return nullptr;
|
|
|
|
return stack.front().get();
|
|
|
|
}
|
|
|
|
|
2022-09-05 18:27:09 +02:00
|
|
|
void CMapUndoManager::onUndoRedo()
|
|
|
|
{
|
|
|
|
//true if there's anything on the stack
|
|
|
|
undoCallback((bool)peekUndo(), (bool)peekRedo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::setUndoCallback(std::function<void(bool, bool)> functor)
|
|
|
|
{
|
|
|
|
undoCallback = functor;
|
|
|
|
onUndoRedo(); //inform immediately
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
CMapEditManager::CMapEditManager(CMap * map)
|
2013-04-29 18:51:39 +03:00
|
|
|
: map(map), terrainSel(map), objectSel(map)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CMap * CMapEditManager::getMap()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
return map;
|
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
void CMapEditManager::clearTerrain(CRandomGenerator * gen)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
|
|
|
execute(make_unique<CClearTerrainOperation>(map, gen ? gen : &(this->gen)));
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2022-06-20 16:39:50 +02:00
|
|
|
void CMapEditManager::drawTerrain(Terrain terType, CRandomGenerator * gen)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
execute(make_unique<CDrawTerrainOperation>(map, terrainSel, terType, gen ? gen : &(this->gen)));
|
|
|
|
terrainSel.clearSelection();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2022-06-20 16:39:50 +02:00
|
|
|
void CMapEditManager::drawRoad(const std::string & roadType, CRandomGenerator* gen)
|
2015-01-03 01:29:42 +02:00
|
|
|
{
|
|
|
|
execute(make_unique<CDrawRoadsOperation>(map, terrainSel, roadType, gen ? gen : &(this->gen)));
|
|
|
|
terrainSel.clearSelection();
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
void CMapEditManager::drawRiver(const std::string & riverType, CRandomGenerator* gen)
|
|
|
|
{
|
|
|
|
execute(make_unique<CDrawRiversOperation>(map, terrainSel, riverType, gen ? gen : &(this->gen)));
|
|
|
|
terrainSel.clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 01:29:42 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapEditManager::insertObject(CGObjectInstance * obj)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
execute(make_unique<CInsertObjectOperation>(map, obj));
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 23:04:28 +02:00
|
|
|
void CMapEditManager::moveObject(CGObjectInstance * obj, const int3 & pos)
|
|
|
|
{
|
|
|
|
execute(make_unique<CMoveObjectOperation>(map, obj, pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapEditManager::removeObject(CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
execute(make_unique<CRemoveObjectOperation>(map, obj));
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:54:48 +02:00
|
|
|
void CMapEditManager::removeObjects(std::set<CGObjectInstance*> & objects)
|
|
|
|
{
|
|
|
|
auto composedOperation = make_unique<CComposedOperation>(map);
|
|
|
|
for (auto obj : objects)
|
|
|
|
{
|
|
|
|
composedOperation->addOperation(make_unique<CRemoveObjectOperation>(map, obj));
|
|
|
|
}
|
|
|
|
execute(std::move(composedOperation));
|
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
void CMapEditManager::execute(std::unique_ptr<CMapOperation> && operation)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
operation->execute();
|
|
|
|
undoManager.addOperation(std::move(operation));
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CTerrainSelection & CMapEditManager::getTerrainSelection()
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
return terrainSel;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CObjectSelection & CMapEditManager::getObjectSelection()
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
return objectSel;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMapUndoManager & CMapEditManager::getUndoManager()
|
|
|
|
{
|
|
|
|
return undoManager;
|
2022-09-06 18:47:41 +02:00
|
|
|
}
|