mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
Implemented lock function
Now a layer can be locked and unlocked and it's possible to edit a layer only if the layer is not locked.
This commit is contained in:
parent
750414d63a
commit
066582e309
@ -20,6 +20,10 @@ function copySelection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function pasteSelection() {
|
function pasteSelection() {
|
||||||
|
// Can't paste if the layer is locked
|
||||||
|
if (currentLayer.isLocked) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
endSelection();
|
endSelection();
|
||||||
|
|
||||||
isPasting = true;
|
isPasting = true;
|
||||||
|
@ -103,6 +103,7 @@ class Layer {
|
|||||||
this.menuEntry = menuEntry;
|
this.menuEntry = menuEntry;
|
||||||
|
|
||||||
if (menuEntry != null) {
|
if (menuEntry != null) {
|
||||||
|
console.log("Aggiungo eventi");
|
||||||
menuEntry.onclick = () => this.select();
|
menuEntry.onclick = () => this.select();
|
||||||
menuEntry.getElementsByTagName("button")[0].onclick = () => this.toggleLock();
|
menuEntry.getElementsByTagName("button")[0].onclick = () => this.toggleLock();
|
||||||
menuEntry.getElementsByTagName("button")[1].onclick = () => this.toggleVisibility();
|
menuEntry.getElementsByTagName("button")[1].onclick = () => this.toggleVisibility();
|
||||||
@ -164,9 +165,11 @@ class Layer {
|
|||||||
|
|
||||||
toggleLock() {
|
toggleLock() {
|
||||||
if (this.isLocked) {
|
if (this.isLocked) {
|
||||||
|
console.log("unlocked");
|
||||||
this.unlock();
|
this.unlock();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
console.log("locked");
|
||||||
this.lock();
|
this.lock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,10 +192,16 @@ class Layer {
|
|||||||
|
|
||||||
lock() {
|
lock() {
|
||||||
this.isLocked = true;
|
this.isLocked = true;
|
||||||
|
|
||||||
|
this.menuEntry.getElementsByClassName("default-icon")[0].style.display = "none";
|
||||||
|
this.menuEntry.getElementsByClassName("edited-icon")[0].style.display = "inline-block";
|
||||||
}
|
}
|
||||||
|
|
||||||
unlock() {
|
unlock() {
|
||||||
this.isLocked = false;
|
this.isLocked = false;
|
||||||
|
|
||||||
|
this.menuEntry.getElementsByClassName("default-icon")[0].style.display = "inline-block";
|
||||||
|
this.menuEntry.getElementsByClassName("edited-icon")[0].style.display = "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
|
@ -8,8 +8,8 @@ window.addEventListener("mousedown", function (mouseEvent) {
|
|||||||
currentMouseEvent = mouseEvent;
|
currentMouseEvent = mouseEvent;
|
||||||
canDraw = true;
|
canDraw = true;
|
||||||
|
|
||||||
//if no document has been created yet, or this is a dialog open
|
//if no document has been created yet, or this is a dialog open, or the currentLayer is locked
|
||||||
if (!documentCreated || dialogueOpen) return;
|
if (!documentCreated || dialogueOpen || currentLayer.isLocked) return;
|
||||||
//prevent right mouse clicks and such, which will open unwanted menus
|
//prevent right mouse clicks and such, which will open unwanted menus
|
||||||
//mouseEvent.preventDefault();
|
//mouseEvent.preventDefault();
|
||||||
|
|
||||||
@ -36,7 +36,9 @@ window.addEventListener("mousedown", function (mouseEvent) {
|
|||||||
|
|
||||||
currentTool.updateCursor();
|
currentTool.updateCursor();
|
||||||
|
|
||||||
if (canDraw) {
|
console.log(currentLayer.isLocked);
|
||||||
|
|
||||||
|
if (!currentLayer.isLocked && canDraw) {
|
||||||
draw(mouseEvent);
|
draw(mouseEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,10 +71,11 @@ window.addEventListener("mouseup", function (mouseEvent) {
|
|||||||
|
|
||||||
closeMenu();
|
closeMenu();
|
||||||
|
|
||||||
if (!documentCreated || dialogueOpen) return;
|
if (!documentCreated || dialogueOpen || currentLayer.isLocked) return;
|
||||||
|
|
||||||
if (currentTool.name == 'eyedropper' && mouseEvent.target.className == 'drawingCanvas') {
|
if (currentTool.name == 'eyedropper' && mouseEvent.target.className == 'drawingCanvas') {
|
||||||
var cursorLocation = getCursorPosition(mouseEvent);
|
var cursorLocation = getCursorPosition(mouseEvent);
|
||||||
|
// TODO: adjust so that if the picked colour is transparent, the underlying layer is checked
|
||||||
var selectedColor = context.getImageData(Math.floor(cursorLocation[0]/zoom),Math.floor(cursorLocation[1]/zoom),1,1);
|
var selectedColor = context.getImageData(Math.floor(cursorLocation[0]/zoom),Math.floor(cursorLocation[1]/zoom),1,1);
|
||||||
var newColor = rgbToHex(selectedColor.data[0],selectedColor.data[1],selectedColor.data[2]);
|
var newColor = rgbToHex(selectedColor.data[0],selectedColor.data[1],selectedColor.data[2]);
|
||||||
|
|
||||||
@ -91,6 +94,8 @@ window.addEventListener("mouseup", function (mouseEvent) {
|
|||||||
if (selectedColor) selectedColor.classList.remove("selected");
|
if (selectedColor) selectedColor.classList.remove("selected");
|
||||||
|
|
||||||
//set current color
|
//set current color
|
||||||
|
|
||||||
|
// TODO: set it for all the layers
|
||||||
context.fillStyle = '#'+newColor;
|
context.fillStyle = '#'+newColor;
|
||||||
|
|
||||||
//make color selected
|
//make color selected
|
||||||
@ -121,7 +126,7 @@ window.addEventListener("mouseup", function (mouseEvent) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (currentTool == 'fill' && mouseEvent.target.className == 'drawingCanvas') {
|
else if (currentTool == 'fill' && mouseEvent.target.className == 'drawingCanvas') {
|
||||||
console.log('filling');
|
//console.log('filling');
|
||||||
//if you clicked on anything but the canvas, do nothing
|
//if you clicked on anything but the canvas, do nothing
|
||||||
if (!mouseEvent.target == currentLayer.canvas) return;
|
if (!mouseEvent.target == currentLayer.canvas) return;
|
||||||
|
|
||||||
@ -165,6 +170,7 @@ window.addEventListener("mouseup", function (mouseEvent) {
|
|||||||
|
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
|
// TODO: Make it snap to the pixel grid
|
||||||
function setPreviewPosition(preview, cursor, size){
|
function setPreviewPosition(preview, cursor, size){
|
||||||
preview.style.left = (
|
preview.style.left = (
|
||||||
currentLayer.canvas.offsetLeft
|
currentLayer.canvas.offsetLeft
|
||||||
@ -190,8 +196,8 @@ function draw (mouseEvent) {
|
|||||||
|
|
||||||
var cursorLocation = lastMousePos;
|
var cursorLocation = lastMousePos;
|
||||||
|
|
||||||
//if a document hasnt yet been created, exit this function
|
//if a document hasnt yet been created or the current layer is locked, exit this function
|
||||||
if (!documentCreated || dialogueOpen) return;
|
if (!documentCreated || dialogueOpen || currentLayer.isLocked) return;
|
||||||
|
|
||||||
|
|
||||||
eyedropperPreview.style.display = 'none';
|
eyedropperPreview.style.display = 'none';
|
||||||
|
@ -6,6 +6,10 @@ function newPixel (width, height, palette) {
|
|||||||
// Setting up the current layer
|
// Setting up the current layer
|
||||||
currentLayer = new Layer(width, height, canvas, layerListEntry);
|
currentLayer = new Layer(width, height, canvas, layerListEntry);
|
||||||
|
|
||||||
|
// 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 checkerboard behind it
|
// Adding the checkerboard behind it
|
||||||
checkerBoard = new Layer(width, height, checkerBoardCanvas);
|
checkerBoard = new Layer(width, height, checkerBoardCanvas);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user