2018-03-28 20:48:11 +02:00
var app = require ( 'electron' ) . remote ,
fs = require ( 'fs' ) ,
nativeImage = require ( 'electron' ) . nativeImage ,
exec = require ( 'child_process' ) . exec ,
originalFs = require ( 'original-fs' ) ,
http = require ( 'http' ) ,
currentwindow = app . getCurrentWindow ( ) ,
dialog _win = null ,
latest _version = false ,
preventClosing = true ;
recent _projects = undefined
2017-10-26 19:00:52 +02:00
const shell = require ( 'electron' ) . shell ;
const { clipboard } = require ( 'electron' )
$ ( document ) . ready ( function ( ) {
if ( app . process . argv . length >= 2 ) {
if ( app . process . argv [ 1 ] . substr ( - 5 ) == '.json' ) {
readFile ( app . process . argv [ 1 ] , true )
}
}
$ ( '.open-in-browser' ) . click ( ( event ) => {
event . preventDefault ( ) ;
shell . openExternal ( event . target . href ) ;
return true ;
} ) ;
$ ( '.web_only' ) . remove ( )
2018-03-28 20:48:11 +02:00
if ( process . platform == 'linux' ) {
$ ( '#app_update_button' ) . remove ( )
}
2017-10-26 19:00:52 +02:00
if ( _ _dirname . includes ( 'C:\\xampp\\htdocs\\blockbench\\web' ) ) {
Blockbench . addFlag ( 'dev' )
$ ( '#file_menu_list' ) . append ( '<li class="menu_seperator"></li>' )
$ ( '#file_menu_list' ) . append ( '<li onclick="Blockbench.reload()"><i class="material-icons">refresh</i>Reload</li>' )
}
} )
getLatestVersion ( true )
//Called on start to show message
function getLatestVersion ( init ) {
2018-03-28 20:48:11 +02:00
if ( process . platform == 'linux' ) return ;
2017-10-26 19:00:52 +02:00
$ . getJSON ( 'http://blockbench.net/api/index.json' , function ( data ) {
if ( data . version ) {
latest _version = data . version
2017-11-16 22:23:41 +01:00
if ( compareVersions ( latest _version , appVersion ) && init === true ) {
2017-10-26 19:00:52 +02:00
showDialog ( 'update_notification' )
$ ( '.dialog#update_notification h2 span' ) . text ( latest _version )
console . log ( 'Found new version: ' + latest _version )
} else if ( init === false ) {
checkForUpdates ( )
}
}
} ) . fail ( function ( ) {
latest _version = false
} )
}
2018-03-28 20:48:11 +02:00
//Recent Projects
function updateRecentProjects ( ) {
if ( recent _projects === undefined ) {
//Setup
recent _projects = [ ]
var raw = localStorage . getItem ( 'recent_projects' )
if ( raw ) {
recent _projects = JSON . parse ( raw )
}
}
//Menu
var list = $ ( 'ul#recent_projects' )
list . html ( '' )
var i = recent _projects . length - 1
while ( i >= 0 ) {
var p = recent _projects [ i ]
var entry = $ ( '<li onclick=readFile(\'' + p . path . split ( '\\' ) . join ( '\\\\' ) + '\',true)><i class="material-icons">insert_drive_file</i>' + p . name + '</li>' )
list . append ( entry )
i -- ;
}
2017-10-26 19:00:52 +02:00
2018-03-28 20:48:11 +02:00
//Set Local Storage
localStorage . setItem ( 'recent_projects' , JSON . stringify ( recent _projects ) )
}
function addRecentProject ( data ) {
var i = recent _projects . length - 1
while ( i >= 0 ) {
var p = recent _projects [ i ]
if ( p . path === data . path ) {
recent _projects . splice ( i , 1 )
}
i -- ;
}
recent _projects . push ( { name : data . name , path : data . path } )
if ( recent _projects . length > 8 ) {
recent _projects . shift ( )
}
updateRecentProjects ( )
}
//Updates
2017-10-26 19:00:52 +02:00
function checkForUpdates ( instant ) {
showDialog ( 'updater' )
setProgressBar ( 'update_bar' , 0 , 1 )
var data ;
if ( latest _version === false ) {
data = [
'<div class="tool" onclick="refreshUpdateDialog()">' ,
'<i class="material-icons">refresh</i>' ,
'<div class="tooltip">Refresh</div>' ,
'</div>' ,
'<div class="dialog_bar narrow">' ,
'<i class="material-icons blue_icon">cloud_off</i>No internet connection' ,
'</div>'
] . join ( '' )
} else if ( latest _version !== appVersion ) {
data = [
'<div class="dialog_bar narrow">Latest version: ' + latest _version + '</div>' ,
'<div class="dialog_bar narrow">Installed version: ' + appVersion + '</div>' ,
'<div class=""><button type="button" class="large uc_btn" id="update_button" onclick="installUpdate()">Update</button></div>'
] . join ( '' )
if ( instant ) {
setTimeout ( function ( ) {
installUpdate ( )
} , 60 )
}
} else {
data = [
'<div class="tool" onclick="refreshUpdateDialog()">' ,
'<i class="material-icons">refresh</i>' ,
'<div class="tooltip">Refresh</div>' ,
'</div>' ,
'<div class="dialog_bar narrow">' ,
'<i class="material-icons blue_icon">check</i>Blockbench is up-to-date!' ,
'</div>'
] . join ( '' )
}
$ ( '#updater_content' ) . html ( data )
}
function refreshUpdateDialog ( ) {
data = '<div class="dialog_bar narrow"><i class="material-icons blue_icon spinning">refresh</i>Clearing cache data</div>'
$ ( '#updater_content' ) . html ( data )
currentwindow . webContents . session . clearCache ( function ( ) {
data = '<div class="dialog_bar narrow"><i class="material-icons blue_icon spinning">refresh</i>Connecting to server</div>'
$ ( '#updater_content' ) . html ( data )
getLatestVersion ( false )
} )
}
function installUpdate ( ) {
console . log ( 'Starting Update' )
var received _bytes = 0 ;
var total _bytes = 0 ;
2018-01-02 20:34:44 +01:00
$ ( '.uc_btn' ) . css ( 'visibility' , 'hidden' )
2017-10-26 19:00:52 +02:00
var asar _path = _ _dirname
if ( asar _path . includes ( '.asar' ) === false ) {
asar _path = asar _path + osfs + 'resources' + osfs + 'app.asar'
}
var file = originalFs . createWriteStream ( asar _path )
var request = http . get ( "http://blockbench.net/api/app.asar" , function ( response ) {
response . pipe ( file ) ;
total _bytes = parseInt ( response . headers [ 'content-length' ] ) ;
response . on ( 'end' , updateInstallationEnd )
response . on ( 'data' , function ( chunk ) {
received _bytes += chunk . length ;
setProgressBar ( 'update_bar' , received _bytes / total _bytes , 1 ) ;
} )
} ) ;
}
function updateInstallationEnd ( ) {
hideDialog ( )
var exe _path = _ _dirname . split ( osfs )
exe _path . splice ( - 2 )
exe _path = exe _path . join ( osfs ) + osfs + 'blockbench.exe'
if ( showSaveDialog ( true ) ) {
exec ( exe _path )
} else {
showQuickMessage ( 'Restart the app to update' )
}
}
2018-03-28 20:48:11 +02:00
//Default Pack
2017-10-26 19:00:52 +02:00
function openDefaultTexturePath ( ) {
var answer = app . dialog . showMessageBox ( currentwindow , {
type : 'info' ,
buttons : [ 'Remove' , 'Continue' ] ,
noLink : true ,
title : 'Info' ,
message : 'Select "textures"-folder of the default resource pack' ,
detail : 'Extract the default resource pack from the Minecraft jar or google and download it. Then locate the "textures" folder and open it. Blockbench will remember that location and try to fetch textures from there if it can\'t find them in the current resource pack.' ,
} )
if ( answer === 0 ) {
settings . default _path = { value : false , hidden : true }
} else {
app . dialog . showOpenDialog ( currentwindow , {
title : 'Select default "textures" Folder' ,
properties : [ 'openDirectory' ] ,
} , function ( filePaths ) {
settings . default _path = { value : filePaths [ 0 ] , hidden : true }
} )
}
}
2018-03-28 20:48:11 +02:00
//Texture Paths
function findEntityTexture ( mob , return _path ) {
var textures = {
'geometry.chicken' : 'chicken' ,
'geometry.blaze' : 'blaze' ,
'geometry.llamaspit' : 'llama/spit' ,
'geometry.llama' : 'llama/llama_creamy' ,
'geometry.dragon' : 'dragon/dragon' ,
'geometry.ghast' : 'ghast/ghast' ,
'geometry.slime' : 'slime/slime' ,
'geometry.slime.armor' : 'slime/slime' ,
'geometry.lavaslime' : 'slime/magmacube' ,
'geometry.silverfish' : 'silverfish' ,
'geometry.shulker' : 'shulker/shulker_undyed' ,
'geometry.rabbit' : 'rabbit/brown' ,
'geometry.horse' : 'horse/horse_brown' ,
'geometry.horse.v2' : 'horse2/horse_brown' ,
'geometry.humanoid' : 'steve' ,
'geometry.creeper' : 'creeper/creeper' ,
'geometry.enderman' : 'enderman/enderman' ,
'geometry.zombie' : 'zombie/zombie' ,
'geometry.zombie.husk' : 'zombie/husk' ,
2018-04-05 17:11:29 +02:00
'geometry.zombie.drowned' : 'zombie/drowned' ,
2018-03-28 20:48:11 +02:00
'geometry.pigzombie' : 'pig/pigzombie' ,
'geometry.pigzombie.baby' : 'pig/pigzombie' ,
'geometry.skeleton' : 'skeleton/skeleton' ,
'geometry.skeleton.wither' : 'skeleton/wither_skeleton' ,
'geometry.skeleton.stray' : 'skeleton/stray' ,
'geometry.squid' : 'squid' ,
'geometry.spider' : 'spider/spider' ,
'geometry.cow' : 'cow/cow' ,
'geometry.mooshroom' : 'cow/mooshroom' ,
'geometry.sheep.sheared' : 'sheep/sheep' ,
'geometry.sheep' : 'sheep/sheep' ,
'geometry.pig' : 'pig/pig' ,
'geometry.bat' : 'bat' ,
'geometry.irongolem' : 'iron_golem' ,
'geometry.snowgolem' : 'snow_golem' ,
'geometry.zombie.villager' : 'zombie_villager/zombie_villager' ,
'geometry.evoker' : 'illager/evoker' ,
'geometry.vex' : 'vex/vex' ,
'geometry.vindicator' : 'vindicator' ,
'geometry.wolf' : 'wolf/wolf' ,
'geometry.ocelot' : 'cat/ocelot' ,
2018-04-05 17:11:29 +02:00
'geometry.trident' : 'trident' ,
2018-03-28 20:48:11 +02:00
'geometry.guardian' : 'guardian' ,
'geometry.polarbear' : 'polarbear' ,
'geometry.villager' : 'villager/villager' ,
'geometry.villager.witch' : 'witch' ,
'geometry.witherBoss' : 'wither_boss/wither' ,
'geometry.agent' : 'agent' ,
'geometry.armor_stand' : 'armor_stand' ,
'geometry.parrot' : 'parrot/parrot_red_blue' ,
'geometry.bed' : 'bed/white' ,
'geometry.player_head' : 'steve' ,
'geometry.mob_head' : 'skeleton/skeleton' ,
2018-04-05 17:11:29 +02:00
'geometry.dragon_head' : 'dragon/dragon' ,
'geometry.cod' : 'fish/fish' ,
'geometry.pufferfish.small' : 'fish/pufferfish' ,
'geometry.pufferfish.mid' : 'fish/pufferfish' ,
'geometry.pufferfish.large' : 'fish/pufferfish' ,
'geometry.salmon' : 'fish/salmon' ,
'geometry.tropicalfish_a' : 'fish/tropical_a' ,
'geometry.tropicalfish_b' : 'fish/tropical_b'
2018-03-28 20:48:11 +02:00
}
var path = textures [ mob . split ( ':' ) [ 0 ] ]
if ( path ) {
var texture _path = Prop . file _path . split ( osfs )
texture _path . splice ( - 2 )
texture _path . push ( 'textures' )
texture _path . push ( 'entity' )
texture _path = texture _path . concat ( path . split ( '/' ) )
texture _path = texture _path . join ( osfs )
if ( return _path ) {
return texture _path + '.png' ;
} else {
if ( fs . existsSync ( texture _path + '.png' ) ) {
var texture = new Texture ( { keep _size : true } ) . fromPath ( texture _path + '.png' ) . add ( )
} else if ( fs . existsSync ( texture _path + '.tga' ) ) {
var texture = new Texture ( { keep _size : true } ) . fromPath ( texture _path + '.tga' ) . add ( )
}
}
}
}
2017-10-26 19:00:52 +02:00
//Save Dialogs
function saveFileBlock ( ) {
app . dialog . showSaveDialog ( currentwindow , {
filters : [ {
name : 'JSON Model' ,
extensions : [ 'json' ]
} ] ,
defaultPath : Project . name
} , function ( fileName ) {
if ( fileName === undefined ) {
return ;
}
Prop . file _path = fileName ;
2018-03-28 20:48:11 +02:00
Project . name = pathToName ( fileName , true ) ;
2017-10-26 19:00:52 +02:00
saveFile ( )
} )
}
function saveFileOptifine ( ) {
app . dialog . showSaveDialog ( currentwindow , {
filters : [ {
name : 'Optifine Model' ,
extensions : [ 'jpm' ]
} ] ,
defaultPath : Project . name
} , function ( fileName ) {
if ( fileName === undefined ) {
return ;
}
var content = buildOptifineModel ( )
fs . writeFile ( fileName , content , function ( err ) {
if ( err ) {
console . log ( 'Error Saving Entity Model: ' + err )
}
showQuickMessage ( 'Saved as Optifine entity model' )
} )
} )
}
function saveFileEntity ( ) {
app . dialog . showSaveDialog ( currentwindow , {
filters : [ {
name : 'Entity Model' ,
extensions : [ 'json' ]
} ] ,
defaultPath : Project . name
} , function ( fileName ) {
if ( fileName === undefined ) {
return ;
}
2017-11-16 22:23:41 +01:00
var content = buildEntityModel ( { raw : true } )
2017-10-26 19:00:52 +02:00
2017-11-16 22:23:41 +01:00
writeFileEntity ( content , fileName )
2017-10-26 19:00:52 +02:00
} )
}
function saveFileObj ( ) {
app . dialog . showSaveDialog ( currentwindow , { filters : [ { name : 'Alias Wavefront' , extensions : [ 'obj' ] } ] } , function ( fileName ) {
if ( fileName === undefined ) {
return ;
}
scene . remove ( three _grid )
scene . remove ( Transformer )
var exporter = new THREE . OBJExporter ( ) ;
var content = exporter . parse ( scene , pathToName ( fileName , false ) ) ;
scene . add ( three _grid )
scene . add ( Transformer )
//OBJECT
fs . writeFile ( fileName , content . obj , function ( err ) { } )
//MATERIAL
fs . writeFile ( fileName . split ( '.obj' ) . join ( '.mtl' ) , content . mtl , function ( err ) { } )
//IMAGES
if ( settings . obj _textures . value === true ) {
for ( var key in content . images ) {
if ( content . images . hasOwnProperty ( key ) && content . images [ key ] . path ) {
var native _image _instance = nativeImage . createFromPath ( content . images [ key ] . path )
var image = native _image _instance . toPNG ( )
var image _path = fileName . split ( osfs )
image _path . pop ( )
image _path = image _path . join ( osfs ) + osfs + content . images [ key ] . name
fs . writeFile ( image _path , image , function ( err ) { } )
}
}
}
showQuickMessage ( 'Saved as obj model' )
} )
}
//Writers
function saveFile ( props ) {
if ( Prop . file _path !== 'Unknown' ) {
Prop . project _saved = true ;
2018-03-28 20:48:11 +02:00
setProjectTitle ( pathToName ( Prop . file _path , false ) )
2017-11-16 22:23:41 +01:00
2018-03-28 20:48:11 +02:00
if ( Blockbench . entity _mode === false ) {
2017-11-16 22:23:41 +01:00
var content = buildBlockModel ( )
fs . writeFile ( Prop . file _path , content , function ( err ) {
if ( err ) {
console . log ( 'Error Saving File: ' + err )
}
if ( props && props . closeAfter ) {
preventClosing = false
setTimeout ( function ( ) {
currentwindow . close ( )
} , 12 )
}
showQuickMessage ( 'Saved as ' + pathToName ( Prop . file _path , true ) )
} )
} else {
var content = buildEntityModel ( { raw : true } )
writeFileEntity ( content , Prop . file _path )
}
} else {
2018-03-28 20:48:11 +02:00
if ( Blockbench . entity _mode === false ) {
2017-11-16 22:23:41 +01:00
saveFileBlock ( )
} else {
saveFileEntity ( )
}
}
}
function writeFileEntity ( content , fileName ) {
Prop . file _path = fileName
fs . readFile ( fileName , 'utf-8' , function ( errx , data ) {
var obj = { }
if ( ! errx ) {
try {
obj = JSON . parse ( data )
} catch ( err ) {
err = err + ''
var answer = app . dialog . showMessageBox ( currentwindow , {
type : 'warning' ,
buttons : [ 'Create Backup and Overwrite' , 'Overwrite' , 'Cancel' ] ,
title : 'Blockbench' ,
message : 'Blockbench cannot combine this model with the old file' ,
detail : err ,
noLink : false
} )
if ( answer === 0 ) {
var backup _file _name = pathToName ( fileName , true ) + ' backup ' + new Date ( ) . toLocaleString ( ) . split ( ':' ) . join ( '_' )
backup _file _name = fileName . replace ( pathToName ( fileName , false ) , backup _file _name )
fs . writeFile ( backup _file _name , data , function ( err2 ) {
if ( err2 ) {
console . log ( 'Error saving backup model: ' , err2 )
}
} )
}
if ( answer === 2 ) {
return ;
}
2017-10-26 19:00:52 +02:00
}
2017-11-16 22:23:41 +01:00
}
var model _name = Project . parent
if ( model _name == '' ) model _name = 'geometry.unknown'
obj [ model _name ] = content
content = autoStringify ( obj )
fs . writeFile ( fileName , content , function ( err ) {
if ( err ) {
console . log ( 'Error Saving Entity Model: ' + err )
2017-10-26 19:00:52 +02:00
}
2017-11-16 22:23:41 +01:00
showQuickMessage ( 'Saved as bedrock entity model' )
2017-10-26 19:00:52 +02:00
} )
2017-11-16 22:23:41 +01:00
} )
2017-10-26 19:00:52 +02:00
}
//Open
function openFile ( makeNew ) {
app . dialog . showOpenDialog ( currentwindow , { filters : [ { name : 'Model' , extensions : [ 'json' ] } ] } , function ( fileNames ) {
if ( fileNames !== undefined ) {
2018-03-28 20:48:11 +02:00
addRecentProject ( { name : pathToName ( fileNames [ 0 ] ) , path : fileNames [ 0 ] } )
2017-10-26 19:00:52 +02:00
readFile ( fileNames [ 0 ] , makeNew )
}
} )
}
function readFile ( filepath , makeNew ) {
fs . readFile ( filepath , 'utf-8' , function ( err , data ) {
if ( err ) {
console . log ( err )
return ;
}
2018-03-28 20:48:11 +02:00
addRecentProject ( { name : pathToName ( filepath ) , path : filepath } )
2017-10-26 19:00:52 +02:00
loadFile ( data , filepath , makeNew )
} )
}
function openTexture ( ) {
var start _path ;
if ( textures . length > 0 ) {
var arr = textures [ 0 ] . path . split ( osfs )
arr . splice ( - 1 )
start _path = arr . join ( osfs )
2018-03-28 20:48:11 +02:00
} else if ( Prop . file _path ) {
2017-10-26 19:00:52 +02:00
var arr = Prop . file _path . split ( osfs )
arr . splice ( - 3 )
arr . push ( 'textures' )
start _path = arr . join ( osfs )
}
app . dialog . showOpenDialog ( currentwindow , {
properties : [ 'openFile' , 'multiSelections' ] ,
defaultPath : start _path ,
filters : [ {
name : 'PNG Texture' ,
extensions : [ 'png' ]
} ]
} , function ( fileNames ) {
if ( fileNames !== undefined ) {
fileNames . forEach ( function ( path ) {
fs . readFile ( path , function ( err ) {
if ( err ) {
console . log ( err )
}
new Texture ( ) . fromPath ( path ) . add ( ) . fillParticle ( )
} )
} )
}
} )
}
function importExtrusion ( makeNew ) {
app . dialog . showOpenDialog ( currentwindow , {
properties : [ 'openFile' ] ,
filters : [ {
name : 'PNG Texture' ,
extensions : [ 'png' ]
} ]
} , function ( fileNames ) {
if ( fileNames !== undefined ) {
var path = fileNames [ 0 ]
fs . readFile ( path , function ( err ) {
if ( err ) {
console . log ( err )
}
if ( makeNew === true ) {
if ( newProject ( ) == false ) return ;
}
g _makeNew = makeNew
new Texture ( ) . fromPath ( path ) . add ( ) . fillParticle ( )
showDialog ( 'image_extruder' )
drawExtrusionImage ( path )
} )
}
} )
}
2018-03-28 20:48:11 +02:00
//Drop
2017-10-26 19:00:52 +02:00
document . ondragover = document . ondrop = ( ev ) => {
ev . preventDefault ( )
}
document . body . ondrop = ( ev ) => {
if ( ev . dataTransfer == undefined ) {
return ;
}
if ( ev . dataTransfer . files [ 0 ] != undefined ) {
2018-03-28 20:48:11 +02:00
2017-10-26 19:00:52 +02:00
ev . preventDefault ( )
2018-03-28 20:48:11 +02:00
var fileArray = ev . dataTransfer . files ;
if ( fileArray [ 0 ] . path . substr ( - 4 ) . toUpperCase ( ) == 'JSON' ) {
readFile ( fileArray [ 0 ] . path , true )
} else if ( fileArray [ 0 ] . path . substr ( - 7 ) . toUpperCase ( ) == 'BBSTYLE' ) {
2017-10-26 19:00:52 +02:00
2018-03-28 20:48:11 +02:00
fs . readFile ( fileArray [ 0 ] . path , 'utf-8' , function ( err , data ) {
2017-10-26 19:00:52 +02:00
if ( err ) {
console . log ( err )
return ;
}
applyBBStyle ( data )
} )
2018-03-28 20:48:11 +02:00
} else if ( fileArray [ 0 ] . path . substr ( - 3 ) . toUpperCase ( ) == 'PNG' ) {
2017-10-26 19:00:52 +02:00
if ( ev . target == canvas1 ) {
2018-03-28 20:48:11 +02:00
active _scene . background . image = fileArray [ 0 ] . path
2017-10-26 19:00:52 +02:00
enterScene ( true )
} else {
2018-03-28 20:48:11 +02:00
if ( $ ( 'li.texture' ) . has ( ev . target ) . length ) {
var id = $ ( 'li.texture' ) . has ( ev . target ) . attr ( 'texid' )
var texture = getTextureById ( id )
if ( texture && texture . error ) {
texture . fromPath ( fileArray [ 0 ] . path )
return ;
}
}
2017-10-26 19:00:52 +02:00
var len = fileArray . length ;
for ( var i = 0 ; i < len ; i ++ ) {
new Texture ( ) . fromPath ( fileArray [ i ] . path ) . add ( ) . fillParticle ( )
}
textures . forEach ( function ( s ) {
if ( s === textures [ textures . length - 1 ] ) {
s . selected = true ;
} else {
s . selected = false ;
}
} )
loadTextureDraggable ( )
}
}
}
}
2018-03-28 20:48:11 +02:00
function dropTexture ( ev ) {
ev . preventDefault ( ) ;
ev . stopPropagation ( ) ;
}
function loadBackgroundImage ( event ) {
if ( event !== undefined ) {
if ( event . altKey === true ) {
textPrompt ( 'Background Image Path' , 'active_scene.background.image' , true )
return ;
}
}
app . dialog . showOpenDialog ( currentwindow , { properties : [ 'openFile' ] , filters : [ { name : 'Image' , extensions : [ 'png' , 'jpg' , 'jpg' , 'jpeg' , 'gif' ] } ] } , function ( fileNames ) {
if ( fileNames !== undefined ) {
active _scene . background . image = fileNames [ 0 ]
enterScene ( true )
}
} )
}
//Zoom
function setZoomLevel ( mode ) {
switch ( mode ) {
case 'in' : Prop . zoom += 5 ; break ;
case 'out' : Prop . zoom -= 5 ; break ;
case 'reset' : Prop . zoom = 100 ; break ;
}
var level = ( Prop . zoom - 100 ) / 12
currentwindow . webContents . setZoomLevel ( level )
setScreenRatio ( )
}
//Close
window . onbeforeunload = function ( ) {
if ( preventClosing === true ) {
setTimeout ( function ( ) {
showSaveDialog ( true )
} , 2 )
return true ;
}
}
2017-10-26 19:00:52 +02:00
function showSaveDialog ( close ) {
if ( Blockbench . flags . includes ( 'allow_reload' ) ) {
close = false
}
if ( Prop . project _saved === false && elements . length > 0 ) {
var answer = app . dialog . showMessageBox ( currentwindow , {
type : 'question' ,
buttons : [ 'Save' , 'Discard' , 'Cancel' ] ,
title : 'Blockbench' ,
message : 'Do you want to save your model?' ,
noLink : true
} )
if ( answer === 0 ) {
saveFile ( { closeAfter : close } )
return false ;
} else if ( answer === 2 ) {
return false ;
} else {
if ( close === true ) {
preventClosing = false
setTimeout ( function ( ) {
currentwindow . close ( )
} , 12 )
}
return true ;
}
} else {
if ( close === true ) {
preventClosing = false
setTimeout ( function ( ) {
currentwindow . close ( )
} , 12 )
}
return true ;
}
}