entity export/import involving constraints now works

This commit is contained in:
Seth Alves 2017-05-08 15:44:31 -07:00
parent c8cdad5b4e
commit 94dfbc81f3
11 changed files with 84 additions and 95 deletions

View file

@ -1529,6 +1529,44 @@ void EntityTree::pruneTree() {
recurseTreeWithOperator(&theOperator);
}
QByteArray EntityTree::remapActionDataIDs(QByteArray actionData, QHash<EntityItemID, EntityItemID>& map) {
QDataStream serializedActionsStream(actionData);
QVector<QByteArray> serializedActions;
serializedActionsStream >> serializedActions;
auto actionFactory = DependencyManager::get<EntityDynamicFactoryInterface>();
QHash<QUuid, EntityDynamicPointer> remappedActions;
foreach(QByteArray serializedAction, serializedActions) {
QDataStream serializedActionStream(serializedAction);
EntityDynamicType actionType;
QUuid oldActionID;
serializedActionStream >> actionType;
serializedActionStream >> oldActionID;
EntityDynamicPointer action = actionFactory->factoryBA(nullptr, serializedAction);
if (action) {
action->remapIDs(map);
remappedActions[action->getID()] = action;
}
}
QVector<QByteArray> remappedSerializedActions;
QHash<QUuid, EntityDynamicPointer>::const_iterator i = remappedActions.begin();
while (i != remappedActions.end()) {
EntityDynamicPointer action = i.value();
QByteArray bytesForAction = action->serialize();
remappedSerializedActions << bytesForAction;
i++;
}
QByteArray result;
QDataStream remappedSerializedActionsStream(&result, QIODevice::WriteOnly);
remappedSerializedActionsStream << remappedSerializedActions;
return result;
}
QVector<EntityItemID> EntityTree::sendEntities(EntityEditPacketSender* packetSender, EntityTreePointer localTree,
float x, float y, float z) {
SendEntitiesOperationArgs args;
@ -1552,7 +1590,7 @@ bool EntityTree::sendEntitiesOperation(OctreeElementPointer element, void* extra
SendEntitiesOperationArgs* args = static_cast<SendEntitiesOperationArgs*>(extraData);
EntityTreeElementPointer entityTreeElement = std::static_pointer_cast<EntityTreeElement>(element);
auto getMapped = [&](EntityItemID oldID) {
auto getMapped = [&args](EntityItemID oldID) {
if (oldID.isNull()) {
return EntityItemID();
}
@ -1565,7 +1603,8 @@ bool EntityTree::sendEntitiesOperation(OctreeElementPointer element, void* extra
}
};
entityTreeElement->forEachEntity([&](EntityItemPointer item) {
entityTreeElement->forEachEntity([&args, &getMapped, &element](EntityItemPointer item) {
EntityItemID oldID = item->getEntityItemID();
EntityItemID newID = getMapped(oldID);
@ -1604,43 +1643,7 @@ bool EntityTree::sendEntitiesOperation(OctreeElementPointer element, void* extra
QByteArray actionData = properties.getActionData();
if (!actionData.isEmpty()) {
QDataStream serializedActionsStream(actionData);
QVector<QByteArray> serializedActions;
serializedActionsStream >> serializedActions;
auto actionFactory = DependencyManager::get<EntityDynamicFactoryInterface>();
QHash<QUuid, EntityDynamicPointer> remappedActions;
foreach(QByteArray serializedAction, serializedActions) {
QDataStream serializedActionStream(serializedAction);
EntityDynamicType actionType;
QUuid oldActionID;
serializedActionStream >> actionType;
serializedActionStream >> oldActionID;
QUuid actionID = QUuid::createUuid(); // give the action a new ID
(*args->map)[oldActionID] = actionID;
EntityDynamicPointer action = actionFactory->factoryBA(nullptr, serializedAction);
if (action) {
action->remapIDs(*args->map);
remappedActions[actionID] = action;
}
}
QVector<QByteArray> remappedSerializedActions;
QHash<QUuid, EntityDynamicPointer>::const_iterator i = remappedActions.begin();
while (i != remappedActions.end()) {
const QUuid id = i.key();
EntityDynamicPointer action = remappedActions[id];
QByteArray bytesForAction = action->serialize();
remappedSerializedActions << bytesForAction;
i++;
}
QByteArray result;
QDataStream remappedSerializedActionsStream(&result, QIODevice::WriteOnly);
remappedSerializedActionsStream << remappedSerializedActions;
properties.setActionData(result);
properties.setActionData(remapActionDataIDs(actionData, *args->map));
}
// set creation time to "now" for imported entities
@ -1664,19 +1667,6 @@ bool EntityTree::sendEntitiesOperation(OctreeElementPointer element, void* extra
return newID;
});
QHash<EntityItemID, EntityItemID>::iterator i = (*args->map).begin();
while (i != (*args->map).end()) {
EntityItemID newID = i.value();
if (args->otherTree->findEntityByEntityItemID(newID)) {
i++;
} else {
EntityItemID oldID = i.key();
i = (*args->map).erase(i);
}
}
return true;
}

View file

@ -205,6 +205,8 @@ public:
virtual void dumpTree() override;
virtual void pruneTree() override;
static QByteArray remapActionDataIDs(QByteArray actionData, QHash<EntityItemID, EntityItemID>& map);
QVector<EntityItemID> sendEntities(EntityEditPacketSender* packetSender, EntityTreePointer localTree,
float x, float y, float z);

View file

@ -40,7 +40,7 @@ QList<btRigidBody*> ObjectConstraintBallSocket::getRigidBodies() {
result += getRigidBody();
QUuid otherEntityID;
withReadLock([&]{
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
});
if (!otherEntityID.isNull()) {
result += getOtherRigidBody(otherEntityID);
@ -76,7 +76,7 @@ btTypedConstraint* ObjectConstraintBallSocket::getConstraint() {
withReadLock([&]{
constraint = static_cast<btPoint2PointConstraint*>(_constraint);
pivotInA = _pivotInA;
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
pivotInB = _pivotInB;
});
if (constraint) {
@ -136,7 +136,7 @@ bool ObjectConstraintBallSocket::updateArguments(QVariantMap arguments) {
otherEntityID = QUuid(EntityDynamicInterface::extractStringArgument("ball-socket constraint",
arguments, "otherEntityID", ok, false));
if (!ok) {
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
}
ok = true;
@ -147,7 +147,7 @@ bool ObjectConstraintBallSocket::updateArguments(QVariantMap arguments) {
if (somethingChanged ||
pivotInA != _pivotInA ||
otherEntityID != _otherEntityID ||
otherEntityID != _otherID ||
pivotInB != _pivotInB) {
// something changed
needUpdate = true;
@ -157,7 +157,7 @@ bool ObjectConstraintBallSocket::updateArguments(QVariantMap arguments) {
if (needUpdate) {
withWriteLock([&] {
_pivotInA = pivotInA;
_otherEntityID = otherEntityID;
_otherID = otherEntityID;
_pivotInB = pivotInB;
_active = true;
@ -180,7 +180,7 @@ QVariantMap ObjectConstraintBallSocket::getArguments() {
withReadLock([&] {
if (_constraint) {
arguments["pivot"] = glmToQMap(_pivotInA);
arguments["otherEntityID"] = _otherEntityID;
arguments["otherEntityID"] = _otherID;
arguments["otherPivot"] = glmToQMap(_pivotInB);
}
});
@ -200,7 +200,7 @@ QByteArray ObjectConstraintBallSocket::serialize() const {
dataStream << _tag;
dataStream << _pivotInA;
dataStream << _otherEntityID;
dataStream << _otherID;
dataStream << _pivotInB;
});
@ -232,7 +232,7 @@ void ObjectConstraintBallSocket::deserialize(QByteArray serializedArguments) {
dataStream >> _tag;
dataStream >> _pivotInA;
dataStream >> _otherEntityID;
dataStream >> _otherID;
dataStream >> _pivotInB;
_active = true;

View file

@ -38,8 +38,6 @@ protected:
void updateBallSocket();
glm::vec3 _pivotInA;
EntityItemID _otherEntityID;
glm::vec3 _pivotInB;
};

View file

@ -40,7 +40,7 @@ QList<btRigidBody*> ObjectConstraintConeTwist::getRigidBodies() {
result += getRigidBody();
QUuid otherEntityID;
withReadLock([&]{
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
});
if (!otherEntityID.isNull()) {
result += getOtherRigidBody(otherEntityID);
@ -95,7 +95,7 @@ btTypedConstraint* ObjectConstraintConeTwist::getConstraint() {
constraint = static_cast<btConeTwistConstraint*>(_constraint);
pivotInA = _pivotInA;
axisInA = _axisInA;
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
pivotInB = _pivotInB;
axisInB = _axisInB;
});
@ -180,7 +180,7 @@ bool ObjectConstraintConeTwist::updateArguments(QVariantMap arguments) {
otherEntityID = QUuid(EntityDynamicInterface::extractStringArgument("coneTwist constraint",
arguments, "otherEntityID", ok, false));
if (!ok) {
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
}
ok = true;
@ -235,7 +235,7 @@ bool ObjectConstraintConeTwist::updateArguments(QVariantMap arguments) {
if (somethingChanged ||
pivotInA != _pivotInA ||
axisInA != _axisInA ||
otherEntityID != _otherEntityID ||
otherEntityID != _otherID ||
pivotInB != _pivotInB ||
axisInB != _axisInB ||
swingSpan1 != _swingSpan1 ||
@ -253,7 +253,7 @@ bool ObjectConstraintConeTwist::updateArguments(QVariantMap arguments) {
withWriteLock([&] {
_pivotInA = pivotInA;
_axisInA = axisInA;
_otherEntityID = otherEntityID;
_otherID = otherEntityID;
_pivotInB = pivotInB;
_axisInB = axisInB;
_swingSpan1 = swingSpan1;
@ -284,7 +284,7 @@ QVariantMap ObjectConstraintConeTwist::getArguments() {
if (_constraint) {
arguments["pivot"] = glmToQMap(_pivotInA);
arguments["axis"] = glmToQMap(_axisInA);
arguments["otherEntityID"] = _otherEntityID;
arguments["otherEntityID"] = _otherID;
arguments["otherPivot"] = glmToQMap(_pivotInB);
arguments["otherAxis"] = glmToQMap(_axisInB);
arguments["swingSpan1"] = _swingSpan1;
@ -312,7 +312,7 @@ QByteArray ObjectConstraintConeTwist::serialize() const {
dataStream << _pivotInA;
dataStream << _axisInA;
dataStream << _otherEntityID;
dataStream << _otherID;
dataStream << _pivotInB;
dataStream << _axisInB;
dataStream << _swingSpan1;
@ -352,7 +352,7 @@ void ObjectConstraintConeTwist::deserialize(QByteArray serializedArguments) {
dataStream >> _pivotInA;
dataStream >> _axisInA;
dataStream >> _otherEntityID;
dataStream >> _otherID;
dataStream >> _pivotInB;
dataStream >> _axisInB;
dataStream >> _swingSpan1;

View file

@ -40,7 +40,6 @@ protected:
glm::vec3 _pivotInA;
glm::vec3 _axisInA;
EntityItemID _otherEntityID;
glm::vec3 _pivotInB;
glm::vec3 _axisInB;

View file

@ -40,7 +40,7 @@ QList<btRigidBody*> ObjectConstraintHinge::getRigidBodies() {
result += getRigidBody();
QUuid otherEntityID;
withReadLock([&]{
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
});
if (!otherEntityID.isNull()) {
result += getOtherRigidBody(otherEntityID);
@ -90,7 +90,7 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() {
constraint = static_cast<btHingeConstraint*>(_constraint);
pivotInA = _pivotInA;
axisInA = _axisInA;
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
pivotInB = _pivotInB;
axisInB = _axisInB;
});
@ -182,7 +182,7 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
otherEntityID = QUuid(EntityDynamicInterface::extractStringArgument("hinge constraint",
arguments, "otherEntityID", ok, false));
if (!ok) {
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
}
ok = true;
@ -231,7 +231,7 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
if (somethingChanged ||
pivotInA != _pivotInA ||
axisInA != _axisInA ||
otherEntityID != _otherEntityID ||
otherEntityID != _otherID ||
pivotInB != _pivotInB ||
axisInB != _axisInB ||
low != _low ||
@ -248,7 +248,7 @@ bool ObjectConstraintHinge::updateArguments(QVariantMap arguments) {
withWriteLock([&] {
_pivotInA = pivotInA;
_axisInA = axisInA;
_otherEntityID = otherEntityID;
_otherID = otherEntityID;
_pivotInB = pivotInB;
_axisInB = axisInB;
_low = low;
@ -277,7 +277,7 @@ QVariantMap ObjectConstraintHinge::getArguments() {
withReadLock([&] {
arguments["pivot"] = glmToQMap(_pivotInA);
arguments["axis"] = glmToQMap(_axisInA);
arguments["otherEntityID"] = _otherEntityID;
arguments["otherEntityID"] = _otherID;
arguments["otherPivot"] = glmToQMap(_pivotInB);
arguments["otherAxis"] = glmToQMap(_axisInB);
arguments["low"] = _low;
@ -305,7 +305,7 @@ QByteArray ObjectConstraintHinge::serialize() const {
withReadLock([&] {
dataStream << _pivotInA;
dataStream << _axisInA;
dataStream << _otherEntityID;
dataStream << _otherID;
dataStream << _pivotInB;
dataStream << _axisInB;
dataStream << _low;
@ -342,7 +342,7 @@ void ObjectConstraintHinge::deserialize(QByteArray serializedArguments) {
withWriteLock([&] {
dataStream >> _pivotInA;
dataStream >> _axisInA;
dataStream >> _otherEntityID;
dataStream >> _otherID;
dataStream >> _pivotInB;
dataStream >> _axisInB;
dataStream >> _low;

View file

@ -40,7 +40,6 @@ protected:
glm::vec3 _pivotInA;
glm::vec3 _axisInA;
EntityItemID _otherEntityID;
glm::vec3 _pivotInB;
glm::vec3 _axisInB;

View file

@ -34,7 +34,7 @@ QList<btRigidBody*> ObjectConstraintSlider::getRigidBodies() {
result += getRigidBody();
QUuid otherEntityID;
withReadLock([&]{
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
});
if (!otherEntityID.isNull()) {
result += getOtherRigidBody(otherEntityID);
@ -77,7 +77,7 @@ btTypedConstraint* ObjectConstraintSlider::getConstraint() {
constraint = static_cast<btSliderConstraint*>(_constraint);
pointInA = _pointInA;
axisInA = _axisInA;
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
pointInB = _pointInB;
axisInB = _axisInB;
});
@ -160,7 +160,7 @@ bool ObjectConstraintSlider::updateArguments(QVariantMap arguments) {
otherEntityID = QUuid(EntityDynamicInterface::extractStringArgument("slider constraint",
arguments, "otherEntityID", ok, false));
if (!ok) {
otherEntityID = _otherEntityID;
otherEntityID = _otherID;
}
ok = true;
@ -202,7 +202,7 @@ bool ObjectConstraintSlider::updateArguments(QVariantMap arguments) {
if (somethingChanged ||
pointInA != _pointInA ||
axisInA != _axisInA ||
otherEntityID != _otherEntityID ||
otherEntityID != _otherID ||
pointInB != _pointInB ||
axisInB != _axisInB ||
linearLow != _linearLow ||
@ -218,7 +218,7 @@ bool ObjectConstraintSlider::updateArguments(QVariantMap arguments) {
withWriteLock([&] {
_pointInA = pointInA;
_axisInA = axisInA;
_otherEntityID = otherEntityID;
_otherID = otherEntityID;
_pointInB = pointInB;
_axisInB = axisInB;
_linearLow = linearLow;
@ -247,7 +247,7 @@ QVariantMap ObjectConstraintSlider::getArguments() {
if (_constraint) {
arguments["point"] = glmToQMap(_pointInA);
arguments["axis"] = glmToQMap(_axisInA);
arguments["otherEntityID"] = _otherEntityID;
arguments["otherEntityID"] = _otherID;
arguments["otherPoint"] = glmToQMap(_pointInB);
arguments["otherAxis"] = glmToQMap(_axisInB);
arguments["linearLow"] = _linearLow;
@ -275,7 +275,7 @@ QByteArray ObjectConstraintSlider::serialize() const {
dataStream << _pointInA;
dataStream << _axisInA;
dataStream << _otherEntityID;
dataStream << _otherID;
dataStream << _pointInB;
dataStream << _axisInB;
dataStream << _linearLow;
@ -313,7 +313,7 @@ void ObjectConstraintSlider::deserialize(QByteArray serializedArguments) {
dataStream >> _pointInA;
dataStream >> _axisInA;
dataStream >> _otherEntityID;
dataStream >> _otherID;
dataStream >> _pointInB;
dataStream >> _axisInB;
dataStream >> _linearLow;

View file

@ -40,7 +40,6 @@ protected:
glm::vec3 _pointInA;
glm::vec3 _axisInA;
EntityItemID _otherEntityID;
glm::vec3 _pointInB;
glm::vec3 _axisInB;

View file

@ -26,15 +26,17 @@ ObjectDynamic::~ObjectDynamic() {
void ObjectDynamic::remapIDs(QHash<EntityItemID, EntityItemID>& map) {
withWriteLock([&]{
if (!map.contains(_id)) {
map[_id] = QUuid::createUuid();
if (!_id.isNull()) {
// just force our ID to something new -- action IDs don't go into the map
_id = QUuid::createUuid();
}
_id = map[_id];
if (!map.contains(_otherID)) {
map[_otherID] = QUuid::createUuid();
if (!_otherID.isNull()) {
if (!map.contains(_otherID)) {
map.insert(_otherID, QUuid::createUuid());
}
_otherID = map.value(_otherID);
}
_otherID = map[_otherID];
});
}