IO.loadImageData = function (gl, textureHandle, textureTarget, dataTarget, file, options, callback) {
var defaultOptions = { flip: false, lod: 0 };
options = merge(defaultOptions, options);
var fullPath = path.resolve(IO.getWorkingDirectory(), file);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(textureTarget, textureHandle);
var canvas = plask.SkCanvas.createFromImage(fullPath);
if (options.flip) {
gl.texImage2DSkCanvas(dataTarget, options.lod, canvas);
}
else {
gl.texImage2DSkCanvasNoFlip(dataTarget, options.lod, canvas);
}
if (callback) {
callback(canvas);
}
};
IO.watchTextFile = function (file, callback) {
fs.watch(file, {}, function (event, fileName) {
if (event == 'change') {
var data = fs.readFileSync(file, 'utf8');
if (callback) {
callback(data);
}
}
});
};
IO.saveTextFile = function (file, data) {
fs.writeFileSync(file, data);
};
return IO;
};
var WebIO = function () {
function IO() {
}
IO.getWorkingDirectory = function () {
return '.';
};
IO.loadTextFile = function (url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onreadystatechange = function (e) {
if (request.readyState == 4) {
if (request.status == 200) {
if (callback) {
callback(request.responseText);
}
} else {
Log.error('WebIO.loadTextFile error : ' + request.statusText);
}
}
};
request.send(null);
};
IO.loadImageData = function (gl, textureHandle, textureTarget, dataTarget, url, options, callback) {
var defaultOptions = { flip: false, lod: 0 };
options = merge(defaultOptions, options);
var image = new Image();
if (options.crossOrigin) image.crossOrigin = options.crossOrigin;
image.onload = function () {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(textureTarget, textureHandle);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, options.flip);
gl.texImage2D(dataTarget, options.lod, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
if (callback) {
callback(image);
}
};
image.src = url;
};
IO.watchTextFile = function () {
console.log('Warning: WebIO.watch is not implemented!');
};
IO.saveTextFile = function (url, data, callback) {
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.onreadystatechange = function (e) {
if (request.readyState == 4) {
if (request.status == 200) {
if (callback) {
callback(request.responseText, request);
}
} else {
Log.error('WebIO.saveTextFile error : ' + request.statusText);
}
}
};
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.send('data=' + encodeURIComponent(data));
};
return IO;
};
if (Platform.isPlask) module.exports = PlaskIO();
else if (Platform.isBrowser) module.exports = WebIO();