mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
created tool class which keeps track of cursors, incorporates changeTool; made changelog data its own file
This commit is contained in:
70
js/_tools.js
Normal file
70
js/_tools.js
Normal file
@ -0,0 +1,70 @@
|
||||
//tools container / list, automatically managed when you create a new Tool();
|
||||
var tool = {};
|
||||
|
||||
//class for tools
|
||||
class Tool {
|
||||
constructor (name, options) {
|
||||
|
||||
//stores the name in object, only needed for legacy functions from when currentTool was just a string
|
||||
this.name = name;
|
||||
|
||||
//copy options to this object
|
||||
if (options.cursor) {
|
||||
//passed statically as a string
|
||||
if (typeof options.cursor == 'string') this.cursor = options.cursor;
|
||||
//passed a function which should be used as a getter function
|
||||
if (typeof options.cursor == 'function') Object.defineProperty(this, 'cursor', { get: options.cursor});
|
||||
}
|
||||
|
||||
if (options.imageCursor) this.cursor = "url(\'/pixel-editor/"+options.imageCursor+".png\'), auto";
|
||||
|
||||
if (options.brushPreview) {
|
||||
this.brushPreview = true;
|
||||
this.currentBrushSize = 1;
|
||||
this.previousBrushSize = 1;
|
||||
}
|
||||
|
||||
//add to tool object so it can be referenced
|
||||
tool[name] = this;
|
||||
}
|
||||
|
||||
get brushSize () {
|
||||
return this.currentBrushSize;
|
||||
}
|
||||
|
||||
set brushSize (value) {
|
||||
this.currentBrushSize = value;
|
||||
this.updateCursor();
|
||||
}
|
||||
|
||||
|
||||
//switch to this tool (replaced global changeTool())
|
||||
switchTo () {
|
||||
|
||||
console.log('changing tool to',this.name)
|
||||
|
||||
// Ending any selection in progress
|
||||
if (currentTool.name.includes("select") && !this.name.includes("select") && !selectionCanceled) {
|
||||
endSelection();
|
||||
}
|
||||
|
||||
//set tool and temp tje tje tpp;
|
||||
currentTool = this;
|
||||
currentToolTemp = this;
|
||||
|
||||
var tools = document.getElementById("tools-menu").children;
|
||||
|
||||
for (var i = 0; i < tools.length; i++) {
|
||||
tools[i].classList.remove("selected");
|
||||
}
|
||||
|
||||
//give the button of the selected tool the .selected class
|
||||
document.getElementById(this.name+"-button").parentNode.classList.add("selected");
|
||||
|
||||
//change cursor
|
||||
this.updateCursor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*global dragging currentTool, currentToolTemp, selectionCanceled, endSelection*/
|
Reference in New Issue
Block a user