Fix coding style and better output

This commit is contained in:
Clément Léger 2016-04-05 23:01:27 +02:00
parent 1b553d28e3
commit 664821b9f5

View File

@ -17,10 +17,11 @@
ns.CExportController.prototype.onCDownloadButtonClick_ = function (evt) { ns.CExportController.prototype.onCDownloadButtonClick_ = function (evt) {
var fileName = this.getPiskelName_() + '.c'; var fileName = this.getPiskelName_() + '.c';
var cName = this.getPiskelName_().replace(" ","_"); var cName = this.getPiskelName_().replace(' ','_');
var outputCanvas = this.getFramesheetAsCanvas(); var outputCanvas = this.getFramesheetAsCanvas();
var width = this.piskelController.getWidth(); var width = this.piskelController.getWidth();
var height = this.piskelController.getHeight(); var height = this.piskelController.getHeight();
var frameCount = this.piskelController.getFrameCount();
// Create a background canvas that will be filled with the transparent color before each render. // Create a background canvas that will be filled with the transparent color before each render.
var background = pskl.utils.CanvasUtils.createCanvas(width, height); var background = pskl.utils.CanvasUtils.createCanvas(width, height);
@ -30,15 +31,15 @@
// Useful defines for C routines // Useful defines for C routines
var frameStr = '#include <stdint.h>\n\n'; var frameStr = '#include <stdint.h>\n\n';
frameStr += '#define ' + cName.toUpperCase() + '_FRAME_COUNT ' + this.piskelController.getFrameCount() + '\n'; frameStr += '#define ' + cName.toUpperCase() + '_FRAME_COUNT ' + this.piskelController.getFrameCount() + '\n';
frameStr += '#define '+ cName.toUpperCase() + '_WIDTH ' + width + '\n'; frameStr += '#define ' + cName.toUpperCase() + '_FRAME_WIDTH ' + width + '\n';
frameStr += '#define '+ cName.toUpperCase() + '_HEIGHT ' + height + '\n\n'; frameStr += '#define ' + cName.toUpperCase() + '_FRAME_HEIGHT ' + height + '\n\n';
frameStr += '/* Piskel \"' + this.getPiskelName_() + '\" */\n\n'; frameStr += '/* Piskel data for \"' + this.getPiskelName_() + '\" */\n\n';
frameStr += 'uint32_t ' + this.getPiskelName_().toLowerCase(); frameStr += 'uint32_t ' + cName.toLowerCase();
frameStr += '_data[' + this.piskelController.getFrameCount() + '][' + width * height + '] = {\n'; frameStr += '_data[' + frameCount + '][' + width * height + '] = {\n';
for (var i = 0 ; i < this.piskelController.getFrameCount() ; i++) { for (var i = 0 ; i < frameCount ; i++) {
var render = this.piskelController.renderFrameAt(i, true); var render = this.piskelController.renderFrameAt(i, true);
context.clearRect(0, 0, width, height); context.clearRect(0, 0, width, height);
context.fillRect(0, 0, width, height); context.fillRect(0, 0, width, height);
@ -47,30 +48,38 @@
var pix = imgd.data; var pix = imgd.data;
frameStr += '{\n'; frameStr += '{\n';
for (var i = 0; i < pix.length; i += 4) { for (var j = 0; j < pix.length; j += 4) {
frameStr += this.rgbToCUint(pix[i], pix[i+1], pix[i+2]); frameStr += this.rgbToCHex(pix[j], pix[j + 1], pix[j + 2]);
if (i != pix.length - 4) if (j != pix.length - 4) {
frameStr += ', '; frameStr += ', ';
if (i % (width * 4) == 0) }
if (((j + 4) % (width * 4)) === 0) {
frameStr += '\n'; frameStr += '\n';
} }
frameStr += '\n}'; }
if (i != (frameCount - 1)) {
frameStr += '},\n';
} else {
frameStr += '}\n';
}
} }
frameStr += '};' frameStr += '};\n';
pskl.utils.BlobUtils.stringToBlob(frameStr, function(blob) { pskl.utils.BlobUtils.stringToBlob(frameStr, function(blob) {
pskl.utils.FileUtils.downloadAsFile(blob, fileName); pskl.utils.FileUtils.downloadAsFile(blob, fileName);
}.bind(this), 'application/text'); }.bind(this), 'application/text');
}; };
ns.CExportController.prototype.getPiskelName_ = function () { ns.CExportController.prototype.getPiskelName_ = function () {
return this.piskelController.getPiskel().getDescriptor().name; return this.piskelController.getPiskel().getDescriptor().name;
}; };
ns.CExportController.prototype.rgbToCUint = function (r, g, b) { ns.CExportController.prototype.rgbToCHex = function (r, g, b) {
return '0x' + r.toString(16).slice(-2) + g.toString(16).slice(-2) + b.toString(16).slice(-2); var hexStr = '0x';
hexStr += ('00' + r.toString(16)).substr(-2);
hexStr += ('00' + g.toString(16)).substr(-2);
hexStr += ('00' + b.toString(16)).substr(-2);
return hexStr;
}; };
ns.CExportController.prototype.getFramesheetAsCanvas = function () { ns.CExportController.prototype.getFramesheetAsCanvas = function () {