2020-06-20 00:31:36 +03:00
|
|
|
let firstPixel = true;
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
function newPixel (width, height, palette) {
|
2020-06-20 00:31:36 +03:00
|
|
|
if (firstPixel) {
|
|
|
|
layerList = document.getElementById("layers-menu");
|
|
|
|
layerListEntry = layerList.firstElementChild;
|
|
|
|
|
2020-06-20 23:49:02 +03:00
|
|
|
// Setting up the current layer
|
|
|
|
currentLayer = new Layer(width, height, canvas, layerListEntry);
|
|
|
|
canvas.style.zIndex = 2;
|
2020-06-20 00:31:36 +03:00
|
|
|
}
|
|
|
|
else {
|
2020-06-20 23:49:02 +03:00
|
|
|
let nLayers = layers.length;
|
|
|
|
for (let i=2; i < layers.length - 2; i++) {
|
|
|
|
let currentEntry = layers[i].menuEntry;
|
|
|
|
let associatedLayer;
|
|
|
|
|
|
|
|
if (currentEntry != null) {
|
|
|
|
// Getting the associated layer
|
|
|
|
associatedLayer = getLayerByID(currentEntry.id);
|
|
|
|
|
|
|
|
// Deleting its canvas
|
|
|
|
associatedLayer.canvas.remove();
|
|
|
|
|
|
|
|
// Adding the id to the unused ones
|
|
|
|
unusedIDs.push(currentEntry.id);
|
|
|
|
// Removing the entry from the menu
|
|
|
|
currentEntry.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removing the old layers from the list
|
|
|
|
for (let i=2; i<nLayers - 2; i++) {
|
|
|
|
layers.splice(2, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting up the current layer
|
|
|
|
layers[1] = new Layer(width, height, layers[1].canvas, layers[1].menuEntry);
|
|
|
|
currentLayer = layers[1];
|
|
|
|
|
|
|
|
canvas = currentLayer.canvas;
|
|
|
|
context = currentLayer.context;
|
|
|
|
canvas.style.zIndex = 2;
|
|
|
|
|
2020-06-20 00:31:36 +03:00
|
|
|
// TODO: clean layers before creating a new pixel
|
|
|
|
// Devo togliere tutte le entries tranne la prima
|
|
|
|
// Devo pulire la preview della prima entry
|
|
|
|
// Devo cancellare tutte le tele tranne quella con id pixel-canvas
|
|
|
|
}
|
2020-06-19 00:24:37 +03:00
|
|
|
|
2020-03-04 16:36:40 +03:00
|
|
|
// Adding the checkerboard behind it
|
2020-03-04 17:46:25 +03:00
|
|
|
checkerBoard = new Layer(width, height, checkerBoardCanvas);
|
2019-03-31 17:32:49 +03:00
|
|
|
|
2020-03-04 16:36:40 +03:00
|
|
|
// Creating the vfx layer on top of everything
|
2020-03-04 17:46:25 +03:00
|
|
|
VFXLayer = new Layer(width, height, VFXCanvas);
|
2020-03-04 16:36:40 +03:00
|
|
|
|
2020-06-19 00:24:37 +03:00
|
|
|
// Tmp layer to draw previews on
|
2020-03-04 21:46:19 +03:00
|
|
|
TMPLayer = new Layer(width, height, TMPCanvas);
|
|
|
|
|
2019-03-31 17:32:49 +03:00
|
|
|
canvasSize = currentLayer.canvasSize;
|
2019-03-31 18:15:03 +03:00
|
|
|
|
2020-06-20 23:49:02 +03:00
|
|
|
if (firstPixel) {
|
|
|
|
// Cloning the entry so that when I change something on the first layer, those changes aren't
|
|
|
|
// propagated to the other ones
|
|
|
|
layerListEntry = layerListEntry.cloneNode(true);
|
|
|
|
// Adding the first layer and the checkerboard to the list of layers
|
|
|
|
layers.push(checkerBoard);
|
|
|
|
layers.push(currentLayer);
|
|
|
|
layers.push(VFXLayer);
|
|
|
|
layers.push(TMPLayer);
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//remove current palette
|
|
|
|
colors = document.getElementsByClassName('color-button');
|
|
|
|
while (colors.length > 0) {
|
|
|
|
colors[0].parentElement.remove();
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//add colors from selected palette
|
|
|
|
var selectedPalette = getText('palette-button');
|
|
|
|
if (selectedPalette != 'Choose a palette...') {
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//if this palette isnt the one specified in the url, then reset the url
|
|
|
|
if (!palettes[selectedPalette].specified)
|
|
|
|
history.pushState(null, null, '/pixel-editor/app');
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//fill the palette with specified palette
|
|
|
|
createColorPalette(palettes[selectedPalette].colors,true);
|
2020-06-18 17:04:45 +03:00
|
|
|
fillCheckerboard();
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
//this wasn't a specified palette, so reset the url
|
|
|
|
history.pushState(null, null, '/pixel-editor/app');
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//generate default colors
|
|
|
|
var fg = hslToRgb(Math.floor(Math.random()*255), 230,70);
|
|
|
|
var bg = hslToRgb(Math.floor(Math.random()*255), 230,170);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//convert colors to hex
|
|
|
|
var defaultForegroundColor = rgbToHex(fg.r,fg.g,fg.b);
|
|
|
|
var defaultBackgroundColor = rgbToHex(bg.r,bg.g,bg.b);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//add colors to paletee
|
|
|
|
addColor(defaultForegroundColor).classList.add('selected');
|
|
|
|
addColor(defaultBackgroundColor);
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-18 17:04:45 +03:00
|
|
|
//fill background of canvas with bg color
|
2019-03-31 14:28:46 +03:00
|
|
|
fillCheckerboard();
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//set current drawing color as foreground color
|
2019-03-31 17:32:49 +03:00
|
|
|
currentLayer.context.fillStyle = '#'+defaultForegroundColor;
|
2020-03-07 01:21:42 +03:00
|
|
|
currentGlobalColor = '#' + defaultForegroundColor;
|
2019-03-27 02:20:54 +03:00
|
|
|
selectedPalette = 'none';
|
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//reset undo and redo states
|
|
|
|
undoStates = [];
|
|
|
|
redoStates = [];
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
closeDialogue();
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
document.getElementById('save-as-button').classList.remove('disabled');
|
|
|
|
documentCreated = true;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-06-20 23:49:02 +03:00
|
|
|
firstPixel = false;
|
|
|
|
}
|