Add undo/redo for group/ungroup

This commit is contained in:
David Rowe 2017-09-13 15:16:16 +12:00
parent 8c9026c892
commit 003c1c7bc5

View file

@ -83,18 +83,33 @@ Groups = function () {
// Groups all selections into one.
var DYNAMIC_AND_COLLISIONLESS = { dynamic: true, collisionless: true },
rootID,
undoData = [],
redoData = [],
i,
lengthI,
j,
lengthJ;
// If the first group has physics (i.e., root entity is dynamic) make all entities in child groups dynamic and
// collisionless. (Don't need to worry about other groups physics properties because only those of the the first entity
// in the linkset are used by High Fidelity.) See Selection.applyPhysics().
// collisionless. (Don't need to worry about other groups physics properties because only those of the first entity in
// the linkset are used by High Fidelity.) See Selection.applyPhysics().
if (selections[0][0].dynamic) {
for (i = 1, lengthI = selections.length; i < lengthI; i += 1) {
for (j = 0, lengthJ = selections[i].length; j < lengthJ; j += 1) {
undoData.push({
entityID: selections[i][j].id,
properties: {
dynamic: selections[i][j].dynamic,
collisionless: selections[i][j].collisionless
}
});
Entities.editEntity(selections[i][j].id, DYNAMIC_AND_COLLISIONLESS);
selections[i][j].dynamic = true;
selections[i][j].collisionless = true;
redoData.push({
entityID: selections[i][j].id,
properties: DYNAMIC_AND_COLLISIONLESS
});
}
}
}
@ -102,9 +117,17 @@ Groups = function () {
// Make the first entity in the first group the root and link the first entities of all other groups to it.
rootID = rootEntityIDs[0];
for (i = 1, lengthI = rootEntityIDs.length; i < lengthI; i += 1) {
undoData.push({
entityID: rootEntityIDs[i],
properties: { parentID: Uuid.NULL }
});
Entities.editEntity(rootEntityIDs[i], {
parentID: rootID
});
redoData.push({
entityID: rootEntityIDs[i],
properties: { parentID: rootID }
});
}
// Update selection.
@ -114,6 +137,12 @@ Groups = function () {
selections[0] = selections[0].concat(selections[i]);
}
selections.splice(1, selections.length - 1);
// Add history entry.
History.push(
{ setProperties: undoData },
{ setProperties: redoData }
);
}
function ungroup() {
@ -129,6 +158,8 @@ Groups = function () {
hasGroupChildren = false,
isUngroupAll,
NONDYNAMIC_AND_NONCOLLISIONLESS = { dynamic: false, collisionless: false },
undoData = [],
redoData = [],
i,
lengthI,
j,
@ -172,9 +203,17 @@ Groups = function () {
isUngroupAll = hasSoloChildren !== hasGroupChildren;
for (i = childrenIDs.length - 1; i >= 0; i -= 1) {
if (isUngroupAll || childrenIndexIsGroup[i]) {
undoData.push({
entityID: childrenIDs[i],
properties: { parentID: selections[0][childrenIndexes[i]].parentID }
});
Entities.editEntity(childrenIDs[i], {
parentID: Uuid.NULL
});
redoData.push({
entityID: childrenIDs[i],
properties: { parentID: Uuid.NULL }
});
rootEntityIDs.push(childrenIDs[i]);
selections[0][childrenIndexes[i]].parentID = Uuid.NULL;
selections.push(selections[0].splice(childrenIndexes[i], childrenIndexes[i + 1] - childrenIndexes[i]));
@ -186,10 +225,29 @@ Groups = function () {
if (selections[0][0].dynamic) {
for (i = 1, lengthI = selections.length; i < lengthI; i += 1) {
for (j = 0, lengthJ = selections[i].length; j < lengthJ; j += 1) {
undoData.push({
entityID: selections[i][j].id,
properties: {
dynamic: selections[i][j].dynamic,
collisionless: selections[i][j].collisionless
}
});
Entities.editEntity(selections[i][j].id, NONDYNAMIC_AND_NONCOLLISIONLESS);
selections[i][j].dynamic = false;
selections[i][j].collisionless = false;
redoData.push({
entityID: selections[i][j].id,
properties: NONDYNAMIC_AND_NONCOLLISIONLESS
});
}
}
}
// Add history entry.
History.push(
{ setProperties: undoData },
{ setProperties: redoData }
);
}
function clear() {