Row insertion implemented.

This commit is contained in:
Mal 2020-02-05 22:29:09 +01:00
parent fa648ae730
commit bd83449e79
3 changed files with 37 additions and 13 deletions

View File

@ -3,7 +3,7 @@ import TilorswiftFieldEnteredEvent from "./events/TilorswiftFieldEnteredEvent.js
export default class Field export default class Field
{ {
constructor(tileset, index = 0) constructor(tileset, index = -1)
{ {
this.tileset = tileset; this.tileset = tileset;
this.index = index; this.index = index;

View File

@ -27,18 +27,7 @@ export default class Terrain
this.htmlElement.style.height = this.tileset.getTileHeight() * this.tilesY + 'px'; this.htmlElement.style.height = this.tileset.getTileHeight() * this.tilesY + 'px';
for (let r = 0; r < this.tilesY; r++) { for (let r = 0; r < this.tilesY; r++) {
let row = []; this.insertRow();
let tr = document.createElement('tr');
for (let col = 0; col < this.tilesX; col++) {
let field = new Field(this.tileset);
let td = field.getElement();
row.push(field);
tr.appendChild(td);
}
this.fields.push(row);
this.htmlElement.appendChild(tr);
} }
window.addEventListener( window.addEventListener(
@ -59,6 +48,35 @@ export default class Terrain
return this.tileset; return this.tileset;
} }
addRows(quantity, index)
{
for (let q = 0; q < quantity; q++) {
this.insertRow(index);
this.tilesY++;
}
}
insertRow(index = undefined)
{
let row = [];
let tr = document.createElement('tr');
for (let col = 0; col < this.tilesX; col++) {
let field = new Field(this.tileset);
let td = field.getElement();
row.push(field);
tr.appendChild(td);
}
if (index === undefined || index >= this.tilesY - 1) {
this.fields.push(row);
this.htmlElement.appendChild(tr);
} else {
this.fields = this.fields.slice(0, index).concat(row).concat(this.fields.slice(index));
this.htmlElement.insertBefore(tr, this.htmlElement.childNodes[index]);
}
}
setFieldIndex(x, y, index) setFieldIndex(x, y, index)
{ {
this.fields[y][x].setIndex(index); this.fields[y][x].setIndex(index);

View File

@ -158,3 +158,9 @@ body {
.field:hover .selection { .field:hover .selection {
opacity: 0.5; opacity: 0.5;
} }
/*
tr:hover > td > .selection {
opacity: 0.5;
}
*/