Add undo/redo for creating entities

This commit is contained in:
David Rowe 2017-09-12 20:59:03 +12:00
parent 68c4a2f7f6
commit 3bb3e57f71
2 changed files with 70 additions and 2 deletions

View file

@ -317,7 +317,9 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {
isTriggerClicked,
properties,
CREATE_OFFSET = { x: 0, y: 0.05, z: -0.02 },
INVERSE_HAND_BASIS_ROTATION = Quat.fromVec3Degrees({ x: 0, y: 0, z: -90 });
INVERSE_HAND_BASIS_ROTATION = Quat.fromVec3Degrees({ x: 0, y: 0, z: -90 }),
entityID,
createdEntities;
itemIndex = paletteItemOverlays.indexOf(intersectionOverlayID);
@ -350,7 +352,13 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {
Vec3.multiplyQbyV(controlHand.orientation(),
Vec3.sum({ x: 0, y: properties.dimensions.z / 2, z: 0 }, CREATE_OFFSET)));
properties.rotation = Quat.multiply(controlHand.orientation(), INVERSE_HAND_BASIS_ROTATION);
Entities.addEntity(properties);
entityID = Entities.addEntity(properties);
if (entityID !== Uuid.NULL) {
createdEntities = [{ entityID: entityID, properties: properties }];
History.push({ deleteEntities: createdEntities }, { createEntities: createdEntities });
} else {
Feedback.play(otherSide, Feedback.GENERAL_ERROR);
}
// Lower and unhighlight item.
Overlays.editOverlay(paletteItemHoverOverlays[itemIndex], {

View file

@ -63,6 +63,41 @@ History = (function () {
undoPosition += 1;
}
function updateEntityIDs(oldEntityID, newEntityID) {
// Replace oldEntityID value with newEntityID in history.
var i,
length;
function updateEntityIDsInProperty(properties) {
var i,
length;
if (properties) {
for (i = 0, length = properties.length; i < length; i += 1) {
if (properties[i].entityID === oldEntityID) {
properties[i].entityID = newEntityID;
}
if (properties[i].properties && properties[i].properties.parentID === oldEntityID) {
properties[i].properties.parentID = newEntityID;
}
}
}
}
for (i = 0, length = history.length; i < length; i += 1) {
if (history[i].undoData) {
updateEntityIDsInProperty(history[i].undoData.setProperties);
updateEntityIDsInProperty(history[i].undoData.createEntities);
updateEntityIDsInProperty(history[i].undoData.deleteEntities);
}
if (history[i].redoData) {
updateEntityIDsInProperty(history[i].redoData.setProperties);
updateEntityIDsInProperty(history[i].redoData.createEntities);
updateEntityIDsInProperty(history[i].redoData.deleteEntities);
}
}
}
function hasUndo() {
return undoPosition > -1;
}
@ -72,19 +107,44 @@ History = (function () {
}
function undo() {
var undoData,
i,
length;
if (undoPosition > -1) {
undoData = history[undoPosition].undoData;
// TODO
if (undoData.deleteEntities) {
for (i = 0, length = undoData.deleteEntities.length; i < length; i += 1) {
Entities.deleteEntity(undoData.deleteEntities[i].entityID);
}
}
undoPosition -= 1;
}
}
function redo() {
var redoData,
entityID,
i,
length;
if (undoPosition < history.length - 1) {
redoData = history[undoPosition + 1].redoData;
// TODO
if (redoData.createEntities) {
for (i = 0, length = redoData.createEntities.length; i < length; i += 1) {
entityID = Entities.addEntity(redoData.createEntities[i].properties);
updateEntityIDs(redoData.createEntities[i].entityID, entityID);
}
}
undoPosition += 1;
}
}