Condense create and clone to have one history entry

This commit is contained in:
David Rowe 2017-09-14 11:52:36 +12:00
parent d501a8f7cc
commit 6d42e82711
3 changed files with 38 additions and 21 deletions

View file

@ -318,8 +318,7 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {
properties,
CREATE_OFFSET = { x: 0, y: 0.05, z: -0.02 },
INVERSE_HAND_BASIS_ROTATION = Quat.fromVec3Degrees({ x: 0, y: 0, z: -90 }),
entityID,
createdEntities;
entityID;
itemIndex = paletteItemOverlays.indexOf(intersectionOverlayID);
@ -354,8 +353,10 @@ CreatePalette = function (side, leftInputs, rightInputs, uiCommandCallback) {
properties.rotation = Quat.multiply(controlHand.orientation(), INVERSE_HAND_BASIS_ROTATION);
entityID = Entities.addEntity(properties);
if (entityID !== Uuid.NULL) {
createdEntities = [{ entityID: entityID, properties: properties }];
History.push({ deleteEntities: createdEntities }, { createEntities: createdEntities });
History.prePush(
{ deleteEntities: [{ entityID: entityID }] },
{ createEntities: [{ entityID: entityID, properties: properties }] }
);
} else {
Feedback.play(otherSide, Feedback.GENERAL_ERROR);
}

View file

@ -46,7 +46,9 @@ History = (function () {
*/
],
MAX_HISTORY_ITEMS = 100,
undoPosition = -1; // The next history item to undo; the next history item to redo = undoIndex + 1.
undoPosition = -1, // The next history item to undo; the next history item to redo = undoIndex + 1.
undoData = {},
redoData = {};
function doKick(entityID) {
var properties,
@ -69,7 +71,17 @@ History = (function () {
Script.setTimeout(function () { doKick(entityID); }, KICK_DELAY);
}
function push(undoData, redoData) {
function prePush(undo, redo) {
// Stores undo and redo data to include in the next history entry.
undoData = undo;
redoData = redo;
}
function push(undo, redo) {
// Add a history entry.
undoData = Object.merge(undoData, undo);
redoData = Object.merge(redoData, redo);
// Wipe any redo history after current undo position.
if (undoPosition < history.length - 1) {
history.splice(undoPosition + 1, history.length - undoPosition - 1);
@ -82,6 +94,9 @@ History = (function () {
history.push({ undoData: undoData, redoData: redoData });
undoPosition += 1;
undoData = {};
redoData = {};
}
function updateEntityIDs(oldEntityID, newEntityID) {
@ -136,6 +151,13 @@ History = (function () {
if (undoPosition > -1) {
undoData = history[undoPosition].undoData;
if (undoData.createEntities) {
for (i = 0, length = undoData.createEntities.length; i < length; i += 1) {
entityID = Entities.addEntity(undoData.createEntities[i].properties);
updateEntityIDs(undoData.createEntities[i].entityID, entityID);
}
}
if (undoData.setProperties) {
for (i = 0, length = undoData.setProperties.length; i < length; i += 1) {
Entities.editEntity(undoData.setProperties[i].entityID, undoData.setProperties[i].properties);
@ -145,13 +167,6 @@ History = (function () {
}
}
if (undoData.createEntities) {
for (i = 0, length = undoData.createEntities.length; i < length; i += 1) {
entityID = Entities.addEntity(undoData.createEntities[i].properties);
updateEntityIDs(undoData.createEntities[i].entityID, entityID);
}
}
if (undoData.deleteEntities) {
for (i = 0, length = undoData.deleteEntities.length; i < length; i += 1) {
Entities.deleteEntity(undoData.deleteEntities[i].entityID);
@ -172,6 +187,13 @@ History = (function () {
if (undoPosition < history.length - 1) {
redoData = history[undoPosition + 1].redoData;
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);
}
}
if (redoData.setProperties) {
for (i = 0, length = redoData.setProperties.length; i < length; i += 1) {
Entities.editEntity(redoData.setProperties[i].entityID, redoData.setProperties[i].properties);
@ -181,13 +203,6 @@ History = (function () {
}
}
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);
}
}
if (redoData.deleteEntities) {
for (i = 0, length = redoData.deleteEntities.length; i < length; i += 1) {
Entities.deleteEntity(redoData.deleteEntities[i].entityID);
@ -199,6 +214,7 @@ History = (function () {
}
return {
prePush: prePush,
push: push,
hasUndo: hasUndo,
hasRedo: hasRedo,

View file

@ -571,7 +571,7 @@ Selection = function (side) {
rootEntityID = selection[0].id;
// Add history entry.
History.push(
History.prePush(
{ deleteEntities: undoData },
{ createEntities: redoData }
);