diff options
author | MetroWind <chris.corsair@gmail.com> | 2025-08-29 21:58:39 -0700 |
---|---|---|
committer | MetroWind <chris.corsair@gmail.com> | 2025-08-29 21:58:39 -0700 |
commit | 12fff735867d3984f7ff824b2a93b1093f8910d7 (patch) | |
tree | 8cf5d9b8e00de1cb5503d8f21c447a3ec9eef697 /scripts/main.js |
WIP
Diffstat (limited to 'scripts/main.js')
-rw-r--r-- | scripts/main.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/scripts/main.js b/scripts/main.js new file mode 100644 index 0000000..7a8b9d4 --- /dev/null +++ b/scripts/main.js | |||
@@ -0,0 +1,93 @@ | |||
1 | function genGrid(x_count, y_count, cell_size) | ||
2 | { | ||
3 | let lines = []; | ||
4 | for(let x = 0; x < x_count+1; x++) | ||
5 | { | ||
6 | lines.push(h("line", { | ||
7 | "x1": x * cell_size + 0.5, "x2": x * cell_size + 0.5, "y1": 0.5, | ||
8 | "y2": y_count * cell_size + 0.5, | ||
9 | })); | ||
10 | } | ||
11 | for(let y = 0; y < y_count+1; y++) | ||
12 | { | ||
13 | lines.push(h("line", { | ||
14 | "y1": y * cell_size + 0.5, "y2": y * cell_size + 0.5, "x1": 0.5, | ||
15 | "x2": x_count * cell_size + 0.5, | ||
16 | })); | ||
17 | } | ||
18 | return h("g", {"stroke": "black", "stroke-width": 1}, lines); | ||
19 | } | ||
20 | |||
21 | function AssetView({asset_index}) | ||
22 | { | ||
23 | return h("div", {"class": "Asset", "draggable": true, | ||
24 | "id": `Asset${asset_index}`}, | ||
25 | h("svg", ASSET_SVG_PROPS, ASSET_WHITE_BG, | ||
26 | TILES[asset_index].inner_svg)); | ||
27 | } | ||
28 | |||
29 | function AssetsView() | ||
30 | { | ||
31 | let views = TILES.slice(1).map((t, i) => | ||
32 | h(AssetView, {asset_index: i + 1})); | ||
33 | return h("div", {}, views); | ||
34 | } | ||
35 | |||
36 | function PlanGridView({content}) | ||
37 | { | ||
38 | const [mouse_coord, setMouseCoord] = preactHooks.useState(null); | ||
39 | |||
40 | function onDragOver(e) | ||
41 | { | ||
42 | e.preventDefault(); | ||
43 | let b = e.target.getBoundingClientRect(); | ||
44 | let x = e.clientX - b.left; | ||
45 | let y = e.clientY - b.top; | ||
46 | setMouseCoord([x, y]); | ||
47 | } | ||
48 | |||
49 | function onDragLeave(e) | ||
50 | { | ||
51 | setMouseCoord(null); | ||
52 | } | ||
53 | |||
54 | let hilight_box = null; | ||
55 | if(mouse_coord !== null) | ||
56 | { | ||
57 | let cell_x = Math.floor(mouse_coord[0] / (CELL_SIZE + 1)); | ||
58 | let cell_y = Math.floor(mouse_coord[1] / (CELL_SIZE + 1)); | ||
59 | hilight_box = h("rect", { | ||
60 | "x": cell_x * (CELL_SIZE + 1) + 0.5, "y": cell_y * (CELL_SIZE + 1) + 0.5, | ||
61 | "width": CELL_SIZE + 1, "height": CELL_SIZE + 1, | ||
62 | "stroke": "#2ed573", "stroke-width": 3, "fill": "transparent"}); | ||
63 | } | ||
64 | let grid_content = content.map((idx, i) => { | ||
65 | let cell_x = i % GRID_SIZE_X; | ||
66 | let cell_y = Math.floor(i / GRID_SIZE_Y); | ||
67 | return h("g", {"transform": `translate(${cell_x * (CELL_SIZE + 1)} \ | ||
68 | ${cell_y * (CELL_SIZE + 1)})`}, TILES[idx]); | ||
69 | }); | ||
70 | |||
71 | return h("svg", {"width": GRID_SIZE_X * (CELL_SIZE + 1) + 1, | ||
72 | "height": GRID_SIZE_Y * (CELL_SIZE + 1) + 1, | ||
73 | "version": "1.1", "ondragover": onDragOver, | ||
74 | "id": "Grid", "ondragleave": onDragLeave}, | ||
75 | grid_content, genGrid(GRID_SIZE_X, GRID_SIZE_Y, CELL_SIZE + 1), | ||
76 | hilight_box); | ||
77 | } | ||
78 | |||
79 | function PlanView(props) | ||
80 | { | ||
81 | return h("div", {}, | ||
82 | h("p", {}, "The plan:"), | ||
83 | h(PlanGridView, {})); | ||
84 | } | ||
85 | |||
86 | function App(props) | ||
87 | { | ||
88 | return h("div", {}, | ||
89 | h(AssetsView, {}), | ||
90 | h(PlanView, {})); | ||
91 | } | ||
92 | |||
93 | preact.render(h(App, {}), document.getElementById("Plan")); | ||