blockbench/js/blockbench.js

302 lines
11 KiB
JavaScript
Raw Normal View History

2017-10-27 01:00:52 +08:00
var osfs = '/'
var prev_side = 'north';
var uv_clipboard;
var pe_list_data = []
var open_dialog = false;
2018-10-18 01:50:25 +08:00
var open_interface = false;
2017-10-27 01:00:52 +08:00
var tex_version = 1;
var pe_list;
2019-04-13 00:44:18 +08:00
const Pressing = {
shift: false,
ctrl: false,
alt: false,
overrides: {
shift: false,
ctrl: false,
alt: false,
}
2019-04-13 00:44:18 +08:00
}
2019-07-19 23:31:22 +08:00
var Prop = {
2021-11-30 06:50:51 +08:00
_active_panel : 'preview',
get active_panel() {
return Prop._active_panel
},
set active_panel(panel) {
let last_panel = Prop._active_panel;
if (last_panel != panel) {
Prop._active_panel = panel;
Blockbench.dispatchEvent('change_active_panel', {last_panel, panel})
}
},
2019-07-18 00:02:07 +08:00
file_path : '',
file_name : '',
2019-12-16 03:04:31 +08:00
recording : null,
2019-07-18 00:02:07 +08:00
fps : 0,
progress : 0,
session : false,
connections : 0,
2021-01-10 01:33:42 +08:00
facing : 'north',
show_right_bar : true,
show_left_bar : true,
2018-10-18 01:50:25 +08:00
}
2020-04-26 02:25:07 +08:00
2019-01-09 22:54:35 +08:00
const mouse_pos = {x:0,y:0}
const sort_collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
2017-10-27 01:00:52 +08:00
2017-11-17 05:23:41 +08:00
function canvasGridSize(shift, ctrl) {
2018-10-18 01:50:25 +08:00
if (!shift && !ctrl) {
return 16 / Math.clamp(settings.edit_size.value, 1, 512)
2018-10-18 01:50:25 +08:00
} else if (ctrl && shift) {
return 16 / Math.clamp(settings.ctrl_shift_size.value, 1, 4096)
2018-10-18 01:50:25 +08:00
} else if (ctrl) {
return 16 / Math.clamp(settings.ctrl_size.value, 1, 4096)
2018-10-18 01:50:25 +08:00
} else {
return 16 / Math.clamp(settings.shift_size.value, 1, 4096)
2018-10-18 01:50:25 +08:00
}
2017-10-27 01:00:52 +08:00
}
function updateNslideValues() {
2018-10-18 01:50:25 +08:00
2021-01-03 18:14:24 +08:00
if (Outliner.selected.length) {
2018-10-18 01:50:25 +08:00
BarItems.slider_pos_x.update()
BarItems.slider_pos_y.update()
BarItems.slider_pos_z.update()
BarItems.slider_size_x.update()
BarItems.slider_size_y.update()
BarItems.slider_size_z.update()
2019-03-10 05:06:35 +08:00
BarItems.slider_inflate.update()
2020-03-05 03:56:17 +08:00
if (!Project.box_uv) {
BarItems.slider_face_tint.update()
}
2018-10-18 01:50:25 +08:00
}
2021-01-03 18:14:24 +08:00
if (Outliner.selected.length || (Format.bone_rig && Group.selected)) {
2018-10-18 01:50:25 +08:00
BarItems.slider_origin_x.update()
BarItems.slider_origin_y.update()
BarItems.slider_origin_z.update()
BarItems.slider_rotation_x.update()
BarItems.slider_rotation_y.update()
BarItems.slider_rotation_z.update()
2019-07-18 00:02:07 +08:00
if (Format.bone_rig) {
BarItems.bone_reset_toggle.setIcon(Group.selected && Group.selected.reset ? 'check_box' : 'check_box_outline_blank')
2018-10-18 01:50:25 +08:00
} else {
2021-01-03 18:14:24 +08:00
BarItems.rescale_toggle.setIcon(Outliner.selected[0].rescale ? 'check_box' : 'check_box_outline_blank')
2018-10-18 01:50:25 +08:00
}
}
2021-01-03 18:14:24 +08:00
if (Texture.all.length) {
BarItems.animated_texture_frame.update();
}
2017-10-27 01:00:52 +08:00
}
//Selections
2020-12-31 22:08:35 +08:00
function updateSelection(options = {}) {
2021-08-07 18:57:27 +08:00
Outliner.elements.forEach(obj => {
2020-04-26 02:25:07 +08:00
if (selected.includes(obj) && !obj.selected && !obj.locked) {
2019-07-18 00:02:07 +08:00
obj.selectLow()
2020-04-26 02:25:07 +08:00
} else if ((!selected.includes(obj) || obj.locked) && obj.selected) {
2019-07-18 00:02:07 +08:00
obj.unselect()
2018-10-18 01:50:25 +08:00
}
if (obj instanceof Mesh) {
if (Project.selected_vertices[obj.uuid]) {
Project.selected_vertices[obj.uuid].forEachReverse(vkey => {
if (vkey in obj.vertices == false) {
Project.selected_vertices[obj.uuid].remove(vkey);
}
})
}
if (Project.selected_vertices[obj.uuid] && (Project.selected_vertices[obj.uuid].length == 0 || !obj.selected)) {
delete Project.selected_vertices[obj.uuid];
}
2021-08-07 18:57:27 +08:00
}
2019-07-18 00:02:07 +08:00
})
2020-04-26 02:25:07 +08:00
if (Group.selected && Group.selected.locked) Group.selected.unselect()
UVEditor.vue._computedWatchers.mappable_elements.run();
2019-12-16 03:04:31 +08:00
2021-07-30 00:17:26 +08:00
Outliner.elements.forEach(element => {
2021-08-03 04:55:30 +08:00
if (element.preview_controller.updateSelection) {
element.preview_controller.updateSelection(element);
2018-10-18 01:50:25 +08:00
}
})
2021-07-30 00:17:26 +08:00
for (var i = Outliner.selected.length-1; i >= 0; i--) {
if (!selected.includes(Outliner.selected[i])) {
Outliner.selected.splice(i, 1)
2019-07-18 00:02:07 +08:00
}
}
2021-07-30 00:17:26 +08:00
if (Outliner.selected.length) {
2021-01-31 19:51:10 +08:00
document.querySelectorAll('.selection_only').forEach(node => node.style.setProperty('visibility', 'visible'));
if (Modes.edit && Toolbox.selected.id == 'resize_tool' && Format.meshes) {
if (Mesh.selected.length) {
Interface.removeSuggestedModifierKey('alt', 'modifier_actions.resize_both_sides');
Interface.addSuggestedModifierKey('alt', 'modifier_actions.resize_one_side');
} else {
Interface.removeSuggestedModifierKey('alt', 'modifier_actions.resize_one_side');
Interface.addSuggestedModifierKey('alt', 'modifier_actions.resize_both_sides');
}
}
2019-02-04 04:09:35 +08:00
} else {
2019-07-19 23:31:22 +08:00
if (Format.bone_rig && Group.selected) {
2021-01-31 19:51:10 +08:00
document.querySelectorAll('.selection_only').forEach(node => node.style.setProperty('visibility', 'hidden'));
document.querySelectorAll('.selection_only#panel_element').forEach(node => node.style.setProperty('visibility', 'visible'));
2019-07-19 23:31:22 +08:00
} else {
2021-01-31 19:51:10 +08:00
document.querySelectorAll('.selection_only').forEach(node => node.style.setProperty('visibility', 'hidden'));
2021-02-01 03:31:45 +08:00
if (Outliner.selected.length) {
document.querySelectorAll('.selection_only#panel_element').forEach(node => node.style.setProperty('visibility', 'visible'));
2019-07-19 23:31:22 +08:00
}
}
2022-07-09 22:08:59 +08:00
if (Group.selected || (Outliner.selected[0] && Outliner.selected[0].constructor.animator)) {
document.querySelectorAll('.selection_only#panel_bone').forEach(node => node.style.setProperty('visibility', 'visible'));
2021-02-01 03:31:45 +08:00
}
if (Modes.paint) {
document.querySelectorAll('.selection_only#panel_uv').forEach(node => node.style.setProperty('visibility', 'visible'));
2020-01-24 01:53:36 +08:00
}
}
2021-08-18 04:02:23 +08:00
if (Outliner.selected.length || (Format.single_texture && Modes.paint)) {
UVEditor.selected_faces.forEachReverse((fkey, i) => {
if (!UVEditor.getMappableElements().find(el => el.faces[fkey])) {
UVEditor.selected_faces.splice(i, 1);
}
})
2021-08-18 04:02:23 +08:00
UVEditor.loadData()
2019-07-18 00:02:07 +08:00
}
if (Modes.animate) {
2019-12-16 03:04:31 +08:00
updateKeyframeSelection();
2021-02-13 21:32:31 +08:00
if (Timeline.selected_animator && !Timeline.selected_animator.selected) {
Timeline.selected_animator = null;
}
2019-07-18 00:02:07 +08:00
}
2019-12-16 03:04:31 +08:00
BarItems.cube_counter.update();
updateNslideValues();
Interface.status_bar.vue.updateSelectionInfo();
if (settings.highlight_cubes.value || (Mesh.all[0])) updateCubeHighlights();
if (Toolbox.selected.id == 'seam_tool' && Mesh.selected[0]) {
let value;
let selected_vertices = Mesh.selected[0].getSelectedVertices();
Mesh.selected[0].forAllFaces((face) => {
if (value == '') return;
let vertices = face.getSortedVertices();
vertices.forEach((vkey_a, i) => {
let vkey_b = vertices[i+1] || vertices[0];
if (selected_vertices.includes(vkey_a) && selected_vertices.includes(vkey_b)) {
let seam = Mesh.selected[0].getSeam([vkey_a, vkey_b]) || 'auto';
if (value == undefined) {
value = seam;
} else if (value !== seam) {
value = '';
}
}
})
});
BarItems.select_seam.set(value || 'auto');
}
2021-07-30 00:17:26 +08:00
Canvas.updatePivotMarker();
2019-12-16 03:04:31 +08:00
Transformer.updateSelection();
2020-07-16 15:32:59 +08:00
Preview.all.forEach(preview => {
2020-01-24 01:53:36 +08:00
preview.updateAnnotations();
})
2018-11-12 04:19:08 +08:00
2019-12-16 03:04:31 +08:00
BARS.updateConditions();
2019-07-18 00:02:07 +08:00
delete TickUpdates.selection;
2019-12-16 03:04:31 +08:00
Blockbench.dispatchEvent('update_selection');
2017-10-27 01:00:52 +08:00
}
function selectAll() {
2019-12-16 03:04:31 +08:00
if (Modes.animate) {
selectAllKeyframes()
2021-08-19 21:18:01 +08:00
} else if (Prop.active_panel == 'uv') {
UVEditor.selectAll()
} else if (Modes.edit && Mesh.selected.length && Mesh.selected.length === Outliner.selected.length && BarItems.selection_mode.value !== 'object') {
let unselect = Mesh.selected[0].getSelectedVertices().length == Object.keys(Mesh.selected[0].vertices).length;
Mesh.selected.forEach(mesh => {
if (unselect) {
delete Project.selected_vertices[mesh.uuid];
} else {
mesh.getSelectedVertices(true).replace(Object.keys(mesh.vertices));
}
})
updateSelection();
2019-12-16 03:04:31 +08:00
} else if (Modes.edit || Modes.paint) {
let selectable_elements = Outliner.elements.filter(element => !element.locked);
if (Outliner.selected.length < selectable_elements.length) {
if (Outliner.root.length == 1 && !Outliner.root[0].locked) {
Outliner.root[0].select();
} else {
selectable_elements.forEach(obj => {
obj.selectLow()
})
TickUpdates.selection = true;
}
2019-12-16 03:04:31 +08:00
} else {
unselectAll()
}
2018-10-18 01:50:25 +08:00
}
Blockbench.dispatchEvent('select_all')
2017-10-27 01:00:52 +08:00
}
function unselectAll() {
2021-08-07 18:57:27 +08:00
Project.selected_elements.forEachReverse(obj => obj.unselect())
2019-07-18 00:02:07 +08:00
if (Group.selected) Group.selected.unselect()
Group.all.forEach(function(s) {
2018-10-18 01:50:25 +08:00
s.selected = false
})
2021-08-07 18:57:27 +08:00
for (let key in Project.selected_vertices) {
delete Project.selected_vertices[key];
}
2020-03-05 03:56:17 +08:00
TickUpdates.selection = true;
2017-10-27 01:00:52 +08:00
}
2019-04-08 00:53:33 +08:00
//Backup
2022-05-30 04:29:19 +08:00
const AutoBackupModels = {};
2019-04-08 00:53:33 +08:00
setInterval(function() {
2021-07-20 18:07:47 +08:00
if (Project && (Outliner.root.length || Project.textures.length)) {
2019-04-08 00:53:33 +08:00
try {
2022-05-30 04:29:19 +08:00
var model = Codecs.project.compile({compressed: false, backup: true, raw: true});
AutoBackupModels[Project.uuid] = model;
localStorage.setItem('backup_model', JSON.stringify(AutoBackupModels));
2019-04-08 00:53:33 +08:00
} catch (err) {
console.error('Unable to create backup. ', err)
2019-04-08 00:53:33 +08:00
}
}
}, 1e3*30)
2017-10-27 01:00:52 +08:00
//Misc
2019-03-10 05:06:35 +08:00
const TickUpdates = {
2019-07-19 23:31:22 +08:00
Run() {
2020-03-05 03:56:17 +08:00
try {
if (TickUpdates.selection) {
delete TickUpdates.selection;
updateSelection()
}
2021-08-18 04:02:23 +08:00
if (TickUpdates.UVEditor) {
delete TickUpdates.UVEditor;
UVEditor.loadData()
2020-03-05 03:56:17 +08:00
}
if (TickUpdates.texture_list) {
delete TickUpdates.texture_list;
loadTextureDraggable();
}
if (TickUpdates.keyframe_selection) {
delete TickUpdates.keyframe_selection;
Vue.nextTick(updateKeyframeSelection)
}
if (TickUpdates.keybind_conflicts) {
delete TickUpdates.keybind_conflicts;
updateKeybindConflicts();
}
} catch (err) {
console.error(err);
2019-04-08 00:53:33 +08:00
}
2018-10-18 01:50:25 +08:00
}
2017-10-27 01:00:52 +08:00
}
2019-03-10 05:06:35 +08:00
2020-07-16 15:32:59 +08:00
const documentReady = new Promise((resolve, reject) => {
$(document).ready(function() {
resolve()
})
});
2018-10-18 01:50:25 +08:00
const entityMode = {
2020-03-11 05:19:17 +08:00
hardcodes: JSON.parse('{"geometry.chicken":{"body":{"rotation":[90,0,0]}},"geometry.llama":{"chest1":{"rotation":[0,90,0]},"chest2":{"rotation":[0,90,0]},"body":{"rotation":[90,0,0]}},"geometry.cow":{"body":{"rotation":[90,0,0]}},"geometry.sheep.sheared":{"body":{"rotation":[90,0,0]}},"geometry.sheep":{"body":{"rotation":[90,0,0]}},"geometry.phantom":{"body":{"rotation":[0,0,0]},"wing0":{"rotation":[0,0,5.7]},"wingtip0":{"rotation":[0,0,5.7]},"wing1":{"rotation":[0,0,-5.7]},"wingtip1":{"rotation":[0,0,-5.7]},"head":{"rotation":[11.5,0,0]},"tail":{"rotation":[0,0,0]},"tailtip":{"rotation":[0,0,0]}},"geometry.pig":{"body":{"rotation":[90,0,0]}},"geometry.ocelot":{"body":{"rotation":[90,0,0]},"tail1":{"rotation":[90,0,0]},"tail2":{"rotation":[90,0,0]}},"geometry.cat":{"body":{"rotation":[90,0,0]},"tail1":{"rotation":[90,0,0]},"tail2":{"rotation":[90,0,0]}},"geometry.turtle":{"eggbelly":{"rotation":[90,0,0]},"body":{"rotation":[90,0,0]}},"geometry.villager.witch":{"hat2":{"rotation":[-3,0,1.5]},"hat3":{"rotation":[-6,0,3]},"hat4":{"rotation":[-12,0,6]}},"geometry.pufferfish.mid":{"spines_top_front":{"rotation":[45,0,0]},"spines_top_back":{"rotation":[-45,0,0]},"spines_bottom_front":{"rotation":[-45,0,0]},"spines_bottom_back":{"rotation":[45,0,0]},"spines_left_front":{"rotation":[0,45,0]},"spines_left_back":{"rotation":[0,-45,0]},"spines_right_front":{"rotation":[0,-45,0]},"spines_right_back":{"rotation":[0,45,0]}},"geometry.pufferfish.large":{"spines_top_front":{"rotation":[45,0,0]},"spines_top_back":{"rotation":[-45,0,0]},"spines_bottom_front":{"rotation":[-45,0,0]},"spines_bottom_back":{"rotation":[45,0,0]},"spines_left_front":{"rotation":[0,45,0]},"spines_left_back":{"rotation":[0,-45,0]},"spines_right_front":{"rotation":[0,-45,0]},"spines_right_back":{"rotation":[0,45,0]}},"geometry.tropicalfish_a":{"leftFin":{"rotation":[0,-35,0]},"rightFin":{"rotation":[0,35,0]}},"geometry.tropicalfish_b":{"leftFin":{"rotation":[0,-35,0]},"rightFin":{"rotation":[0,35,0]}}}')
2018-10-18 01:50:25 +08:00
}