Show each block's Minecraft texture in the studio's block lists, as v3e did

This commit is contained in:
piexel committed 2026-11-10 21:12:19 +01:00
1 parent f9e33f560d
commit 3ac240f936
3 files changed
+76 -25

No files matched your search

@@ -0,0 +1,34 @@
import { useEffect, useRef, useState } from 'react'
import type { BlockColors } from '../scene/blockColors'
import { cn } from '@/lib/utils'
/** The block's top texture as Minecraft draws it, 16 px and pixel-sharp; an empty square while it loads or when it has none. */
export function BlockIcon({ block, blocks, className }: { block: string; blocks: BlockColors; className?: string }): React.JSX.Element {
const canvasRef = useRef<HTMLCanvasElement>(null)
const [missing, setMissing] = useState(true)
useEffect(() => {
let active = true
setMissing(true)
void blocks.topTexture(block).then((texture) => {
const canvas = canvasRef.current
if (!active || texture === null || canvas === null) {
return
}
canvas.width = texture.width
canvas.height = texture.height
canvas.getContext('2d')!.putImageData(texture, 0, 0)
setMissing(false)
})
return () => {
active = false
}
}, [block, blocks])
return (
<span className={cn('relative size-4 shrink-0', className)}>
<canvas ref={canvasRef} className={cn('size-full [image-rendering:pixelated]', missing && 'invisible')} />
{missing && <span className="absolute inset-0 rounded-[2px] border border-foreground/25" />}
</span>
)
}
@@ -9,7 +9,7 @@ import { createPatternView, type PatternBlocks, type PatternLayer, type PatternS
import { BlockColors } from '../scene/blockColors' import { BlockColors } from '../scene/blockColors'
import { BlockModels } from '../scene/blockModels' import { BlockModels } from '../scene/blockModels'
import { BridgeAssets } from '../scene/BridgeAssets' import { BridgeAssets } from '../scene/BridgeAssets'
import { hexColor } from './colors' import { BlockIcon } from './BlockIcon'
import { EntryBrowser } from './EntryBrowser' import { EntryBrowser } from './EntryBrowser'
import { Panel, SidebarPanel } from './Sidebar' import { Panel, SidebarPanel } from './Sidebar'
import { TiledCanvas } from './TiledCanvas' import { TiledCanvas } from './TiledCanvas'
@@ -18,13 +18,13 @@ const DEFAULT_NAMESPACE_PREFIX = 'minecraft:'
// A layer thinner than the depth shown, or a palette with no block for the spot. // A layer thinner than the depth shown, or a palette with no block for the spot.
const NONE_LABEL = 'nothing at this depth' const NONE_LABEL = 'nothing at this depth'
/** The blocks the shown layer's tiles placed so far by name, with their colours, and whether some spots got none. */ /** The blocks the shown layer's tiles placed so far by name, each with one full state for its icon, and whether some spots got none. */
interface LegendBlocks { interface LegendBlocks {
colors: Map<string, number> examples: Map<string, string>
someNone: boolean someNone: boolean
} }
const EMPTY_LEGEND: LegendBlocks = { colors: new Map(), someNone: false } const EMPTY_LEGEND: LegendBlocks = { examples: new Map(), someNone: false }
const PILL = 'pointer-events-none absolute rounded-md border border-overlay-border bg-card/90 px-3 py-1.5 text-[12px] text-secondary-text' const PILL = 'pointer-events-none absolute rounded-md border border-overlay-border bg-card/90 px-3 py-1.5 text-[12px] text-secondary-text'
interface PatternStudioProps { interface PatternStudioProps {
@@ -47,7 +47,7 @@ export function PatternStudio({ client, view, packRevision, onSummary, tell }: P
[view.id, entry, depth, packRevision] [view.id, entry, depth, packRevision]
) )
const [status, setStatus] = useState<PatternStatus | null>(null) const [status, setStatus] = useState<PatternStatus | null>(null)
const { legend, createView } = usePatternView(client, layer, setStatus, tell) const { legend, blockColors, createView } = usePatternView(client, layer, setStatus, tell)
const viewRef = useRef<PatternView | null>(null) const viewRef = useRef<PatternView | null>(null)
useEffect(() => viewRef.current?.show(layer, 'blocks'), [layer]) useEffect(() => viewRef.current?.show(layer, 'blocks'), [layer])
@@ -89,7 +89,7 @@ export function PatternStudio({ client, view, packRevision, onSummary, tell }: P
</SidebarPanel> </SidebarPanel>
<Panel className="w-[250px]"> <Panel className="w-[250px]">
<EntryFacts entry={entry} /> <EntryFacts entry={entry} />
<Legend blocks={legend} /> <Legend blocks={legend} looks={blockColors} />
</Panel> </Panel>
</> </>
) )
@@ -134,7 +134,7 @@ function usePatternView(
layer: PatternLayer | null, layer: PatternLayer | null,
onStatus: (status: PatternStatus) => void, onStatus: (status: PatternStatus) => void,
tell: (message: string) => void tell: (message: string) => void
): { legend: LegendBlocks; createView(canvas: HTMLCanvasElement): PatternView } { ): { legend: LegendBlocks; blockColors: BlockColors; createView(canvas: HTMLCanvasElement): PatternView } {
const blockColors = useMemo(() => { const blockColors = useMemo(() => {
const images = new BridgeAssets(client, () => {}) const images = new BridgeAssets(client, () => {})
return new BlockColors(new BlockModels(images), images) return new BlockColors(new BlockModels(images), images)
@@ -154,17 +154,17 @@ function usePatternView(
}), }),
[client, blockColors] [client, blockColors]
) )
return { legend, createView } return { legend, blockColors, createView }
} }
/** The legend with the tile's blocks added, one row per block whatever its state; unchanged when it had them all. */ /** The legend with the tile's blocks added, one row per block whatever its state; unchanged when it had them all. */
function withBlocks(shown: LegendBlocks, tile: PatternBlocks): LegendBlocks { function withBlocks(shown: LegendBlocks, tile: PatternBlocks): LegendBlocks {
const added = [...tile.colors].map(([block, color]) => [shortName(block), color] as const).filter(([name]) => !shown.colors.has(name)) const added = [...tile.colors.keys()].map((block) => [shortName(block), block] as const).filter(([name]) => !shown.examples.has(name))
const someNone = shown.someNone || tile.someNone const someNone = shown.someNone || tile.someNone
if (added.length === 0 && someNone === shown.someNone) { if (added.length === 0 && someNone === shown.someNone) {
return shown return shown
} }
return { colors: new Map([...shown.colors, ...added]), someNone } return { examples: new Map([...added, ...shown.examples]), someNone }
} }
function EntryFacts({ entry }: { entry: PatternEntry | null }): React.JSX.Element | null { function EntryFacts({ entry }: { entry: PatternEntry | null }): React.JSX.Element | null {
@@ -185,23 +185,23 @@ function EntryFacts({ entry }: { entry: PatternEntry | null }): React.JSX.Elemen
) )
} }
/** The blocks the shown tiles place, each with the colour it is drawn in from afar. */ /** The blocks the shown tiles place, each with its texture. */
function Legend({ blocks }: { blocks: LegendBlocks }): React.JSX.Element { function Legend({ blocks, looks }: { blocks: LegendBlocks; looks: BlockColors }): React.JSX.Element {
const sorted = [...blocks.colors.entries()].sort(([first], [second]) => first.localeCompare(second)) const sorted = [...blocks.examples].sort(([first], [second]) => first.localeCompare(second))
return ( return (
<section className="flex min-h-0 flex-[3] flex-col"> <section className="flex min-h-0 flex-[3] flex-col">
<h2 className="px-4 pt-2.5 pb-1.5 text-[11px] font-semibold text-muted-foreground">Blocks</h2> <h2 className="px-4 pt-2.5 pb-1.5 text-[11px] font-semibold text-muted-foreground">Blocks</h2>
<ScrollArea className="min-h-0 flex-1"> <ScrollArea className="min-h-0 flex-1">
<div className="px-4 pb-2"> <div className="px-4 pb-2">
{sorted.map(([block, color]) => ( {sorted.map(([block, example]) => (
<div key={block} className="flex h-[26px] items-center gap-2"> <div key={block} className="flex h-[26px] items-center gap-2">
<span className="size-3 shrink-0 rounded-[3px] border border-foreground/25" style={{ background: hexColor(color) }} /> <BlockIcon block={example} blocks={looks} />
<span className="min-w-0 flex-1 truncate">{block}</span> <span className="min-w-0 flex-1 truncate">{block}</span>
</div> </div>
))} ))}
{blocks.someNone && ( {blocks.someNone && (
<div className="flex h-[26px] items-center gap-2 text-secondary-text"> <div className="flex h-[26px] items-center gap-2 text-secondary-text">
<span className="size-3 shrink-0 rounded-[3px] border border-foreground/25 bg-map-background" /> <span className="size-4 shrink-0 rounded-[2px] border border-foreground/25 bg-map-background" />
<span className="min-w-0 flex-1 truncate">Nothing at this depth</span> <span className="min-w-0 flex-1 truncate">Nothing at this depth</span>
</div> </div>
)} )}
+26 -9
View File
@@ -8,6 +8,7 @@ import { parsedText } from '../editor/jsonValues'
import { nodeDrawingPart, nodeWithPath } from '../editor/nodeLayout' import { nodeDrawingPart, nodeWithPath } from '../editor/nodeLayout'
import { SuggestionStore } from '../editor/suggestionStore' import { SuggestionStore } from '../editor/suggestionStore'
import { useNodeEditor } from '../editor/useNodeEditor' import { useNodeEditor } from '../editor/useNodeEditor'
import { BlockColors } from '../scene/blockColors'
import { BlockIndex, blockAtHit, NO_BLOCK } from '../scene/blockPick' import { BlockIndex, blockAtHit, NO_BLOCK } from '../scene/blockPick'
import { BlockModels } from '../scene/blockModels' import { BlockModels } from '../scene/blockModels'
import { BridgeAssets } from '../scene/BridgeAssets' import { BridgeAssets } from '../scene/BridgeAssets'
@@ -16,6 +17,7 @@ import { blocksInPart, blocksOutsidePart } from '../scene/partFocus'
import { INTS_PER_BLOCK, type BlockScene } from '../scene/sceneDecoding' import { INTS_PER_BLOCK, type BlockScene } from '../scene/sceneDecoding'
import { buildSceneMesh, type SceneMesh } from '../scene/sceneMesh' import { buildSceneMesh, type SceneMesh } from '../scene/sceneMesh'
import { BakeMeter, useBakeTiming } from './BakeMeter' import { BakeMeter, useBakeTiming } from './BakeMeter'
import { BlockIcon } from './BlockIcon'
import { EntryBrowser } from './EntryBrowser' import { EntryBrowser } from './EntryBrowser'
import { useEntryFiles } from './EntryFileDialogs' import { useEntryFiles } from './EntryFileDialogs'
import { NodeInspector } from './NodeInspector' import { NodeInspector } from './NodeInspector'
@@ -45,6 +47,7 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce
const [waiting, setWaiting] = useState<string | null>(null) const [waiting, setWaiting] = useState<string | null>(null)
const images = useMemo(() => new BridgeAssets(client, setWaiting), [client]) const images = useMemo(() => new BridgeAssets(client, setWaiting), [client])
const models = useMemo(() => new BlockModels(images), [images]) const models = useMemo(() => new BlockModels(images), [images])
const blockLooks = useMemo(() => new BlockColors(models, images), [models, images])
const entries = useSceneEntries(client, view.id, packRevision, tell) const entries = useSceneEntries(client, view.id, packRevision, tell)
const [chosen, setChosen] = useState<{ key: string; variant: number } | null>(null) const [chosen, setChosen] = useState<{ key: string; variant: number } | null>(null)
const entry = entries.find((candidate) => candidate.key === chosen?.key) ?? entries[0] ?? null const entry = entries.find((candidate) => candidate.key === chosen?.key) ?? entries[0] ?? null
@@ -140,7 +143,7 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce
{selected !== null && editor.state !== null ? ( {selected !== null && editor.state !== null ? (
<NodeInspector key={JSON.stringify(selected.path)} node={selected} state={editor.state} values={values} suggestions={suggestions} onEdit={editor.edit} /> <NodeInspector key={JSON.stringify(selected.path)} node={selected} state={editor.state} values={values} suggestions={suggestions} onEdit={editor.edit} />
) : ( ) : (
<BlockList scene={scene} farLeavesOnly={farLeavesOnly} onFarLeavesOnly={setFarLeavesOnly} /> <BlockList scene={scene} blocks={blockLooks} farLeavesOnly={farLeavesOnly} onFarLeavesOnly={setFarLeavesOnly} />
)} )}
</Panel> </Panel>
</> </>
@@ -325,12 +328,13 @@ function disposeMesh(mesh: SceneMesh | null): void {
interface BlockListProps { interface BlockListProps {
scene: BlockScene | null scene: BlockScene | null
blocks: BlockColors
farLeavesOnly: boolean farLeavesOnly: boolean
onFarLeavesOnly(only: boolean): void onFarLeavesOnly(only: boolean): void
} }
/** The blocks of the scene, most used first, each with its count (v3e ObjectOverviewPanel), and how many leaves are too far from wood. */ /** The blocks of the scene, most used first, each with its count (v3e ObjectOverviewPanel), and how many leaves are too far from wood. */
function BlockList({ scene, farLeavesOnly, onFarLeavesOnly }: BlockListProps): React.JSX.Element { function BlockList({ scene, blocks, farLeavesOnly, onFarLeavesOnly }: BlockListProps): React.JSX.Element {
const counts = useMemo(() => (scene === null ? [] : blockCounts(scene)), [scene]) const counts = useMemo(() => (scene === null ? [] : blockCounts(scene)), [scene])
const farLeaves = useMemo(() => (scene === null ? 0 : countFarLeaves(scene)), [scene]) const farLeaves = useMemo(() => (scene === null ? 0 : countFarLeaves(scene)), [scene])
return ( return (
@@ -348,9 +352,10 @@ function BlockList({ scene, farLeavesOnly, onFarLeavesOnly }: BlockListProps): R
)} )}
<ScrollArea className="min-h-0 flex-1"> <ScrollArea className="min-h-0 flex-1">
<div className="px-4 pb-2"> <div className="px-4 pb-2">
{counts.map(([state, count]) => ( {counts.map(({ material, example, count }) => (
<div key={state} title={state} className="flex h-[26px] items-center gap-2"> <div key={material} title={material} className="flex h-[26px] items-center gap-2">
<span className="min-w-0 flex-1 truncate">{state.replace(DEFAULT_NAMESPACE_PREFIX, '')}</span> <BlockIcon block={example} blocks={blocks} />
<span className="min-w-0 flex-1 truncate">{material.replace(DEFAULT_NAMESPACE_PREFIX, '')}</span>
<span className="shrink-0 text-[11.5px] text-secondary-text tabular-nums">{NUMBER.format(count)}</span> <span className="shrink-0 text-[11.5px] text-secondary-text tabular-nums">{NUMBER.format(count)}</span>
</div> </div>
))} ))}
@@ -360,13 +365,25 @@ function BlockList({ scene, farLeavesOnly, onFarLeavesOnly }: BlockListProps): R
) )
} }
interface BlockUse {
material: string
/** One full state of the block, so its icon has a model to come from. */
example: string
count: number
}
// Block states that differ only in how they face count as one block, as a builder reads the list. // Block states that differ only in how they face count as one block, as a builder reads the list.
function blockCounts(scene: BlockScene): [string, number][] { function blockCounts(scene: BlockScene): BlockUse[] {
const counts = new Map<string, number>() const uses = new Map<string, BlockUse>()
for (let offset = 3; offset < scene.blocks.length; offset += INTS_PER_BLOCK) { for (let offset = 3; offset < scene.blocks.length; offset += INTS_PER_BLOCK) {
const state = scene.palette[scene.blocks[offset]] const state = scene.palette[scene.blocks[offset]]
const material = state.includes('[') ? state.slice(0, state.indexOf('[')) : state const material = state.includes('[') ? state.slice(0, state.indexOf('[')) : state
counts.set(material, (counts.get(material) ?? 0) + 1) const use = uses.get(material)
if (use === undefined) {
uses.set(material, { material, example: state, count: 1 })
} else {
use.count++
}
} }
return [...counts.entries()].sort((first, second) => second[1] - first[1]) return [...uses.values()].sort((first, second) => second.count - first.count)
} }