mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-17 01:32:21 +02:00
Map objects now use shared_ptr (editor)
This commit is contained in:
@ -698,7 +698,7 @@ void MainWindow::addGroupIntoCatalog(const QString & groupName, bool useCustomNa
|
|||||||
}
|
}
|
||||||
|
|
||||||
//create object to extract name
|
//create object to extract name
|
||||||
std::unique_ptr<CGObjectInstance> temporaryObj(factory->create(nullptr, templ));
|
auto temporaryObj(factory->create(nullptr, templ));
|
||||||
QString translated = useCustomName ? QString::fromStdString(temporaryObj->getObjectName().c_str()) : subGroupName;
|
QString translated = useCustomName ? QString::fromStdString(temporaryObj->getObjectName().c_str()) : subGroupName;
|
||||||
itemType->setText(translated);
|
itemType->setText(translated);
|
||||||
|
|
||||||
@ -1401,8 +1401,8 @@ void MainWindow::on_actionLock_triggered()
|
|||||||
{
|
{
|
||||||
for(auto obj : controller.map()->objects)
|
for(auto obj : controller.map()->objects)
|
||||||
{
|
{
|
||||||
controller.scene(mapLevel)->selectionObjectsView.setLockObject(obj, true);
|
controller.scene(mapLevel)->selectionObjectsView.setLockObject(obj.get(), true);
|
||||||
controller.scene(mapLevel)->objectsView.setLockObject(obj, true);
|
controller.scene(mapLevel)->objectsView.setLockObject(obj.get(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -110,19 +110,16 @@ void MapController::repairMap(CMap * map)
|
|||||||
|
|
||||||
//fix owners for objects
|
//fix owners for objects
|
||||||
auto allImpactedObjects(map->objects);
|
auto allImpactedObjects(map->objects);
|
||||||
allImpactedObjects.insert(allImpactedObjects.end(), map->predefinedHeroes.begin(), map->predefinedHeroes.end());
|
|
||||||
|
for (const auto & hero : map->predefinedHeroes)
|
||||||
|
allImpactedObjects.push_back(hero);
|
||||||
|
|
||||||
for(auto obj : allImpactedObjects)
|
for(auto obj : allImpactedObjects)
|
||||||
{
|
{
|
||||||
//fix flags
|
//fix flags
|
||||||
if(obj->getOwner() == PlayerColor::UNFLAGGABLE)
|
if(obj->getOwner() == PlayerColor::UNFLAGGABLE)
|
||||||
{
|
{
|
||||||
if(dynamic_cast<CGMine*>(obj.get()) ||
|
if(obj->asOwnable())
|
||||||
dynamic_cast<CGDwelling*>(obj.get()) ||
|
|
||||||
dynamic_cast<CGTownInstance*>(obj.get()) ||
|
|
||||||
dynamic_cast<CGGarrison*>(obj.get()) ||
|
|
||||||
dynamic_cast<CGShipyard*>(obj.get()) ||
|
|
||||||
dynamic_cast<FlaggableMapObject*>(obj.get()) ||
|
|
||||||
dynamic_cast<CGHeroInstance*>(obj.get()))
|
|
||||||
obj->tempOwner = PlayerColor::NEUTRAL;
|
obj->tempOwner = PlayerColor::NEUTRAL;
|
||||||
}
|
}
|
||||||
//fix hero instance
|
//fix hero instance
|
||||||
@ -364,9 +361,9 @@ void MapController::pasteFromClipboard(int level)
|
|||||||
QStringList errors;
|
QStringList errors;
|
||||||
for(auto & objUniquePtr : _clipboard)
|
for(auto & objUniquePtr : _clipboard)
|
||||||
{
|
{
|
||||||
auto * obj = CMemorySerializer::deepCopy(*objUniquePtr).release();
|
auto obj = CMemorySerializer::deepCopyShared(*objUniquePtr);
|
||||||
QString errorMsg;
|
QString errorMsg;
|
||||||
if (!canPlaceObject(level, obj, errorMsg))
|
if (!canPlaceObject(level, obj.get(), errorMsg))
|
||||||
{
|
{
|
||||||
errors.push_back(std::move(errorMsg));
|
errors.push_back(std::move(errorMsg));
|
||||||
continue;
|
continue;
|
||||||
@ -376,10 +373,10 @@ void MapController::pasteFromClipboard(int level)
|
|||||||
obj->pos = newPos;
|
obj->pos = newPos;
|
||||||
obj->pos.z = level;
|
obj->pos.z = level;
|
||||||
|
|
||||||
Initializer init(*this, obj, defaultPlayer);
|
Initializer init(*this, obj.get(), defaultPlayer);
|
||||||
_map->getEditManager()->insertObject(obj);
|
_map->getEditManager()->insertObject(obj);
|
||||||
_scenes[level]->selectionObjectsView.selectObject(obj);
|
_scenes[level]->selectionObjectsView.selectObject(obj.get());
|
||||||
_mapHandler->invalidate(obj);
|
_mapHandler->invalidate(obj.get());
|
||||||
}
|
}
|
||||||
if(!errors.isEmpty())
|
if(!errors.isEmpty())
|
||||||
QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
|
QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
|
||||||
@ -397,8 +394,7 @@ bool MapController::discardObject(int level) const
|
|||||||
_scenes[level]->selectionObjectsView.clear();
|
_scenes[level]->selectionObjectsView.clear();
|
||||||
if(_scenes[level]->selectionObjectsView.newObject)
|
if(_scenes[level]->selectionObjectsView.newObject)
|
||||||
{
|
{
|
||||||
delete _scenes[level]->selectionObjectsView.newObject;
|
_scenes[level]->selectionObjectsView.newObject.reset();
|
||||||
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
|
||||||
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
||||||
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
|
||||||
_scenes[level]->selectionObjectsView.draw();
|
_scenes[level]->selectionObjectsView.draw();
|
||||||
@ -407,7 +403,7 @@ bool MapController::discardObject(int level) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MapController::createObject(int level, CGObjectInstance * obj) const
|
void MapController::createObject(int level, std::shared_ptr<CGObjectInstance> obj) const
|
||||||
{
|
{
|
||||||
_scenes[level]->selectionObjectsView.newObject = obj;
|
_scenes[level]->selectionObjectsView.newObject = obj;
|
||||||
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
_scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
||||||
@ -438,10 +434,10 @@ void MapController::commitObstacleFill(int level)
|
|||||||
|
|
||||||
for(auto & sel : _obstaclePainters)
|
for(auto & sel : _obstaclePainters)
|
||||||
{
|
{
|
||||||
for(auto * o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
|
for(auto o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
|
||||||
{
|
{
|
||||||
_mapHandler->invalidate(o);
|
_mapHandler->invalidate(o.get());
|
||||||
_scenes[level]->objectsView.setDirty(o);
|
_scenes[level]->objectsView.setDirty(o.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -509,7 +505,7 @@ void MapController::commitObjectShift(int level)
|
|||||||
|
|
||||||
void MapController::commitObjectCreate(int level)
|
void MapController::commitObjectCreate(int level)
|
||||||
{
|
{
|
||||||
auto * newObj = _scenes[level]->selectionObjectsView.newObject;
|
auto newObj = _scenes[level]->selectionObjectsView.newObject;
|
||||||
if(!newObj)
|
if(!newObj)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -521,11 +517,11 @@ void MapController::commitObjectCreate(int level)
|
|||||||
|
|
||||||
newObj->pos = pos;
|
newObj->pos = pos;
|
||||||
|
|
||||||
Initializer init(*this, newObj, defaultPlayer);
|
Initializer init(*this, newObj.get(), defaultPlayer);
|
||||||
|
|
||||||
_map->getEditManager()->insertObject(newObj);
|
_map->getEditManager()->insertObject(newObj);
|
||||||
_mapHandler->invalidate(newObj);
|
_mapHandler->invalidate(newObj.get());
|
||||||
_scenes[level]->objectsView.setDirty(newObj);
|
_scenes[level]->objectsView.setDirty(newObj.get());
|
||||||
|
|
||||||
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
_scenes[level]->selectionObjectsView.newObject = nullptr;
|
||||||
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
|
||||||
|
@ -59,7 +59,7 @@ public:
|
|||||||
void pasteFromClipboard(int level);
|
void pasteFromClipboard(int level);
|
||||||
|
|
||||||
bool discardObject(int level) const;
|
bool discardObject(int level) const;
|
||||||
void createObject(int level, CGObjectInstance * obj) const;
|
void createObject(int level, std::shared_ptr<CGObjectInstance> obj) const;
|
||||||
bool canPlaceObject(int level, CGObjectInstance * obj, QString & error) const;
|
bool canPlaceObject(int level, CGObjectInstance * obj, QString & error) const;
|
||||||
|
|
||||||
static ModCompatibilityInfo modAssessmentAll();
|
static ModCompatibilityInfo modAssessmentAll();
|
||||||
|
@ -259,9 +259,9 @@ void MapHandler::initObjectRects()
|
|||||||
tileObjects.resize(map->width * map->height * (map->twoLevel ? 2 : 1));
|
tileObjects.resize(map->width * map->height * (map->twoLevel ? 2 : 1));
|
||||||
|
|
||||||
//initializing objects / rects
|
//initializing objects / rects
|
||||||
for(const CGObjectInstance * elem : map->objects)
|
for(const auto & elem : map->objects)
|
||||||
{
|
{
|
||||||
addObject(elem);
|
addObject(elem.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto & tt : tileObjects)
|
for(auto & tt : tileObjects)
|
||||||
|
@ -89,7 +89,7 @@ void AbstractSettings::initialize(MapController & c)
|
|||||||
std::string AbstractSettings::getTownName(const CMap & map, int objectIdx)
|
std::string AbstractSettings::getTownName(const CMap & map, int objectIdx)
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
if(auto town = dynamic_cast<const CGTownInstance*>(map.objects[objectIdx].get()))
|
if(auto town = dynamic_cast<const CGTownInstance*>(map.objects.at(objectIdx).get()))
|
||||||
{
|
{
|
||||||
name = town->getNameTranslated();
|
name = town->getNameTranslated();
|
||||||
|
|
||||||
@ -102,7 +102,7 @@ std::string AbstractSettings::getTownName(const CMap & map, int objectIdx)
|
|||||||
std::string AbstractSettings::getHeroName(const CMap & map, int objectIdx)
|
std::string AbstractSettings::getHeroName(const CMap & map, int objectIdx)
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
if(auto hero = dynamic_cast<const CGHeroInstance*>(map.objects[objectIdx].get()))
|
if(auto hero = dynamic_cast<const CGHeroInstance*>(map.objects.at(objectIdx).get()))
|
||||||
{
|
{
|
||||||
name = hero->getNameTranslated();
|
name = hero->getNameTranslated();
|
||||||
}
|
}
|
||||||
@ -112,7 +112,7 @@ std::string AbstractSettings::getHeroName(const CMap & map, int objectIdx)
|
|||||||
std::string AbstractSettings::getMonsterName(const CMap & map, int objectIdx)
|
std::string AbstractSettings::getMonsterName(const CMap & map, int objectIdx)
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
if(auto monster = dynamic_cast<const CGCreature*>(map.objects[objectIdx].get()))
|
if(auto monster = dynamic_cast<const CGCreature*>(map.objects.at(objectIdx).get()))
|
||||||
{
|
{
|
||||||
name = boost::str(boost::format("%1% at %2%") % monster->getObjectName() % monster->anchorPos().toString());
|
name = boost::str(boost::format("%1% at %2%") % monster->getObjectName() % monster->anchorPos().toString());
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ public:
|
|||||||
std::vector<int> result;
|
std::vector<int> result;
|
||||||
for(int i = 0; i < map.objects.size(); ++i)
|
for(int i = 0; i < map.objects.size(); ++i)
|
||||||
{
|
{
|
||||||
if(auto obj = dynamic_cast<T*>(map.objects[i].get()))
|
if(auto obj = dynamic_cast<T*>(map.objects.at(i).get()))
|
||||||
result.push_back(i);
|
result.push_back(i);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -57,7 +57,7 @@ public:
|
|||||||
{
|
{
|
||||||
for(int i = 0; i < map.objects.size(); ++i)
|
for(int i = 0; i < map.objects.size(); ++i)
|
||||||
{
|
{
|
||||||
if(auto obj = dynamic_cast<T*>(map.objects[i].get()))
|
if(auto obj = dynamic_cast<T*>(map.objects.at(i).get()))
|
||||||
{
|
{
|
||||||
if(obj->pos == pos)
|
if(obj->pos == pos)
|
||||||
return i;
|
return i;
|
||||||
|
@ -331,7 +331,7 @@ void LoseConditions::onObjectPicked(const CGObjectInstance * obj)
|
|||||||
for(int i = 0; i < loseTypeWidget->count(); ++i)
|
for(int i = 0; i < loseTypeWidget->count(); ++i)
|
||||||
{
|
{
|
||||||
auto data = controller->map()->objects.at(loseTypeWidget->itemData(i).toInt());
|
auto data = controller->map()->objects.at(loseTypeWidget->itemData(i).toInt());
|
||||||
if(data == obj)
|
if(data.get() == obj)
|
||||||
{
|
{
|
||||||
loseTypeWidget->setCurrentIndex(i);
|
loseTypeWidget->setCurrentIndex(i);
|
||||||
break;
|
break;
|
||||||
|
@ -63,7 +63,7 @@ TimedEvent::TimedEvent(MapController & c, QListWidgetItem * t, QWidget *parent)
|
|||||||
auto id = ObjectInstanceID(idAsVariant.toInt());
|
auto id = ObjectInstanceID(idAsVariant.toInt());
|
||||||
auto obj = controller.map()->objects[id];
|
auto obj = controller.map()->objects[id];
|
||||||
if(obj)
|
if(obj)
|
||||||
insertObjectToDelete(obj);
|
insertObjectToDelete(obj.get());
|
||||||
}
|
}
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
@ -559,7 +559,7 @@ void VictoryConditions::onObjectPicked(const CGObjectInstance * obj)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto data = controller->map()->objects.at(w->itemData(i).toInt());
|
auto data = controller->map()->objects.at(w->itemData(i).toInt());
|
||||||
if(data == obj)
|
if(data.get() == obj)
|
||||||
{
|
{
|
||||||
w->setCurrentIndex(i);
|
w->setCurrentIndex(i);
|
||||||
break;
|
break;
|
||||||
|
@ -386,7 +386,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
|
|||||||
sc->selectionTerrainView.clear();
|
sc->selectionTerrainView.clear();
|
||||||
sc->selectionTerrainView.draw();
|
sc->selectionTerrainView.draw();
|
||||||
|
|
||||||
if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject))
|
if(sc->selectionObjectsView.newObject && sc->selectionObjectsView.isSelected(sc->selectionObjectsView.newObject.get()))
|
||||||
{
|
{
|
||||||
if(event->button() == Qt::RightButton)
|
if(event->button() == Qt::RightButton)
|
||||||
controller->discardObject(sc->level);
|
controller->discardObject(sc->level);
|
||||||
@ -614,11 +614,11 @@ void MapView::dropEvent(QDropEvent * event)
|
|||||||
if(sc->selectionObjectsView.newObject)
|
if(sc->selectionObjectsView.newObject)
|
||||||
{
|
{
|
||||||
QString errorMsg;
|
QString errorMsg;
|
||||||
if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject, errorMsg))
|
if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject.get(), errorMsg))
|
||||||
{
|
{
|
||||||
auto * obj = sc->selectionObjectsView.newObject;
|
auto obj = sc->selectionObjectsView.newObject;
|
||||||
controller->commitObjectCreate(sc->level);
|
controller->commitObjectCreate(sc->level);
|
||||||
openObjectProperties(obj, false);
|
openObjectProperties(obj.get(), false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -643,7 +643,7 @@ void MapView::dragMoveEvent(QDragMoveEvent * event)
|
|||||||
if(sc->selectionObjectsView.newObject)
|
if(sc->selectionObjectsView.newObject)
|
||||||
{
|
{
|
||||||
sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
|
sc->selectionObjectsView.shift = QPoint(tile.x, tile.y);
|
||||||
sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject);
|
sc->selectionObjectsView.selectObject(sc->selectionObjectsView.newObject.get());
|
||||||
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
|
||||||
sc->selectionObjectsView.draw();
|
sc->selectionObjectsView.draw();
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ PlayerParams::PlayerParams(MapController & ctrl, int playerId, QWidget *parent)
|
|||||||
int foundMainTown = -1;
|
int foundMainTown = -1;
|
||||||
for(int i = 0, townIndex = 0; i < controller.map()->objects.size(); ++i)
|
for(int i = 0, townIndex = 0; i < controller.map()->objects.size(); ++i)
|
||||||
{
|
{
|
||||||
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects[i].get()))
|
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects.at(i).get()))
|
||||||
{
|
{
|
||||||
auto * ctown = town->getTown();
|
auto * ctown = town->getTown();
|
||||||
|
|
||||||
@ -223,7 +223,7 @@ void PlayerParams::onTownPicked(const CGObjectInstance * obj)
|
|||||||
for(int i = 0; i < ui->mainTown->count(); ++i)
|
for(int i = 0; i < ui->mainTown->count(); ++i)
|
||||||
{
|
{
|
||||||
auto town = controller.map()->objects.at(ui->mainTown->itemData(i).toInt());
|
auto town = controller.map()->objects.at(ui->mainTown->itemData(i).toInt());
|
||||||
if(town == obj)
|
if(town.get() == obj)
|
||||||
{
|
{
|
||||||
ui->mainTown->setCurrentIndex(i);
|
ui->mainTown->setCurrentIndex(i);
|
||||||
break;
|
break;
|
||||||
|
@ -455,8 +455,7 @@ void SelectionObjectsLayer::update()
|
|||||||
selectedObjects.clear();
|
selectedObjects.clear();
|
||||||
onSelection();
|
onSelection();
|
||||||
shift = QPoint();
|
shift = QPoint();
|
||||||
delete newObject;
|
newObject.reset();
|
||||||
newObject = nullptr;
|
|
||||||
|
|
||||||
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
pixmap.reset(new QPixmap(map->width * 32, map->height * 32));
|
||||||
//pixmap->fill(QColor(0, 0, 0, 0));
|
//pixmap->fill(QColor(0, 0, 0, 0));
|
||||||
@ -477,7 +476,7 @@ void SelectionObjectsLayer::draw()
|
|||||||
|
|
||||||
for(auto * obj : selectedObjects)
|
for(auto * obj : selectedObjects)
|
||||||
{
|
{
|
||||||
if(obj != newObject)
|
if(obj != newObject.get())
|
||||||
{
|
{
|
||||||
QRect bbox(obj->anchorPos().x, obj->anchorPos().y, 1, 1);
|
QRect bbox(obj->anchorPos().x, obj->anchorPos().y, 1, 1);
|
||||||
for(auto & t : obj->getBlockedPos())
|
for(auto & t : obj->getBlockedPos())
|
||||||
|
@ -191,7 +191,7 @@ public:
|
|||||||
void unlockAll();
|
void unlockAll();
|
||||||
|
|
||||||
QPoint shift;
|
QPoint shift;
|
||||||
CGObjectInstance * newObject;
|
std::shared_ptr<CGObjectInstance> newObject;
|
||||||
//FIXME: magic number
|
//FIXME: magic number
|
||||||
SelectionMode selectionMode = SelectionMode::NOTHING;
|
SelectionMode selectionMode = SelectionMode::NOTHING;
|
||||||
|
|
||||||
|
@ -90,13 +90,9 @@ std::set<Validator::Issue> Validator::validate(const CMap * map)
|
|||||||
//owners for objects
|
//owners for objects
|
||||||
if(o->getOwner() == PlayerColor::UNFLAGGABLE)
|
if(o->getOwner() == PlayerColor::UNFLAGGABLE)
|
||||||
{
|
{
|
||||||
if(dynamic_cast<CGMine *>(o.get()) ||
|
if(o->asOwnable())
|
||||||
dynamic_cast<CGDwelling *>(o.get()) ||
|
|
||||||
dynamic_cast<CGTownInstance *>(o.get()) ||
|
|
||||||
dynamic_cast<CGGarrison *>(o.get()) ||
|
|
||||||
dynamic_cast<CGHeroInstance *>(o.get()))
|
|
||||||
{
|
{
|
||||||
issues.insert({ tr("Armored instance %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true });
|
issues.insert({ tr("Ownable object %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
|
if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
|
||||||
|
Reference in New Issue
Block a user