2022-02-23 19:16:23 +03:00
|
|
|
// localStorage.setItem("lpe-cache",`{}`)
|
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');
|
2022-10-21 10:52:55 +03:00
|
|
|
const importImageHolder = document.getElementById('import-image-browse-holder');
|
2021-07-23 17:35:42 +03:00
|
|
|
|
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);
|
2022-10-21 10:52:55 +03:00
|
|
|
Events.on('change', importImageHolder, loadImage);
|
2022-08-24 06:04:18 +03:00
|
|
|
Events.on('click', 'export-confirm', exportProject);
|
2022-02-23 19:16:23 +03:00
|
|
|
Events.on("click", "save-project-confirm", saveProject);
|
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);
|
|
|
|
Dialogue.showDialogue('save-project', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function openPixelExportWindow() {
|
|
|
|
let selectedPalette = Util.getText('palette-button');
|
2022-08-24 07:34:19 +03:00
|
|
|
|
2021-08-14 23:47:49 +03:00
|
|
|
|
|
|
|
if (selectedPalette != 'Choose a palette...'){
|
|
|
|
var paletteAbbreviation = palettes[selectedPalette].name;
|
2022-08-24 07:34:19 +03:00
|
|
|
var fileName = 'pixel-'+paletteAbbreviation+'-'+currFile.canvasSize[0]+'x'+currFile.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);
|
|
|
|
Dialogue.showDialogue('export', false);
|
|
|
|
}
|
2021-07-15 23:21:19 +03:00
|
|
|
|
2021-08-14 23:47:49 +03:00
|
|
|
function saveProject() {
|
|
|
|
// Get name
|
2022-02-23 19:16:23 +03:00
|
|
|
// debugger;
|
2021-08-14 23:47:49 +03:00
|
|
|
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*/
|
2022-02-23 19:16:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
LayerList.closeOptionsMenu(); // is this the right place for this?
|
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
|
|
|
|
2022-10-24 13:40:34 +03:00
|
|
|
exportCanvas.getContext("2d").willReadFrequently = true;
|
|
|
|
emptyCanvas.getContext("2d").willReadFrequently = true;
|
|
|
|
|
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();
|
|
|
|
}
|
2022-02-23 19:16:23 +03:00
|
|
|
function localStorageCheck() {
|
|
|
|
return !!localStorage.getItem("lpe-cache");
|
|
|
|
}
|
|
|
|
function localStorageSave() {
|
|
|
|
const lpeStr = getProjectData();
|
|
|
|
const lpe = JSON.parse(lpeStr);
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('BEFORE JSON.stringify(lpe.colors,null,4) === ',JSON.stringify(lpe.colors,null,4));
|
|
|
|
console.log([...ColorModule.getCurrentPalette()]);
|
|
|
|
if(lpe.colors.length < 1)lpe.colors = [...ColorModule.getCurrentPalette()];
|
2022-02-23 19:16:23 +03:00
|
|
|
if(lpe.colors.length < 1)lpe.colors.push("#000000");
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('AFTER JSON.stringify(lpe.colors,null,4) === ',JSON.stringify(lpe.colors,null,4));
|
2022-02-23 19:16:23 +03:00
|
|
|
if(!lpe.canvasWidth)lpe.canvasWidth = 16;
|
|
|
|
if(!lpe.canvasHeight)lpe.canvasHeight = 16;
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('LPE saved === ',lpe);
|
2022-02-23 19:16:23 +03:00
|
|
|
localStorage.setItem("lpe-cache", JSON.stringify(lpe));
|
|
|
|
}
|
|
|
|
function localStorageReset() {
|
|
|
|
localStorage.setItem("lpe-cache", JSON.stringify({
|
|
|
|
"canvasWidth":16,
|
|
|
|
"canvasHeight":16,
|
|
|
|
"editorMode":"Advanced",
|
|
|
|
"colors":["#000000","#0b6082","#1d8425","#cc1919"],
|
|
|
|
"selectedLayer":0,
|
|
|
|
"layers":[
|
|
|
|
{"canvas":{},"context":{"mozImageSmoothingEnabled":false},"isSelected":true,"isVisible":true,"isLocked":false,"oldLayerName":null,"menuEntry":{},"id":"layer0","name":"Layer 0","src":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAHZJREFUOE9jZKAQMFKon4EuBvxHcyWKpdhcgK4BpB+nS9ElYJqJ9hqyQpI1ozsNZABRNnMnNIEt+7qgjhGrBpgCWOCBFKJHN0gNTgOQFSPbhi5OlAHYEhpBL+DThO4tgoGGHB7YwgKvAbj8j+xCgi4glNkoNgAA3JApEbHObDkAAAAASUVORK5CYII="},
|
|
|
|
{"canvas":{},"context":{"mozImageSmoothingEnabled":false},"isSelected":false,"isVisible":true,"isLocked":false,"oldLayerName":null,"menuEntry":{},"id":"layer1","name":"Layer 1","src":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAGNJREFUOE9jZKAQMCLrl21R/Q/iP665jSKOzw7qGgCyCeQKsl0AM4AUb2D1KymuoJ0BxHoDZ3QRG6V445uYsCBoACGvEExxhFxBlAH4XEHQAEKpkygDkFMoumuINgCWI9HDBAChJjwRzAXQUwAAAABJRU5ErkJggg=="},
|
|
|
|
{"canvas":{},"context":{"mozImageSmoothingEnabled":false},"isSelected":false,"isVisible":true,"isLocked":false,"oldLayerName":null,"menuEntry":{},"id":"layer2","name":"Layer 2","src":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAGNJREFUOE9jZKAQMJKi/4yk5H+YepPnz8F68RqArAFdI4yP1QBsNuFyKYYBMM0wJxLyIlYDiNWMNQxALhimBuCKUoKBSChKcRpASCPOhESsRoIGEBuVKF4g1XaMhERqMgYZAACIaEgR0hnFxgAAAABJRU5ErkJggg=="}
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
}
|
2022-02-25 17:49:30 +03:00
|
|
|
function defaultLPE(w,h,colors) {
|
|
|
|
return {
|
|
|
|
"canvasWidth":w,
|
|
|
|
"canvasHeight":h,
|
|
|
|
"editorMode":"Advanced",
|
|
|
|
colors,
|
|
|
|
"selectedLayer":0,
|
|
|
|
"layers":[
|
|
|
|
{"canvas":{},"context":{"mozImageSmoothingEnabled":false},"isSelected":true,"isVisible":true,"isLocked":false,"oldLayerName":null,"menuEntry":{},"id":"layer0","name":"Layer 0","src":emptyCanvasSrc(w,h)}
|
|
|
|
]
|
|
|
|
};
|
2022-02-27 17:14:42 +03:00
|
|
|
|
|
|
|
function emptyCanvasSrc(w,h) {
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
canvas.width = w;
|
|
|
|
canvas.height = h;
|
|
|
|
return canvas.toDataURL();
|
|
|
|
}
|
2022-02-25 17:49:30 +03:00
|
|
|
}
|
2022-02-23 19:16:23 +03:00
|
|
|
function localStorageLoad() {
|
|
|
|
////console.log("loading from localStorage");
|
|
|
|
////console.log('JSON.parse(localStorage.getItem("lpe-cache") ?? "{}") === ',JSON.parse(localStorage.getItem("lpe-cache") ?? "{}"));
|
|
|
|
const lpe = JSON.parse(localStorage.getItem("lpe-cache") ?? "{}");
|
|
|
|
//console.log('LPE loaded === ',lpe);
|
|
|
|
return lpe;
|
|
|
|
}
|
2021-07-15 23:21:19 +03:00
|
|
|
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();
|
2022-03-23 15:47:57 +03:00
|
|
|
// openFile();
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
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
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('this === ',this);
|
2022-02-25 17:49:30 +03:00
|
|
|
Startup.newPixel({
|
|
|
|
canvasWidth: this.width,
|
|
|
|
canvasHeight: 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]);
|
|
|
|
}
|
2022-02-23 19:16:23 +03:00
|
|
|
function openProject(lpeData) {
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('lpeData === ',lpeData);
|
2021-07-15 23:21:19 +03:00
|
|
|
// Getting all the data
|
2022-02-23 19:16:23 +03:00
|
|
|
if(lpeData){
|
|
|
|
_parseLPE(lpeData);
|
|
|
|
} else {
|
2022-03-23 15:47:57 +03:00
|
|
|
let file = browseHolder.files[0];
|
2022-02-23 19:16:23 +03:00
|
|
|
let reader = new FileReader();
|
|
|
|
// Converting the data to a json object and creating a new pixel (see _newPixel.js for more)
|
|
|
|
reader.onload = function (e) {
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('this === ',this);
|
|
|
|
console.log('e === ',e);
|
2022-02-23 19:16:23 +03:00
|
|
|
let dictionary = JSON.parse(e.target.result);
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('FileManager.js => openProject => loaded lpe dictionary === ',dictionary);
|
2022-02-23 19:16:23 +03:00
|
|
|
_parseLPE(dictionary);
|
2021-10-25 20:23:06 +03:00
|
|
|
}
|
2022-03-23 15:47:57 +03:00
|
|
|
reader.readAsText(file, "UTF-8");
|
2022-02-23 19:16:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function _parseLPE(dictionary) {
|
2022-03-23 15:47:57 +03:00
|
|
|
Startup.newPixel(dictionary);
|
2021-07-15 23:21:19 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 19:16:23 +03:00
|
|
|
function loadFromLPE(dictionary) {
|
|
|
|
ColorModule.resetPalette();
|
|
|
|
|
|
|
|
//console.log('dictionary === ',dictionary);
|
|
|
|
|
|
|
|
EditorState.switchMode(dictionary.editorMode ?? 'Advanced');
|
|
|
|
|
|
|
|
if(dictionary.colors)ColorModule.createColorPalette(dictionary.colors);
|
2022-03-23 15:47:57 +03:00
|
|
|
|
|
|
|
// Startup.newPixel(dictionary);
|
2022-02-23 19:16:23 +03:00
|
|
|
}
|
2021-07-15 23:21:19 +03:00
|
|
|
function getProjectData() {
|
|
|
|
// use a dictionary
|
|
|
|
let dictionary = {};
|
|
|
|
// sorting layers by increasing z-index
|
2022-02-23 19:16:23 +03:00
|
|
|
let layersCopy = currFile.layers.filter(n=>!!n.menuEntry).slice();
|
2021-12-06 19:37:43 +03:00
|
|
|
dictionary['canvasWidth'] = currFile.canvasSize[0];
|
|
|
|
dictionary['canvasHeight'] = currFile.canvasSize[1];
|
2021-07-23 19:54:09 +03:00
|
|
|
dictionary['editorMode'] = EditorState.getCurrentMode();
|
2022-02-23 19:16:23 +03:00
|
|
|
dictionary.colors = [
|
|
|
|
...ColorModule.getCurrentPalette()
|
|
|
|
];
|
|
|
|
dictionary.layers = layersCopy
|
|
|
|
.map((n,i)=>{
|
|
|
|
//console.log('n.name === ',n.name);
|
|
|
|
if(n.isSelected)dictionary.selectedLayer = i;
|
|
|
|
return {
|
|
|
|
...n,
|
|
|
|
src: n.canvas.toDataURL(),
|
|
|
|
};
|
|
|
|
});
|
2021-07-15 23:21:19 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-22 12:46:48 +03:00
|
|
|
currentImportPivotElement = undefined;
|
|
|
|
currentImportPivotPosition = 'middle';
|
|
|
|
isImportWindowInitialized = false;
|
|
|
|
|
2022-10-21 10:52:55 +03:00
|
|
|
/**
|
|
|
|
* Displays the import image window to allow for configurations
|
|
|
|
* to be made be the image is imported.
|
|
|
|
*/
|
|
|
|
function openImportImageWindow() {
|
2022-10-21 11:18:10 +03:00
|
|
|
// Reset window values.
|
|
|
|
importImageHolder.value = null;
|
2022-10-22 12:46:48 +03:00
|
|
|
|
2022-10-21 11:18:10 +03:00
|
|
|
document.getElementById('import-image-match-size').checked = false;
|
2022-10-22 12:46:48 +03:00
|
|
|
document.getElementById('import-image-update-palette').checked = false;
|
2022-10-21 11:18:10 +03:00
|
|
|
document.getElementById('import-image-name').innerText = "";
|
|
|
|
|
2022-10-22 12:46:48 +03:00
|
|
|
// Workaround to prevent events from firing twice for the import window.
|
|
|
|
if (!this.isImportWindowInitialized) {
|
|
|
|
// Getting the pivot buttons and setting the default pivot selection.
|
|
|
|
let pivotButtons = document.getElementsByClassName("pivot-button");
|
|
|
|
this.currentImportPivotElement = document.querySelector('.import-image-location-pivot .rc-selected-pivot');
|
|
|
|
|
|
|
|
// Add event handlers for each pivot.
|
|
|
|
for (let i=0; i < pivotButtons.length; i++) {
|
|
|
|
Events.on("click", pivotButtons[i], onImportPivotChanged.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
Events.on("click", "select-image", () => document.getElementById('import-image-browse-holder')?.click());
|
|
|
|
Events.on("click", "import-image-confirm", importImage);
|
2022-10-22 13:02:00 +03:00
|
|
|
|
|
|
|
this.isImportWindowInitialized = true;
|
2022-10-22 12:46:48 +03:00
|
|
|
}
|
2022-10-21 11:18:10 +03:00
|
|
|
|
2022-10-21 10:52:55 +03:00
|
|
|
Dialogue.showDialogue('import-image', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the image and draws it to the current canvas layer. Called when
|
|
|
|
* the import image window is finalized.
|
|
|
|
*/
|
|
|
|
function importImage() {
|
|
|
|
if (!importImageHolder.files || importImageHolder.files.length === 0) {
|
|
|
|
alert('Please select a file before attempting to import.')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileReader = new FileReader();
|
|
|
|
|
|
|
|
// Once the image has been loaded draw the image to the current layer at the top right.
|
2022-10-22 12:46:48 +03:00
|
|
|
fileReader.onload = (e) => {
|
2022-10-21 10:52:55 +03:00
|
|
|
var img = new Image();
|
2022-10-22 12:46:48 +03:00
|
|
|
|
2022-10-21 10:52:55 +03:00
|
|
|
img.onload = () => {
|
|
|
|
let shouldResizeCanvas = document.getElementById('import-image-match-size').checked;
|
2022-10-22 12:46:48 +03:00
|
|
|
let shouldImportColors = document.getElementById('import-image-update-palette').checked;
|
2022-10-21 10:52:55 +03:00
|
|
|
|
|
|
|
// Resize the canvas to the image size if the flag was set to true.
|
|
|
|
if (shouldResizeCanvas) {
|
|
|
|
currFile.resizeCanvas(null, { x: img.width, y: img.height }, null, false);
|
|
|
|
}
|
|
|
|
|
2022-10-22 12:46:48 +03:00
|
|
|
// Calculate pivot offset and draw the imported image. Ensure the pivot position accounts for the imported images dimensions.
|
|
|
|
let offset = Util.getPivotPosition(this.currentImportPivotPosition, currFile.canvasSize[0], currFile.canvasSize[1], img.width, img.height);
|
|
|
|
currFile.currentLayer.context.drawImage(img, offset.x, offset.y);
|
|
|
|
|
|
|
|
if (shouldImportColors) {
|
|
|
|
ColorModule.updatePaletteFromLayers();
|
|
|
|
}
|
2022-10-21 10:52:55 +03:00
|
|
|
|
|
|
|
Dialogue.closeDialogue();
|
|
|
|
};
|
|
|
|
img.src = e.target.result;
|
|
|
|
};
|
|
|
|
|
|
|
|
fileReader.readAsDataURL(importImageHolder.files[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the import image holder file input fires an onchange event.
|
|
|
|
*/
|
|
|
|
function loadImage() {
|
|
|
|
if (importImageHolder.files && importImageHolder.files[0]) {
|
|
|
|
let fileName = document.getElementById("import-image-browse-holder").value;
|
|
|
|
let extension = Util.getFileExtension(fileName);
|
|
|
|
|
|
|
|
// Display the file name in the window.
|
|
|
|
document.getElementById('import-image-name').innerText = importImageHolder.files[0].name;
|
|
|
|
|
|
|
|
// Checking if the extension is supported
|
|
|
|
if (extension !== 'png') {
|
|
|
|
alert('Only PNG files are currently allowed to be imported at this time.')
|
|
|
|
importImageHolder.value = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 12:46:48 +03:00
|
|
|
/**
|
|
|
|
* Called when the selected pivot for the import image is changed.
|
|
|
|
* @param {*} event The event for the selected pivot.
|
|
|
|
*/
|
|
|
|
function onImportPivotChanged(event) {
|
|
|
|
this.currentImportPivotPosition = event.target.getAttribute("value");
|
|
|
|
|
|
|
|
// Setting the selected class
|
|
|
|
this.currentImportPivotElement.classList.remove("rc-selected-pivot");
|
|
|
|
this.currentImportPivotElement = event.target;
|
|
|
|
this.currentImportPivotElement.classList.add("rc-selected-pivot");
|
|
|
|
}
|
2022-10-24 13:48:08 +03:00
|
|
|
|
2022-02-23 19:16:23 +03:00
|
|
|
function upgradeLPE(dictionary) {
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('dictionary === ',dictionary);
|
2022-02-23 19:16:23 +03:00
|
|
|
if(dictionary.color0 && !dictionary.colors) {
|
|
|
|
dictionary.colors = [];
|
|
|
|
let colorIdx = 0;
|
|
|
|
while(dictionary[`color${colorIdx}`]) {
|
|
|
|
dictionary.colors.push(dictionary[`color${colorIdx}`]);
|
|
|
|
delete dictionary[`color${colorIdx}`];
|
|
|
|
colorIdx++;
|
|
|
|
}
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('Object.keys(dictionary) === ',Object.keys(dictionary));
|
2022-02-23 19:16:23 +03:00
|
|
|
dictionary.layers = Object.keys(dictionary).reduce((r,k,i)=>{
|
2022-03-23 15:47:57 +03:00
|
|
|
if(k.slice(0,5) === "layer" && dictionary[k]){
|
2022-02-23 19:16:23 +03:00
|
|
|
if(dictionary[k].isSelected){
|
|
|
|
dictionary.selectedLayer = r.length;
|
|
|
|
}
|
|
|
|
r.push({
|
|
|
|
...dictionary[k],
|
|
|
|
src: dictionary[`${k}ImageData`]
|
|
|
|
});
|
|
|
|
|
|
|
|
delete dictionary[k];
|
|
|
|
delete dictionary[`${k}ImageData`];
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
},[]);
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('dictionary.layers === ',dictionary.layers);
|
2022-02-23 19:16:23 +03:00
|
|
|
}
|
2022-03-23 15:47:57 +03:00
|
|
|
console.log('dictionary === ',dictionary);
|
2022-02-23 19:16:23 +03:00
|
|
|
return dictionary;
|
|
|
|
}
|
2022-02-25 17:49:30 +03:00
|
|
|
function toggleCache(elm){
|
|
|
|
console.log('elm === ',elm);
|
|
|
|
FileManager.cacheEnabled = !FileManager.cacheEnabled;
|
|
|
|
localStorage.setItem("lpe-cache-enabled",FileManager.cacheEnabled ? "1" : "0");
|
|
|
|
elm.textContent = cacheBtnText(FileManager.cacheEnabled);
|
|
|
|
}
|
|
|
|
function cacheBtnText(cacheEnabled) {
|
|
|
|
return `${cacheEnabled ? "Disable" : "Enable"} auto-cache`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheEnabled = !!Number(localStorage.getItem("lpe-cache-enabled"));
|
|
|
|
document.getElementById("auto-cache-button").textContent = cacheBtnText(cacheEnabled);
|
2021-07-23 17:35:42 +03:00
|
|
|
|
2022-03-23 15:47:57 +03:00
|
|
|
const ret = {
|
2022-02-25 17:49:30 +03:00
|
|
|
cacheEnabled,
|
2022-02-23 19:16:23 +03:00
|
|
|
loadFromLPE,
|
2022-02-25 17:49:30 +03:00
|
|
|
toggleCache,
|
2022-02-23 19:16:23 +03:00
|
|
|
getProjectData,
|
|
|
|
localStorageReset,
|
|
|
|
localStorageCheck,
|
|
|
|
localStorageSave,
|
|
|
|
localStorageLoad,
|
|
|
|
upgradeLPE,
|
2022-02-25 17:49:30 +03:00
|
|
|
defaultLPE,
|
2021-07-15 23:21:19 +03:00
|
|
|
saveProject,
|
2022-02-23 19:16:23 +03:00
|
|
|
openProject,
|
2021-07-15 23:21:19 +03:00
|
|
|
exportProject,
|
2021-08-14 23:47:49 +03:00
|
|
|
openPixelExportWindow,
|
|
|
|
openSaveProjectWindow,
|
2022-10-21 10:52:55 +03:00
|
|
|
openImportImageWindow,
|
2021-07-15 23:21:19 +03:00
|
|
|
open
|
|
|
|
}
|
2022-10-24 13:48:08 +03:00
|
|
|
|
2022-03-23 15:47:57 +03:00
|
|
|
Object.keys(ret).forEach(k=>{
|
|
|
|
if(typeof ret[k] === "function"){
|
|
|
|
const orig = ret[k];
|
|
|
|
ret[k] = function() {
|
|
|
|
DEBUG_ARR.push(`called FileManager -> ${k}`);
|
|
|
|
return orig.call(this,...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return ret;
|
2021-07-15 23:21:19 +03:00
|
|
|
})();
|