blockbench/js/blockbench.js

241 lines
8.6 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
}
2017-10-27 01:00:52 +08:00
var main_uv;
2019-07-19 23:31:22 +08:00
var Prop = {
2019-07-18 00:02:07 +08:00
active_panel : 'preview',
2021-05-07 21:57:24 +08:00
view_mode : 'textured',
2019-07-18 00:02:07 +08:00
file_path : '',
file_name : '',
added_models : 0,
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
2019-02-04 04:09:35 +08:00
function onVueSetup(func) {
if (!onVueSetup.funcs) {
onVueSetup.funcs = []
2018-10-18 01:50:25 +08:00
}
2019-02-04 04:09:35 +08:00
onVueSetup.funcs.push(func)
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-02-01 03:31:45 +08:00
if (Modes.animate && NullObject.selected[0]) {
2020-08-30 21:00:41 +08:00
BarItems.slider_ik_chain_length.update();
2021-02-01 03:31:45 +08:00
BarItems.ik_enabled.setIcon(NullObject.selected[0].ik_enabled ? 'check_box' : 'check_box_outline_blank')
2019-12-16 03:04:31 +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 = {}) {
2019-07-18 00:02:07 +08:00
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
}
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()
2019-12-16 03:04:31 +08:00
2019-07-18 00:02:07 +08:00
Cube.all.forEach(cube => {
if (cube.visibility) {
var mesh = cube.mesh
2018-12-03 02:37:06 +08:00
if (mesh && mesh.outline) {
2019-07-18 00:02:07 +08:00
mesh.outline.visible = cube.selected
2018-10-18 01:50:25 +08:00
}
}
})
2019-07-18 00:02:07 +08:00
for (var i = Cube.selected.length-1; i >= 0; i--) {
if (!selected.includes(Cube.selected[i])) {
Cube.selected.splice(i, 1)
}
}
if (Cube.selected.length) {
2021-01-31 19:51:10 +08:00
document.querySelectorAll('.selection_only').forEach(node => node.style.setProperty('visibility', 'visible'));
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#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) {
2021-01-31 19:51:10 +08:00
document.querySelectorAll('.selection_only#element').forEach(node => node.style.setProperty('visibility', 'visible'));
2019-07-19 23:31:22 +08:00
}
}
2021-02-01 03:31:45 +08:00
if (Group.selected || NullObject.selected[0]) {
document.querySelectorAll('.selection_only#bone').forEach(node => node.style.setProperty('visibility', 'visible'));
}
2020-01-24 01:53:36 +08:00
if (Format.single_texture && Modes.paint) {
2021-01-31 19:51:10 +08:00
document.querySelectorAll('.selection_only#uv').forEach(node => node.style.setProperty('visibility', 'visible'));
2020-01-24 01:53:36 +08:00
}
}
if (Cube.selected.length || (Format.single_texture && Modes.paint)) {
main_uv.jquery.size.find('.uv_mapping_overlay').remove()
main_uv.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();
2020-12-31 22:08:35 +08:00
if (settings.highlight_cubes.value) updateCubeHighlights();
2019-12-16 03:04:31 +08:00
Canvas.updateOrigin();
Transformer.updateSelection();
Transformer.update();
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()
} else if (Modes.edit || Modes.paint) {
2021-01-31 05:30:34 +08:00
if (Outliner.selected.length < Outliner.elements.length) {
if (Outliner.root.length == 1) {
Outliner.root[0].select();
} else {
2021-01-31 05:30:34 +08:00
Outliner.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() {
2019-07-18 00:02:07 +08:00
selected.forEachReverse(obj => obj.unselect())
if (Group.selected) Group.selected.unselect()
Group.all.forEach(function(s) {
2018-10-18 01:50:25 +08:00
s.selected = false
})
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
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 {
var model = Codecs.project.compile({compressed: false, backup: true});
2019-04-08 00:53:33 +08:00
localStorage.setItem('backup_model', model)
} catch (err) {
console.log('Unable to create backup. ', err)
}
}
}, 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()
}
if (TickUpdates.main_uv) {
delete TickUpdates.main_uv;
main_uv.loadData()
}
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
}