reconstruct cloneIDs lists on start, read/write locks on clone ID functions, fix removing last cloneable

This commit is contained in:
David Back 2018-05-17 10:28:48 -07:00
parent 900b373c96
commit 3730cbe36c
3 changed files with 46 additions and 11 deletions

View file

@ -3096,18 +3096,36 @@ void EntityItem::setCloneOriginID(const QUuid& value) {
}
bool EntityItem::addCloneID(const QUuid& cloneID) {
if (!_cloneIDs.contains(cloneID)) {
_cloneIDs.append(cloneID);
return true;
}
withWriteLock([&] {
if (!_cloneIDs.contains(cloneID)) {
_cloneIDs.append(cloneID);
return true;
}
});
return false;
}
bool EntityItem::removeCloneID(const QUuid& cloneID) {
int index = _cloneIDs.indexOf(cloneID);
if (index > 0) {
_cloneIDs.removeAt(index);
return true;
}
withWriteLock([&] {
int index = _cloneIDs.indexOf(cloneID);
if (index >= 0) {
_cloneIDs.removeAt(index);
return true;
}
});
return false;
}
const QList<QUuid> EntityItem::getCloneIDs() const {
QList<QUuid> result;
withReadLock([&] {
result = _cloneIDs;
});
return result;
}
void EntityItem::setCloneIDs(const QList<QUuid>& cloneIDs) {
withWriteLock([&] {
_cloneIDs = cloneIDs;
});
}

View file

@ -509,7 +509,8 @@ public:
bool addCloneID(const QUuid& cloneID);
bool removeCloneID(const QUuid& cloneID);
const QList<QUuid>& getCloneIDs() const { return _cloneIDs; }
const QList<QUuid> getCloneIDs() const;
void setCloneIDs(const QList<QUuid>& cloneIDs);
signals:
void requestRenderUpdate();

View file

@ -631,7 +631,7 @@ void EntityTree::cleanupCloneIDs(const EntityItemID& entityID) {
EntityItemPointer entity = findEntityByEntityItemID(entityID);
if (entity) {
// remove clone ID from it's clone origin's clone ID list if clone origin exists
const QUuid cloneOriginID = entity->getCloneOriginID();
const QUuid& cloneOriginID = entity->getCloneOriginID();
if (!cloneOriginID.isNull()) {
EntityItemPointer cloneOrigin = findEntityByID(cloneOriginID);
if (cloneOrigin) {
@ -2385,6 +2385,8 @@ bool EntityTree::readFromMap(QVariantMap& map) {
return false;
}
QMap<QUuid, QList<QUuid>> cloneIDs;
bool success = true;
foreach (QVariant entityVariant, entitiesQList) {
// QVariantMap --> QScriptValue --> EntityItemProperties --> Entity
@ -2477,6 +2479,20 @@ bool EntityTree::readFromMap(QVariantMap& map) {
qCDebug(entities) << "adding Entity failed:" << entityItemID << properties.getType();
success = false;
}
const QUuid& cloneOriginID = entity->getCloneOriginID();
if (!cloneOriginID.isNull()) {
cloneIDs[cloneOriginID].push_back(entity->getEntityItemID());
}
}
for (auto iter = cloneIDs.begin(); iter != cloneIDs.end(); ++iter) {
const QUuid& entityID = iter.key();
const QList<QUuid>& cloneIDs = iter.value();
EntityItemPointer entity = findEntityByID(entityID);
if (entity) {
entity->setCloneIDs(cloneIDs);
}
}
return success;