1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-04 09:42:40 +02:00
vcmi/lib/mapping/CMapEditManager.cpp

206 lines
4.6 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
#include "CMapEditManager.h"
2022-09-17 13:04:01 +02:00
#include "../mapObjects/CObjectClassesHandler.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../VCMI_Lib.h"
#include "CDrawRoadsOperation.h"
#include "../mapping/CMap.h"
2022-09-17 13:04:01 +02:00
#include "CMapOperation.h"
VCMI_LIB_NAMESPACE_BEGIN
2022-09-17 13:04:01 +02:00
CMapUndoManager::CMapUndoManager() :
undoRedoLimit(100000), //not sure if we ever need to bother about undo limit
undoCallback([](bool, bool) {})
{
}
void CMapUndoManager::undo()
{
doOperation(undoStack, redoStack, true);
2022-09-17 13:04:01 +02:00
}
void CMapUndoManager::redo()
{
doOperation(redoStack, undoStack, false);
}
void CMapUndoManager::clearAll()
{
undoStack.clear();
redoStack.clear();
2022-09-17 13:04:01 +02:00
onUndoRedo();
}
int CMapUndoManager::getUndoRedoLimit() const
{
return undoRedoLimit;
}
void CMapUndoManager::setUndoRedoLimit(int value)
{
assert(value >= 0);
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-17 13:04:01 +02:00
onUndoRedo();
}
const CMapOperation * CMapUndoManager::peekRedo() const
{
return peek(redoStack);
}
const CMapOperation * CMapUndoManager::peekUndo() const
{
return peek(undoStack);
}
void CMapUndoManager::addOperation(std::unique_ptr<CMapOperation> && operation)
{
undoStack.push_front(std::move(operation));
if(undoStack.size() > undoRedoLimit) undoStack.pop_back();
redoStack.clear();
2022-09-17 13:04:01 +02:00
onUndoRedo();
}
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-17 13:04:01 +02:00
onUndoRedo();
}
const CMapOperation * CMapUndoManager::peek(const TStack & stack) const
{
if(stack.empty()) return nullptr;
return stack.front().get();
}
2022-09-17 13:04:01 +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)
{
2023-02-11 18:30:06 +02:00
undoCallback = std::move(functor);
2022-09-17 13:04:01 +02:00
onUndoRedo(); //inform immediately
}
CMapEditManager::CMapEditManager(CMap * map)
: map(map), terrainSel(map), objectSel(map)
{
}
CMap * CMapEditManager::getMap()
{
return map;
}
void CMapEditManager::clearTerrain(CRandomGenerator * gen)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CClearTerrainOperation>(map, gen ? gen : &(this->gen)));
}
2022-09-29 11:44:46 +02:00
void CMapEditManager::drawTerrain(TerrainId terType, CRandomGenerator * gen)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CDrawTerrainOperation>(map, terrainSel, terType, gen ? gen : &(this->gen)));
terrainSel.clearSelection();
}
2022-09-29 11:44:46 +02:00
void CMapEditManager::drawRoad(RoadId roadType, CRandomGenerator* gen)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CDrawRoadsOperation>(map, terrainSel, roadType, gen ? gen : &(this->gen)));
terrainSel.clearSelection();
}
2022-09-29 11:44:46 +02:00
void CMapEditManager::drawRiver(RiverId riverType, CRandomGenerator* gen)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CDrawRiversOperation>(map, terrainSel, riverType, gen ? gen : &(this->gen)));
terrainSel.clearSelection();
}
void CMapEditManager::insertObject(CGObjectInstance * obj)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CInsertObjectOperation>(map, obj));
}
2022-09-17 13:04:01 +02:00
void CMapEditManager::insertObjects(std::set<CGObjectInstance*>& objects)
{
2022-12-07 23:36:20 +02:00
auto composedOperation = std::make_unique<CComposedOperation>(map);
2023-02-11 18:30:06 +02:00
for(auto * obj : objects)
{
2022-12-07 23:36:20 +02:00
composedOperation->addOperation(std::make_unique<CInsertObjectOperation>(map, obj));
}
2022-09-17 13:04:01 +02:00
execute(std::move(composedOperation));
}
2022-09-17 13:04:01 +02:00
void CMapEditManager::moveObject(CGObjectInstance * obj, const int3 & pos)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CMoveObjectOperation>(map, obj, pos));
}
2022-09-17 13:04:01 +02:00
void CMapEditManager::removeObject(CGObjectInstance * obj)
{
2022-12-07 23:36:20 +02:00
execute(std::make_unique<CRemoveObjectOperation>(map, obj));
}
2022-09-17 13:04:01 +02:00
void CMapEditManager::removeObjects(std::set<CGObjectInstance*> & objects)
{
2022-12-07 23:36:20 +02:00
auto composedOperation = std::make_unique<CComposedOperation>(map);
2023-02-11 18:30:06 +02:00
for(auto * obj : objects)
{
2022-12-07 23:36:20 +02:00
composedOperation->addOperation(std::make_unique<CRemoveObjectOperation>(map, obj));
}
2022-09-17 13:04:01 +02:00
execute(std::move(composedOperation));
}
2022-09-17 13:04:01 +02:00
void CMapEditManager::execute(std::unique_ptr<CMapOperation> && operation)
{
2022-09-17 13:04:01 +02:00
operation->execute();
undoManager.addOperation(std::move(operation));
}
2022-09-17 13:04:01 +02:00
CTerrainSelection & CMapEditManager::getTerrainSelection()
{
2022-09-17 13:04:01 +02:00
return terrainSel;
}
2022-09-17 13:04:01 +02:00
CObjectSelection & CMapEditManager::getObjectSelection()
{
2022-09-17 13:04:01 +02:00
return objectSel;
}
2022-09-17 13:04:01 +02:00
CMapUndoManager & CMapEditManager::getUndoManager()
{
2022-09-17 13:04:01 +02:00
return undoManager;
}
VCMI_LIB_NAMESPACE_END