blockbench/js/undo.js

499 lines
13 KiB
JavaScript
Raw Normal View History

2021-05-05 19:54:41 +08:00
class UndoSystem {
constructor() {
this.index = 0;
this.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 (
2021-05-05 19:54:41 +08:00
aspects && this.current_save &&
Objector.equalKeys(aspects, this.current_save.aspects) &&
2019-07-18 00:02:07 +08:00
aspects.elements !== selected &&
2021-05-05 19:54:41 +08:00
this.history.length == this.index
2019-07-18 00:02:07 +08:00
) {
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
*/
2021-05-05 19:54:41 +08:00
this.current_save = new UndoSystem.save(aspects)
return this.current_save;
}
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;
}
2021-05-05 19:54:41 +08:00
if (!this.current_save) return;
aspects = aspects || this.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 = {
2021-05-05 19:54:41 +08:00
before: this.current_save,
post: new UndoSystem.save(aspects),
2018-10-18 01:50:25 +08:00
action: action
}
2021-05-05 19:54:41 +08:00
this.current_save = entry.post
if (this.history.length > this.index) {
this.history.length = this.index;
delete this.current_save;
2018-10-18 01:50:25 +08:00
}
2021-05-05 19:54:41 +08:00
this.history.push(entry)
2018-10-18 01:50:25 +08:00
2021-05-05 19:54:41 +08:00
if (this.history.length > settings.undo_limit.value) {
this.history.shift()
2018-10-18 01:50:25 +08:00
}
2021-05-05 19:54:41 +08:00
this.index = this.history.length
2018-12-16 23:18:20 +08:00
if (!aspects || !aspects.keep_saved) {
2021-05-05 19:54:41 +08:00
Project.saved = false;
2018-12-16 23:18:20 +08:00
}
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;
2021-05-05 19:54:41 +08:00
}
2019-08-18 00:26:14 +08:00
cancelEdit() {
2021-05-05 19:54:41 +08:00
if (!this.current_save) return;
2019-01-09 22:54:35 +08:00
outlines.children.length = 0
2021-05-05 19:54:41 +08:00
this.loadSave(this.current_save, new UndoSystem.save(this.current_save.aspects))
delete this.current_save;
}
2019-08-18 00:26:14 +08:00
addKeyframeCasualties(arr) {
if (!arr || arr.length == 0) return;
2021-05-05 19:54:41 +08:00
if (!this.current_save.keyframes) {
this.current_save.keyframes = {
animation: Animation.selected.uuid
2019-08-18 00:26:14 +08:00
}
}
arr.forEach(kf => {
2021-05-05 19:54:41 +08:00
this.current_save.affected = true
this.current_save.keyframes[kf.uuid] = kf.getUndoCopy();
2019-08-18 00:26:14 +08:00
})
2021-05-05 19:54:41 +08:00
}
2019-08-18 00:26:14 +08:00
undo(remote) {
2021-05-05 19:54:41 +08:00
if (this.history.length <= 0 || this.index < 1) return;
2018-10-18 01:50:25 +08:00
2021-05-05 19:54:41 +08:00
Project.saved = false;
this.index--;
2018-10-18 01:50:25 +08:00
2021-05-05 19:54:41 +08:00
var entry = this.history[this.index]
this.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})
2021-05-05 19:54:41 +08:00
}
2019-08-18 00:26:14 +08:00
redo(remote) {
2021-05-05 19:54:41 +08:00
if (this.history.length <= 0) return;
if (this.index >= this.history.length) {
2018-10-18 01:50:25 +08:00
return;
}
2021-05-05 19:54:41 +08:00
Project.saved = false;
2018-10-18 01:50:25 +08:00
2021-05-05 19:54:41 +08:00
var entry = this.history[this.index]
this.index++;
this.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})
2021-05-05 19:54:41 +08:00
}
2019-08-18 00:26:14 +08:00
remoteEdit(entry) {
2021-05-05 19:54:41 +08:00
this.loadSave(entry.post, entry.before, 'session')
2019-04-08 00:53:33 +08:00
if (entry.save_history !== false) {
2021-05-05 19:54:41 +08:00
delete this.current_save;
this.history.push(entry)
if (this.history.length > settings.undo_limit.value) {
this.history.shift()
2019-04-08 00:53:33 +08:00
}
2021-05-05 19:54:41 +08:00
this.index = this.history.length
Project.saved = false;
2019-04-08 00:53:33 +08:00
Blockbench.dispatchEvent('finished_edit', {remote: true})
}
2021-05-05 19:54:41 +08:00
}
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;
2021-05-05 19:54:41 +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]
2020-10-04 17:13:38 +08:00
var new_element = OutlinerElement.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 {
2019-07-26 19:33:29 +08:00
new_element = NonGroup.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)) {
2020-10-04 17:13:38 +08:00
var obj = OutlinerElement.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
}
}
}
loadOutlinerDraggable()
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
2020-10-04 17:13:38 +08:00
var sel_group = OutlinerElement.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) {
2020-10-04 17:13:38 +08:00
var group = OutlinerElement.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]) {
2021-05-05 19:54:41 +08:00
var tex = this.getItemByUUID(textures, uuid)
2018-10-18 01:50:25 +08:00
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]) {
2021-05-05 19:54:41 +08:00
var tex = this.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) {
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
2021-05-05 19:54:41 +08:00
var animation = (reference.animations && reference.animations[uuid]) ? this.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]) {
2021-05-05 19:54:41 +08:00
var animation = this.getItemByUUID(Animator.animations, uuid)
2019-04-08 00:53:33 +08:00
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
}
var added = 0;
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);
added++;
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
}
}
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
}
2021-05-05 19:54:41 +08:00
UndoSystem.save = class {
constructor(aspects) {
var scope = this;
this.aspects = aspects;
if (aspects.selection) {
this.selection = []
selected.forEach(function(obj) {
scope.selection.push(obj.uuid)
})
if (Group.selected) {
this.selection_group = Group.selected.uuid
}
}
if (aspects.elements) {
this.elements = {}
aspects.elements.forEach(function(obj) {
scope.elements[obj.uuid] = obj.getUndoCopy(aspects)
})
}
if (aspects.outliner) {
this.outliner = compileGroups(true)
}
if (aspects.group) {
this.group = aspects.group.getChildlessCopy(true)
}
if (aspects.textures) {
this.textures = {}
aspects.textures.forEach(t => {
var tex = t.getUndoCopy(aspects.bitmap)
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);
})
}
if (aspects.selected_texture && Texture.all.length) {
this.selected_texture = Texture.selected ? Texture.selected.uuid : null;
}
if (aspects.settings) {
this.settings = aspects.settings
}
if (aspects.uv_mode) {
this.uv_mode = {
box_uv: Project.box_uv,
width: Project.texture_width,
height: Project.texture_height
}
}
if (aspects.animations) {
this.animations = {}
aspects.animations.forEach(a => {
scope.animations[a.uuid] = a.getUndoCopy();
})
}
if (aspects.keyframes && Animation.selected && Timeline.animators.length) {
this.keyframes = {
animation: Animation.selected.uuid
}
aspects.keyframes.forEach(kf => {
scope.keyframes[kf.uuid] = kf.getUndoCopy()
})
}
if (aspects.display_slots) {
scope.display_slots = {}
aspects.display_slots.forEach(slot => {
if (display[slot]) {
scope.display_slots[slot] = display[slot].copy()
} else {
scope.display_slots[slot] = null
}
})
}
}
addTexture(texture) {
if (!this.textures) return;
if (this.aspects.textures.safePush(texture)) {
this.textures[texture.uuid] = texture.getUndoCopy(this.aspects.bitmap)
}
2018-12-27 21:03:04 +08:00
}
}
2021-05-05 19:54:41 +08:00
let Undo = null;
2018-12-27 21:03:04 +08:00
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,
2018-12-27 21:03:04 +08:00
keybind: new Keybind({key: 90, ctrl: true}),
2021-05-05 19:54:41 +08:00
click(e) {
Project.undo.undo(e);
}
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,
2018-12-27 21:03:04 +08:00
keybind: new Keybind({key: 89, ctrl: true}),
2021-05-05 19:54:41 +08:00
click(e) {
Project.undo.redo(e);
}
2018-12-27 21:03:04 +08:00
})
})