1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/lib/mapping/CMapEditManager.cpp

207 lines
4.8 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"
#include "../mapObjects/CGHeroInstance.h"
#include "../VCMI_Lib.h"
#include "CDrawRoadsOperation.h"
2022-09-17 13:04:01 +02:00
#include "CMapOperation.h"
#include <vstd/RNG.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) {})
{
}
2023-05-24 01:12:25 +02:00
CMapUndoManager::~CMapUndoManager() = default;
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()
{
//FIXME: Will crash if an object was added twice to actions
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)
{
}
CMapEditManager::~CMapEditManager() = default;
CMap * CMapEditManager::getMap()
{
return map;
}
void CMapEditManager::clearTerrain(vstd::RNG * gen)
{
execute(std::make_unique<CClearTerrainOperation>(map, gen ? gen : this->gen.get()));
}
void CMapEditManager::drawTerrain(TerrainId terType, int decorationsPercentage, vstd::RNG * gen)
{
execute(std::make_unique<CDrawTerrainOperation>(map, terrainSel, terType, decorationsPercentage, gen ? gen : this->gen.get()));
terrainSel.clearSelection();
}
void CMapEditManager::drawRoad(RoadId roadType, vstd::RNG* gen)
{
execute(std::make_unique<CDrawRoadsOperation>(map, terrainSel, roadType, gen ? gen : this->gen.get()));
terrainSel.clearSelection();
}
void CMapEditManager::drawRiver(RiverId riverType, vstd::RNG* gen)
{
execute(std::make_unique<CDrawRiversOperation>(map, terrainSel, riverType, gen ? gen : this->gen.get()));
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