2020-02-04 21:42:14 +01:00
|
|
|
import ButtonTile from "../ButtonTile.js";
|
|
|
|
import TilorswiftEvent from "../events/TilorswiftEvent.js";
|
|
|
|
import Widget from "./Widget.js";
|
2020-02-04 23:03:33 +01:00
|
|
|
import BrushMode from "../BrushMode.js";
|
2020-02-04 21:42:14 +01:00
|
|
|
|
|
|
|
export default class TilesetPickerWidget extends Widget
|
|
|
|
{
|
2020-02-04 23:03:33 +01:00
|
|
|
constructor(tileset, brush)
|
2020-02-04 21:42:14 +01:00
|
|
|
{
|
|
|
|
super('Terrain');
|
|
|
|
this.tileset = tileset;
|
2020-02-04 23:03:33 +01:00
|
|
|
this.brush = brush;
|
2020-02-04 21:42:14 +01:00
|
|
|
this.htmlElement = this.createElementPicker();
|
|
|
|
this.htmlElementSelector = this.createElementSelector();
|
2020-02-14 00:12:24 +01:00
|
|
|
this.htmlElement.appendChild(this.htmlElementSelector);
|
|
|
|
|
2020-02-04 21:42:14 +01:00
|
|
|
this.loadTileset();
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.BUTTON_TILE_CLICKED,
|
|
|
|
(event) => {
|
|
|
|
if (this.isActive) {
|
|
|
|
this.setTile(event.button.index);
|
2020-02-04 23:03:33 +01:00
|
|
|
this.brush.mode = BrushMode.TERRAIN;
|
2020-02-04 21:42:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadTileset()
|
|
|
|
{
|
|
|
|
for (let t = 0; t < this.tileset.tiles; t++) {
|
2023-09-19 01:14:15 +02:00
|
|
|
const button = new ButtonTile(this.tileset, t);
|
2020-02-04 21:42:14 +01:00
|
|
|
this.htmlElementSelector.appendChild(button.getElement());
|
|
|
|
}
|
2020-02-14 00:12:24 +01:00
|
|
|
|
|
|
|
this.htmlElement.style.backgroundImage = 'url("' + this.tileset.image.src + '")';
|
|
|
|
this.htmlElement.style.backgroundSize = 'auto ' + this.tileset.getTileWidth() + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
reloadTileset(tileset)
|
|
|
|
{
|
|
|
|
this.tileset = tileset;
|
|
|
|
this.htmlElementSelector.innerHTML = '';
|
2020-02-16 00:53:03 +01:00
|
|
|
this.htmlElementSelector.style.width = Math.ceil(Math.sqrt(this.tileset.tiles)) * this.tileset.getTileWidth() + 'px';
|
2020-02-14 00:12:24 +01:00
|
|
|
this.loadTileset();
|
2020-02-04 21:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createElementPicker()
|
|
|
|
{
|
2023-09-19 01:14:15 +02:00
|
|
|
const htmlElement = document.createElement('div');
|
2020-02-04 21:42:14 +01:00
|
|
|
htmlElement.id = 'tileset-picker';
|
|
|
|
htmlElement.style.width = this.tileset.getTileWidth() + 'px';
|
|
|
|
htmlElement.style.height = this.tileset.getTileHeight() + 'px';
|
|
|
|
htmlElement.style.backgroundSize = this.tileset.getTileWidth() + 'px ' + this.tileset.getTileHeight() + 'px';
|
|
|
|
|
|
|
|
return htmlElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
createElementSelector()
|
|
|
|
{
|
2023-09-19 01:14:15 +02:00
|
|
|
const htmlElementSelector = document.createElement('div');
|
2020-02-14 00:12:24 +01:00
|
|
|
htmlElementSelector.id = 'tileset-selector-widget';
|
2020-02-04 21:42:14 +01:00
|
|
|
htmlElementSelector.style.width = Math.ceil(Math.sqrt(this.tileset.tiles)) * this.tileset.getTileWidth() + 'px';
|
|
|
|
htmlElementSelector.style.left = String(this.tileset.getTileWidth() + 1) + 'px';
|
|
|
|
|
|
|
|
return htmlElementSelector;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTile(index)
|
|
|
|
{
|
2023-09-19 01:14:15 +02:00
|
|
|
this.htmlElement.style.backgroundPosition = -this.tileset.getTileWidth() * index + 'px ' + this.tileset.getTileHeight() + 'px';
|
2020-02-04 21:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getElement()
|
|
|
|
{
|
|
|
|
return this.htmlElement;
|
|
|
|
}
|
2023-09-19 01:14:15 +02:00
|
|
|
}
|