2021-07-15 18:10:07 +03:00
|
|
|
const Startup = (() => {
|
2021-07-15 19:33:19 +03:00
|
|
|
let splashPostfix = '';
|
|
|
|
|
2021-07-20 23:52:51 +03:00
|
|
|
Events.on('click', 'create-button', create, false);
|
|
|
|
Events.on('click', 'create-button-splash', create, true);
|
2021-07-15 19:33:19 +03:00
|
|
|
|
2021-07-16 00:30:55 +03:00
|
|
|
function create(isSplash) {
|
2021-07-15 19:33:19 +03:00
|
|
|
// If I'm creating from the splash menu, I append '-splash' so I get the corresponding values
|
2021-07-24 13:37:34 +03:00
|
|
|
if (isSplash)
|
2021-07-15 19:33:19 +03:00
|
|
|
splashPostfix = '-splash';
|
2021-07-24 13:37:34 +03:00
|
|
|
else
|
2021-07-15 19:33:19 +03:00
|
|
|
splashPostfix = '';
|
2021-07-15 18:10:07 +03:00
|
|
|
|
2021-07-15 19:33:19 +03:00
|
|
|
var width = Util.getValue('size-width' + splashPostfix);
|
|
|
|
var height = Util.getValue('size-height' + splashPostfix);
|
|
|
|
var selectedPalette = Util.getText('palette-button' + splashPostfix);
|
|
|
|
|
|
|
|
newPixel(width, height);
|
|
|
|
resetInput();
|
|
|
|
|
|
|
|
//track google event
|
|
|
|
if (typeof ga !== 'undefined')
|
|
|
|
ga('send', 'event', 'Pixel Editor New', selectedPalette, width+'/'+height); /*global ga*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a new, empty file
|
|
|
|
*
|
|
|
|
* @param {*} width Start width of the canvas
|
|
|
|
* @param {*} height Start height of the canvas
|
|
|
|
* @param {*} fileContent If fileContent != null, then the newPixel is being called from the open menu
|
|
|
|
*/
|
2021-07-24 13:37:34 +03:00
|
|
|
function newPixel (width, height, fileContent = null) {
|
2021-07-15 19:33:19 +03:00
|
|
|
// The palette is empty, at the beginning
|
|
|
|
ColorModule.resetPalette();
|
|
|
|
|
|
|
|
initLayers(width, height);
|
|
|
|
initPalette();
|
|
|
|
|
|
|
|
// Closing the "New Pixel dialogue"
|
|
|
|
Dialogue.closeDialogue();
|
|
|
|
// Updating the cursor of the current tool
|
2021-10-27 11:02:21 +03:00
|
|
|
ToolManager.currentTool().updateCursor();
|
2021-07-15 19:33:19 +03:00
|
|
|
|
|
|
|
// The user is now able to export the Pixel
|
|
|
|
document.getElementById('export-button').classList.remove('disabled');
|
|
|
|
|
|
|
|
// Now, if I opened an LPE file
|
|
|
|
if (fileContent != null) {
|
2021-07-16 00:30:55 +03:00
|
|
|
loadFromLPE(fileContent);
|
2021-07-15 19:33:19 +03:00
|
|
|
// Deleting the default layer
|
2021-07-22 23:42:41 +03:00
|
|
|
LayerList.deleteLayer(false);
|
2021-07-20 23:09:20 +03:00
|
|
|
// Selecting the new one
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers[1].selectLayer();
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
2021-07-24 13:37:34 +03:00
|
|
|
|
|
|
|
EditorState.switchMode(EditorState.getCurrentMode());
|
|
|
|
// This is not the first Pixel anymore
|
2021-12-07 14:11:40 +03:00
|
|
|
EditorState.created();
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function initLayers(width, height) {
|
2021-12-06 19:37:43 +03:00
|
|
|
// Setting the general canvasSize
|
|
|
|
currFile.canvasSize = [width, height];
|
|
|
|
|
2021-07-23 14:08:04 +03:00
|
|
|
// If this is the first pixel I'm creating since the app has started
|
2021-12-07 14:11:40 +03:00
|
|
|
if (EditorState.firstPixel()) {
|
2021-07-15 19:33:19 +03:00
|
|
|
// Creating the first layer
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.currentLayer = new Layer(width, height, 'pixel-canvas', "");
|
|
|
|
currFile.currentLayer.canvas.style.zIndex = 2;
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-07-20 22:58:00 +03:00
|
|
|
// Deleting all the extra layers and canvases, leaving only one
|
2021-12-06 19:37:43 +03:00
|
|
|
let nLayers = currFile.layers.length;
|
|
|
|
for (let i=2; i < currFile.layers.length - nAppLayers; i++) {
|
|
|
|
let currentEntry = currFile.layers[i].menuEntry;
|
2021-07-15 19:33:19 +03:00
|
|
|
let associatedLayer;
|
|
|
|
|
|
|
|
if (currentEntry != null) {
|
|
|
|
// Getting the associated layer
|
2021-07-22 23:42:41 +03:00
|
|
|
associatedLayer = LayerList.getLayerByID(currentEntry.id);
|
2021-07-20 22:58:00 +03:00
|
|
|
|
2021-07-15 19:33:19 +03:00
|
|
|
// Deleting its canvas
|
|
|
|
associatedLayer.canvas.remove();
|
2021-07-20 22:58:00 +03:00
|
|
|
|
2021-07-15 19:33:19 +03:00
|
|
|
// Adding the id to the unused ones
|
2021-12-06 19:37:43 +03:00
|
|
|
Layer.unusedIDs.push(currentEntry.id);
|
2021-07-15 19:33:19 +03:00
|
|
|
// Removing the entry from the menu
|
|
|
|
currentEntry.remove();
|
|
|
|
}
|
|
|
|
}
|
2021-07-20 22:58:00 +03:00
|
|
|
|
|
|
|
// Removing the old layers from the list
|
|
|
|
for (let i=2; i<nLayers - nAppLayers; i++) {
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers.splice(2, 1);
|
2021-07-20 22:58:00 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 19:33:19 +03:00
|
|
|
// Setting up the current layer
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers[1] = new Layer(width, height, currFile.layers[1].canvas, currFile.layers[1].menuEntry);
|
|
|
|
currFile.currentLayer = currFile.layers[1];
|
|
|
|
currFile.currentLayer.canvas.style.zIndex = 2;
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
2021-07-20 22:58:00 +03:00
|
|
|
|
|
|
|
// Adding the checkerboard behind it
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.checkerBoard = new Checkerboard(width, height, null);
|
2021-11-12 02:09:20 +03:00
|
|
|
// Pixel grid
|
2021-12-08 01:01:49 +03:00
|
|
|
console.log("CREATED GRID");
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.pixelGrid = new PixelGrid(width, height, "pixel-grid");
|
2021-07-20 22:58:00 +03:00
|
|
|
|
|
|
|
// Creating the vfx layer on top of everything
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.VFXLayer = new Layer(width, height, 'vfx-canvas');
|
2021-07-20 22:58:00 +03:00
|
|
|
// Tmp layer to draw previews on
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.TMPLayer = new Layer(width, height, 'tmp-canvas');
|
2021-07-20 22:58:00 +03:00
|
|
|
|
2021-12-07 14:11:40 +03:00
|
|
|
if (EditorState.firstPixel()) {
|
2021-07-20 22:58:00 +03:00
|
|
|
// Adding the first layer and the checkerboard to the list of layers
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers.push(currFile.checkerBoard);
|
|
|
|
currFile.layers.push(currFile.currentLayer);
|
|
|
|
currFile.layers.push(currFile.TMPLayer);
|
|
|
|
currFile.layers.push(currFile.pixelGrid);
|
|
|
|
currFile.layers.push(currFile.VFXLayer);
|
2021-07-20 22:58:00 +03:00
|
|
|
}
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function initPalette() {
|
|
|
|
// Get selected palette
|
|
|
|
let selectedPalette = Util.getText('palette-button' + splashPostfix);
|
|
|
|
|
|
|
|
//remove current palette
|
2021-07-22 17:40:58 +03:00
|
|
|
let colors = document.getElementsByClassName('color-button');
|
2021-07-15 19:33:19 +03:00
|
|
|
while (colors.length > 0) {
|
|
|
|
colors[0].parentElement.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user selected a palette and isn't opening a file, I load the selected palette
|
|
|
|
if (selectedPalette != 'Choose a palette...') {
|
2021-07-23 19:30:04 +03:00
|
|
|
if (selectedPalette === 'Loaded palette') {
|
|
|
|
ColorModule.createColorPalette(palettes['Loaded palette'].colors);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//if this palette isnt the one specified in the url, then reset the url
|
|
|
|
if (!palettes[selectedPalette].specified)
|
2021-12-06 22:12:57 +03:00
|
|
|
history.pushState(null, null, '/pixel-editor');
|
2021-07-23 19:30:04 +03:00
|
|
|
|
|
|
|
//fill the palette with specified colours
|
|
|
|
ColorModule.createColorPalette(palettes[selectedPalette].colors);
|
|
|
|
}
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
|
|
|
// Otherwise, I just generate 2 semirandom colours
|
|
|
|
else {
|
|
|
|
//this wasn't a specified palette, so reset the url
|
|
|
|
history.pushState(null, null, '/pixel-editor');
|
|
|
|
|
|
|
|
//generate default colors
|
|
|
|
var fg = new Color("hsv", Math.floor(Math.random()*360), 50, 50).rgb;
|
|
|
|
var bg = new Color("hsv", Math.floor(Math.random()*360), 80, 100).rgb;
|
|
|
|
|
|
|
|
//convert colors to hex
|
|
|
|
var defaultForegroundColor = Color.rgbToHex(fg);
|
|
|
|
var defaultBackgroundColor = Color.rgbToHex(bg);
|
|
|
|
|
|
|
|
//add colors to palette
|
|
|
|
ColorModule.addColor(defaultForegroundColor).classList.add('selected');
|
|
|
|
ColorModule.addColor(defaultBackgroundColor);
|
|
|
|
|
|
|
|
//set current drawing color as foreground color
|
2021-07-22 19:02:19 +03:00
|
|
|
ColorModule.updateCurrentColor('#'+defaultForegroundColor);
|
2021-07-15 19:33:19 +03:00
|
|
|
selectedPalette = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 00:30:55 +03:00
|
|
|
function loadFromLPE(fileContent) {
|
2021-07-15 19:33:19 +03:00
|
|
|
// I add every layer the file had in it
|
|
|
|
for (let i=0; i<fileContent['nLayers']; i++) {
|
|
|
|
let layerData = fileContent['layer' + i];
|
|
|
|
let layerImage = fileContent['layer' + i + 'ImageData'];
|
|
|
|
|
|
|
|
if (layerData != null) {
|
|
|
|
// Setting id
|
2021-07-22 23:42:41 +03:00
|
|
|
let createdLayer = LayerList.addLayer(layerData.id, false);
|
2021-07-15 19:33:19 +03:00
|
|
|
// Setting name
|
|
|
|
createdLayer.menuEntry.getElementsByTagName("p")[0].innerHTML = layerData.name;
|
|
|
|
|
|
|
|
// Adding the image (I can do that because they're sorted by increasing z-index)
|
|
|
|
let img = new Image();
|
|
|
|
img.onload = function() {
|
|
|
|
createdLayer.context.drawImage(img, 0, 0);
|
|
|
|
createdLayer.updateLayerPreview();
|
|
|
|
|
|
|
|
if (i == (fileContent['nLayers'] - 1)) {
|
2021-07-19 00:17:41 +03:00
|
|
|
ColorModule.createPaletteFromLayers();
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
img.src = layerImage;
|
|
|
|
|
|
|
|
// Setting visibility and lock options
|
|
|
|
if (!layerData.isVisible) {
|
|
|
|
createdLayer.hide();
|
|
|
|
}
|
|
|
|
if (layerData.isLocked) {
|
|
|
|
createdLayer.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetInput() {
|
|
|
|
//reset new form
|
|
|
|
Util.setValue('size-width', 64);
|
|
|
|
Util.setValue('size-height', 64);
|
|
|
|
|
|
|
|
Util.setText('palette-button', 'Choose a palette...');
|
|
|
|
Util.setText('preset-button', 'Choose a preset...');
|
|
|
|
}
|
|
|
|
|
|
|
|
function newFromTemplate(preset, x, y) {
|
|
|
|
if (preset != '') {
|
|
|
|
const presetProperties = PresetModule.propertiesOf(preset);
|
|
|
|
Util.setText('palette-button-splash', presetProperties.palette);
|
|
|
|
Util.setText('palette-button', presetProperties.palette);
|
2021-07-16 00:30:55 +03:00
|
|
|
|
2021-07-15 19:33:19 +03:00
|
|
|
x = presetProperties.width;
|
|
|
|
y = presetProperties.height;
|
|
|
|
}
|
|
|
|
newPixel(x, y);
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:37:34 +03:00
|
|
|
function splashEditorMode(mode) {
|
|
|
|
editorMode = mode;
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:33:19 +03:00
|
|
|
return {
|
|
|
|
create,
|
|
|
|
newPixel,
|
2021-07-23 14:08:04 +03:00
|
|
|
newFromTemplate,
|
2021-07-24 13:37:34 +03:00
|
|
|
splashEditorMode
|
2021-07-15 19:33:19 +03:00
|
|
|
}
|
2021-07-15 18:10:07 +03:00
|
|
|
})();
|