-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalette.js
More file actions
108 lines (93 loc) · 2.65 KB
/
palette.js
File metadata and controls
108 lines (93 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Scenario } from "./scenario.js";
import { Tools } from "./tools.js";
import { TileElement } from "./tileElement.js";
export class XPalette extends HTMLElement {
container = null;
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
overflow: hidden;
flex: 1 1 1px;
position: relative;
}
#tiles_container {
width: 100%;
height: 100%;
display: flex;
flex-wrap: wrap;
gap: 4px;
overflow-y: scroll;
justify-content: center;
border: 1px solid #333;
border-radius: 4px;
}
.tile {
width: 48px;
height: 48px;
outline: 1px solid #333;
background-size: cover;
cursor: pointer;
}
</style>
<div id="tiles_container">
</div>
`
}
connectedCallback() {
this.container= this.shadowRoot.getElementById("tiles_container");
this.render();
window.addEventListener("update.ui", () => {
this.render();
});
this.addEventListener("click", (event) => {
const tile = event.composedPath().find((element) => {
return element instanceof TileElement;
});
if (!tile) {
return;
}
let tileIndex = tile.getAttribute("data-tile-index");
let noTile = tile.getAttribute("data-no-tile");
if (noTile) {
Tools.getInstance().currentBrush = null;
window.dispatchEvent(new CustomEvent("brush.change", { detail: null}));
return;
}
if (!tileIndex) {
return;
}
const tileObject = Scenario.getInstance().getTileFromPalette(parseInt(tileIndex));
Tools.getInstance().currentBrush = tileObject;
window.dispatchEvent(new CustomEvent("brush.change", { detail: tileObject }));
});
}
clearAll() {
this.container.innerHTML = "";
}
renderTile(tile) {
let tileElement = document.createElement("x-tile-element");
tileElement.classList.add("tile");
if (tile) {
tileElement.setAttribute("data-tile-index", tile.getIndex());
tileElement.setAttribute("image", tile.getImage());
} else {
tileElement.setAttribute('data-no-tile', true);
tileElement.removeAttribute("image");
}
this.container.appendChild(tileElement);
}
render() {
this.clearAll();
const scenario = Scenario.getInstance();
const tiles = scenario.getPalette();
this.renderTile(null);
tiles.forEach(tile => {
this.renderTile(tile);
});
}
}
customElements.define("x-palette", XPalette);