blockbench/js/blockbench.js

828 lines
23 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,
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;
switchAutoUV(v);
}
},
2018-10-17 19:50:25 +02:00
texture_width : 16,
texture_height : 16,
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 });
function initializeApp() {
2018-10-17 19:50:25 +02:00
//Browser Detection
Blockbench.browser = 'electron'
if (isApp === false) {
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
Blockbench.browser = 'firefox'
} else if (!!window.chrome && !!window.chrome.webstore) {
Blockbench.browser = 'chrome'
} else if ((!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) {
Blockbench.browser = 'opera'
} else if (/constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification))) {
Blockbench.browser = 'safari'
} else if (!!document.documentMode) {
Blockbench.browser = 'internet_explorer'
} else if (!!window.StyleMedia) {
Blockbench.browser = 'edge'
}
if (navigator.appVersion.indexOf("Win") != -1) OSName = 'Windows';
if (navigator.appVersion.indexOf("Mac") != -1) OSName = 'MacOS';
if (navigator.appVersion.indexOf("Linux") != -1)OSName = 'Linux';
if (['edge', 'internet_explorer'].includes(Blockbench.browser)) {
alert(capitalizeFirstLetter(Blockbench.browser)+' does not support Blockbench')
}
$('.local_only').remove()
} else {
$('.web_only').remove()
}
2018-12-29 12:26:02 +01:00
if (localStorage.getItem('welcomed_version') != appVersion) {
Blockbench.addFlag('after_update')
2019-07-17 18:02:07 +02:00
localStorage.setItem('welcomed_version', appVersion)
2018-12-29 12:26:02 +01:00
}
2018-10-17 19:50:25 +02:00
BARS.setupActions()
BARS.setupToolbars()
BARS.setupVue()
MenuBar.setup()
//Misc
translateUI()
2019-02-03 21:09:35 +01:00
console.log('Blockbench ' + appVersion + (isApp
? (' Desktop (' + Blockbench.operating_system +')')
: (' Web ('+capitalizeFirstLetter(Blockbench.browser)+')')
))
2019-07-17 18:02:07 +02:00
var startups = parseInt(localStorage.getItem('startups')||0);
localStorage.setItem('startups', startups+1);
2018-10-17 19:50:25 +02:00
if (isApp) {
updateRecentProjects()
}
setInterval(function() {
Prop.fps = framespersecond;
framespersecond = 0;
}, 1000)
main_uv = new UVEditor('main_uv', false, true)
main_uv.setToMainSlot()
2019-02-03 21:09:35 +01:00
onVueSetup.funcs.forEach((func) => {
if (typeof func === 'function') {
func()
}
})
2017-10-26 19:00:52 +02:00
//JQuery UI
2018-10-17 19:50:25 +02:00
$('#cubes_list').droppable({
greedy: true,
accept: 'div.outliner_object',
tolerance: 'pointer',
hoverClass: 'drag_hover',
drop: function(event, ui) {
2019-07-17 18:02:07 +02:00
var item = Outliner.root.findRecursive('uuid', $(ui.draggable).parent().attr('id'))
2018-10-17 19:50:25 +02:00
dropOutlinerObjects(item, undefined, event)
}
})
$('#cubes_list').contextmenu(function(event) {
2019-07-17 18:02:07 +02:00
event.stopPropagation();
event.preventDefault();
2018-12-02 19:37:06 +01:00
Interface.Panels.outliner.menu.show(event)
2018-10-17 19:50:25 +02:00
})
$('#texture_list').contextmenu(function(event) {
2018-12-02 19:37:06 +01:00
Interface.Panels.textures.menu.show(event)
2018-10-17 19:50:25 +02:00
})
setupInterface()
setupDragHandlers()
2019-07-17 18:02:07 +02:00
Modes.options.start.select()
2018-11-11 21:19:08 +01:00
Blockbench.setup_successful = true
2019-07-17 18:02:07 +02:00
2017-10-26 19:00:52 +02:00
}
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) {
2018-12-27 14:03:04 +01:00
return 16 / limitNumber(settings.edit_size.value, 1, 1024)
2018-10-17 19:50:25 +02:00
} else if (ctrl && shift) {
2018-12-27 14:03:04 +01:00
var basic = 16 / limitNumber(settings.edit_size.value, 1, 1024)
2018-10-17 19:50:25 +02:00
var control = 16 / limitNumber(settings.ctrl_size.value, 1, 1024)
var shift = 16 / limitNumber(settings.shift_size.value, 1, 1024)
control = basic / control
return shift / control
} else if (ctrl) {
return 16 / limitNumber(settings.ctrl_size.value, 1, 1024)
} else {
return 16 / limitNumber(settings.shift_size.value, 1, 1024)
}
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')
}
}
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
})
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) {
$('.selection_only').css('visibility', 'visible')
} 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) {
if (Animator.selected && Group.selected) {
Animator.selected.getBoneAnimator().select()
}
updateKeyframeSelection()
}
2018-12-29 12:26:02 +01:00
BarItems.cube_counter.update()
2018-10-17 19:50:25 +02:00
updateNslideValues()
Blockbench.globalMovement = isMovementGlobal()
2018-11-11 21:19:08 +01:00
2019-07-17 18:02:07 +02:00
Canvas.updateOrigin()
Transformer.updateSelection()
2018-10-17 19:50:25 +02:00
Transformer.update()
2019-07-17 18:02:07 +02:00
2018-10-17 19:50:25 +02:00
BARS.updateConditions()
2019-07-17 18:02:07 +02:00
delete TickUpdates.selection;
2018-10-17 19:50:25 +02:00
Blockbench.dispatchEvent('update_selection')
2017-10-26 19:00:52 +02:00
}
function selectAll() {
2018-10-17 19:50:25 +02:00
if (selected.length < elements.length) {
2019-07-17 18:02:07 +02:00
elements.forEach(obj => {
obj.selectLow()
})
updateSelection()
2018-10-17 19:50:25 +02:00
} else {
2019-07-17 18:02:07 +02:00
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()
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
}
array.forEach(function(s) {
if (s.name.toUpperCase().includes(name_seg) === false) return;
if (Math.random() > rdm) return;
selected.push(s)
})
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];
}
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: () => {
Cube.all.forEach(cube => {
Canvas.buildGridBox(cube)
})
},
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'],
2018-12-27 14:03:04 +01:00
keybind: new Keybind({key: 51}),
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-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-03-09 22:06:35 +01:00
}
}
const Screencam = {
2019-07-19 17:31:22 +02:00
fullScreen(options, cb) {
2018-10-17 19:50:25 +02:00
setTimeout(function() {
currentwindow.capturePage(function(screenshot) {
var dataUrl = screenshot.toDataURL()
dataUrl = dataUrl.replace('data:image/png;base64,','')
2018-11-11 21:19:08 +01:00
Jimp.read(Buffer.from(dataUrl, 'base64')).then(function(image) {
2018-10-17 19:50:25 +02:00
if (options && options.width && options.height) {
image.contain(options.width, options.height)
}
image.getBase64(Jimp.MIME_PNG, function(a, dataUrl){
Screencam.returnScreenshot(dataUrl, cb)
})
});
})
}, 40)
},
2019-07-19 17:31:22 +02:00
returnScreenshot(dataUrl, cb) {
2018-10-17 19:50:25 +02:00
if (cb) {
cb(dataUrl)
} else if (isApp) {
var screenshot = nativeImage.createFromDataURL(dataUrl)
var img = new Image()
2018-12-02 19:37:06 +01:00
var is_gif = dataUrl.substr(5, 9) == 'image/gif'
2018-10-17 19:50:25 +02:00
img.src = dataUrl
2018-12-02 19:37:06 +01:00
2018-12-27 14:03:04 +01:00
var btns = [tl('dialog.cancel'), tl('dialog.save')]
2018-12-02 19:37:06 +01:00
if (!is_gif) {
btns.push(tl('message.screenshot.clipboard'))
}
2018-10-17 19:50:25 +02:00
Blockbench.showMessageBox({
translateKey: 'screenshot',
icon: img,
2018-12-02 19:37:06 +01:00
buttons: btns,
2018-12-27 14:03:04 +01:00
confirm: 1,
cancel: 0
2018-10-17 19:50:25 +02:00
}, function(result) {
2018-12-27 14:03:04 +01:00
if (result === 1) {
2019-01-09 15:54:35 +01:00
electron.dialog.showSaveDialog(currentwindow, {filters: [ {name: tl('data.image'), extensions: [is_gif ? 'gif' : 'png']} ]}, function (fileName) {
2018-10-17 19:50:25 +02:00
if (fileName === undefined) {
return;
}
2019-07-17 18:02:07 +02:00
fs.writeFile(fileName, Buffer(dataUrl.split(',')[1], 'base64'), err => {})
2018-10-17 19:50:25 +02:00
})
2018-12-27 14:03:04 +01:00
} else if (result === 2) {
2018-10-17 19:50:25 +02:00
clipboard.writeImage(screenshot)
}
})
} else {
new Dialog({
title: tl('message.screenshot.right_click'),
id: 'screenie',
lines: ['<img src="'+dataUrl+'" width="600px" class="allow_default_menu"></img>'],
draggable: true,
singleButton: true
}).show()
}
},
2019-07-19 17:31:22 +02:00
cleanCanvas(options, cb) {
2018-10-17 19:50:25 +02:00
quad_previews.current.screenshot(options, cb)
2018-12-02 19:37:06 +01:00
},
2019-07-19 17:31:22 +02:00
createGif(options, cb) {
2019-07-17 18:02:07 +02:00
/*
var images = [];
var preview = quad_previews.current;
var interval = setInterval(function() {
var shot = preview.canvas.toDataURL()
images.push(shot);
if (images.length >= options.length/1000*options.fps) {
clearInterval(interval);
gifshot.createGIF({
images,
frameDuration: 10/options.fps,
progressCallback: cl,
text: 'BLOCKBENCH'
}, obj => {
Screencam.returnScreenshot(obj.image, cb);
})
}
}, 1000/options.fps)
//Does not support transparency
*/
2018-12-02 19:37:06 +01:00
if (typeof options !== 'object') {
options = {}
}
2018-12-27 14:03:04 +01:00
if (!options.length) {
options.length = 1000
}
2019-04-07 18:53:33 +02:00
var preview = quad_previews.current;
2018-12-02 19:37:06 +01:00
var interval = options.fps ? (1000/options.fps) : 100
var gif = new GIF({
repeat: options.repeat,
quality: options.quality,
transparent: 0x000000,
})
2018-12-27 14:03:04 +01:00
var frame_count = (options.length/interval)
2019-04-07 18:53:33 +02:00
if (options.turnspeed) {
preview.controls.autoRotate = true;
preview.controls.autoRotateSpeed = options.turnspeed;
}
2018-12-27 14:03:04 +01:00
gif.on('finished', blob => {
var reader = new FileReader()
reader.onload = () => {
if (!options.silent) {
Blockbench.setProgress(0)
Blockbench.setStatusBarText()
}
Screencam.returnScreenshot(reader.result, cb)
}
reader.readAsDataURL(blob)
})
if (!options.silent) {
Blockbench.setStatusBarText(tl('status_bar.recording_gif'))
gif.on('progress', Blockbench.setProgress)
}
var frames = 0;
var loop = setInterval(() => {
var img = new Image()
2019-04-07 18:53:33 +02:00
img.src = preview.canvas.toDataURL()
2018-12-27 14:03:04 +01:00
img.onload = () => {
gif.addFrame(img, {delay: interval})
}
Blockbench.setProgress(interval*frames/options.length)
frames++;
2018-12-02 19:37:06 +01:00
}, interval)
setTimeout(() => {
clearInterval(loop)
2018-12-27 14:03:04 +01:00
if (!options.silent) {
Blockbench.setStatusBarText(tl('status_bar.processing_gif'))
}
2018-12-02 19:37:06 +01:00
gif.render()
if (Animator.open && Timeline.playing) {
Timeline.pause()
}
2019-04-07 18:53:33 +02:00
if (options.turnspeed) {
preview.controls.autoRotate = false;
}
2018-12-27 14:03:04 +01:00
}, options.length)
2018-10-17 19:50:25 +02:00
}
2017-10-26 19:00:52 +02:00
}
2019-03-09 22:06:35 +01:00
const Clipbench = {
2019-07-17 18:02:07 +02:00
elements: [],
2019-07-19 17:31:22 +02:00
copy(event, cut) {
2018-10-17 19:50:25 +02:00
var p = Prop.active_panel
2019-07-17 18:02:07 +02:00
var text = window.getSelection()+'';
2019-04-12 18:44:18 +02:00
if (text) {
Clipbench.setText(text)
} else if (open_dialog == 'uv_dialog') {
2018-10-17 19:50:25 +02:00
uv_dialog.copy(event)
2019-04-12 18:44:18 +02:00
2018-10-17 19:50:25 +02:00
} else if (display_mode) {
2018-12-16 16:18:20 +01:00
DisplayMode.copy()
2019-04-12 18:44:18 +02:00
2018-11-11 21:19:08 +01:00
} else if (Animator.open) {
if (Timeline.selected.length) {
Clipbench.setKeyframes(Timeline.selected)
}
2018-10-17 19:50:25 +02:00
} else if (p == 'uv' || p == 'preview') {
main_uv.copy(event)
2019-04-12 18:44:18 +02:00
2018-10-17 19:50:25 +02:00
} else if (p == 'textures' && isApp) {
if (textures.selected) {
Clipbench.setTexture(textures.selected)
}
} else if (p == 'outliner') {
2019-07-17 18:02:07 +02:00
Clipbench.setElements()
2018-10-17 19:50:25 +02:00
Clipbench.setGroup()
2019-07-17 18:02:07 +02:00
if (Group.selected) {
Clipbench.setGroup(Group.selected)
2018-10-17 19:50:25 +02:00
} else {
2019-07-17 18:02:07 +02:00
Clipbench.setElements(selected)
2018-10-17 19:50:25 +02:00
}
if (cut) {
2019-04-07 18:53:33 +02:00
BarItems.delete.trigger()
2018-10-17 19:50:25 +02:00
}
}
},
2019-07-19 17:31:22 +02:00
paste(event) {
2018-10-17 19:50:25 +02:00
var p = Prop.active_panel
if (open_dialog == 'uv_dialog') {
uv_dialog.paste(event)
} else if (display_mode) {
2018-12-16 16:18:20 +01:00
DisplayMode.paste()
2018-11-11 21:19:08 +01:00
} else if (Animator.open) {
//
if (isApp) {
var raw = clipboard.readHTML()
try {
var data = JSON.parse(raw)
if (data.type === 'keyframes' && data.content) {
Clipbench.keyframes = data.content
}
} catch (err) {}
}
if (Clipbench.keyframes && Clipbench.keyframes.length) {
if (!Animator.selected) return;
var bone = Animator.selected.getBoneAnimator()
if (bone) {
2019-07-17 18:02:07 +02:00
var keyframes = [];
Undo.initEdit({keyframes, keep_saved: true});
Clipbench.keyframes.forEach(function(data, i) {
var base_kf = new Keyframe(data);
base_kf.time = Timeline.second + data.time_offset;
bone.pushKeyframe(base_kf);
keyframes.push(base_kf);
base_kf.select(i ? {ctrlKey: true} : null)
2018-11-11 21:19:08 +01:00
})
2019-07-17 18:02:07 +02:00
Animator.preview()
Vue.nextTick(Timeline.update);
Undo.finishEdit('paste keyframes');
2018-11-11 21:19:08 +01:00
}
}
2018-10-17 19:50:25 +02:00
} else if (p == 'uv' || p == 'preview') {
main_uv.paste(event)
} else if (p == 'textures' && isApp) {
var img = clipboard.readImage()
if (img) {
var dataUrl = img.toDataURL()
2019-04-07 18:53:33 +02:00
var texture = new Texture({name: 'pasted', folder: 'blocks' }).fromDataURL(dataUrl).fillParticle().add(true)
2018-10-17 19:50:25 +02:00
setTimeout(function() {
texture.openMenu()
},40)
}
} else if (p == 'outliner') {
2019-07-17 18:02:07 +02:00
Undo.initEdit({outliner: true, elements: [], selection: true});
2018-10-17 19:50:25 +02:00
//Group
2019-03-09 22:06:35 +01:00
var target = 'root'
2019-07-17 18:02:07 +02:00
if (Group.selected) {
target = Group.selected
Group.selected.isOpen = true
2018-10-17 19:50:25 +02:00
} else if (selected[0]) {
2019-03-09 22:06:35 +01:00
target = selected[0]
2018-10-17 19:50:25 +02:00
}
selected.length = 0
if (isApp) {
var raw = clipboard.readHTML()
try {
var data = JSON.parse(raw)
2019-07-17 18:02:07 +02:00
if (data.type === 'elements' && data.content) {
Clipbench.group = undefined;
Clipbench.elements = data.content;
2018-10-17 19:50:25 +02:00
} else if (data.type === 'group' && data.content) {
2019-07-17 18:02:07 +02:00
Clipbench.group = data.content;
Clipbench.elements = [];
2018-10-17 19:50:25 +02:00
}
} catch (err) {}
}
if (Clipbench.group) {
2019-03-09 22:06:35 +01:00
function iterate(obj, parent) {
if (obj.children) {
var copy = new Group(obj)
if (obj.children && obj.children.length) {
obj.children.forEach((child) => {
iterate(child, copy)
})
}
copy.addTo(parent)
} else {
2019-07-17 18:02:07 +02:00
NonGroup.fromSave(obj).addTo(parent).selectLow()
2019-03-09 22:06:35 +01:00
}
2019-01-09 15:54:35 +01:00
}
2019-03-09 22:06:35 +01:00
iterate(Clipbench.group, target)
updateSelection()
2018-10-17 19:50:25 +02:00
2019-07-17 18:02:07 +02:00
} else if (Clipbench.elements && Clipbench.elements.length) {
Clipbench.elements.forEach(function(obj) {
NonGroup.fromSave(obj).addTo(target).selectLow()
2018-10-17 19:50:25 +02:00
})
updateSelection()
}
2019-07-17 18:02:07 +02:00
Undo.finishEdit('paste', {outliner: true, elements: selected, selection: true});
2018-10-17 19:50:25 +02:00
}
},
2019-07-19 17:31:22 +02:00
setTexture(texture) {
2018-10-17 19:50:25 +02:00
//Sets the raw image of the texture
if (!isApp) return;
if (texture.mode === 'bitmap') {
var img = nativeImage.createFromDataURL(texture.source)
} else {
var img = nativeImage.createFromPath(texture.source.split('?')[0])
}
clipboard.writeImage(img)
},
2019-07-19 17:31:22 +02:00
setGroup(group) {
2018-10-17 19:50:25 +02:00
if (!group) {
Clipbench.group = undefined
return;
}
2019-07-17 18:02:07 +02:00
Clipbench.group = group.getSaveCopy()
2018-10-17 19:50:25 +02:00
if (isApp) {
clipboard.writeHTML(JSON.stringify({type: 'group', content: Clipbench.group}))
}
},
2019-07-19 17:31:22 +02:00
setElements(arr) {
2019-07-17 18:02:07 +02:00
if (!arr) {
Clipbench.elements = []
2018-10-17 19:50:25 +02:00
return;
}
2019-07-17 18:02:07 +02:00
arr.forEach(function(obj) {
Clipbench.elements.push(obj.getSaveCopy())
2018-10-17 19:50:25 +02:00
})
if (isApp) {
2019-07-17 18:02:07 +02:00
clipboard.writeHTML(JSON.stringify({type: 'elements', content: Clipbench.elements}))
2018-10-17 19:50:25 +02:00
}
2018-11-11 21:19:08 +01:00
},
2019-07-19 17:31:22 +02:00
setKeyframes(keyframes) {
2018-11-11 21:19:08 +01:00
Clipbench.keyframes = []
if (!keyframes || keyframes.length === 0) {
return;
}
var first = keyframes[0];
keyframes.forEach(function(kf) {
if (kf.time < first.time) {
first = kf
}
})
keyframes.forEach(function(kf) {
Clipbench.keyframes.push({
channel: kf.channel,
x: kf.x,
y: kf.y,
z: kf.z,
w: kf.w,
isQuaternion: kf.isQuaternion,
time_offset: kf.time - first.time,
})
})
if (isApp) {
clipboard.writeHTML(JSON.stringify({type: 'keyframes', content: Clipbench.keyframes}))
}
2019-04-07 18:53:33 +02:00
},
2019-07-19 17:31:22 +02:00
setText(text) {
2019-04-07 18:53:33 +02:00
if (isApp) {
clipboard.writeText(text)
} else {
document.execCommand('copy')
}
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 = {
old_res: {},
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]}}},
}