mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Show each block's Minecraft texture in the studio's block lists, as v3e did
This commit is contained in:
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 { 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<string, number>
|
||||
examples: Map<string, string>
|
||||
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<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)
|
||||
|
||||
useEffect(() => viewRef.current?.show(layer, 'blocks'), [layer])
|
||||
@@ -89,7 +89,7 @@ export function PatternStudio({ client, view, packRevision, onSummary, tell }: P
|
||||
</SidebarPanel>
|
||||
<Panel className="w-[250px]">
|
||||
<EntryFacts entry={entry} />
|
||||
<Legend blocks={legend} />
|
||||
<Legend blocks={legend} looks={blockColors} />
|
||||
</Panel>
|
||||
</>
|
||||
)
|
||||
@@ -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 (
|
||||
<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>
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
<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">
|
||||
<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>
|
||||
</div>
|
||||
))}
|
||||
{blocks.someNone && (
|
||||
<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>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -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<string | null>(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 ? (
|
||||
<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>
|
||||
</>
|
||||
@@ -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
|
||||
)}
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
<div className="px-4 pb-2">
|
||||
{counts.map(([state, count]) => (
|
||||
<div key={state} title={state} className="flex h-[26px] items-center gap-2">
|
||||
<span className="min-w-0 flex-1 truncate">{state.replace(DEFAULT_NAMESPACE_PREFIX, '')}</span>
|
||||
{counts.map(({ material, example, count }) => (
|
||||
<div key={material} title={material} className="flex h-[26px] items-center gap-2">
|
||||
<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>
|
||||
</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.
|
||||
function blockCounts(scene: BlockScene): [string, number][] {
|
||||
const counts = new Map<string, number>()
|
||||
function blockCounts(scene: BlockScene): BlockUse[] {
|
||||
const uses = new Map<string, BlockUse>()
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user