2020-03-04 21:46:19 +03:00
|
|
|
/** TODO LIST FOR LAYERS
|
2020-09-09 07:15:22 +03:00
|
|
|
|
2020-06-18 00:09:11 +03:00
|
|
|
GENERAL REQUIREMENTS:
|
2020-09-09 07:15:22 +03:00
|
|
|
- Saving the state of an artwork to a .lospec file so that people can work on it later keeping
|
2020-06-18 00:09:11 +03:00
|
|
|
the layers they created? That'd be cool, even for the app users, that could just double click on a lospec
|
|
|
|
file for it to be opened right in the pixel editor
|
|
|
|
|
|
|
|
OPTIONAL:
|
|
|
|
|
2020-09-09 07:15:22 +03:00
|
|
|
1 - Fix issues
|
2020-06-18 17:04:45 +03:00
|
|
|
*/
|
|
|
|
|
2020-06-19 00:24:37 +03:00
|
|
|
let layerList;
|
|
|
|
let layerListEntry;
|
2020-06-19 17:20:51 +03:00
|
|
|
|
|
|
|
let layerDragSource = null;
|
|
|
|
|
2020-06-18 17:04:45 +03:00
|
|
|
let layerCount = 1;
|
|
|
|
let maxZIndex = 3;
|
|
|
|
|
2020-06-20 00:31:36 +03:00
|
|
|
let unusedIDs = [];
|
|
|
|
let currentID = layerCount;
|
2020-06-23 00:07:40 +03:00
|
|
|
let idToDelete;
|
|
|
|
let layerOptions = document.getElementById("layer-properties-menu");
|
2020-06-20 00:31:36 +03:00
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
let isRenamingLayer = false;
|
2020-06-24 23:34:21 +03:00
|
|
|
let oldLayerName = null;
|
2020-06-23 18:34:03 +03:00
|
|
|
|
2020-06-23 19:10:10 +03:00
|
|
|
on('click',"add-layer-button", addLayer, false);
|
2020-03-04 21:46:19 +03:00
|
|
|
|
2020-03-04 17:46:25 +03:00
|
|
|
/** Handler class for a single canvas (a single layer)
|
|
|
|
*
|
|
|
|
* @param width Canvas width
|
|
|
|
* @param height Canvas height
|
|
|
|
* @param canvas HTML canvas element
|
|
|
|
*/
|
2020-06-19 00:24:37 +03:00
|
|
|
class Layer {
|
|
|
|
constructor(width, height, canvas, menuEntry) {
|
|
|
|
this.canvasSize = [width, height];
|
|
|
|
this.canvas = canvas;
|
|
|
|
this.context = this.canvas.getContext('2d');
|
|
|
|
this.isSelected = false;
|
|
|
|
this.isVisible = true;
|
|
|
|
this.isLocked = false;
|
|
|
|
this.menuEntry = menuEntry;
|
|
|
|
|
2020-06-20 00:31:36 +03:00
|
|
|
let id = unusedIDs.pop();
|
2020-06-24 23:34:21 +03:00
|
|
|
console.log("id creato: " + id);
|
2020-06-20 00:31:36 +03:00
|
|
|
|
|
|
|
if (id == null) {
|
|
|
|
id = currentID;
|
|
|
|
currentID++;
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:15:22 +03:00
|
|
|
this.id = "layer" + id;
|
2020-06-20 00:31:36 +03:00
|
|
|
|
|
|
|
if (menuEntry != null) {
|
2020-06-27 14:29:28 +03:00
|
|
|
this.name = menuEntry.getElementsByTagName("p")[0].innerHTML;
|
2020-06-20 00:31:36 +03:00
|
|
|
menuEntry.id = "layer" + id;
|
2020-06-23 00:07:40 +03:00
|
|
|
menuEntry.onclick = () => this.selectLayer();
|
2020-06-19 00:24:37 +03:00
|
|
|
menuEntry.getElementsByTagName("button")[0].onclick = () => this.toggleLock();
|
|
|
|
menuEntry.getElementsByTagName("button")[1].onclick = () => this.toggleVisibility();
|
2020-06-19 17:20:51 +03:00
|
|
|
|
2020-06-23 00:07:40 +03:00
|
|
|
menuEntry.addEventListener("mouseup", this.openOptionsMenu, false);
|
2020-06-19 17:20:51 +03:00
|
|
|
menuEntry.addEventListener("dragstart", this.layerDragStart, false);
|
|
|
|
menuEntry.addEventListener("drop", this.layerDragDrop, false);
|
|
|
|
menuEntry.addEventListener("dragover", this.layerDragOver, false);
|
|
|
|
menuEntry.addEventListener("dragleave", this.layerDragLeave, false);
|
|
|
|
menuEntry.addEventListener("dragend", this.layerDragEnd, false);
|
2020-06-20 23:49:02 +03:00
|
|
|
|
|
|
|
menuEntry.getElementsByTagName("canvas")[0].getContext('2d').imageSmoothingEnabled = false;
|
2020-06-19 00:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:46:25 +03:00
|
|
|
// Initializes the canvas
|
2020-06-19 00:24:37 +03:00
|
|
|
initialize() {
|
2020-03-04 17:46:25 +03:00
|
|
|
var maxHorizontalZoom = Math.floor(window.innerWidth/this.canvasSize[0]*0.75);
|
|
|
|
var maxVerticalZoom = Math.floor(window.innerHeight/this.canvasSize[1]*0.75);
|
|
|
|
|
|
|
|
zoom = Math.min(maxHorizontalZoom,maxVerticalZoom);
|
|
|
|
if (zoom < 1) zoom = 1;
|
|
|
|
|
|
|
|
//resize canvas
|
|
|
|
this.canvas.width = this.canvasSize[0];
|
|
|
|
this.canvas.height = this.canvasSize[1];
|
|
|
|
this.canvas.style.width = (this.canvas.width*zoom)+'px';
|
|
|
|
this.canvas.style.height = (this.canvas.height*zoom)+'px';
|
|
|
|
|
|
|
|
//unhide canvas
|
|
|
|
this.canvas.style.display = 'block';
|
|
|
|
|
|
|
|
//center canvas in window
|
|
|
|
this.canvas.style.left = 64+canvasView.clientWidth/2-(this.canvasSize[0]*zoom/2)+'px';
|
|
|
|
this.canvas.style.top = 48+canvasView.clientHeight/2-(this.canvasSize[1]*zoom/2)+'px';
|
2020-04-04 10:41:56 +03:00
|
|
|
|
2020-03-07 18:49:01 +03:00
|
|
|
this.context.imageSmoothingEnabled = false;
|
2020-03-15 18:32:48 +03:00
|
|
|
this.context.mozImageSmoothingEnabled = false;
|
2020-06-19 00:24:37 +03:00
|
|
|
}
|
2020-06-19 17:20:51 +03:00
|
|
|
|
2020-06-24 14:30:24 +03:00
|
|
|
setID(id) {
|
|
|
|
this.id = id;
|
|
|
|
if (this.menuEntry != null) {
|
|
|
|
this.menuEntry.id = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 17:20:51 +03:00
|
|
|
layerDragStart(element) {
|
|
|
|
layerDragSource = this;
|
|
|
|
element.dataTransfer.effectAllowed = 'move';
|
2020-06-20 00:31:36 +03:00
|
|
|
element.dataTransfer.setData('text/html', this.id);
|
2020-06-19 17:20:51 +03:00
|
|
|
|
|
|
|
this.classList.add('dragElem');
|
|
|
|
}
|
|
|
|
|
|
|
|
layerDragOver(element) {
|
|
|
|
if (element.preventDefault) {
|
|
|
|
element.preventDefault(); // Necessary. Allows us to drop.
|
|
|
|
}
|
|
|
|
this.classList.add('layerdragover');
|
|
|
|
|
|
|
|
element.dataTransfer.dropEffect = 'move';
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
layerDragLeave(element) {
|
|
|
|
this.classList.remove('layerdragover');
|
|
|
|
}
|
|
|
|
|
|
|
|
layerDragDrop(element) {
|
|
|
|
if (element.stopPropagation) {
|
|
|
|
element.stopPropagation(); // Stops some browsers from redirecting.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do anything if dropping the same column we're dragging.
|
|
|
|
if (layerDragSource != this) {
|
2020-06-20 00:31:36 +03:00
|
|
|
let toDropID = element.dataTransfer.getData('text/html');
|
|
|
|
let thisID = this.id;
|
2020-09-09 07:15:22 +03:00
|
|
|
|
2020-06-26 00:37:41 +03:00
|
|
|
moveLayers(toDropID, thisID);
|
2020-06-19 17:20:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.classList.remove('layerdragover');
|
2020-06-20 00:31:36 +03:00
|
|
|
dragging = false;
|
2020-06-19 17:20:51 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
layerDragEnd(element) {
|
|
|
|
this.classList.remove('layerdragover');
|
2020-09-09 07:15:22 +03:00
|
|
|
}
|
2020-06-19 17:20:51 +03:00
|
|
|
|
2020-03-04 17:46:25 +03:00
|
|
|
// Resizes canvas
|
2020-06-19 00:24:37 +03:00
|
|
|
resize() {
|
2020-03-04 17:46:25 +03:00
|
|
|
let newWidth = (this.canvas.width * zoom) + 'px';
|
|
|
|
let newHeight = (this.canvas.height *zoom)+ 'px';
|
|
|
|
|
|
|
|
this.canvas.style.width = newWidth;
|
|
|
|
this.canvas.style.height = newHeight;
|
2020-06-19 00:24:37 +03:00
|
|
|
}
|
2020-07-21 23:30:46 +03:00
|
|
|
|
|
|
|
setCanvasOffset (offsetLeft, offsetTop) {
|
|
|
|
//horizontal offset
|
|
|
|
var minXOffset = -this.canvasSize[0] * zoom + 164;
|
|
|
|
var maxXOffset = window.innerWidth - 148;
|
|
|
|
|
|
|
|
if (offsetLeft < minXOffset)
|
|
|
|
this.canvas.style.left = minXOffset +'px';
|
|
|
|
else if (offsetLeft > maxXOffset)
|
|
|
|
this.canvas.style.left = maxXOffset +'px';
|
|
|
|
else
|
|
|
|
this.canvas.style.left = offsetLeft +'px';
|
|
|
|
|
|
|
|
//vertical offset
|
|
|
|
var minYOffset = -this.canvasSize[1] * zoom + 164;
|
|
|
|
var maxYOffset = window.innerHeight - 100;
|
|
|
|
|
|
|
|
if (offsetTop < minYOffset)
|
|
|
|
this.canvas.style.top = minYOffset +'px';
|
|
|
|
else if (offsetTop > maxYOffset)
|
|
|
|
this.canvas.style.top = maxYOffset +'px';
|
|
|
|
else
|
|
|
|
this.canvas.style.top = offsetTop +'px';
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:46:25 +03:00
|
|
|
// Copies the otherCanvas' position and size
|
2020-06-19 00:24:37 +03:00
|
|
|
copyData(otherCanvas) {
|
2020-03-04 17:46:25 +03:00
|
|
|
this.canvas.style.width = otherCanvas.canvas.style.width;
|
|
|
|
this.canvas.style.height = otherCanvas.canvas.style.height;
|
|
|
|
|
|
|
|
this.canvas.style.left = otherCanvas.canvas.style.left;
|
|
|
|
this.canvas.style.top = otherCanvas.canvas.style.top;
|
2020-06-19 00:24:37 +03:00
|
|
|
}
|
2020-06-18 17:04:45 +03:00
|
|
|
|
2020-06-23 00:07:40 +03:00
|
|
|
openOptionsMenu(event) {
|
|
|
|
if (event.which == 3) {
|
|
|
|
let target = event.target;
|
|
|
|
let offsets = getElementAbsolutePosition(this);
|
|
|
|
|
|
|
|
while (target != null && target.classList != null && !target.classList.contains("layers-menu-entry")) {
|
|
|
|
target = target.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
idToDelete = target.id;
|
|
|
|
|
|
|
|
layerOptions.style.visibility = "visible";
|
|
|
|
layerOptions.style.top = "0";
|
|
|
|
layerOptions.style.marginTop = "" + (event.clientY - 25) + "px";
|
|
|
|
|
|
|
|
getLayerByID(idToDelete).selectLayer();
|
|
|
|
}
|
|
|
|
}
|
2020-06-18 17:04:45 +03:00
|
|
|
|
2020-06-23 00:07:40 +03:00
|
|
|
closeOptionsMenu(event) {
|
|
|
|
layerOptions.style.visibility = "hidden";
|
2020-06-23 18:34:03 +03:00
|
|
|
currentLayer.menuEntry.getElementsByTagName("p")[0].setAttribute("contenteditable", false);
|
|
|
|
isRenamingLayer = false;
|
2020-06-24 23:34:21 +03:00
|
|
|
|
|
|
|
if (oldLayerName != null) {
|
2020-06-27 14:29:28 +03:00
|
|
|
let name = this.menuEntry.getElementsByTagName("p")[0].innerHTML;
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
new HistoryStateRenameLayer(oldLayerName, name, currentLayer);
|
2020-06-24 23:34:21 +03:00
|
|
|
oldLayerName = null;
|
|
|
|
}
|
2020-06-23 00:07:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
selectLayer(layer) {
|
|
|
|
if (layer == null) {
|
|
|
|
// Deselecting the old layer
|
|
|
|
currentLayer.deselectLayer();
|
|
|
|
|
|
|
|
// Selecting the current layer
|
|
|
|
this.isSelected = true;
|
|
|
|
this.menuEntry.classList.add("selected-layer");
|
|
|
|
currentLayer = getLayerByName(this.menuEntry.getElementsByTagName("p")[0].innerHTML);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
currentLayer.deselectLayer();
|
|
|
|
|
|
|
|
layer.isSelected = true;
|
|
|
|
layer.menuEntry.classList.add("selected-layer");
|
|
|
|
currentLayer = layer;
|
|
|
|
}
|
2020-09-12 12:45:27 +03:00
|
|
|
/*
|
2020-06-23 18:34:03 +03:00
|
|
|
canvas = currentLayer.canvas;
|
|
|
|
context = currentLayer.context;
|
2020-09-12 12:45:27 +03:00
|
|
|
*/
|
2020-06-19 00:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleLock() {
|
|
|
|
if (this.isLocked) {
|
|
|
|
this.unlock();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleVisibility() {
|
|
|
|
if (this.isVisible) {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.show();
|
|
|
|
}
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 00:07:40 +03:00
|
|
|
deselectLayer() {
|
2020-06-18 17:04:45 +03:00
|
|
|
this.isSelected = false;
|
2020-06-19 00:24:37 +03:00
|
|
|
this.menuEntry.classList.remove("selected-layer");
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:24:37 +03:00
|
|
|
lock() {
|
2020-06-18 17:04:45 +03:00
|
|
|
this.isLocked = true;
|
2020-06-19 16:25:25 +03:00
|
|
|
this.menuEntry.getElementsByClassName("layer-button")[0].style.visibility = "visible";
|
2020-06-19 13:57:07 +03:00
|
|
|
|
|
|
|
this.menuEntry.getElementsByClassName("default-icon")[0].style.display = "none";
|
|
|
|
this.menuEntry.getElementsByClassName("edited-icon")[0].style.display = "inline-block";
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:24:37 +03:00
|
|
|
unlock() {
|
2020-06-18 17:04:45 +03:00
|
|
|
this.isLocked = false;
|
2020-06-19 16:25:25 +03:00
|
|
|
this.menuEntry.getElementsByClassName("layer-button")[0].style.visibility = "hidden";
|
2020-06-19 13:57:07 +03:00
|
|
|
|
|
|
|
this.menuEntry.getElementsByClassName("default-icon")[0].style.display = "inline-block";
|
|
|
|
this.menuEntry.getElementsByClassName("edited-icon")[0].style.display = "none";
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:24:37 +03:00
|
|
|
show() {
|
2020-06-18 17:04:45 +03:00
|
|
|
this.isVisible = true;
|
2020-06-19 00:24:37 +03:00
|
|
|
this.canvas.style.visibility = "visible";
|
2020-06-19 16:25:25 +03:00
|
|
|
this.menuEntry.getElementsByClassName("layer-button")[1].style.visibility = "hidden";
|
2020-06-19 00:24:37 +03:00
|
|
|
|
|
|
|
// Changing icon
|
|
|
|
this.menuEntry.getElementsByClassName("default-icon")[1].style.display = "inline-block";
|
|
|
|
this.menuEntry.getElementsByClassName("edited-icon")[1].style.display = "none";
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:24:37 +03:00
|
|
|
hide() {
|
2020-06-18 17:04:45 +03:00
|
|
|
this.isVisible = false;
|
2020-06-19 00:24:37 +03:00
|
|
|
this.canvas.style.visibility = "hidden";
|
2020-06-19 16:25:25 +03:00
|
|
|
this.menuEntry.getElementsByClassName("layer-button")[1].style.visibility = "visible";
|
2020-06-19 00:24:37 +03:00
|
|
|
|
|
|
|
// Changing icon
|
|
|
|
this.menuEntry.getElementsByClassName("default-icon")[1].style.display = "none";
|
|
|
|
this.menuEntry.getElementsByClassName("edited-icon")[1].style.display = "inline-block";
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
2020-06-19 16:16:22 +03:00
|
|
|
|
|
|
|
updateLayerPreview() {
|
|
|
|
// Getting the canvas
|
|
|
|
let destination = this.menuEntry.getElementsByTagName("canvas")[0];
|
|
|
|
let widthRatio = this.canvasSize[0] / this.canvasSize[1];
|
|
|
|
let heightRatio = this.canvasSize[1] / this.canvasSize[0];
|
|
|
|
|
|
|
|
// Computing width and height for the preview image
|
|
|
|
let previewWidth = destination.width;
|
|
|
|
let previewHeight = destination.height;
|
|
|
|
|
|
|
|
// If the sprite is rectangular, I apply the ratio to the preview as well
|
|
|
|
if (widthRatio < 1) {
|
|
|
|
previewWidth = destination.width * widthRatio;
|
|
|
|
}
|
|
|
|
else if (widthRatio > 1) {
|
|
|
|
previewHeight = destination.height * heightRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
// La appiccico sulla preview
|
|
|
|
destination.getContext('2d').clearRect(0, 0, destination.width, destination.height);
|
2020-09-09 07:15:22 +03:00
|
|
|
destination.getContext('2d').drawImage(this.canvas,
|
2020-06-19 16:16:22 +03:00
|
|
|
// This is necessary to center the preview in the canvas
|
2020-09-09 07:15:22 +03:00
|
|
|
(destination.width - previewWidth) / 2, (destination.height - previewHeight) / 2,
|
2020-06-19 16:16:22 +03:00
|
|
|
previewWidth, previewHeight);
|
|
|
|
}
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
2020-06-18 17:04:45 +03:00
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
function flatten(onlyVisible) {
|
|
|
|
if (!onlyVisible) {
|
|
|
|
// Selecting the first layer
|
|
|
|
let firstLayer = layerList.firstElementChild;
|
2020-06-25 13:10:34 +03:00
|
|
|
let nToFlatten = layerList.childElementCount - 1;
|
2020-06-23 18:34:03 +03:00
|
|
|
getLayerByID(firstLayer.id).selectLayer();
|
|
|
|
|
2020-06-25 13:10:34 +03:00
|
|
|
for (let i = 0; i < nToFlatten; i++) {
|
2020-06-23 18:34:03 +03:00
|
|
|
merge();
|
|
|
|
}
|
2020-06-25 13:10:34 +03:00
|
|
|
|
|
|
|
new HistoryStateFlattenAll(nToFlatten);
|
2020-06-23 18:34:03 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Getting all the visible layers
|
|
|
|
let visibleLayers = [];
|
2020-06-25 14:19:31 +03:00
|
|
|
let nToFlatten = 0;
|
2020-06-23 18:34:03 +03:00
|
|
|
|
|
|
|
for (let i=0; i<layers.length; i++) {
|
2020-07-21 00:33:17 +03:00
|
|
|
console.log(layers[i].name);
|
2020-06-23 18:34:03 +03:00
|
|
|
if (layers[i].menuEntry != null && layers[i].isVisible) {
|
|
|
|
visibleLayers.push(layers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 00:33:17 +03:00
|
|
|
console.log("da piallare: " + visibleLayers.length);
|
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
// Sorting them by z-index
|
2020-06-27 14:29:28 +03:00
|
|
|
visibleLayers.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? -1 : 1);
|
2020-06-23 18:34:03 +03:00
|
|
|
// Selecting the last visible layer (the only one that won't get deleted)
|
|
|
|
visibleLayers[visibleLayers.length - 1].selectLayer();
|
|
|
|
|
|
|
|
// Merging all the layer but the last one
|
|
|
|
for (let i=0; i<visibleLayers.length - 1; i++) {
|
2020-06-25 14:19:31 +03:00
|
|
|
nToFlatten++;
|
2020-06-27 14:29:28 +03:00
|
|
|
console.log(visibleLayers[i].menuEntry.nextElementSibling);
|
2020-06-25 14:19:31 +03:00
|
|
|
new HistoryStateFlattenTwoVisibles(
|
|
|
|
visibleLayers[i + 1].context.getImageData(0, 0, visibleLayers[i].canvasSize[0], visibleLayers[i].canvasSize[1]),
|
2020-06-27 14:29:28 +03:00
|
|
|
visibleLayers[i].menuEntry.nextElementSibling,
|
2020-06-25 14:19:31 +03:00
|
|
|
layers.indexOf(visibleLayers[i]),
|
|
|
|
visibleLayers[i], visibleLayers[i + 1]
|
|
|
|
);
|
|
|
|
|
2020-06-24 00:10:56 +03:00
|
|
|
mergeLayers(visibleLayers[i + 1].context, visibleLayers[i].context);
|
2020-06-23 18:34:03 +03:00
|
|
|
|
|
|
|
// Deleting the above layer
|
|
|
|
visibleLayers[i].canvas.remove();
|
|
|
|
visibleLayers[i].menuEntry.remove();
|
|
|
|
layers.splice(layers.indexOf(visibleLayers[i]), 1);
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:19:31 +03:00
|
|
|
new HistoryStateFlattenVisible(nToFlatten);
|
2020-06-23 18:34:03 +03:00
|
|
|
// Updating the layer preview
|
|
|
|
currentLayer.updateLayerPreview();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:40:47 +03:00
|
|
|
function merge(saveHistory = true) {
|
2020-06-23 18:34:03 +03:00
|
|
|
// Saving the layer that should be merged
|
|
|
|
let toMerge = currentLayer;
|
|
|
|
let toMergeIndex = layers.indexOf(toMerge);
|
|
|
|
// Getting layer below
|
|
|
|
let layerBelow = getLayerByID(currentLayer.menuEntry.nextElementSibling.id);
|
2020-09-09 07:15:22 +03:00
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
// If I have something to merge with
|
|
|
|
if (layerBelow != null) {
|
|
|
|
// Selecting that layer
|
|
|
|
layerBelow.selectLayer();
|
|
|
|
|
2020-06-25 12:40:47 +03:00
|
|
|
if (saveHistory) {
|
2020-09-09 07:15:22 +03:00
|
|
|
new HistoryStateMergeLayer(toMergeIndex, toMerge,
|
2020-06-25 12:40:47 +03:00
|
|
|
layerBelow.context.getImageData(0, 0, layerBelow.canvasSize[0], layerBelow.canvasSize[1]),
|
|
|
|
layerBelow);
|
|
|
|
}
|
2020-06-25 13:10:34 +03:00
|
|
|
|
2020-06-24 00:10:56 +03:00
|
|
|
mergeLayers(currentLayer.context, toMerge.context);
|
2020-06-23 18:34:03 +03:00
|
|
|
|
|
|
|
// Deleting the above layer
|
|
|
|
toMerge.canvas.remove();
|
|
|
|
toMerge.menuEntry.remove();
|
|
|
|
layers.splice(toMergeIndex, 1);
|
|
|
|
|
|
|
|
// Updating the layer preview
|
|
|
|
currentLayer.updateLayerPreview();
|
|
|
|
}
|
2020-09-09 07:15:22 +03:00
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
}
|
|
|
|
|
2020-06-25 00:40:49 +03:00
|
|
|
function deleteLayer(saveHistory = true) {
|
2020-06-23 00:07:40 +03:00
|
|
|
// Cannot delete all the layers
|
|
|
|
if (layers.length != 4) {
|
|
|
|
let layerIndex = layers.indexOf(currentLayer);
|
|
|
|
let toDelete = layers[layerIndex];
|
2020-06-25 00:40:49 +03:00
|
|
|
let previousSibling = toDelete.menuEntry.previousElementSibling;
|
2020-06-23 00:07:40 +03:00
|
|
|
// Adding the ids to the unused ones
|
2020-06-24 23:34:21 +03:00
|
|
|
console.log("id cancellato: " + toDelete.id);
|
|
|
|
unusedIDs.push(toDelete.id);
|
2020-06-23 00:07:40 +03:00
|
|
|
|
|
|
|
// Selecting the next layer
|
|
|
|
if (layerIndex != (layers.length - 3)) {
|
|
|
|
layers[layerIndex + 1].selectLayer();
|
|
|
|
}
|
|
|
|
// or the previous one if the next one doesn't exist
|
|
|
|
else {
|
2020-09-09 07:15:22 +03:00
|
|
|
layers[layerIndex - 1].selectLayer();
|
2020-06-23 00:07:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting canvas and entry
|
|
|
|
toDelete.canvas.remove();
|
|
|
|
toDelete.menuEntry.remove();
|
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
// Removing the layer from the list
|
2020-06-23 00:07:40 +03:00
|
|
|
layers.splice(layerIndex, 1);
|
2020-06-25 00:40:49 +03:00
|
|
|
|
|
|
|
if (saveHistory) {
|
|
|
|
new HistoryStateDeleteLayer(toDelete, previousSibling, layerIndex);
|
|
|
|
}
|
2020-06-23 00:07:40 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
// Closing the menu
|
2020-06-23 00:07:40 +03:00
|
|
|
currentLayer.closeOptionsMenu();
|
|
|
|
}
|
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
function renameLayer(event) {
|
|
|
|
let layerIndex = layers.indexOf(currentLayer);
|
|
|
|
let toRename = currentLayer;
|
|
|
|
let p = currentLayer.menuEntry.getElementsByTagName("p")[0];
|
|
|
|
|
2020-06-24 23:34:21 +03:00
|
|
|
oldLayerName = p.innerHTML;
|
|
|
|
|
2020-06-23 18:34:03 +03:00
|
|
|
p.setAttribute("contenteditable", true);
|
|
|
|
p.classList.add("layer-name-editable");
|
|
|
|
p.focus();
|
|
|
|
|
|
|
|
simulateInput(65, true, false, false);
|
|
|
|
|
|
|
|
isRenamingLayer = true;
|
|
|
|
}
|
2020-06-23 00:07:40 +03:00
|
|
|
|
2020-06-24 14:30:24 +03:00
|
|
|
// Swaps two layer entries in the layer menu
|
2020-06-26 00:37:41 +03:00
|
|
|
function moveLayers(toDropLayer, staticLayer, saveHistory = true) {
|
|
|
|
let toDrop = getLayerByID(toDropLayer);
|
2020-09-11 22:48:55 +03:00
|
|
|
let staticc = getLayerByID(staticLayer);
|
2020-06-28 17:57:19 +03:00
|
|
|
let layerCopy = layers.slice();
|
2020-06-20 00:31:36 +03:00
|
|
|
|
2020-06-26 18:38:39 +03:00
|
|
|
let beforeToDrop = toDrop.menuEntry.nextElementSibling;
|
|
|
|
let nMoved = 0;
|
2020-06-20 00:31:36 +03:00
|
|
|
|
2020-06-26 00:37:41 +03:00
|
|
|
layerCopy.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? 1 : -1);
|
|
|
|
|
|
|
|
let toDropIndex = layerCopy.indexOf(toDrop);
|
2020-09-11 22:48:55 +03:00
|
|
|
let staticIndex = layerCopy.indexOf(staticc);
|
2020-06-26 00:37:41 +03:00
|
|
|
|
2020-09-11 22:48:55 +03:00
|
|
|
layerList.insertBefore(toDrop.menuEntry, staticc.menuEntry);
|
2020-06-26 00:37:41 +03:00
|
|
|
|
|
|
|
if (toDropIndex < staticIndex) {
|
|
|
|
let tmp = toDrop.canvas.style.zIndex;
|
|
|
|
let tmp2;
|
|
|
|
|
|
|
|
for (let i=toDropIndex+1; i<=staticIndex; i++) {
|
|
|
|
tmp2 = layerCopy[i].canvas.style.zIndex;
|
2020-06-26 18:38:39 +03:00
|
|
|
|
|
|
|
if (saveHistory) {
|
|
|
|
new HistoryStateMoveTwoLayers(layerCopy[i], tmp2, tmp);
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:37:41 +03:00
|
|
|
layerCopy[i].canvas.style.zIndex = tmp;
|
|
|
|
tmp = tmp2;
|
2020-06-26 18:38:39 +03:00
|
|
|
nMoved++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saveHistory) {
|
|
|
|
new HistoryStateMoveTwoLayers(toDrop, toDrop.canvas.style.zIndex, tmp);
|
2020-06-26 00:37:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
toDrop.canvas.style.zIndex = tmp;
|
2020-09-09 07:15:22 +03:00
|
|
|
|
2020-06-26 18:38:39 +03:00
|
|
|
if (saveHistory) {
|
2020-09-11 22:48:55 +03:00
|
|
|
new HistoryStateMoveLayer(beforeToDrop, toDrop, staticc, nMoved);
|
2020-06-26 18:38:39 +03:00
|
|
|
}
|
2020-06-26 00:37:41 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// BUG QUI
|
|
|
|
let tmp = toDrop.canvas.style.zIndex;
|
|
|
|
let tmp2;
|
|
|
|
|
|
|
|
for (let i=toDropIndex-1; i>staticIndex; i--) {
|
|
|
|
tmp2 = layerCopy[i].canvas.style.zIndex;
|
2020-06-26 18:38:39 +03:00
|
|
|
|
|
|
|
if (saveHistory) {
|
|
|
|
new HistoryStateMoveTwoLayers(layerCopy[i], tmp2, tmp);
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:37:41 +03:00
|
|
|
layerCopy[i].canvas.style.zIndex = tmp;
|
|
|
|
tmp = tmp2;
|
2020-06-26 18:38:39 +03:00
|
|
|
nMoved++;
|
2020-06-26 00:37:41 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 18:38:39 +03:00
|
|
|
if (saveHistory) {
|
|
|
|
new HistoryStateMoveTwoLayers(toDrop, toDrop.canvas.style.zIndex, tmp);
|
|
|
|
}
|
2020-06-21 00:50:57 +03:00
|
|
|
|
2020-06-26 18:38:39 +03:00
|
|
|
toDrop.canvas.style.zIndex = tmp;
|
2020-06-20 00:31:36 +03:00
|
|
|
|
2020-06-26 18:38:39 +03:00
|
|
|
if (saveHistory) {
|
2020-09-11 22:48:55 +03:00
|
|
|
new HistoryStateMoveLayer(beforeToDrop, toDrop, staticc, nMoved);
|
2020-06-21 00:50:57 +03:00
|
|
|
}
|
2020-06-26 18:38:39 +03:00
|
|
|
|
2020-06-24 14:30:24 +03:00
|
|
|
}
|
2020-06-20 00:31:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 17:04:45 +03:00
|
|
|
// Finds a layer given its name
|
|
|
|
function getLayerByName(name) {
|
|
|
|
for (let i=0; i<layers.length; i++) {
|
2020-06-19 00:24:37 +03:00
|
|
|
if (layers[i].menuEntry != null) {
|
|
|
|
if (layers[i].menuEntry.getElementsByTagName("p")[0].innerHTML == name) {
|
|
|
|
return layers[i];
|
|
|
|
}
|
2020-06-18 17:04:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 00:31:36 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finds a layer given its id
|
|
|
|
function getLayerByID(id) {
|
|
|
|
for (let i=0; i<layers.length; i++) {
|
|
|
|
if (layers[i].menuEntry != null) {
|
|
|
|
if (layers[i].menuEntry.id == id) {
|
|
|
|
return layers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 17:04:45 +03:00
|
|
|
return null;
|
2020-06-23 19:10:10 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 14:30:24 +03:00
|
|
|
function addLayer(id, saveHistory = true) {
|
2020-06-28 17:57:19 +03:00
|
|
|
// layers.length - 3
|
2020-06-25 00:40:49 +03:00
|
|
|
let index = layers.length - 3;
|
2020-06-23 19:10:10 +03:00
|
|
|
// Creating a new canvas
|
|
|
|
let newCanvas = document.createElement("canvas");
|
|
|
|
// Setting up the new canvas
|
|
|
|
canvasView.append(newCanvas);
|
|
|
|
maxZIndex++;
|
|
|
|
newCanvas.style.zIndex = maxZIndex;
|
|
|
|
newCanvas.classList.add("drawingCanvas");
|
|
|
|
|
|
|
|
console.log("Tela creata: " + newCanvas);
|
|
|
|
|
2020-09-09 07:15:22 +03:00
|
|
|
if (!layerListEntry) return console.warn('skipping adding layer because no document');
|
|
|
|
|
2020-06-23 19:10:10 +03:00
|
|
|
// Clone the default layer
|
|
|
|
let toAppend = layerListEntry.cloneNode(true);
|
|
|
|
// Setting the default name for the layer
|
|
|
|
toAppend.getElementsByTagName('p')[0].innerHTML = "Layer " + layerCount;
|
|
|
|
// Removing the selected class
|
|
|
|
toAppend.classList.remove("selected-layer");
|
|
|
|
// Adding the layer to the list
|
|
|
|
layerCount++;
|
|
|
|
|
|
|
|
// Creating a layer object
|
|
|
|
let newLayer = new Layer(currentLayer.canvasSize[0], currentLayer.canvasSize[1], newCanvas, toAppend);
|
|
|
|
newLayer.context.fillStyle = currentLayer.context.fillStyle;
|
|
|
|
newLayer.copyData(currentLayer);
|
2020-06-25 00:40:49 +03:00
|
|
|
layers.splice(index, 0, newLayer);
|
2020-09-09 07:15:22 +03:00
|
|
|
|
2020-06-23 19:10:10 +03:00
|
|
|
// Insert it before the Add layer button
|
|
|
|
layerList.insertBefore(toAppend, layerList.childNodes[0]);
|
2020-06-24 14:30:24 +03:00
|
|
|
|
2020-06-24 23:34:21 +03:00
|
|
|
if (id != null && typeof(id) == "string") {
|
2020-06-24 14:30:24 +03:00
|
|
|
newLayer.setID(id);
|
|
|
|
}
|
|
|
|
// Basically "if I'm not adding a layer because redo() is telling meto do so", then I can save the history
|
|
|
|
if (saveHistory) {
|
2020-06-25 00:40:49 +03:00
|
|
|
new HistoryStateAddLayer(newLayer, index);
|
2020-06-24 14:30:24 +03:00
|
|
|
}
|
2020-06-28 17:57:19 +03:00
|
|
|
|
|
|
|
return newLayer;
|
2020-09-09 07:15:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
layerList = document.getElementById("layers-menu");
|