2017-11-07 20:39:55 +13:00

59 lines
1.8 KiB
JavaScript

function IO()
{
Module.call(this,"io","File import/export tools.");
this.image = null;
this.methods.load = new Method("load","browser","Press enter to open the file browser.",function(q){
var filepath = dialog.showOpenDialog({properties: ['openFile']});
if(!filepath){ console.log("Nothing to load"); return; }
fs.readFile(filepath[0], 'utf-8', (err, data) => {
if(err){ alert("An error ocurred reading the file :" + err.message); return; }
var img = new Image();
img.src = filepath[0];
img.onload = function() {
ronin.io.image = img;
ronin.commander.inject("io draw:20,20|100x100");
}
});
});
this.methods.draw = new Method("draw","X,Y|WxH","Draw the loaded image pixels.",function(q){
if(!ronin.io.image){ return; }
ronin.io.draw_image(ronin.render.context(),ronin.io.image,ronin.commander.query().methods.draw);
ronin.io.image = null;
ronin.preview.clear();
});
this.methods.save = new Method("save","name","Export canvas.",function(q){
var fs = require('fs');
var data = ronin.render.to_base64('jpg').replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
dialog.showSaveDialog((fileName) => {
if (fileName === undefined){ return; }
fs.writeFile(fileName+'.jpg', buf);
});
});
this.preview = function(q)
{
ronin.preview.clear();
if(ronin.commander.query().methods.draw && this.image){
this.draw_image(ronin.preview.context(),this.image,ronin.commander.query().methods.draw);
}
}
this.draw_image = function(ctx = ronin.preview.context(),img,params)
{
var width = parseInt(img.naturalWidth * 0.5);
var height = parseInt(img.naturalHeight * 0.5);
var scale = (params.width/width) * 2;
ctx.drawImage(img, params.x * 2,params.y * 2,width * scale,height * scale);
}
}