diff --git a/studio/src/renderer/src/studio/BlockIcon.tsx b/studio/src/renderer/src/studio/BlockIcon.tsx new file mode 100644 index 000000000..aba3232d9 --- /dev/null +++ b/studio/src/renderer/src/studio/BlockIcon.tsx @@ -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(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 ( + + + {missing && } + + ) +} diff --git a/studio/src/renderer/src/studio/PatternStudio.tsx b/studio/src/renderer/src/studio/PatternStudio.tsx index fa7ccd2c7..44b2150ca 100644 --- a/studio/src/renderer/src/studio/PatternStudio.tsx +++ b/studio/src/renderer/src/studio/PatternStudio.tsx @@ -9,7 +9,7 @@ import { createPatternView, type PatternBlocks, type PatternLayer, type PatternS import { BlockColors } from '../scene/blockColors' import { BlockModels } from '../scene/blockModels' import { BridgeAssets } from '../scene/BridgeAssets' -import { hexColor } from './colors' +import { BlockIcon } from './BlockIcon' import { EntryBrowser } from './EntryBrowser' import { Panel, SidebarPanel } from './Sidebar' 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. 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 { - colors: Map + examples: Map 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' interface PatternStudioProps { @@ -47,7 +47,7 @@ export function PatternStudio({ client, view, packRevision, onSummary, tell }: P [view.id, entry, depth, packRevision] ) const [status, setStatus] = useState(null) - const { legend, createView } = usePatternView(client, layer, setStatus, tell) + const { legend, blockColors, createView } = usePatternView(client, layer, setStatus, tell) const viewRef = useRef(null) useEffect(() => viewRef.current?.show(layer, 'blocks'), [layer]) @@ -89,7 +89,7 @@ export function PatternStudio({ client, view, packRevision, onSummary, tell }: P - + ) @@ -134,7 +134,7 @@ function usePatternView( layer: PatternLayer | null, onStatus: (status: PatternStatus) => void, tell: (message: string) => void -): { legend: LegendBlocks; createView(canvas: HTMLCanvasElement): PatternView } { +): { legend: LegendBlocks; blockColors: BlockColors; createView(canvas: HTMLCanvasElement): PatternView } { const blockColors = useMemo(() => { const images = new BridgeAssets(client, () => {}) return new BlockColors(new BlockModels(images), images) @@ -154,17 +154,17 @@ function usePatternView( }), [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. */ 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 if (added.length === 0 && someNone === shown.someNone) { 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 { @@ -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. */ -function Legend({ blocks }: { blocks: LegendBlocks }): React.JSX.Element { - const sorted = [...blocks.colors.entries()].sort(([first], [second]) => first.localeCompare(second)) +/** The blocks the shown tiles place, each with its texture. */ +function Legend({ blocks, looks }: { blocks: LegendBlocks; looks: BlockColors }): React.JSX.Element { + const sorted = [...blocks.examples].sort(([first], [second]) => first.localeCompare(second)) return (

Blocks

- {sorted.map(([block, color]) => ( + {sorted.map(([block, example]) => (
- + {block}
))} {blocks.someNone && (
- + Nothing at this depth
)} diff --git a/studio/src/renderer/src/studio/SceneStudio.tsx b/studio/src/renderer/src/studio/SceneStudio.tsx index 1883d2d49..309568f62 100644 --- a/studio/src/renderer/src/studio/SceneStudio.tsx +++ b/studio/src/renderer/src/studio/SceneStudio.tsx @@ -8,6 +8,7 @@ import { parsedText } from '../editor/jsonValues' import { nodeDrawingPart, nodeWithPath } from '../editor/nodeLayout' import { SuggestionStore } from '../editor/suggestionStore' import { useNodeEditor } from '../editor/useNodeEditor' +import { BlockColors } from '../scene/blockColors' import { BlockIndex, blockAtHit, NO_BLOCK } from '../scene/blockPick' import { BlockModels } from '../scene/blockModels' 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 { buildSceneMesh, type SceneMesh } from '../scene/sceneMesh' import { BakeMeter, useBakeTiming } from './BakeMeter' +import { BlockIcon } from './BlockIcon' import { EntryBrowser } from './EntryBrowser' import { useEntryFiles } from './EntryFileDialogs' import { NodeInspector } from './NodeInspector' @@ -45,6 +47,7 @@ export function SceneStudio({ client, view, packRevision, onSummary, tell }: Sce const [waiting, setWaiting] = useState(null) const images = useMemo(() => new BridgeAssets(client, setWaiting), [client]) 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 [chosen, setChosen] = useState<{ key: string; variant: number } | null>(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 ? ( ) : ( - + )} @@ -325,12 +328,13 @@ function disposeMesh(mesh: SceneMesh | null): void { interface BlockListProps { scene: BlockScene | null + blocks: BlockColors farLeavesOnly: boolean 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. */ -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 farLeaves = useMemo(() => (scene === null ? 0 : countFarLeaves(scene)), [scene]) return ( @@ -348,9 +352,10 @@ function BlockList({ scene, farLeavesOnly, onFarLeavesOnly }: BlockListProps): R )}
- {counts.map(([state, count]) => ( -
- {state.replace(DEFAULT_NAMESPACE_PREFIX, '')} + {counts.map(({ material, example, count }) => ( +
+ + {material.replace(DEFAULT_NAMESPACE_PREFIX, '')} {NUMBER.format(count)}
))} @@ -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. -function blockCounts(scene: BlockScene): [string, number][] { - const counts = new Map() +function blockCounts(scene: BlockScene): BlockUse[] { + const uses = new Map() for (let offset = 3; offset < scene.blocks.length; offset += INTS_PER_BLOCK) { const state = scene.palette[scene.blocks[offset]] 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) }