2021-11-12 02:09:20 +03:00
|
|
|
// OPTIMIZABLE: render the grid only for the current viewport
|
|
|
|
class PixelGrid extends Layer {
|
|
|
|
// Start colour of the pixel grid (can be changed in the preferences)
|
|
|
|
pixelGridColor = "#000000";
|
|
|
|
// Distance between one line and another in HTML pixels
|
2021-12-11 12:48:14 +03:00
|
|
|
lineDistance = 10;
|
2021-11-12 02:09:20 +03:00
|
|
|
// The grid is not visible by default
|
|
|
|
pixelGridVisible = false;
|
|
|
|
// The grid is enabled, but is disabled in order to save performance with big sprites
|
|
|
|
pixelGridEnabled = true;
|
|
|
|
|
|
|
|
constructor(width, height, canvas, menuEntry) {
|
|
|
|
super(width, height, canvas, menuEntry);
|
|
|
|
this.initialize();
|
2021-12-08 01:01:49 +03:00
|
|
|
Events.onCustom("refreshPixelGrid", this.fillPixelGrid.bind(this));
|
|
|
|
Events.onCustom("switchedToAdvanced", this.togglePixelGrid.bind(this));
|
|
|
|
Events.onCustom("switchedToBasic", this.togglePixelGrid.bind(this));
|
2021-11-12 02:09:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
super.initialize();
|
|
|
|
this.fillPixelGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
disablePixelGrid() {
|
|
|
|
this.pixelGridEnabled = false;
|
|
|
|
this.canvas.style.display = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
enablePixelGrid() {
|
2021-12-08 01:01:49 +03:00
|
|
|
if (!this.pixelGridVisible)
|
|
|
|
return;
|
2021-11-12 02:09:20 +03:00
|
|
|
this.pixelGridEnabled = true;
|
|
|
|
this.canvas.style.display = "inline-block";
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:01:49 +03:00
|
|
|
hidePixelGrid() {
|
|
|
|
let button = document.getElementById("toggle-pixelgrid-button");
|
|
|
|
this.pixelGridVisible = false;
|
|
|
|
button.innerHTML = "Show pixel grid";
|
|
|
|
this.canvas.style.display = "none";
|
|
|
|
}
|
|
|
|
showPixelGrid() {
|
|
|
|
let button = document.getElementById("toggle-pixelgrid-button");
|
|
|
|
this.pixelGridVisible = true;
|
|
|
|
button.innerHTML = "Hide pixel grid";
|
|
|
|
this.canvas.style.display = "inline-block";
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:09:20 +03:00
|
|
|
repaintPixelGrid(factor) {
|
|
|
|
this.lineDistance += factor;
|
|
|
|
this.fillPixelGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Shows or hides the pixel grid depening on its current visibility
|
|
|
|
* (triggered by the show pixel grid button in the top menu)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
togglePixelGrid(newState) {
|
|
|
|
//Set the state based on the passed newState variable, otherwise just toggle it
|
2021-12-08 01:01:49 +03:00
|
|
|
if (newState == 'on')
|
|
|
|
this.showPixelGrid();
|
|
|
|
else if (newState == 'off')
|
|
|
|
this.hidePixelGrid();
|
|
|
|
else
|
|
|
|
if (this.pixelGridVisible)
|
|
|
|
this.hidePixelGrid();
|
|
|
|
else
|
|
|
|
this.showPixelGrid();
|
2021-11-12 02:09:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Fills the pixelGridCanvas with the pixelgrid
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fillPixelGrid() {
|
2021-12-06 19:37:43 +03:00
|
|
|
let originalSize = currFile.canvasSize;
|
2021-11-12 02:09:20 +03:00
|
|
|
|
|
|
|
// The pixelGridCanvas is lineDistance times bigger so that the lines don't take 1 canvas pixel
|
|
|
|
// (which would cover the whole canvas with the pixelGridColour), but they take 1/lineDistance canvas pixels
|
|
|
|
this.canvas.width = originalSize[0] * Math.round(this.lineDistance);
|
|
|
|
this.canvas.height = originalSize[1] * Math.round(this.lineDistance);
|
|
|
|
|
|
|
|
this.context.strokeStyle = Settings.getCurrSettings().pixelGridColour;
|
|
|
|
|
2021-12-11 12:48:14 +03:00
|
|
|
console.log("Line ditance: " + this.lineDistance)
|
|
|
|
|
2021-11-12 02:09:20 +03:00
|
|
|
// OPTIMIZABLE, could probably be a bit more elegant
|
|
|
|
// Draw horizontal lines
|
|
|
|
for (let i=0; i<this.canvas.width / Math.round(this.lineDistance); i++) {
|
|
|
|
this.context.beginPath();
|
|
|
|
this.context.moveTo(i * Math.round(this.lineDistance) + 0.5, 0);
|
|
|
|
this.context.lineTo(i * Math.round(this.lineDistance) + 0.5,
|
|
|
|
originalSize[1] * Math.round(this.lineDistance));
|
|
|
|
this.context.stroke();
|
|
|
|
this.context.closePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw vertical lines
|
|
|
|
for (let i=0; i<this.canvas.height / Math.round(this.lineDistance); i++) {
|
|
|
|
this.context.beginPath();
|
|
|
|
this.context.moveTo(0, i * Math.round(this.lineDistance) + 0.5);
|
|
|
|
this.context.lineTo(originalSize[0] * Math.round(this.lineDistance),
|
|
|
|
i * Math.round(this.lineDistance) + 0.5);
|
|
|
|
this.context.stroke();
|
|
|
|
this.context.closePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.pixelGridVisible) {
|
|
|
|
this.canvas.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|