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"
|
|
|
|
|
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"
|
2022-09-17 13:04:01 +02:00
|
|
|
#include "CMapOperation.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
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) {})
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-24 01:12:25 +02:00
|
|
|
CMapUndoManager::~CMapUndoManager() = default;
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
void CMapUndoManager::undo()
|
|
|
|
{
|
|
|
|
doOperation(undoStack, redoStack, true);
|
2022-09-17 13:04:01 +02:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::redo()
|
|
|
|
{
|
|
|
|
doOperation(redoStack, undoStack, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapUndoManager::clearAll()
|
|
|
|
{
|
2024-06-24 20:06:50 +02:00
|
|
|
//FIXME: Will crash if an object was added twice to actions
|
2013-04-19 14:43:11 +03:00
|
|
|
undoStack.clear();
|
|
|
|
redoStack.clear();
|
2022-09-17 13:04:01 +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-17 13:04:01 +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-17 13:04:01 +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-17 13:04:01 +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-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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-07-13 17:46:55 +02:00
|
|
|
CMapEditManager::~CMapEditManager() = default;
|
|
|
|
|
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
|
|
|
|
2024-08-12 20:26:30 +02:00
|
|
|
void CMapEditManager::clearTerrain(vstd::RNG * customGen)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2024-08-12 20:26:30 +02:00
|
|
|
execute(std::make_unique<CClearTerrainOperation>(map, customGen ? customGen : gen.get()));
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2024-08-12 20:26:30 +02:00
|
|
|
void CMapEditManager::drawTerrain(TerrainId terType, int decorationsPercentage, vstd::RNG * customGen)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2024-08-12 20:26:30 +02:00
|
|
|
execute(std::make_unique<CDrawTerrainOperation>(map, terrainSel, terType, decorationsPercentage, customGen ? customGen : gen.get()));
|
2013-04-29 18:51:39 +03:00
|
|
|
terrainSel.clearSelection();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2024-08-12 20:26:30 +02:00
|
|
|
void CMapEditManager::drawRoad(RoadId roadType, vstd::RNG* customGen)
|
2015-01-03 01:29:42 +02:00
|
|
|
{
|
2024-08-12 20:26:30 +02:00
|
|
|
execute(std::make_unique<CDrawRoadsOperation>(map, terrainSel, roadType, customGen ? customGen : gen.get()));
|
2015-01-03 01:29:42 +02:00
|
|
|
terrainSel.clearSelection();
|
|
|
|
}
|
|
|
|
|
2024-08-12 20:26:30 +02:00
|
|
|
void CMapEditManager::drawRiver(RiverId riverType, vstd::RNG* customGen)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2024-08-12 20:26:30 +02:00
|
|
|
execute(std::make_unique<CDrawRiversOperation>(map, terrainSel, riverType, customGen ? customGen : gen.get()));
|
2022-08-09 07:54:32 +02:00
|
|
|
terrainSel.clearSelection();
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CMapEditManager::insertObject(CGObjectInstance * obj)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
execute(std::make_unique<CInsertObjectOperation>(map, obj));
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMapEditManager::insertObjects(std::set<CGObjectInstance*>& objects)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
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)
|
2013-04-25 19:09:48 +03:00
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
composedOperation->addOperation(std::make_unique<CInsertObjectOperation>(map, obj));
|
2013-04-25 19:09:48 +03:00
|
|
|
}
|
2022-09-17 13:04:01 +02:00
|
|
|
execute(std::move(composedOperation));
|
2013-04-25 19:09:48 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMapEditManager::moveObject(CGObjectInstance * obj, const int3 & pos)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
execute(std::make_unique<CMoveObjectOperation>(map, obj, pos));
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMapEditManager::removeObject(CGObjectInstance * obj)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
execute(std::make_unique<CRemoveObjectOperation>(map, obj));
|
2013-04-29 18:51:39 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMapEditManager::removeObjects(std::set<CGObjectInstance*> & objects)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
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)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
composedOperation->addOperation(std::make_unique<CRemoveObjectOperation>(map, obj));
|
2015-03-22 21:32:22 +02:00
|
|
|
}
|
2022-09-17 13:04:01 +02:00
|
|
|
execute(std::move(composedOperation));
|
2015-03-22 21:32:22 +02:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
void CMapEditManager::execute(std::unique_ptr<CMapOperation> && operation)
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2022-09-17 13:04:01 +02:00
|
|
|
operation->execute();
|
|
|
|
undoManager.addOperation(std::move(operation));
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
CTerrainSelection & CMapEditManager::getTerrainSelection()
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2022-09-17 13:04:01 +02:00
|
|
|
return terrainSel;
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
CObjectSelection & CMapEditManager::getObjectSelection()
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2022-09-17 13:04:01 +02:00
|
|
|
return objectSel;
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
CMapUndoManager & CMapEditManager::getUndoManager()
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2022-09-17 13:04:01 +02:00
|
|
|
return undoManager;
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|