blockbench/js/undo.js

491 lines
13 KiB
JavaScript
Raw Normal View History

2018-10-18 01:50:25 +08:00
var Undo = {
index: 0,
history: [],
2019-08-18 00:26:14 +08:00
initEdit(aspects) {
2019-07-18 00:02:07 +08:00
if (aspects && aspects.cubes) {
console.warn('Aspect "cubes" is deprecated. Please use "elements" instead.');
aspects.elements = aspects.cubes;
}
/*
if (
aspects && Undo.current_save &&
Objector.equalKeys(aspects, Undo.current_save.aspects) &&
aspects.elements !== selected &&
Undo.history.length == Undo.index
) {
return;
2018-10-18 01:50:25 +08:00
}
2019-07-18 00:02:07 +08:00
- This still causes issues, for example with different texture selections
*/
2018-10-18 01:50:25 +08:00
Undo.current_save = new Undo.save(aspects)
2019-07-18 00:02:07 +08:00
return Undo.current_save;
2018-10-18 01:50:25 +08:00
},
2019-08-18 00:26:14 +08:00
finishEdit(action, aspects) {
2019-07-18 00:02:07 +08:00
if (aspects && aspects.cubes) {
console.warn('Aspect "cubes" is deprecated. Please use "elements" instead.');
aspects.elements = aspects.cubes;
}
2019-02-04 04:09:35 +08:00
if (!Undo.current_save) return;
2018-12-16 23:18:20 +08:00
aspects = aspects || Undo.current_save.aspects
2018-10-18 01:50:25 +08:00
//After
2019-02-04 04:09:35 +08:00
Blockbench.dispatchEvent('finish_edit', {aspects})
2018-10-18 01:50:25 +08:00
var entry = {
before: Undo.current_save,
2018-12-16 23:18:20 +08:00
post: new Undo.save(aspects),
2018-10-18 01:50:25 +08:00
action: action
}
Undo.current_save = entry.post
2019-07-18 00:02:07 +08:00
if (Undo.history.length > Undo.index) {
Undo.history.length = Undo.index;
delete Undo.current_save;
2018-10-18 01:50:25 +08:00
}
Undo.history.push(entry)
if (Undo.history.length > settings.undo_limit.value) {
Undo.history.shift()
}
Undo.index = Undo.history.length
2018-12-16 23:18:20 +08:00
if (!aspects || !aspects.keep_saved) {
Prop.project_saved = false;
}
2019-02-04 04:09:35 +08:00
Blockbench.dispatchEvent('finished_edit', {aspects})
2019-04-08 00:53:33 +08:00
if (EditSession.active) {
EditSession.sendEdit(entry)
}
2019-07-18 00:02:07 +08:00
return entry;
2018-10-18 01:50:25 +08:00
},
2019-08-18 00:26:14 +08:00
cancelEdit() {
2019-01-09 22:54:35 +08:00
if (!Undo.current_save) return;
outlines.children.length = 0
Undo.loadSave(Undo.current_save, new Undo.save(Undo.current_save.aspects))
delete Undo.current_save;
},
2019-08-18 00:26:14 +08:00
addKeyframeCasualties(arr) {
if (!arr || arr.length == 0) return;
if (!Undo.current_save.keyframes) {
Undo.current_save.keyframes = {
animation: Animation.selected.uuid
2019-08-18 00:26:14 +08:00
}
}
arr.forEach(kf => {
Undo.current_save.affected = true
Undo.current_save.keyframes[kf.uuid] = kf.getUndoCopy();
})
},
undo(remote) {
2018-10-18 01:50:25 +08:00
if (Undo.history.length <= 0 || Undo.index < 1) return;
Prop.project_saved = false;
Undo.index--;
var entry = Undo.history[Undo.index]
Undo.loadSave(entry.before, entry.post)
2019-04-08 00:53:33 +08:00
if (EditSession.active && remote !== true) {
EditSession.sendAll('command', 'undo')
}
2019-02-04 04:09:35 +08:00
Blockbench.dispatchEvent('undo', {entry})
2018-10-18 01:50:25 +08:00
},
2019-08-18 00:26:14 +08:00
redo(remote) {
2018-10-18 01:50:25 +08:00
if (Undo.history.length <= 0) return;
if (Undo.index >= Undo.history.length) {
return;
}
Prop.project_saved = false;
2019-07-18 00:02:07 +08:00
var entry = Undo.history[Undo.index]
Undo.index++;
2018-10-18 01:50:25 +08:00
Undo.loadSave(entry.post, entry.before)
2019-04-08 00:53:33 +08:00
if (EditSession.active && remote !== true) {
EditSession.sendAll('command', 'redo')
}
2019-02-04 04:09:35 +08:00
Blockbench.dispatchEvent('redo', {entry})
2018-10-18 01:50:25 +08:00
},
2019-08-18 00:26:14 +08:00
remoteEdit(entry) {
2019-04-08 00:53:33 +08:00
Undo.loadSave(entry.post, entry.before, 'session')
if (entry.save_history !== false) {
delete Undo.current_save;
Undo.history.push(entry)
if (Undo.history.length > settings.undo_limit.value) {
Undo.history.shift()
}
Undo.index = Undo.history.length
Prop.project_saved = false;
Blockbench.dispatchEvent('finished_edit', {remote: true})
}
},
2019-08-18 00:26:14 +08:00
getItemByUUID(list, uuid) {
2018-10-18 01:50:25 +08:00
if (!list || typeof list !== 'object' || !list.length) {return false;}
var i = 0;
while (i < list.length) {
if (list[i].uuid === uuid) {
return list[i]
}
i++;
}
return false;
},
save: function(aspects) {
var scope = this;
2019-07-18 00:02:07 +08:00
this.aspects = aspects;
2018-10-18 01:50:25 +08:00
if (aspects.selection) {
this.selection = []
selected.forEach(function(obj) {
scope.selection.push(obj.uuid)
})
2019-07-18 00:02:07 +08:00
if (Group.selected) {
this.selection_group = Group.selected.uuid
2018-10-18 01:50:25 +08:00
}
}
2019-07-18 00:02:07 +08:00
if (aspects.elements) {
this.elements = {}
aspects.elements.forEach(function(obj) {
scope.elements[obj.uuid] = obj.getUndoCopy(aspects)
2018-10-18 01:50:25 +08:00
})
}
2018-12-03 02:37:06 +08:00
if (aspects.outliner) {
2018-10-18 01:50:25 +08:00
this.outliner = compileGroups(true)
}
2018-12-03 02:37:06 +08:00
if (aspects.group) {
2020-07-16 15:32:59 +08:00
this.group = aspects.group.getChildlessCopy(true)
2018-10-18 01:50:25 +08:00
}
2018-12-03 02:37:06 +08:00
if (aspects.textures) {
2018-10-18 01:50:25 +08:00
this.textures = {}
2020-07-16 15:32:59 +08:00
aspects.textures.forEach(t => {
2018-10-18 01:50:25 +08:00
var tex = t.getUndoCopy(aspects.bitmap)
2020-07-16 15:32:59 +08:00
this.textures[t.uuid] = tex
})
}
if (aspects.texture_order && Texture.all.length) {
this.texture_order = [];
Texture.all.forEach(tex => {
this.texture_order.push(tex.uuid);
2018-10-18 01:50:25 +08:00
})
}
2020-07-18 19:31:46 +08:00
if (aspects.selected_texture && Texture.all.length) {
this.selected_texture = Texture.selected ? Texture.selected.uuid : null;
}
2018-10-18 01:50:25 +08:00
if (aspects.settings) {
this.settings = aspects.settings
}
2019-07-18 00:02:07 +08:00
if (aspects.uv_mode) {
this.uv_mode = {
box_uv: Project.box_uv,
2018-10-18 01:50:25 +08:00
width: Project.texture_width,
height: Project.texture_height
}
}
2018-12-03 02:37:06 +08:00
2019-04-08 00:53:33 +08:00
if (aspects.animations) {
this.animations = {}
aspects.animations.forEach(a => {
2019-08-01 06:01:47 +08:00
scope.animations[a.uuid] = a.getUndoCopy();
2019-04-08 00:53:33 +08:00
})
2018-12-03 02:37:06 +08:00
}
if (aspects.keyframes && Animation.selected && Timeline.animators.length) {
2018-12-03 02:37:06 +08:00
this.keyframes = {
animation: Animation.selected.uuid
2018-12-03 02:37:06 +08:00
}
aspects.keyframes.forEach(kf => {
2019-08-01 06:01:47 +08:00
scope.keyframes[kf.uuid] = kf.getUndoCopy()
2018-12-03 02:37:06 +08:00
})
}
2018-12-27 21:03:04 +08:00
if (aspects.display_slots) {
scope.display_slots = {}
aspects.display_slots.forEach(slot => {
2019-01-09 22:54:35 +08:00
if (display[slot]) {
scope.display_slots[slot] = display[slot].copy()
} else {
scope.display_slots[slot] = null
}
2018-12-27 21:03:04 +08:00
})
}
if (aspects.exploded_view !== undefined) {
this.exploded_view = !!aspects.exploded_view;
}
2018-10-18 01:50:25 +08:00
},
2019-08-18 00:26:14 +08:00
loadSave(save, reference, mode) {
2019-04-08 00:53:33 +08:00
var is_session = mode === 'session';
2020-05-31 21:54:04 +08:00
if (save.uv_mode) {
Project.box_uv = save.uv_mode.box_uv;
Project.texture_width = save.uv_mode.width;
Project.texture_height = save.uv_mode.height;
Canvas.updateAllUVs()
}
2019-07-18 00:02:07 +08:00
if (save.elements) {
for (var uuid in save.elements) {
if (save.elements.hasOwnProperty(uuid)) {
var element = save.elements[uuid]
var new_element = OutlinerNode.uuids[uuid]
2019-07-18 00:02:07 +08:00
if (new_element) {
for (var face in new_element.faces) {
new_element.faces[face].reset()
}
new_element.extend(element)
2019-07-26 19:33:29 +08:00
if (new_element.type == 'cube') {
2019-07-18 00:02:07 +08:00
Canvas.adaptObjectPosition(new_element)
Canvas.adaptObjectFaceGeo(new_element)
2019-07-18 00:02:07 +08:00
Canvas.adaptObjectFaces(new_element)
Canvas.updateUV(new_element)
2018-10-18 01:50:25 +08:00
}
} else {
new_element = OutlinerElement.fromSave(element, true);
2018-10-18 01:50:25 +08:00
}
}
}
2019-07-18 00:02:07 +08:00
for (var uuid in reference.elements) {
if (reference.elements.hasOwnProperty(uuid) && !save.elements.hasOwnProperty(uuid)) {
var obj = OutlinerNode.uuids[uuid]
2018-10-18 01:50:25 +08:00
if (obj) {
2019-04-08 00:53:33 +08:00
obj.remove()
2018-10-18 01:50:25 +08:00
}
}
}
Canvas.updateVisibility()
}
if (save.outliner) {
2019-07-18 00:02:07 +08:00
Group.selected = undefined
2018-10-18 01:50:25 +08:00
parseGroups(save.outliner)
2019-04-08 00:53:33 +08:00
if (is_session) {
function iterate(arr) {
arr.forEach((obj) => {
delete obj.isOpen;
if (obj.children) {
iterate(obj.children)
}
})
}
iterate(save.outliner)
}
2019-07-18 00:02:07 +08:00
if (Format.bone_rig) {
2018-10-18 01:50:25 +08:00
Canvas.updateAllPositions()
}
}
2019-04-08 00:53:33 +08:00
if (save.selection_group && !is_session) {
2019-07-18 00:02:07 +08:00
Group.selected = undefined
var sel_group = OutlinerNode.uuids[save.selection_group]
2018-10-18 01:50:25 +08:00
if (sel_group) {
sel_group.select()
}
}
2019-04-08 00:53:33 +08:00
if (save.selection && !is_session) {
2018-10-18 01:50:25 +08:00
selected.length = 0;
elements.forEach(function(obj) {
if (save.selection.includes(obj.uuid)) {
2019-07-18 00:02:07 +08:00
obj.selectLow()
2018-10-18 01:50:25 +08:00
}
})
}
if (save.group) {
var group = OutlinerNode.uuids[save.group.uuid]
2018-10-18 01:50:25 +08:00
if (group) {
2019-04-08 00:53:33 +08:00
if (is_session) {
delete save.group.isOpen;
}
2018-10-18 01:50:25 +08:00
group.extend(save.group)
2019-07-18 00:02:07 +08:00
if (Format.bone_rig) {
2018-10-18 01:50:25 +08:00
group.forEachChild(function(obj) {
Canvas.adaptObjectPosition(obj)
}, Cube)
2018-10-18 01:50:25 +08:00
}
}
}
if (save.textures) {
Painter.current = {}
for (var uuid in save.textures) {
if (reference.textures[uuid]) {
var tex = Undo.getItemByUUID(textures, uuid)
if (tex) {
2019-04-08 00:53:33 +08:00
var require_reload = tex.mode !== save.textures[uuid].mode;
2020-03-05 03:56:17 +08:00
tex.extend(save.textures[uuid]).updateSource()
tex.keep_size = true;
2019-04-08 00:53:33 +08:00
if (require_reload || reference.textures[uuid] === true) {
tex.load()
}
2018-10-18 01:50:25 +08:00
}
} else {
2019-04-08 00:53:33 +08:00
var tex = new Texture(save.textures[uuid], uuid)
tex.load().add(false)
2018-10-18 01:50:25 +08:00
}
}
for (var uuid in reference.textures) {
if (!save.textures[uuid]) {
2020-07-16 15:32:59 +08:00
var tex = Undo.getItemByUUID(Texture.all, uuid)
2018-10-18 01:50:25 +08:00
if (tex) {
2020-07-16 15:32:59 +08:00
Texture.all.splice(Texture.all.indexOf(tex), 1)
}
if (Texture.selected == tex) {
Texture.selected = textures.selected = undefined;
2018-10-18 01:50:25 +08:00
}
}
}
Canvas.updateAllFaces()
}
2020-07-16 15:32:59 +08:00
if (save.texture_order) {
Texture.all.sort((a, b) => {
return save.texture_order.indexOf(a.uuid) - save.texture_order.indexOf(b.uuid);
})
Canvas.updateLayeredTextures()
}
2020-07-18 19:31:46 +08:00
if (save.selected_texture) {
let tex = Texture.all.find(tex => tex.uuid == save.selected_texture);
if (tex instanceof Texture) tex.select()
} else if (save.selected_texture === null) {
2020-07-18 19:31:46 +08:00
unselectTextures()
}
2018-10-18 01:50:25 +08:00
if (save.settings) {
for (var key in save.settings) {
settings[key].value = save.settings[key]
}
}
2018-12-03 02:37:06 +08:00
2019-04-08 00:53:33 +08:00
if (save.animations) {
for (var uuid in save.animations) {
2018-12-03 02:37:06 +08:00
2019-08-18 00:26:14 +08:00
var animation = (reference.animations && reference.animations[uuid]) ? Undo.getItemByUUID(Animator.animations, uuid) : null;
2019-04-08 00:53:33 +08:00
if (!animation) {
animation = new Animation()
animation.uuid = uuid
}
animation.extend(save.animations[uuid]).add(false)
if (save.animations[uuid].selected) {
animation.select()
}
2018-12-03 02:37:06 +08:00
}
2019-04-08 00:53:33 +08:00
for (var uuid in reference.animations) {
if (!save.animations[uuid]) {
var animation = Undo.getItemByUUID(Animator.animations, uuid)
if (animation) {
animation.remove(false)
}
}
2018-12-03 02:37:06 +08:00
}
}
2019-04-08 00:53:33 +08:00
if (save.keyframes) {
var animation = Animation.selected;
2019-04-08 00:53:33 +08:00
if (!animation || animation.uuid !== save.keyframes.animation) {
2018-12-03 02:37:06 +08:00
animation = Animator.animations.findInArray('uuid', save.keyframes.animation)
2019-04-08 00:53:33 +08:00
if (animation.select && Animator.open && is_session) {
2019-01-09 22:54:35 +08:00
animation.select()
}
2018-12-03 02:37:06 +08:00
}
2019-04-08 00:53:33 +08:00
if (animation) {
2018-12-03 02:37:06 +08:00
2019-08-18 00:26:14 +08:00
function getKeyframe(uuid, animator) {
var i = 0;
while (i < animator.keyframes.length) {
if (animator.keyframes[i].uuid === uuid) {
return animator.keyframes[i];
2019-04-08 00:53:33 +08:00
}
2019-08-18 00:26:14 +08:00
i++;
2018-12-03 02:37:06 +08:00
}
2019-08-18 00:26:14 +08:00
}
for (var uuid in save.keyframes) {
if (uuid.length === 36 && save.keyframes.hasOwnProperty(uuid)) {
var data = save.keyframes[uuid];
var animator = animation.animators[data.animator];
if (!animator) continue;
var kf = getKeyframe(uuid, animator);
if (kf) {
kf.extend(data)
} else {
animator.addKeyframe(data, uuid);
2019-04-08 00:53:33 +08:00
}
2018-12-03 02:37:06 +08:00
}
2019-08-18 00:26:14 +08:00
}
for (var uuid in reference.keyframes) {
if (uuid.length === 36 && reference.keyframes.hasOwnProperty(uuid) && !save.keyframes.hasOwnProperty(uuid)) {
var data = reference.keyframes[uuid];
var animator = animation.animators[data.animator];
if (!animator) continue;
var kf = getKeyframe(uuid, animator)
if (kf) {
kf.remove()
2019-04-08 00:53:33 +08:00
}
2018-12-03 02:37:06 +08:00
}
}
2019-08-18 00:26:14 +08:00
updateKeyframeSelection()
2018-12-03 02:37:06 +08:00
}
}
2018-12-27 21:03:04 +08:00
if (save.display_slots) {
for (var slot in save.display_slots) {
2019-01-09 22:54:35 +08:00
var data = save.display_slots[slot]
if (!display[slot] && data) {
2018-12-27 21:03:04 +08:00
display[slot] = new DisplaySlot()
2019-01-09 22:54:35 +08:00
} else if (data === null && display[slot]) {
display[slot].default()
2018-12-27 21:03:04 +08:00
}
2019-01-09 22:54:35 +08:00
display[slot].extend(data).update()
2018-12-27 21:03:04 +08:00
}
}
Blockbench.dispatchEvent('load_undo_save', {save, reference, mode})
2019-03-10 05:06:35 +08:00
if (open_dialog == 'uv_dialog') {
for (var key in uv_dialog.editors) {
if (uv_dialog.editors[key]) {
uv_dialog.editors[key].loadData()
}
}
}
2018-10-18 01:50:25 +08:00
updateSelection()
2019-12-16 03:04:31 +08:00
if ((save.outliner || save.group) && Format.bone_rig) {
Canvas.updateAllBones();
}
if (Modes.animate) {
Animator.preview();
}
2018-10-18 01:50:25 +08:00
}
2018-12-27 21:03:04 +08:00
}
Undo.save.prototype.addTexture = function(texture) {
if (!this.textures) return;
if (this.aspects.textures.safePush(texture)) {
this.textures[texture.uuid] = texture.getUndoCopy(this.aspects.bitmap)
}
}
BARS.defineActions(function() {
2019-08-18 00:26:14 +08:00
new Action('undo', {
2018-12-27 21:03:04 +08:00
icon: 'undo',
category: 'edit',
2019-07-18 00:02:07 +08:00
condition: () => (!open_dialog || open_dialog === 'uv_dialog' || open_dialog === 'toolbar_edit'),
2019-03-10 05:06:35 +08:00
work_in_dialog: true,
keybind: new Keybind({key: 'z', ctrl: true}),
2019-03-10 05:06:35 +08:00
click: Undo.undo
2018-12-27 21:03:04 +08:00
})
2019-08-18 00:26:14 +08:00
new Action('redo', {
2018-12-27 21:03:04 +08:00
icon: 'redo',
category: 'edit',
2019-07-18 00:02:07 +08:00
condition: () => (!open_dialog || open_dialog === 'uv_dialog' || open_dialog === 'toolbar_edit'),
2019-03-10 05:06:35 +08:00
work_in_dialog: true,
keybind: new Keybind({key: 'y', ctrl: true}),
2019-03-10 05:06:35 +08:00
click: Undo.redo
2018-12-27 21:03:04 +08:00
})
})