Merge pull request #13686 from huffman/fix/undo-create-entity

Fix added entities not being undoable in Create
This commit is contained in:
John Conklin II 2018-08-01 13:11:17 -07:00 committed by GitHub
commit f02e91806c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -352,7 +352,12 @@ var toolBar = (function () {
properties.userData = JSON.stringify({ grabbableKey: { grabbable: false } }); properties.userData = JSON.stringify({ grabbableKey: { grabbable: false } });
} }
SelectionManager.saveProperties();
entityID = Entities.addEntity(properties); entityID = Entities.addEntity(properties);
pushCommandForSelections([{
entityID: entityID,
properties: properties
}], [], true);
if (properties.type === "ParticleEffect") { if (properties.type === "ParticleEffect") {
selectParticleEntity(entityID); selectParticleEntity(entityID);
@ -1882,6 +1887,7 @@ Controller.keyReleaseEvent.connect(keyReleaseEvent);
Controller.keyPressEvent.connect(keyPressEvent); Controller.keyPressEvent.connect(keyPressEvent);
function recursiveAdd(newParentID, parentData) { function recursiveAdd(newParentID, parentData) {
if (parentData.children !== undefined) {
var children = parentData.children; var children = parentData.children;
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
var childProperties = children[i].properties; var childProperties = children[i].properties;
@ -1890,6 +1896,7 @@ function recursiveAdd(newParentID, parentData) {
recursiveAdd(newChildID, children[i]); recursiveAdd(newChildID, children[i]);
} }
} }
}
// When an entity has been deleted we need a way to "undo" this deletion. Because it's not currently // When an entity has been deleted we need a way to "undo" this deletion. Because it's not currently
// possible to create an entity with a specific id, earlier undo commands to the deleted entity // possible to create an entity with a specific id, earlier undo commands to the deleted entity
@ -1897,17 +1904,23 @@ function recursiveAdd(newParentID, parentData) {
var DELETED_ENTITY_MAP = {}; var DELETED_ENTITY_MAP = {};
function applyEntityProperties(data) { function applyEntityProperties(data) {
var properties = data.setProperties; var editEntities = data.editEntities;
var selectedEntityIDs = []; var selectedEntityIDs = [];
var selectEdits = data.createEntities.length == 0 || !data.selectCreated;
var i, entityID; var i, entityID;
for (i = 0; i < properties.length; i++) { for (i = 0; i < editEntities.length; i++) {
entityID = properties[i].entityID; var entityID = editEntities[i].entityID;
if (DELETED_ENTITY_MAP[entityID] !== undefined) { if (DELETED_ENTITY_MAP[entityID] !== undefined) {
entityID = DELETED_ENTITY_MAP[entityID]; entityID = DELETED_ENTITY_MAP[entityID];
} }
Entities.editEntity(entityID, properties[i].properties); var entityProperties = editEntities[i].properties;
if (entityProperties !== null) {
Entities.editEntity(entityID, entityProperties);
}
if (selectEdits) {
selectedEntityIDs.push(entityID); selectedEntityIDs.push(entityID);
} }
}
for (i = 0; i < data.createEntities.length; i++) { for (i = 0; i < data.createEntities.length; i++) {
entityID = data.createEntities[i].entityID; entityID = data.createEntities[i].entityID;
var entityProperties = data.createEntities[i].properties; var entityProperties = data.createEntities[i].properties;
@ -1935,31 +1948,39 @@ function applyEntityProperties(data) {
// For currently selected entities, push a command to the UndoStack that uses the current entity properties for the // For currently selected entities, push a command to the UndoStack that uses the current entity properties for the
// redo command, and the saved properties for the undo command. Also, include create and delete entity data. // redo command, and the saved properties for the undo command. Also, include create and delete entity data.
function pushCommandForSelections(createdEntityData, deletedEntityData) { function pushCommandForSelections(createdEntityData, deletedEntityData, doNotSaveEditProperties) {
doNotSaveEditProperties = false;
var undoData = { var undoData = {
setProperties: [], editEntities: [],
createEntities: deletedEntityData || [], createEntities: deletedEntityData || [],
deleteEntities: createdEntityData || [], deleteEntities: createdEntityData || [],
selectCreated: true selectCreated: true
}; };
var redoData = { var redoData = {
setProperties: [], editEntities: [],
createEntities: createdEntityData || [], createEntities: createdEntityData || [],
deleteEntities: deletedEntityData || [], deleteEntities: deletedEntityData || [],
selectCreated: false selectCreated: true
}; };
for (var i = 0; i < SelectionManager.selections.length; i++) { for (var i = 0; i < SelectionManager.selections.length; i++) {
var entityID = SelectionManager.selections[i]; var entityID = SelectionManager.selections[i];
var initialProperties = SelectionManager.savedProperties[entityID]; var initialProperties = SelectionManager.savedProperties[entityID];
var currentProperties = Entities.getEntityProperties(entityID); var currentProperties = null;
if (!initialProperties) { if (!initialProperties) {
continue; continue;
} }
undoData.setProperties.push({
if (doNotSaveEditProperties) {
initialProperties = null;
} else {
currentProperties = Entities.getEntityProperties(entityID);
}
undoData.editEntities.push({
entityID: entityID, entityID: entityID,
properties: initialProperties properties: initialProperties
}); });
redoData.setProperties.push({ redoData.editEntities.push({
entityID: entityID, entityID: entityID,
properties: currentProperties properties: currentProperties
}); });