blockbench/js/blockbench.js

483 lines
14 KiB
JavaScript
Raw Normal View History

2017-10-26 19:00:52 +02:00
var osfs = '/'
2018-03-28 20:48:11 +02:00
var selected = [];
2017-10-26 19:00:52 +02:00
var prev_side = 'north';
var uv_clipboard;
var outliner, texturelist;
var pe_list_data = []
var open_dialog = false;
2018-10-17 19:50:25 +02:00
var open_interface = false;
2017-10-26 19:00:52 +02:00
var tex_version = 1;
var pe_list;
2019-04-12 18:44:18 +02:00
const Pressing = {
shift: false,
ctrl: false,
alt: false,
}
2017-10-26 19:00:52 +02:00
var main_uv;
2019-07-19 17:31:22 +02:00
var Prop = {
2019-07-17 18:02:07 +02:00
active_panel : 'preview',
wireframe : false,
file_path : '',
file_name : '',
added_models : 0,
2019-12-15 20:04:31 +01:00
recording : null,
2019-07-17 18:02:07 +02:00
project_saved : true,
fps : 0,
zoom : 100,
progress : 0,
session : false,
connections : 0,
facing : 'north'
2018-10-17 19:50:25 +02:00
}
const Project = {
name : '',
parent : '',
2019-07-17 18:02:07 +02:00
geometry_name : '',
2018-10-17 19:50:25 +02:00
description : '',
2019-07-17 18:02:07 +02:00
_box_uv : false,
get box_uv() {return Project._box_uv},
set box_uv(v) {
if (Project._box_uv != v) {
Project._box_uv = v;
2019-12-15 20:04:31 +01:00
switchBoxUV(v);
2019-07-17 18:02:07 +02:00
}
},
2019-12-15 20:04:31 +01:00
get texture_width() {return Project._texture_width},
get texture_height() {return Project._texture_height},
set texture_width(n) {
n = parseInt(n)||16
Vue.nextTick(updateProjectResolution)
Project._texture_width = n;
},
set texture_height(n) {
n = parseInt(n)||16
Vue.nextTick(updateProjectResolution)
Project._texture_height = n;
},
_texture_width : 16,
_texture_height : 16,
2018-10-17 19:50:25 +02:00
ambientocclusion: true,
2019-07-17 18:02:07 +02:00
get optional_box_uv() {
return Format.optional_box_uv;
}
2017-10-26 19:00:52 +02:00
}
2019-01-09 15:54:35 +01:00
const mouse_pos = {x:0,y:0}
const sort_collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
2017-10-26 19:00:52 +02:00
$.ajaxSetup({ cache: false });
2019-02-03 21:09:35 +01:00
function onVueSetup(func) {
if (!onVueSetup.funcs) {
onVueSetup.funcs = []
2018-10-17 19:50:25 +02:00
}
2019-02-03 21:09:35 +01:00
onVueSetup.funcs.push(func)
2017-10-26 19:00:52 +02:00
}
2017-11-16 22:23:41 +01:00
function canvasGridSize(shift, ctrl) {
2018-10-17 19:50:25 +02:00
if (!shift && !ctrl) {
2019-07-21 23:17:36 +02:00
return 16 / limitNumber(settings.edit_size.value, 1, 512)
2018-10-17 19:50:25 +02:00
} else if (ctrl && shift) {
2019-07-21 23:17:36 +02:00
var basic = 16 / limitNumber(settings.edit_size.value, 1, 512)
var control = 16 / limitNumber(settings.ctrl_size.value, 1, 4096)
var shift = 16 / limitNumber(settings.shift_size.value, 1, 4096)
2018-10-17 19:50:25 +02:00
control = basic / control
return shift / control
} else if (ctrl) {
2019-07-21 23:17:36 +02:00
return 16 / limitNumber(settings.ctrl_size.value, 1, 4096)
2018-10-17 19:50:25 +02:00
} else {
2019-07-21 23:17:36 +02:00
return 16 / limitNumber(settings.shift_size.value, 1, 4096)
2018-10-17 19:50:25 +02:00
}
2017-10-26 19:00:52 +02:00
}
function updateNslideValues() {
2018-10-17 19:50:25 +02:00
if (selected.length) {
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-09 22:06:35 +01:00
BarItems.slider_inflate.update()
2018-10-17 19:50:25 +02:00
}
2019-07-17 18:02:07 +02:00
if (selected.length || (Format.bone_rig && Group.selected)) {
2018-10-17 19:50:25 +02: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-17 18:02:07 +02:00
if (Format.bone_rig) {
BarItems.bone_reset_toggle.setIcon(Group.selected && Group.selected.reset ? 'check_box' : 'check_box_outline_blank')
2018-10-17 19:50:25 +02:00
} else {
BarItems.rescale_toggle.setIcon(selected[0].rescale ? 'check_box' : 'check_box_outline_blank')
}
}
2019-12-15 20:04:31 +01:00
if (Modes.animate && Group.selected) {
//BarItems.slider_ik_chain_length.update();
//BarItems.ik_enabled.setIcon(Group.selected.ik_enabled ? 'check_box' : 'check_box_outline_blank')
}
2017-10-26 19:00:52 +02:00
}
2019-08-01 00:01:47 +02:00
function setProjectResolution(width, height, modify_uv) {
2019-08-01 11:07:33 +02:00
if (Project.texture_width / width != Project.texture_width / height) {
modify_uv = false;
}
Undo.initEdit({uv_mode: true, elements: Cube.all, uv_only: true})
2019-08-01 00:01:47 +02:00
let old_res = {
x: Project.texture_width,
y: Project.texture_height
}
Project.texture_width = width;
Project.texture_height = height;
if (modify_uv) {
var multiplier = [
2019-08-01 11:07:33 +02:00
Project.texture_width/old_res.x,
Project.texture_height/old_res.y
2019-08-01 00:01:47 +02:00
]
function shiftCube(cube, axis) {
if (Project.box_uv) {
obj.uv_offset[axis] *= multiplier[axis];
} else {
for (var face in cube.faces) {
var uv = cube.faces[face];
uv[axis] *= multiplier[axis];
uv[axis+2] *= multiplier[axis];
}
}
}
if (old_res.x != Project.texture_width && Math.areMultiples(old_res.x, Project.texture_width)) {
Cube.all.forEach(cube => shiftCube(cube, 0));
}
if (old_res.y != Project.texture_height && Math.areMultiples(old_res.x, Project.texture_width)) {
Cube.all.forEach(cube => shiftCube(cube, 1));
}
}
2019-08-01 11:07:33 +02:00
Undo.finishEdit('Changed project resolution')
2019-08-01 00:01:47 +02:00
Canvas.updateAllUVs()
if (selected.length) {
main_uv.loadData()
}
}
2019-12-15 20:04:31 +01:00
function updateProjectResolution() {
$('#project_resolution_status').text(`${Project.texture_width}${Project.texture_height}`);
}
2017-10-26 19:00:52 +02:00
//Selections
function updateSelection() {
2019-07-17 18:02:07 +02:00
elements.forEach(obj => {
if (selected.includes(obj) && !obj.selected) {
obj.selectLow()
} else if (!selected.includes(obj) && obj.selected) {
obj.unselect()
2018-10-17 19:50:25 +02:00
}
2019-07-17 18:02:07 +02:00
})
2019-12-15 20:04:31 +01:00
2019-07-17 18:02:07 +02:00
Cube.all.forEach(cube => {
if (cube.visibility) {
var mesh = cube.mesh
2018-12-02 19:37:06 +01:00
if (mesh && mesh.outline) {
2019-07-17 18:02:07 +02:00
mesh.outline.visible = cube.selected
2018-10-17 19:50:25 +02:00
}
}
})
2019-07-17 18:02:07 +02: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) {
2019-02-03 21:09:35 +01:00
main_uv.jquery.size.find('.uv_mapping_overlay').remove()
2018-10-17 19:50:25 +02:00
main_uv.loadData()
2019-07-17 18:02:07 +02:00
$('.selection_only').css('visibility', 'visible')
2019-02-03 21:09:35 +01:00
} else {
2019-07-19 17:31:22 +02:00
if (Format.bone_rig && Group.selected) {
2019-07-26 13:33:29 +02:00
$('.selection_only').css('visibility', 'hidden')
$('.selection_only#element').css('visibility', 'visible')
2019-07-19 17:31:22 +02:00
} else {
$('.selection_only').css('visibility', 'hidden')
if (Locator.selected.length) {
$('.selection_only#element').css('visibility', 'visible')
}
}
2019-07-17 18:02:07 +02:00
}
if (Modes.animate) {
2019-12-15 20:04:31 +01:00
updateKeyframeSelection();
2019-07-17 18:02:07 +02:00
}
2019-12-15 20:04:31 +01:00
BarItems.cube_counter.update();
updateNslideValues();
Blockbench.globalMovement = isMovementGlobal();
updateCubeHighlights();
Canvas.updateOrigin();
Transformer.updateSelection();
Transformer.update();
2018-11-11 21:19:08 +01:00
2019-12-15 20:04:31 +01:00
BARS.updateConditions();
2019-07-17 18:02:07 +02:00
delete TickUpdates.selection;
2019-12-15 20:04:31 +01:00
Blockbench.dispatchEvent('update_selection');
2017-10-26 19:00:52 +02:00
}
function selectAll() {
2019-12-15 20:04:31 +01:00
if (Modes.animate) {
selectAllKeyframes()
} else if (Modes.edit || Modes.paint) {
if (selected.length < elements.length) {
elements.forEach(obj => {
obj.selectLow()
})
updateSelection()
} else {
unselectAll()
}
2018-10-17 19:50:25 +02:00
}
Blockbench.dispatchEvent('select_all')
2017-10-26 19:00:52 +02:00
}
function unselectAll() {
2019-07-17 18:02:07 +02:00
selected.forEachReverse(obj => obj.unselect())
if (Group.selected) Group.selected.unselect()
Group.all.forEach(function(s) {
2018-10-17 19:50:25 +02:00
s.selected = false
})
updateSelection()
2017-10-26 19:00:52 +02:00
}
function createSelection() {
2018-10-17 19:50:25 +02:00
if ($('#selgen_new').is(':checked')) {
selected.length = 0
}
2019-07-17 18:02:07 +02:00
if (Group.selected) {
Group.selected.unselect()
2018-10-17 19:50:25 +02:00
}
var name_seg = $('#selgen_name').val().toUpperCase()
2019-08-17 18:26:14 +02:00
var tex_seg = $('#selgen_texture').val().toLowerCase()
2018-10-17 19:50:25 +02:00
var rdm = $('#selgen_random').val()/100
var array = elements
2019-07-17 18:02:07 +02:00
if ($('#selgen_group').is(':checked') && Group.selected) {
array = Group.selected.children
2018-10-17 19:50:25 +02:00
}
2019-08-17 18:26:14 +02:00
array.forEach(function(obj) {
if (obj.name.toUpperCase().includes(name_seg) === false) return;
if (obj instanceof Cube && tex_seg && !Format.single_texture) {
var has_tex = false;
for (var key in obj.faces) {
var tex = obj.faces[key].getTexture();
if (tex && tex.name.includes(tex_seg)) {
has_tex = true
}
}
if (!has_tex) return;
}
2018-10-17 19:50:25 +02:00
if (Math.random() > rdm) return;
2019-08-17 18:26:14 +02:00
selected.push(obj)
2018-10-17 19:50:25 +02:00
})
updateSelection()
if (selected.length) {
selected[0].showInOutliner()
}
hideDialog()
2017-10-26 19:00:52 +02:00
}
2018-12-27 14:03:04 +01:00
//Modes
class Mode extends KeybindItem {
constructor(data) {
super(data)
this.id = data.id;
this.name = data.name || tl('mode.'+this.id);
this.selected = false
this.default_tool = data.default_tool;
2019-07-17 18:02:07 +02:00
this.center_windows = data.center_windows||['preview'];
2018-12-27 14:03:04 +01:00
this.selectCubes = data.selectCubes !== false
2019-07-17 18:02:07 +02:00
this.hide_toolbars = data.hide_toolbars
2018-12-27 14:03:04 +01:00
this.condition = data.condition;
this.onSelect = data.onSelect;
this.onUnselect = data.onUnselect;
Modes.options[this.id] = this;
}
select() {
2019-07-17 18:02:07 +02:00
var scope = this;
if (Modes.selected) {
delete Modes[Modes.selected.id];
2019-08-17 18:26:14 +02:00
Modes.previous_id = Modes.selected.id;
2019-07-17 18:02:07 +02:00
}
2018-12-27 14:03:04 +01:00
if (typeof Modes.selected.onUnselect === 'function') {
Modes.selected.onUnselect()
}
if (Modes.selected.selected) {
Modes.selected.selected = false
}
this.selected = true;
Modes.id = this.id
Modes.selected = this;
2019-07-17 18:02:07 +02:00
Modes[Modes.selected.id] = true;
$('#center > #preview').toggle(this.center_windows.includes('preview'));
$('#center > #timeline').toggle(this.center_windows.includes('timeline'));
$('#center > #start_screen').toggle(this.center_windows.includes('start_screen'));
if (this.hide_toolbars) {
$('#main_toolbar .toolbar_wrapper').css('visibility', 'hidden')
} else {
$('#main_toolbar .toolbar_wrapper').css('visibility', 'visible')
}
if (typeof this.onSelect === 'function') {
this.onSelect()
}
2018-12-27 14:03:04 +01:00
updateInterface()
Canvas.updateRenderSides()
if (BarItems[this.default_tool]) {
BarItems[this.default_tool].select()
} else {
BarItems.move_tool.select()
}
updateSelection()
}
trigger() {
if (Condition(this.condition)) {
this.select()
}
}
}
const Modes = {
id: 'edit',
selected: false,
options: {},
};
2019-02-03 21:09:35 +01:00
onVueSetup(function() {
Modes.vue = new Vue({
el: '#mode_selector',
data: {
options: Modes.options
}
})
});
2018-12-27 14:03:04 +01:00
BARS.defineActions(function() {
2019-07-17 18:02:07 +02:00
new Mode({
id: 'start',
category: 'navigate',
center_windows: ['start_screen'],
hide_toolbars: true,
onSelect: function () {
},
onUnselect: function () {
}
})
2018-12-27 14:03:04 +01:00
new Mode({
id: 'edit',
default_tool: 'move_tool',
2019-04-07 18:53:33 +02:00
category: 'navigate',
2019-07-17 18:02:07 +02:00
condition: () => Format,
2018-12-27 14:03:04 +01:00
keybind: new Keybind({key: 49})
})
new Mode({
id: 'paint',
default_tool: 'brush_tool',
2019-04-07 18:53:33 +02:00
category: 'navigate',
2019-07-17 18:02:07 +02:00
condition: () => Format,
keybind: new Keybind({key: 50}),
onSelect: () => {
2019-08-17 18:26:14 +02:00
if (Modes.previous_id == 'animate') {
Animator.preview();
}
2019-07-17 18:02:07 +02:00
Cube.all.forEach(cube => {
Canvas.buildGridBox(cube)
})
2019-12-15 20:04:31 +01:00
$('#main_colorpicker').spectrum('set', ColorPanel.vue._data.main_color);
BarItems.slider_color_h.update();
BarItems.slider_color_s.update();
BarItems.slider_color_v.update();
2019-07-17 18:02:07 +02:00
},
onUnselect: () => {
Cube.all.forEach(cube => {
Canvas.buildGridBox(cube)
})
},
2018-12-27 14:03:04 +01:00
})
new Mode({
id: 'display',
selectCubes: false,
default_tool: 'move_tool',
2019-04-07 18:53:33 +02:00
category: 'navigate',
2018-12-27 14:03:04 +01:00
keybind: new Keybind({key: 51}),
2019-07-17 18:02:07 +02:00
condition: () => Format.display_mode,
2018-12-27 14:03:04 +01:00
onSelect: () => {
enterDisplaySettings()
},
onUnselect: () => {
exitDisplaySettings()
},
})
new Mode({
id: 'animate',
default_tool: 'move_tool',
2019-04-07 18:53:33 +02:00
category: 'navigate',
2019-07-17 18:02:07 +02:00
center_windows: ['preview', 'timeline'],
2019-12-15 20:04:31 +01:00
keybind: new Keybind({key: 52}),
2019-07-17 18:02:07 +02:00
condition: () => Format.animation_mode,
2018-12-27 14:03:04 +01:00
onSelect: () => {
Animator.join()
},
onUnselect: () => {
Animator.leave()
}
})
2019-12-15 20:04:31 +01:00
//Update to 3.2.0
if (Modes.options.animate.keybind.key == 51) {
Modes.options.animate.keybind.set({key: 52})
}
2018-12-27 14:03:04 +01:00
})
2019-04-07 18:53:33 +02:00
//Backup
setInterval(function() {
2019-07-17 18:02:07 +02:00
if (Outliner.root.length || textures.length) {
2019-04-07 18:53:33 +02:00
try {
2019-07-17 18:02:07 +02:00
var model = Codecs.project.compile();
2019-04-07 18:53:33 +02:00
localStorage.setItem('backup_model', model)
} catch (err) {
console.log('Unable to create backup. ', err)
}
}
}, 1e3*30)
2017-10-26 19:00:52 +02:00
//Misc
2019-03-09 22:06:35 +01:00
const TickUpdates = {
2019-07-19 17:31:22 +02:00
Run() {
2019-03-09 22:06:35 +01:00
if (TickUpdates.outliner) {
delete TickUpdates.outliner;
loadOutlinerDraggable()
}
if (TickUpdates.selection) {
delete TickUpdates.selection;
updateSelection()
}
2019-04-07 18:53:33 +02:00
if (TickUpdates.main_uv) {
delete TickUpdates.main_uv;
main_uv.loadData()
}
if (TickUpdates.texture_list) {
delete TickUpdates.texture_list;
loadTextureDraggable();
}
if (TickUpdates.keyframes) {
delete TickUpdates.keyframes;
Vue.nextTick(Timeline.update)
}
2019-09-06 00:16:54 +02:00
if (TickUpdates.keyframe_selection) {
delete TickUpdates.keyframe_selection;
Vue.nextTick(updateKeyframeSelection)
}
2019-12-15 20:04:31 +01:00
if (TickUpdates.keybind_conflicts) {
delete TickUpdates.keybind_conflicts;
updateKeybindConflicts();
2019-04-07 18:53:33 +02:00
}
2018-10-17 19:50:25 +02:00
}
2017-10-26 19:00:52 +02:00
}
2019-03-09 22:06:35 +01:00
2018-10-17 19:50:25 +02:00
const entityMode = {
hardcodes: {"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]}}},
}