diff options
author | MetroWind <chris.corsair@gmail.com> | 2025-08-31 09:10:28 -0700 |
---|---|---|
committer | MetroWind <chris.corsair@gmail.com> | 2025-08-31 09:10:28 -0700 |
commit | 3204043e5cd855855b045d8f410b320dcb772d25 (patch) | |
tree | 5588ae66f826ecd1cd431d14b71bb7aae49cf33b /scripts/main.js | |
parent | 6a6cc2f22e806be895f9fddc9ed6cabab395b6cb (diff) |
Support multiple floors. Use 98 style.
Diffstat (limited to 'scripts/main.js')
-rw-r--r-- | scripts/main.js | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/scripts/main.js b/scripts/main.js index 7347f37..b8de9e4 100644 --- a/scripts/main.js +++ b/scripts/main.js | |||
@@ -1,6 +1,7 @@ | |||
1 | const DEFAULT_APP_STATE = { | 1 | const DEFAULT_APP_STATE = { |
2 | floor: 1, | 2 | floor: 0, // This is 0-based. |
3 | plan: new Array(GRID_SIZE_X * GRID_SIZE_Y).fill(0), | 3 | plan: new Array(FLOOR_COUNT).fill( |
4 | new Array(GRID_SIZE_X * GRID_SIZE_Y).fill(0)), | ||
4 | // mouse_state can be “normal”, or “dnd”. | 5 | // mouse_state can be “normal”, or “dnd”. |
5 | mouse_state: "normal", | 6 | mouse_state: "normal", |
6 | plan_code: "", | 7 | plan_code: "", |
@@ -9,7 +10,9 @@ const DEFAULT_APP_STATE = { | |||
9 | async function serializePlan(app_state) | 10 | async function serializePlan(app_state) |
10 | { | 11 | { |
11 | let rest = new Array(GRID_SIZE_X * GRID_SIZE_Y * (FLOOR_COUNT - 1)).fill(0); | 12 | let rest = new Array(GRID_SIZE_X * GRID_SIZE_Y * (FLOOR_COUNT - 1)).fill(0); |
12 | return compressBytes(Uint8Array.from(app_state.plan.concat(rest))); | 13 | let concated_plans = |
14 | app_state.plan.reduce((big, floor_plan) => big.concat(floor_plan), []); | ||
15 | return compressBytes(Uint8Array.from(concated_plans)); | ||
13 | } | 16 | } |
14 | 17 | ||
15 | function genGrid(x_count, y_count, cell_size) | 18 | function genGrid(x_count, y_count, cell_size) |
@@ -105,11 +108,21 @@ ${cell_y * (CELL_SIZE + 1) + 1})`}, ASSET_WHITE_BG, TILES[idx].inner_svg); | |||
105 | 108 | ||
106 | function PlanView({plan, mouse_state}) | 109 | function PlanView({plan, mouse_state}) |
107 | { | 110 | { |
111 | console.debug("Looking at plan ", plan); | ||
108 | return h("div", {}, | 112 | return h("div", {}, |
109 | h("p", {}, "The plan:"), | ||
110 | h(PlanGridView, {content: plan, mouse_state: mouse_state})); | 113 | h(PlanGridView, {content: plan, mouse_state: mouse_state})); |
111 | } | 114 | } |
112 | 115 | ||
116 | // on_floor_change takes one argument, which is the 0-based floor number. | ||
117 | function FloorSelector({floor, on_floor_change}) | ||
118 | { | ||
119 | return h("div", {"id": "FloorSelectorWrapper", "class": "ButtonRow"}, | ||
120 | h("label", {}, "Floor"), | ||
121 | h("input", {"id": "InputFloor", "type": "number", "step": 1, "min": 1, | ||
122 | "max": 15, "value": floor, | ||
123 | onchange: e => on_floor_change(e.target.value - 1),},)); | ||
124 | } | ||
125 | |||
113 | function App({initial_state}) | 126 | function App({initial_state}) |
114 | { | 127 | { |
115 | const [state, setState] = preactHooks.useState(initial_state); | 128 | const [state, setState] = preactHooks.useState(initial_state); |
@@ -134,7 +147,7 @@ function App({initial_state}) | |||
134 | let asset_index = parseInt(e.target.getAttribute("data-asset-index")); | 147 | let asset_index = parseInt(e.target.getAttribute("data-asset-index")); |
135 | 148 | ||
136 | let new_state = structuredClone(state); | 149 | let new_state = structuredClone(state); |
137 | new_state.plan[cell_index] = asset_index; | 150 | new_state.plan[new_state.floor][cell_index] = asset_index; |
138 | new_state.mouse_state = "normal"; | 151 | new_state.mouse_state = "normal"; |
139 | serializePlan(new_state).then((s) => { | 152 | serializePlan(new_state).then((s) => { |
140 | new_state.plan_code = s; | 153 | new_state.plan_code = s; |
@@ -168,16 +181,27 @@ function App({initial_state}) | |||
168 | encodeURIComponent(svg_str); | 181 | encodeURIComponent(svg_str); |
169 | } | 182 | } |
170 | 183 | ||
184 | // new_floor is 0-based. | ||
185 | function onFloorChange(new_floor) | ||
186 | { | ||
187 | let new_state = structuredClone(state); | ||
188 | new_state.floor = new_floor; | ||
189 | setState(new_state); | ||
190 | } | ||
191 | |||
171 | return h("div", {}, | 192 | return h("div", {}, |
172 | h("h2", {}, `Floor ${state.floor}`), | 193 | h(FloorSelector, {floor: state.floor + 1, |
194 | on_floor_change: onFloorChange}), | ||
173 | h(AssetsView, {on_drag_begin: onDragAssetBegin, | 195 | h(AssetsView, {on_drag_begin: onDragAssetBegin, |
174 | on_drag_end: onDragAssetEnd}), | 196 | on_drag_end: onDragAssetEnd}), |
175 | h(PlanView, {plan: state.plan, mouse_state: state.mouse_state}), | 197 | h("hr", {}), |
176 | h("div", {}, | 198 | h(PlanView, {plan: state.plan[state.floor], |
177 | h("a", {"href": "javascript:void(0);", onclick: onClickSaveImg}, | 199 | mouse_state: state.mouse_state}), |
178 | "Open Image!")), | ||
179 | h("div", {}, | 200 | h("div", {}, |
180 | h("textarea", {readonly: true}, state.plan_code)), | 201 | h("textarea", {readonly: true}, state.plan_code)), |
202 | h("div", {"class": "ButtonRow"}, | ||
203 | h("button", {onclick: onClickSaveImg, type: "button"}, | ||
204 | "Open as Image!")), | ||
181 | ); | 205 | ); |
182 | } | 206 | } |
183 | 207 | ||
@@ -198,7 +222,17 @@ else | |||
198 | { | 222 | { |
199 | decompressBytes(encoded_plan).then(a => { | 223 | decompressBytes(encoded_plan).then(a => { |
200 | let state = structuredClone(DEFAULT_APP_STATE); | 224 | let state = structuredClone(DEFAULT_APP_STATE); |
201 | state.plan = Array.from(a.subarray(0, GRID_SIZE_X * GRID_SIZE_Y)); | 225 | let concated_plan = Array.from(a); |
226 | console.debug(concated_plan); | ||
227 | state.plan = []; | ||
228 | for(let i = 0; i < GRID_SIZE_X * GRID_SIZE_Y * FLOOR_COUNT; | ||
229 | i += GRID_SIZE_X * GRID_SIZE_Y) | ||
230 | { | ||
231 | let floor_slice = | ||
232 | concated_plan.slice(i, i + GRID_SIZE_X * GRID_SIZE_Y); | ||
233 | console.debug(floor_slice); | ||
234 | state.plan.push(floor_slice); | ||
235 | } | ||
202 | state.plan_code = encoded_plan; | 236 | state.plan_code = encoded_plan; |
203 | render(state); | 237 | render(state); |
204 | }); | 238 | }); |