Skip to content

Commit 1a28971

Browse files
committed
wip(editor): show actor and item in toolbox
1 parent a076262 commit 1a28971

1 file changed

Lines changed: 93 additions & 1 deletion

File tree

editor/editor.tsx

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import { memoizedLoading } from "../util/memo.ts"
3434
import { CanvasWrapper as CanvasWrapper_ } from "../util/canvas-wrapper.ts"
3535
import type * as type from "./types.ts"
3636
import { BLOCK_SIZE, CELL_SIZE } from "../util/constants.ts"
37+
import { Item } from "../model/item.ts"
38+
import { Actor } from "../model/actor.ts"
3739

3840
type ToolBase = { id: string }
3941
type CellTool = ToolBase & { kind: "cell"; name: string; def: CellDefinition }
@@ -185,6 +187,28 @@ class ToolManager {
185187
})
186188
}
187189

190+
manager.addItemTool({ kind: "item-remove", id: "item-remove" })
191+
for (const itemDef of Object.values(catalog.items)) {
192+
const id = `item-${itemDef.type}`
193+
manager.addItemTool({
194+
kind: "item",
195+
type: itemDef.type,
196+
def: itemDef,
197+
id,
198+
})
199+
}
200+
201+
manager.addActorTool({ kind: "actor-remove", id: "actor-remove" })
202+
for (const actorDef of Object.values(catalog.actors)) {
203+
const id = `actor-${actorDef.type}`
204+
manager.addActorTool({
205+
kind: "actor",
206+
type: actorDef.type,
207+
def: actorDef,
208+
id,
209+
})
210+
}
211+
188212
return manager
189213
}
190214

@@ -220,6 +244,26 @@ class ToolManager {
220244
)
221245
}
222246

247+
toItem(tool: ItemTool & { kind: "item" }): Item {
248+
return new Item(
249+
null,
250+
0,
251+
0,
252+
// deno-lint-ignore no-explicit-any
253+
tool.type as any,
254+
tool.def,
255+
)
256+
}
257+
258+
toActor(tool: ActorTool & { kind: "actor" }) {
259+
return new Actor(
260+
0,
261+
0,
262+
tool.def,
263+
tool.id,
264+
)
265+
}
266+
223267
#get(index: number): Tool {
224268
if (index < this.tools.length) {
225269
return this.tools[index]
@@ -349,7 +393,7 @@ function Toolbox({ el, on, subscribe }: Context<HTMLElement>) {
349393
el.appendChild(ht.div(
350394
attrs,
351395
<span class="w-4 h-4 block text-xs text-center text-white pointer-events-none">
352-
φ
396+
φp
353397
</span>,
354398
))
355399
continue
@@ -366,6 +410,54 @@ function Toolbox({ el, on, subscribe }: Context<HTMLElement>) {
366410
})
367411
}
368412

413+
for (const itemTool of toolManager.itemTools) {
414+
const id = itemTool.id
415+
const attrs = { id, class: "inline-block p-1 m-1 rounded cursor-pointer" }
416+
if (itemTool.kind === "item-remove") {
417+
el.appendChild(ht.div(
418+
attrs,
419+
<span class="w-4 h-4 block text-xs text-center text-white pointer-events-none">
420+
φi
421+
</span>,
422+
))
423+
continue
424+
}
425+
const item = toolManager.toItem(itemTool)
426+
const div = ht.div(
427+
attrs,
428+
<canvas width="16" height="16" class="pointer-events-none"></canvas>,
429+
)
430+
el.appendChild(div)
431+
const wrapper = new CanvasWrapper(div.querySelector("canvas")!)
432+
item.loadAssets({ loadImage }).then(() => {
433+
wrapper.drawEntity(item)
434+
})
435+
}
436+
437+
for (const actorTool of toolManager.actorTools) {
438+
const id = actorTool.id
439+
const attrs = { id, class: "inline-block p-1 m-1 rounded cursor-pointer" }
440+
if (actorTool.kind === "actor-remove") {
441+
el.appendChild(ht.div(
442+
attrs,
443+
<span class="w-4 h-4 block text-xs text-center text-white pointer-events-none">
444+
φa
445+
</span>,
446+
))
447+
continue
448+
}
449+
const actor = toolManager.toActor(actorTool)
450+
const div = ht.div(
451+
attrs,
452+
<canvas width="16" height="16" class="pointer-events-none"></canvas>,
453+
)
454+
el.appendChild(div)
455+
const wrapper = new CanvasWrapper(div.querySelector("canvas")!)
456+
actor.loadAssets({ loadImage }).then(() => {
457+
wrapper.drawEntity(actor)
458+
})
459+
}
460+
369461
el.appendChild(el.firstChild!) // move mode indicator to last
370462
}
371463

0 commit comments

Comments
 (0)