2021-07-15 23:21:19 +03:00
|
|
|
const FileManager = (() => {
|
|
|
|
|
|
|
|
// Binding the browse holder change event to file loading
|
|
|
|
const browseHolder = document.getElementById('open-image-browse-holder');
|
2021-07-23 17:35:42 +03:00
|
|
|
const browsePaletteHolder = document.getElementById('load-palette-browse-holder');
|
|
|
|
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('change', browseHolder, loadFile);
|
2021-07-23 17:35:42 +03:00
|
|
|
Events.on('change', browsePaletteHolder, loadPalette);
|
2021-07-15 23:21:19 +03:00
|
|
|
|
2021-08-14 23:47:49 +03:00
|
|
|
function openSaveProjectWindow() {
|
2021-07-15 23:21:19 +03:00
|
|
|
//create name
|
|
|
|
let selectedPalette = Util.getText('palette-button');
|
|
|
|
|
|
|
|
if (selectedPalette != 'Choose a palette...'){
|
2021-08-14 23:47:49 +03:00
|
|
|
var paletteAbbreviation = palettes[selectedPalette].abbreviation;
|
2021-12-06 19:37:43 +03:00
|
|
|
var fileName = 'pixel-'+paletteAbbreviation+'-'+currFile.canvasSize[0]+'x'+currFile.canvasSize[1];
|
2021-08-14 23:47:49 +03:00
|
|
|
} else {
|
2021-12-06 19:37:43 +03:00
|
|
|
var fileName = 'pixel-'+currFile.canvasSize[0]+'x'+currFile.canvasSize[1];
|
2021-08-14 23:47:49 +03:00
|
|
|
selectedPalette = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
Util.setValue('lpe-file-name', fileName);
|
|
|
|
Events.on("click", "save-project-confirm", saveProject);
|
|
|
|
Dialogue.showDialogue('save-project', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function openPixelExportWindow() {
|
|
|
|
let selectedPalette = Util.getText('palette-button');
|
|
|
|
|
|
|
|
if (selectedPalette != 'Choose a palette...'){
|
|
|
|
var paletteAbbreviation = palettes[selectedPalette].name;
|
|
|
|
var fileName = 'pixel-'+paletteAbbreviation+'-'+canvasSize[0]+'x'+canvasSize[1]+'.png';
|
2021-07-15 23:21:19 +03:00
|
|
|
} else {
|
2021-12-06 19:37:43 +03:00
|
|
|
var fileName = 'pixel-'+currFile.canvasSize[0]+'x'+currFile.canvasSize[1]+'.png';
|
2021-07-15 23:21:19 +03:00
|
|
|
selectedPalette = 'none';
|
|
|
|
}
|
2021-08-14 23:47:49 +03:00
|
|
|
|
|
|
|
Util.setValue('export-file-name', fileName);
|
|
|
|
Events.on("click", "export-confirm", exportProject);
|
|
|
|
Dialogue.showDialogue('export', false);
|
|
|
|
}
|
2021-07-15 23:21:19 +03:00
|
|
|
|
2021-08-14 23:47:49 +03:00
|
|
|
function saveProject() {
|
|
|
|
// Get name
|
|
|
|
let fileName = Util.getValue("lpe-file-name") + ".lpe";
|
|
|
|
let selectedPalette = Util.getText('palette-button');
|
2021-07-15 23:21:19 +03:00
|
|
|
//set download link
|
|
|
|
const linkHolder = document.getElementById('save-project-link-holder');
|
|
|
|
// create file content
|
|
|
|
const content = getProjectData();
|
|
|
|
|
|
|
|
linkHolder.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(content);
|
|
|
|
linkHolder.download = fileName;
|
|
|
|
linkHolder.click();
|
|
|
|
|
|
|
|
if (typeof ga !== 'undefined')
|
2021-12-06 19:37:43 +03:00
|
|
|
ga('send', 'event', 'Pixel Editor Save', selectedPalette, currFile.canvasSize[0]+'/'+currFile.canvasSize[1]); /*global ga*/
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function exportProject() {
|
2021-12-07 14:11:40 +03:00
|
|
|
if (EditorState.documentCreated()) {
|
2021-07-15 23:21:19 +03:00
|
|
|
//create name
|
2021-08-14 23:47:49 +03:00
|
|
|
let fileName = Util.getValue("export-file-name");
|
2021-07-15 23:21:19 +03:00
|
|
|
//set download link
|
2021-12-06 22:12:57 +03:00
|
|
|
let linkHolder = document.getElementById('save-image-link-holder');
|
2021-07-15 23:21:19 +03:00
|
|
|
// Creating a tmp canvas to flatten everything
|
2021-12-06 22:12:57 +03:00
|
|
|
let exportCanvas = document.createElement("canvas");
|
|
|
|
let emptyCanvas = document.createElement("canvas");
|
|
|
|
let layersCopy = currFile.layers.slice();
|
2021-07-15 23:21:19 +03:00
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
exportCanvas.width = currFile.canvasSize[0];
|
|
|
|
exportCanvas.height = currFile.canvasSize[1];
|
2021-07-15 23:21:19 +03:00
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
emptyCanvas.width = currFile.canvasSize[0];
|
|
|
|
emptyCanvas.height = currFile.canvasSize[1];
|
2021-07-15 23:21:19 +03:00
|
|
|
|
|
|
|
// Sorting the layers by z index
|
|
|
|
layersCopy.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? 1 : -1);
|
|
|
|
|
|
|
|
// Merging every layer on the export canvas
|
|
|
|
for (let i=0; i<layersCopy.length; i++) {
|
2021-12-06 19:37:43 +03:00
|
|
|
if (layersCopy[i].hasCanvas() && layersCopy[i].isVisible) {
|
2021-07-22 23:42:41 +03:00
|
|
|
LayerList.mergeLayers(exportCanvas.getContext('2d'), layersCopy[i].context);
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
// I'm not going to find out why the layer ordering screws up if you don't copy
|
|
|
|
// a blank canvas when layers[i] is not set as visible, but if you have time to
|
|
|
|
// spend, feel free to investigate (comment the else, create 3 layers: hide the
|
|
|
|
// middle one and export, the other 2 will be swapped in their order)
|
|
|
|
else {
|
2021-07-22 23:42:41 +03:00
|
|
|
LayerList.mergeLayers(exportCanvas.getContext('2d'), emptyCanvas.getContext('2d'));
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linkHolder.href = exportCanvas.toDataURL();
|
|
|
|
linkHolder.download = fileName;
|
|
|
|
|
|
|
|
linkHolder.click();
|
|
|
|
|
|
|
|
emptyCanvas.remove();
|
|
|
|
exportCanvas.remove();
|
|
|
|
|
|
|
|
//track google event
|
|
|
|
if (typeof ga !== 'undefined')
|
2021-12-06 19:37:43 +03:00
|
|
|
ga('send', 'event', 'Pixel Editor Export', selectedPalette, currFile.canvasSize[0]+'/'+currFile.canvasSize[1]); /*global ga*/
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function open() {
|
|
|
|
//if a document exists
|
2021-12-07 14:11:40 +03:00
|
|
|
if (EditorState.documentCreated()) {
|
2021-07-15 23:21:19 +03:00
|
|
|
//check if the user wants to overwrite
|
|
|
|
if (confirm('Opening a pixel will discard your current one. Are you sure you want to do that?'))
|
|
|
|
//open file selection dialog
|
|
|
|
document.getElementById('open-image-browse-holder').click();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
//open file selection dialog
|
|
|
|
document.getElementById('open-image-browse-holder').click();
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadFile() {
|
|
|
|
let fileName = document.getElementById("open-image-browse-holder").value;
|
|
|
|
// Getting the extension
|
|
|
|
let extension = (fileName.substring(fileName.lastIndexOf('.')+1, fileName.length) || fileName).toLowerCase();
|
|
|
|
|
|
|
|
if (browseHolder.files && browseHolder.files[0]) {
|
2021-07-24 11:25:55 +03:00
|
|
|
// Checking if the extension is supported
|
2021-07-15 23:21:19 +03:00
|
|
|
if (extension == 'png' || extension == 'gif' || extension == 'lpe') {
|
|
|
|
// If it's a Lospec Pixel Editor tm file, I load the project
|
|
|
|
if (extension == 'lpe') {
|
|
|
|
openProject();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
openFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else alert('Only .LPE project files, PNG and GIF files are allowed at this time.');
|
|
|
|
}
|
2021-07-23 19:30:04 +03:00
|
|
|
|
|
|
|
browseHolder.value = null;
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function openFile() {
|
|
|
|
//load file
|
|
|
|
var fileReader = new FileReader();
|
2021-07-24 11:25:55 +03:00
|
|
|
|
2021-07-15 23:21:19 +03:00
|
|
|
fileReader.onload = function(e) {
|
|
|
|
var img = new Image();
|
2021-07-24 11:25:55 +03:00
|
|
|
|
2021-07-15 23:21:19 +03:00
|
|
|
img.onload = function() {
|
|
|
|
//create a new pixel with the images dimentions
|
|
|
|
Startup.newPixel(this.width, this.height);
|
2021-07-24 11:25:55 +03:00
|
|
|
EditorState.switchMode('Advanced');
|
2021-07-15 23:21:19 +03:00
|
|
|
|
|
|
|
//draw the image onto the canvas
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.currentLayer.context.drawImage(img, 0, 0);
|
2021-07-19 00:17:41 +03:00
|
|
|
ColorModule.createPaletteFromLayers();
|
2021-07-15 23:21:19 +03:00
|
|
|
|
|
|
|
//track google event
|
|
|
|
if (typeof ga !== 'undefined')
|
|
|
|
ga('send', 'event', 'Pixel Editor Load', colorPalette.length, this.width+'/'+this.height); /*global ga*/
|
|
|
|
|
|
|
|
};
|
|
|
|
img.src = e.target.result;
|
|
|
|
};
|
|
|
|
fileReader.readAsDataURL(browseHolder.files[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function openProject() {
|
|
|
|
let file = browseHolder.files[0];
|
|
|
|
let reader = new FileReader();
|
|
|
|
|
|
|
|
// Getting all the data
|
|
|
|
reader.readAsText(file, "UTF-8");
|
|
|
|
// Converting the data to a json object and creating a new pixel (see _newPixel.js for more)
|
|
|
|
reader.onload = function (e) {
|
|
|
|
let dictionary = JSON.parse(e.target.result);
|
|
|
|
Startup.newPixel(dictionary['canvasWidth'], dictionary['canvasHeight'], dictionary);
|
2021-10-27 11:02:21 +03:00
|
|
|
|
|
|
|
for (let i=0; dictionary['color' + i] != null; i++) {
|
2021-10-25 20:23:06 +03:00
|
|
|
ColorModule.addColor(dictionary['color'+i]);
|
|
|
|
}
|
2021-10-27 11:02:21 +03:00
|
|
|
|
|
|
|
// Removing the default colours
|
|
|
|
ColorModule.deleteColor(ColorModule.getCurrentPalette()[0]);
|
|
|
|
ColorModule.deleteColor(ColorModule.getCurrentPalette()[0]);
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProjectData() {
|
|
|
|
// use a dictionary
|
|
|
|
let dictionary = {};
|
|
|
|
// sorting layers by increasing z-index
|
2021-12-06 19:37:43 +03:00
|
|
|
let layersCopy = currFile.layers.slice();
|
2021-07-15 23:21:19 +03:00
|
|
|
layersCopy.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? 1 : -1);
|
|
|
|
// save canvas size
|
2021-12-06 19:37:43 +03:00
|
|
|
dictionary['canvasWidth'] = currFile.canvasSize[0];
|
|
|
|
dictionary['canvasHeight'] = currFile.canvasSize[1];
|
2021-07-15 23:21:19 +03:00
|
|
|
// save editor mode
|
2021-07-23 19:54:09 +03:00
|
|
|
dictionary['editorMode'] = EditorState.getCurrentMode();
|
2021-07-15 23:21:19 +03:00
|
|
|
// save palette
|
|
|
|
for (let i=0; i<ColorModule.getCurrentPalette().length; i++) {
|
|
|
|
dictionary["color" + i] = ColorModule.getCurrentPalette()[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// save number of layers
|
|
|
|
dictionary["nLayers"] = layersCopy.length;
|
|
|
|
|
|
|
|
// save layers
|
|
|
|
for (let i=0; i<layersCopy.length; i++) {
|
|
|
|
// Only saving the layers the user has access to (no vfx, tmp or checkerboard layers)
|
|
|
|
if (layersCopy[i].menuEntry != null) {
|
|
|
|
dictionary["layer" + i] = layersCopy[i];
|
|
|
|
dictionary["layer" + i + "ImageData"] = layersCopy[i].canvas.toDataURL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(dictionary);
|
|
|
|
}
|
|
|
|
|
2021-07-23 17:35:42 +03:00
|
|
|
function loadPalette() {
|
2021-07-23 19:30:04 +03:00
|
|
|
if (browsePaletteHolder.files && browsePaletteHolder.files[0]) {
|
2021-07-23 17:35:42 +03:00
|
|
|
//make sure file is allowed filetype
|
2021-07-23 19:30:04 +03:00
|
|
|
var fileContentType = browsePaletteHolder.files[0].type;
|
2021-07-23 17:35:42 +03:00
|
|
|
if (fileContentType == 'image/png' || fileContentType == 'image/gif') {
|
|
|
|
|
|
|
|
//load file
|
|
|
|
var fileReader = new FileReader();
|
|
|
|
fileReader.onload = function(e) {
|
|
|
|
var img = new Image();
|
|
|
|
img.onload = function() {
|
|
|
|
|
|
|
|
//draw image onto the temporary canvas
|
|
|
|
var loadPaletteCanvas = document.getElementById('load-palette-canvas-holder');
|
|
|
|
var loadPaletteContext = loadPaletteCanvas.getContext('2d');
|
|
|
|
|
|
|
|
loadPaletteCanvas.width = img.width;
|
|
|
|
loadPaletteCanvas.height = img.height;
|
|
|
|
|
|
|
|
loadPaletteContext.drawImage(img, 0, 0);
|
|
|
|
|
|
|
|
//create array to hold found colors
|
|
|
|
var colorPalette = [];
|
|
|
|
var imagePixelData = loadPaletteContext.getImageData(0,0,this.width, this.height).data;
|
|
|
|
|
|
|
|
//loop through pixels looking for colors to add to palette
|
|
|
|
for (var i = 0; i < imagePixelData.length; i += 4) {
|
|
|
|
const newColor = {r:imagePixelData[i],g:imagePixelData[i + 1],b:imagePixelData[i + 2]};
|
|
|
|
var color = '#' + Color.rgbToHex(newColor);
|
|
|
|
if (colorPalette.indexOf(color) == -1) {
|
|
|
|
colorPalette.push(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//add to palettes so that it can be loaded when they click okay
|
|
|
|
palettes['Loaded palette'] = {};
|
|
|
|
palettes['Loaded palette'].colors = colorPalette;
|
|
|
|
Util.setText('palette-button', 'Loaded palette');
|
|
|
|
Util.setText('palette-button-splash', 'Loaded palette');
|
2021-07-23 19:30:04 +03:00
|
|
|
Util.toggle('palette-menu-splash');
|
2021-07-23 17:35:42 +03:00
|
|
|
};
|
|
|
|
img.src = e.target.result;
|
|
|
|
};
|
2021-07-23 19:30:04 +03:00
|
|
|
fileReader.readAsDataURL(browsePaletteHolder.files[0]);
|
2021-07-23 17:35:42 +03:00
|
|
|
}
|
|
|
|
else alert('Only PNG and GIF files are supported at this time.');
|
|
|
|
}
|
2021-07-23 19:30:04 +03:00
|
|
|
|
|
|
|
browsePaletteHolder.value = null;
|
2021-07-23 17:35:42 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 23:21:19 +03:00
|
|
|
return {
|
|
|
|
saveProject,
|
|
|
|
exportProject,
|
2021-08-14 23:47:49 +03:00
|
|
|
openPixelExportWindow,
|
|
|
|
openSaveProjectWindow,
|
2021-07-15 23:21:19 +03:00
|
|
|
open
|
|
|
|
}
|
|
|
|
})();
|