2021-05-05 19:54:41 +08:00
|
|
|
class UndoSystem {
|
|
|
|
constructor() {
|
|
|
|
this.index = 0;
|
|
|
|
this.history = [];
|
|
|
|
}
|
2023-04-08 19:46:11 +08:00
|
|
|
startChange(amended) {
|
2023-04-19 21:54:39 +08:00
|
|
|
/*if (this.current_save && Painter.painting) {
|
2023-04-08 19:46:11 +08:00
|
|
|
throw 'Canceled edit: Cannot perform edits while painting'
|
2023-04-19 21:54:39 +08:00
|
|
|
}*/
|
2023-04-12 04:15:09 +08:00
|
|
|
/*if (this.current_save && Transformer.dragging) {
|
2023-04-08 19:46:11 +08:00
|
|
|
throw 'Canceled edit: Cannot perform other edits while transforming elements'
|
2023-04-12 04:15:09 +08:00
|
|
|
}*/
|
2021-12-08 01:31:16 +08:00
|
|
|
if (!amended && this.amend_edit_menu) {
|
|
|
|
this.closeAmendEditMenu();
|
|
|
|
}
|
2023-04-08 19:46:11 +08:00
|
|
|
}
|
|
|
|
initEdit(aspects, amended = false) {
|
|
|
|
if (aspects && aspects.cubes) {
|
|
|
|
console.warn('Aspect "cubes" is deprecated. Please use "elements" instead.');
|
|
|
|
aspects.elements = aspects.cubes;
|
|
|
|
}
|
|
|
|
this.startChange(amended);
|
2021-05-05 19:54:41 +08:00
|
|
|
this.current_save = new UndoSystem.save(aspects)
|
2023-05-18 00:03:35 +08:00
|
|
|
Blockbench.dispatchEvent('init_edit', {aspects, amended, save: this.current_save})
|
2021-05-05 19:54:41 +08:00
|
|
|
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),
|
2021-06-02 03:08:41 +08:00
|
|
|
action: action,
|
|
|
|
time: Date.now()
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
2021-05-05 19:54:41 +08:00
|
|
|
this.current_save = entry.post
|
|
|
|
if (this.history.length > this.index) {
|
|
|
|
this.history.length = this.index;
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
2023-04-08 19:46:11 +08:00
|
|
|
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})
|
2021-07-24 19:51:42 +08:00
|
|
|
if (Project.EditSession && Project.EditSession.active) {
|
|
|
|
Project.EditSession.sendEdit(entry)
|
2019-04-08 00:53:33 +08:00
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
return entry;
|
2021-05-05 19:54:41 +08:00
|
|
|
}
|
2024-03-13 04:10:39 +08:00
|
|
|
cancelEdit(revert_changes = true) {
|
2021-05-05 19:54:41 +08:00
|
|
|
if (!this.current_save) return;
|
2023-04-08 19:46:11 +08:00
|
|
|
this.startChange();
|
2024-03-13 04:10:39 +08:00
|
|
|
if (revert_changes) {
|
|
|
|
Canvas.outlines.children.empty();
|
|
|
|
this.loadSave(this.current_save, new UndoSystem.save(this.current_save.aspects))
|
|
|
|
}
|
2021-05-05 19:54:41 +08:00
|
|
|
delete this.current_save;
|
|
|
|
}
|
2021-12-08 01:31:16 +08:00
|
|
|
closeAmendEditMenu() {
|
|
|
|
if (this.amend_edit_menu) {
|
2022-08-25 04:04:04 +08:00
|
|
|
this.amend_edit_menu.node.remove();
|
2021-12-08 01:31:16 +08:00
|
|
|
delete this.amend_edit_menu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
amendEdit(form, callback) {
|
2022-08-25 04:04:04 +08:00
|
|
|
let scope = this;
|
2021-12-08 01:31:16 +08:00
|
|
|
let input_elements = {};
|
|
|
|
let dialog = document.createElement('div');
|
|
|
|
dialog.id = 'amend_edit_menu';
|
2022-08-25 04:04:04 +08:00
|
|
|
this.amend_edit_menu = {
|
|
|
|
node: dialog,
|
|
|
|
form: {}
|
|
|
|
};
|
2021-12-08 01:31:16 +08:00
|
|
|
|
|
|
|
let close_button = document.createElement('div');
|
|
|
|
close_button.append(Blockbench.getIconNode('clear'));
|
|
|
|
close_button.className = 'amend_edit_close_button';
|
|
|
|
close_button.title = tl('dialog.close')
|
|
|
|
close_button.addEventListener('click', (event) => {
|
|
|
|
Undo.closeAmendEditMenu();
|
|
|
|
})
|
|
|
|
dialog.append(close_button);
|
|
|
|
|
|
|
|
function updateValue() {
|
|
|
|
let form_values = {};
|
|
|
|
for (let key in form) {
|
2022-08-25 04:04:04 +08:00
|
|
|
let input = scope.amend_edit_menu.form[key];
|
|
|
|
if (input) {
|
|
|
|
if (input.type == 'number') {
|
|
|
|
form_values[key] = input.slider.get();
|
2023-05-21 05:32:24 +08:00
|
|
|
} else if (input.type == 'checkbox') {
|
|
|
|
form_values[key] = !!input.node.checked;
|
2022-08-25 04:04:04 +08:00
|
|
|
}
|
2021-12-11 05:54:05 +08:00
|
|
|
}
|
2021-12-08 01:31:16 +08:00
|
|
|
}
|
2023-08-27 19:54:12 +08:00
|
|
|
if (Undo.history.length != Undo.index) {
|
|
|
|
console.error('Detected error in amending edit. Skipping this edit.');
|
|
|
|
return;
|
|
|
|
}
|
2021-12-09 03:33:13 +08:00
|
|
|
Undo.undo(null, true);
|
2022-08-25 04:04:04 +08:00
|
|
|
callback(form_values, scope.amend_edit_menu.form);
|
2021-12-08 01:31:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let key in form) {
|
|
|
|
let form_line = form[key];
|
2021-12-11 05:54:05 +08:00
|
|
|
if (!Condition(form_line.condition)) continue;
|
2021-12-08 01:31:16 +08:00
|
|
|
let line = document.createElement('div');
|
|
|
|
line.className = 'amend_edit_line';
|
|
|
|
dialog.append(line);
|
2022-08-25 04:04:04 +08:00
|
|
|
this.amend_edit_menu.form[key] = {
|
|
|
|
type: form_line.type || 'number',
|
|
|
|
label: form_line.label
|
|
|
|
}
|
2021-12-08 01:31:16 +08:00
|
|
|
|
2022-08-25 04:04:04 +08:00
|
|
|
if (this.amend_edit_menu.form[key].type == 'number') {
|
2022-10-14 04:52:04 +08:00
|
|
|
let getInterval = form_line.getInterval;
|
|
|
|
if (form_line.interval_type == 'position') getInterval = getSpatialInterval;
|
|
|
|
if (form_line.interval_type == 'rotation') getInterval = getRotationInterval;
|
2022-08-25 04:04:04 +08:00
|
|
|
let slider = new NumSlider({
|
|
|
|
id: 'amend_edit_slider',
|
|
|
|
name: tl(form_line.label),
|
|
|
|
private: true,
|
|
|
|
onChange: updateValue,
|
2022-10-14 04:52:04 +08:00
|
|
|
getInterval,
|
2022-08-25 04:04:04 +08:00
|
|
|
settings: {
|
|
|
|
default: form_line.value || 0,
|
|
|
|
min: form_line.min,
|
|
|
|
max: form_line.max,
|
|
|
|
step: form_line.step||1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
line.append(slider.node);
|
|
|
|
input_elements[key] = slider;
|
|
|
|
this.amend_edit_menu.form[key].slider = slider
|
|
|
|
slider.update();
|
2023-05-21 05:32:24 +08:00
|
|
|
|
|
|
|
} else if (this.amend_edit_menu.form[key].type == 'checkbox') {
|
|
|
|
|
2024-03-17 03:54:44 +08:00
|
|
|
let toggle = Interface.createElement('input', {type: 'checkbox', checked: form_line.value ? true : undefined});
|
2023-05-21 05:32:24 +08:00
|
|
|
toggle.addEventListener('input', updateValue);
|
|
|
|
line.append(toggle);
|
|
|
|
input_elements[key] = toggle;
|
|
|
|
this.amend_edit_menu.form[key].node = toggle;
|
2022-08-25 04:04:04 +08:00
|
|
|
}
|
2021-12-08 01:31:16 +08:00
|
|
|
|
|
|
|
let label = document.createElement('label');
|
|
|
|
label.innerText = tl(form_line.label);
|
|
|
|
line.append(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
let preview_container = document.getElementById('preview');
|
|
|
|
preview_container.append(dialog);
|
|
|
|
}
|
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 = {
|
2020-09-15 20:21:04 +08:00
|
|
|
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
|
|
|
}
|
2021-12-09 03:33:13 +08:00
|
|
|
undo(remote, amended) {
|
2023-04-08 19:46:11 +08:00
|
|
|
this.startChange(amended);
|
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)
|
2021-07-24 19:51:42 +08:00
|
|
|
if (Project.EditSession && remote !== true) {
|
|
|
|
Project.EditSession.sendAll('command', 'undo')
|
2019-04-08 00:53:33 +08:00
|
|
|
}
|
2019-02-04 04:09:35 +08:00
|
|
|
Blockbench.dispatchEvent('undo', {entry})
|
2021-05-05 19:54:41 +08:00
|
|
|
}
|
2021-12-09 03:33:13 +08:00
|
|
|
redo(remote, amended) {
|
2023-04-08 19:46:11 +08:00
|
|
|
this.startChange(amended);
|
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)
|
2021-07-24 19:51:42 +08:00
|
|
|
if (Project.EditSession && remote !== true) {
|
|
|
|
Project.EditSession.sendAll('command', 'redo')
|
2019-04-08 00:53:33 +08:00
|
|
|
}
|
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]
|
|
|
|
|
2021-01-31 05:41:24 +08:00
|
|
|
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)
|
2021-08-22 07:25:50 +08:00
|
|
|
new_element.preview_controller.updateAll(new_element);
|
2018-10-18 01:50:25 +08:00
|
|
|
} else {
|
2021-01-31 05:41:24 +08:00
|
|
|
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)) {
|
2021-01-31 05:41:24 +08:00
|
|
|
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
|
2021-01-31 05:41:24 +08:00
|
|
|
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()
|
2022-12-05 03:45:25 +08:00
|
|
|
if (save.mesh_selection[obj.uuid]) {
|
|
|
|
Project.mesh_selection[obj.uuid] = save.mesh_selection[obj.uuid];
|
2021-12-08 01:31:16 +08:00
|
|
|
}
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save.group) {
|
2021-01-31 05:41:24 +08:00
|
|
|
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) {
|
2021-09-21 17:20:10 +08:00
|
|
|
if (obj.preview_controller) obj.preview_controller.updateTransform(obj);
|
|
|
|
})
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save.textures) {
|
|
|
|
Painter.current = {}
|
|
|
|
for (var uuid in save.textures) {
|
|
|
|
if (reference.textures[uuid]) {
|
2021-07-08 20:30:32 +08:00
|
|
|
var tex = Texture.all.find(tex => tex.uuid == 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;
|
2023-09-23 20:47:52 +08:00
|
|
|
tex.extend(save.textures[uuid]);
|
|
|
|
if (tex.source_overwritten && save.textures[uuid].image_data) {
|
|
|
|
// If the source file was overwritten by more recent changes, make sure to display the original data
|
2023-10-21 05:35:22 +08:00
|
|
|
tex.convertToInternal(save.textures[uuid].image_data);
|
|
|
|
}
|
|
|
|
if (tex.layers_enabled) {
|
2023-10-21 21:30:06 +08:00
|
|
|
tex.updateLayerChanges(true);
|
2023-09-23 20:47:52 +08:00
|
|
|
}
|
|
|
|
tex.updateSource();
|
2020-03-05 03:56:17 +08:00
|
|
|
tex.keep_size = true;
|
2019-04-08 00:53:33 +08:00
|
|
|
if (require_reload || reference.textures[uuid] === true) {
|
|
|
|
tex.load()
|
|
|
|
}
|
2023-11-19 21:47:42 +08:00
|
|
|
tex.syncToOtherProject();
|
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-07-08 20:30:32 +08:00
|
|
|
var tex = Texture.all.find(tex => tex.uuid == 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) {
|
2021-07-08 20:30:32 +08:00
|
|
|
Texture.selected = undefined;
|
2023-05-13 04:16:46 +08:00
|
|
|
Blockbench.dispatchEvent('update_texture_selection');
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-03 02:21:58 +08:00
|
|
|
Canvas.updateAllFaces();
|
|
|
|
updateInterfacePanels();
|
2023-11-07 07:28:26 +08:00
|
|
|
UVEditor.vue.updateTexture();
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2023-10-21 21:30:06 +08:00
|
|
|
if (save.layers) {
|
|
|
|
let affected_textures = [];
|
|
|
|
for (let uuid in save.layers) {
|
|
|
|
if (reference.layers[uuid]) {
|
|
|
|
let tex = Texture.all.find(tex => tex.uuid == save.layers[uuid].texture);
|
|
|
|
let layer = tex && tex.layers.find(l => l.uuid == uuid);
|
|
|
|
if (layer) {
|
|
|
|
layer.extend(save.layers[uuid]);
|
|
|
|
affected_textures.safePush(tex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affected_textures.forEach(tex => {
|
|
|
|
/*if (tex.source_overwritten && save.layers[uuid].image_data) {
|
|
|
|
// If the source file was overwritten by more recent changes, make sure to display the original data
|
|
|
|
tex.convertToInternal(save.layers[uuid].image_data);
|
|
|
|
}*/
|
|
|
|
tex.updateLayerChanges(true);
|
|
|
|
tex.updateSource();
|
|
|
|
tex.keep_size = true;
|
2023-11-19 21:47:42 +08:00
|
|
|
tex.syncToOtherProject();
|
2023-10-21 21:30:06 +08:00
|
|
|
})
|
2023-11-07 07:28:26 +08:00
|
|
|
Canvas.updateAllFaces();
|
|
|
|
UVEditor.vue.updateTexture();
|
2023-10-21 21:30:06 +08:00
|
|
|
}
|
|
|
|
|
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()
|
2021-03-04 22:02:04 +08:00
|
|
|
} 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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-12-14 04:54:36 +08:00
|
|
|
if (save.animation_controllers) {
|
|
|
|
for (var uuid in save.animation_controllers) {
|
|
|
|
|
|
|
|
var controller = (reference.animation_controllers && reference.animation_controllers[uuid]) ? this.getItemByUUID(AnimationController.all, uuid) : null;
|
|
|
|
if (!controller) {
|
|
|
|
controller = new AnimationController();
|
|
|
|
controller.uuid = uuid;
|
|
|
|
}
|
|
|
|
controller.extend(save.animation_controllers[uuid]).add(false);
|
|
|
|
if (save.animation_controllers[uuid].selected) {
|
|
|
|
controller.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var uuid in reference.animation_controllers) {
|
|
|
|
if (!save.animation_controllers[uuid]) {
|
|
|
|
var controller = this.getItemByUUID(AnimationController.all, uuid);
|
|
|
|
if (controller) {
|
|
|
|
controller.remove(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 03:35:59 +08:00
|
|
|
if (save.animation_controller_state) {
|
|
|
|
let controller = AnimationController.all.find(controller => save.animation_controller_state.controller == controller.uuid);
|
|
|
|
let state = controller && controller.states.find(state => state.uuid == save.animation_controller_state.uuid);
|
|
|
|
if (state) {
|
|
|
|
state.extend(save.animation_controller_state);
|
|
|
|
}
|
|
|
|
}
|
2018-12-03 02:37:06 +08:00
|
|
|
|
2019-04-08 00:53:33 +08:00
|
|
|
if (save.keyframes) {
|
2020-09-15 20:21:04 +08:00
|
|
|
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) {
|
2023-12-30 06:57:08 +08:00
|
|
|
for (let slot in save.display_slots) {
|
|
|
|
let data = save.display_slots[slot]
|
2019-01-09 22:54:35 +08:00
|
|
|
|
2021-07-11 04:22:02 +08:00
|
|
|
if (!Project.display_settings[slot] && data) {
|
|
|
|
Project.display_settings[slot] = new DisplaySlot()
|
|
|
|
} else if (data === null && Project.display_settings[slot]) {
|
|
|
|
Project.display_settings[slot].default()
|
2018-12-27 21:03:04 +08:00
|
|
|
}
|
2023-12-30 06:57:08 +08:00
|
|
|
if (Project.display_settings[slot]) {
|
|
|
|
Project.display_settings[slot].extend(data).update();
|
|
|
|
}
|
2018-12-27 21:03:04 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 02:37:36 +08:00
|
|
|
|
2021-11-21 21:43:05 +08:00
|
|
|
if (save.exploded_view !== undefined) {
|
|
|
|
Project.exploded_view = BarItems.explode_skin_model.value = save.exploded_view;
|
|
|
|
BarItems.explode_skin_model.updateEnabledState();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:37:36 +08:00
|
|
|
Blockbench.dispatchEvent('load_undo_save', {save, reference, mode})
|
|
|
|
|
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) {
|
2021-12-08 01:31:16 +08:00
|
|
|
this.selection = [];
|
2022-12-05 03:45:25 +08:00
|
|
|
this.mesh_selection = {};
|
2021-12-08 01:31:16 +08:00
|
|
|
selected.forEach(obj => {
|
|
|
|
this.selection.push(obj.uuid);
|
2022-12-05 03:45:25 +08:00
|
|
|
if (obj instanceof Mesh && Project.mesh_selection[obj.uuid]) {
|
|
|
|
this.mesh_selection[obj.uuid] = JSON.parse(JSON.stringify(Project.mesh_selection[obj.uuid]));
|
2021-12-08 01:31:16 +08:00
|
|
|
}
|
2021-05-05 19:54:41 +08:00
|
|
|
})
|
|
|
|
if (Group.selected) {
|
|
|
|
this.selection_group = Group.selected.uuid
|
|
|
|
}
|
2021-12-08 01:31:16 +08:00
|
|
|
|
2021-05-05 19:54:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 => {
|
2023-10-21 21:30:06 +08:00
|
|
|
let tex = t.getUndoCopy(aspects.bitmap)
|
2021-05-05 19:54:41 +08:00
|
|
|
this.textures[t.uuid] = tex
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-21 21:30:06 +08:00
|
|
|
if (aspects.layers) {
|
|
|
|
this.layers = {};
|
|
|
|
aspects.layers.forEach(layer => {
|
|
|
|
let copy = layer.getUndoCopy(aspects.bitmap)
|
|
|
|
this.layers[layer.uuid] = copy;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:54:41 +08:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|
2022-12-14 04:54:36 +08:00
|
|
|
if (aspects.animation_controllers) {
|
|
|
|
this.animation_controllers = {}
|
|
|
|
aspects.animation_controllers.forEach(a => {
|
|
|
|
scope.animation_controllers[a.uuid] = a.getUndoCopy();
|
|
|
|
})
|
|
|
|
}
|
2022-12-20 03:35:59 +08:00
|
|
|
if (aspects.animation_controller_state) {
|
|
|
|
this.animation_controller_state = aspects.animation_controller_state.getUndoCopy();
|
|
|
|
this.animation_controller_state.controller = aspects.animation_controller_state.controller?.uuid;
|
|
|
|
}
|
2021-05-05 19:54:41 +08:00
|
|
|
|
|
|
|
if (aspects.display_slots) {
|
|
|
|
scope.display_slots = {}
|
|
|
|
aspects.display_slots.forEach(slot => {
|
2021-07-11 04:22:02 +08:00
|
|
|
if (Project.display_settings[slot]) {
|
|
|
|
scope.display_slots[slot] = Project.display_settings[slot].copy()
|
2021-05-05 19:54:41 +08:00
|
|
|
} else {
|
|
|
|
scope.display_slots[slot] = null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-05-08 17:50:19 +08:00
|
|
|
|
|
|
|
if (aspects.exploded_view !== undefined) {
|
|
|
|
this.exploded_view = !!aspects.exploded_view;
|
|
|
|
}
|
2021-05-05 19:54:41 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2023-10-21 21:30:06 +08:00
|
|
|
addTextureOrLayer(texture) {
|
|
|
|
if (texture.layers_enabled && texture.layers[0]) {
|
|
|
|
let layer = texture.getActiveLayer();
|
2023-11-03 02:21:58 +08:00
|
|
|
if (!this.aspects.layers) this.aspects.layers = [];
|
2023-10-21 21:30:06 +08:00
|
|
|
if (this.aspects.layers.safePush(layer)) {
|
|
|
|
if (!this.layers) this.layers = {};
|
|
|
|
this.layers[layer.uuid] = layer.getUndoCopy(this.aspects.bitmap);
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-03 02:21:58 +08:00
|
|
|
if (!this.aspects.textures) this.aspects.textures = [];
|
2023-10-21 21:30:06 +08:00
|
|
|
if (this.aspects.textures.safePush(texture)) {
|
|
|
|
if (!this.textures) this.textures = {};
|
|
|
|
this.textures[texture.uuid] = texture.getUndoCopy(this.aspects.bitmap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 20:51:35 +08:00
|
|
|
addElements(elements, aspects = {}) {
|
|
|
|
if (!this.elements) this.elements = {};
|
|
|
|
elements.forEach(el => {
|
|
|
|
this.elements[el.uuid] = el.getUndoCopy(aspects);
|
|
|
|
})
|
|
|
|
}
|
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',
|
2021-08-18 04:02:23 +08:00
|
|
|
condition: () => Project,
|
2021-02-20 18:58:53 +08:00
|
|
|
keybind: new Keybind({key: 'z', 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',
|
2021-08-18 04:02:23 +08:00
|
|
|
condition: () => Project,
|
2021-02-20 18:58:53 +08:00
|
|
|
keybind: new Keybind({key: 'y', ctrl: true}),
|
2021-05-05 19:54:41 +08:00
|
|
|
click(e) {
|
|
|
|
Project.undo.redo(e);
|
|
|
|
}
|
2018-12-27 21:03:04 +08:00
|
|
|
})
|
2021-06-02 03:08:41 +08:00
|
|
|
new Action('edit_history', {
|
|
|
|
icon: 'history',
|
|
|
|
category: 'edit',
|
2021-08-14 18:57:17 +08:00
|
|
|
condition: () => Project,
|
2021-06-02 03:08:41 +08:00
|
|
|
click() {
|
|
|
|
let steps = [];
|
|
|
|
Undo.history.forEachReverse((entry, index) => {
|
|
|
|
index++;
|
|
|
|
step = {
|
|
|
|
name: entry.action,
|
|
|
|
time: new Date(entry.time).toLocaleTimeString(),
|
|
|
|
index,
|
|
|
|
current: index == Undo.index
|
|
|
|
};
|
|
|
|
steps.push(step);
|
|
|
|
})
|
|
|
|
steps.push({
|
2021-06-06 15:28:22 +08:00
|
|
|
name: 'Original',
|
2021-06-02 03:08:41 +08:00
|
|
|
time: '',
|
|
|
|
index: 0,
|
|
|
|
current: Undo.index == 0
|
|
|
|
})
|
|
|
|
let step_selected = null;
|
|
|
|
const dialog = new Dialog({
|
|
|
|
id: 'edit_history',
|
|
|
|
title: 'action.edit_history',
|
|
|
|
component: {
|
|
|
|
data() {return {
|
|
|
|
steps,
|
|
|
|
selected: null
|
|
|
|
}},
|
|
|
|
methods: {
|
|
|
|
select(index) {
|
|
|
|
this.selected = step_selected = index;
|
|
|
|
},
|
|
|
|
confirm() {
|
|
|
|
dialog.confirm();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<div id="edit_history_list">
|
|
|
|
<ul>
|
|
|
|
<li v-for="step in steps" :class="{current: step.current, selected: step.index == selected}" @click="select(step.index)" @dblclick="confirm()">
|
|
|
|
{{ step.name }}
|
|
|
|
<div class="edit_history_time">{{ step.time }}</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
},
|
|
|
|
onConfirm() {
|
|
|
|
if (step_selected === null) return;
|
|
|
|
|
|
|
|
let difference = step_selected - Undo.index;
|
|
|
|
if (step_selected < Undo.index) {
|
|
|
|
for (let i = 0; i < -difference; i++) {
|
|
|
|
Undo.undo();
|
|
|
|
}
|
|
|
|
} else if (step_selected > Undo.index) {
|
|
|
|
for (let i = 0; i < difference; i++) {
|
|
|
|
Undo.redo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).show();
|
|
|
|
}
|
|
|
|
})
|
2018-12-27 21:03:04 +08:00
|
|
|
})
|