pixel-editor/js/Tool.js

126 lines
3.2 KiB
JavaScript
Raw Normal View History

// REFACTOR: this is a nice base for the Tool class
//tools container / list, automatically managed when you create a new Tool();
var tool = {};
//class for tools
class Tool {
name = "AbstractTool";
2021-10-31 14:49:38 +03:00
isSelected = false;
2021-10-27 11:02:21 +03:00
// Cursor and brush size
cursorType = {};
2021-10-27 11:02:21 +03:00
cursor = undefined;
cursorHTMLElement = undefined;
2021-10-27 11:02:21 +03:00
// Useful coordinates
startMousePos = {};
2021-10-27 11:02:21 +03:00
currMousePos = {};
prevMousePos = {};
endMousePos = {};
2021-10-27 11:02:21 +03:00
// HTML elements
mainButton = undefined;
biggerButton = undefined;
smallerButton = undefined;
brushPreview = document.getElementById("brush-preview");
2021-10-27 11:02:21 +03:00
constructor (name, options) {
this.name = name;
this.cursorType = options;
2021-10-27 11:43:51 +03:00
this.mainButton = document.getElementById(name + "-button");
this.biggerButton = document.getElementById(name + "-bigger-button");
this.smallerButton = document.getElementById(name + "-smaller-button");
2021-10-27 11:02:21 +03:00
}
onSelect() {
2021-11-09 00:25:30 +03:00
if (this.mainButton != undefined)
this.mainButton.parentElement.classList.add("selected");
2021-10-31 14:49:38 +03:00
this.isSelected = true;
2021-10-31 20:31:45 +03:00
switch (this.cursorType.type) {
case 'html':
currFile.canvasView.style.cursor = 'none';
2021-10-31 20:31:45 +03:00
break;
case 'cursor':
2021-11-01 14:31:09 +03:00
this.cursor = this.cursorType.style;
currFile.canvasView.style.cursor = this.cursor || 'default';
2021-10-31 20:31:45 +03:00
break;
default:
break;
}
}
2021-11-09 14:53:19 +03:00
updateCursor() {
this.brushPreview.style.display = 'block';
this.brushPreview.style.width = this.currSize * currFile.zoom + 'px';
this.brushPreview.style.height = this.currSize * currFile.zoom + 'px';
2021-11-09 14:53:19 +03:00
}
2021-10-27 11:43:51 +03:00
2021-11-01 14:31:09 +03:00
onMouseWheel(mousePos, mode) {}
2021-10-27 11:02:21 +03:00
onHover(cursorLocation, cursorTarget) {
this.prevMousePos = this.currMousePos;
this.currMousePos = cursorLocation;
2021-11-09 14:53:19 +03:00
this.updateCursor();
let toSub = 1;
// Prevents the brush to be put in the middle of pixels
2021-10-27 11:02:21 +03:00
if (this.currSize % 2 == 0) {
toSub = 0.5;
}
this.brushPreview.style.left = (Math.floor(cursorLocation[0] / currFile.zoom) * currFile.zoom + currFile.currentLayer.canvas.offsetLeft - this.currSize * currFile.zoom / 2 - currFile.zoom / 2 + toSub * currFile.zoom) + 'px';
this.brushPreview.style.top = (Math.floor(cursorLocation[1] / currFile.zoom) * currFile.zoom + currFile.currentLayer.canvas.offsetTop - this.currSize * currFile.zoom / 2 - currFile.zoom / 2 + toSub * currFile.zoom) + 'px';
2021-10-27 11:02:21 +03:00
2021-10-31 20:31:45 +03:00
if (this.cursorType.type == 'html') {
2021-11-09 14:53:19 +03:00
if (cursorTarget.className == 'drawingCanvas'|| cursorTarget.className == 'drawingCanvas') {
this.brushPreview.style.visibility = 'visible';
currFile.canvasView.style.cursor = 'none';
2021-10-31 20:31:45 +03:00
}
else {
this.brushPreview.style.visibility = 'hidden';
currFile.canvasView.style.cursor = 'default';
2021-10-31 20:31:45 +03:00
}
2021-10-27 11:02:21 +03:00
}
}
2021-10-27 11:02:21 +03:00
onDeselect() {
2021-11-09 00:25:30 +03:00
if (this.mainButton != undefined)
this.mainButton.parentElement.classList.remove("selected");
2021-10-31 14:49:38 +03:00
this.isSelected = false;
2021-11-09 14:53:19 +03:00
this.brushPreview.style.visibility = 'hidden';
currFile.canvasView.style.cursor = 'default';
}
2021-10-27 11:02:21 +03:00
onStart(mousePos) {
this.startMousePos = mousePos;
}
2021-10-27 11:02:21 +03:00
onDrag(mousePos) {
}
2021-10-27 11:02:21 +03:00
onEnd(mousePos) {
this.endMousePos = mousePos;
}
2021-10-27 11:02:21 +03:00
increaseSize() {
if (this.currSize < 128) {
this.currSize++;
this.updateCursor();
}
}
2021-10-27 11:02:21 +03:00
decreaseSize() {
if (this.currSize > 1) {
2021-10-27 11:02:21 +03:00
this.currSize--;
this.updateCursor();
}
}
2021-10-27 11:02:21 +03:00
get size() {
return this.currSize;
}
}