2023-04-03 19:26:24 +00:00
|
|
|
import { ID } from "../../util.js";
|
2022-06-23 19:27:36 +00:00
|
|
|
export class Picker {
|
2023-04-03 19:26:24 +00:00
|
|
|
constructor(parentID, spriteSheet, ranges, style, selected = null, clickEvent) {
|
|
|
|
this.parentID = parentID;
|
2022-06-23 19:27:36 +00:00
|
|
|
this.selected = selected;
|
2023-04-03 19:26:24 +00:00
|
|
|
this.buttons = [];
|
|
|
|
const buttonBar = ID(parentID);
|
2022-06-23 05:44:57 +00:00
|
|
|
|
2023-04-03 19:26:24 +00:00
|
|
|
for (let i = 0; i < ranges[1] - ranges[0]; i++) {
|
|
|
|
let span = document.createElement('span');
|
|
|
|
span.className = `button ${style}`;
|
|
|
|
const n = i + ranges[0];
|
2022-06-23 19:27:36 +00:00
|
|
|
span.appendChild(spriteSheet[n]);
|
|
|
|
buttonBar.appendChild(span);
|
|
|
|
span.no = i;
|
2022-06-23 05:44:57 +00:00
|
|
|
|
2022-06-23 19:27:36 +00:00
|
|
|
span.addEventListener('click', e => {
|
2023-04-03 19:26:24 +00:00
|
|
|
this.select(e.target.no);
|
|
|
|
if (clickEvent instanceof Function) {
|
|
|
|
clickEvent();
|
|
|
|
}
|
2022-06-23 19:27:36 +00:00
|
|
|
});
|
2023-04-03 19:26:24 +00:00
|
|
|
this.buttons.push(span);
|
|
|
|
}
|
|
|
|
if (typeof selected === "number") {
|
|
|
|
this.select(selected);
|
2022-06-23 05:44:57 +00:00
|
|
|
}
|
2022-06-23 19:27:36 +00:00
|
|
|
}
|
|
|
|
select(number) {
|
2023-04-03 19:26:24 +00:00
|
|
|
const target = this.buttons[number];
|
|
|
|
this.selected = number;
|
|
|
|
ID(this.parentID).querySelectorAll('.button').forEach(item => {
|
|
|
|
item.style = "";
|
|
|
|
item.active = "false";
|
|
|
|
});
|
|
|
|
target.style = "background: #4CAF50;";
|
|
|
|
target.active = "true";
|
2022-06-23 19:27:36 +00:00
|
|
|
}
|
2022-06-23 05:44:57 +00:00
|
|
|
|
2022-06-23 19:27:36 +00:00
|
|
|
}
|
2022-06-23 05:44:57 +00:00
|
|
|
|
|
|
|
export function destroy(parentID) {
|
2023-04-03 19:26:24 +00:00
|
|
|
const buttonBar = ID(parentID);
|
2022-06-23 05:44:57 +00:00
|
|
|
while (buttonBar.lastChild) {
|
2023-04-03 19:26:24 +00:00
|
|
|
buttonBar.removeChild(buttonBar.lastChild);
|
2022-06-23 05:44:57 +00:00
|
|
|
}
|
|
|
|
}
|