fix: broken selected block component
This commit is contained in:
parent
bea25fc115
commit
e3ae0de165
1 changed files with 44 additions and 26 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { useContext } from "react";
|
import { useContext, useEffect, useState } from "react";
|
||||||
|
|
||||||
import constants from "@/constants";
|
import constants from "@/constants";
|
||||||
|
|
||||||
|
|
@ -7,54 +7,72 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/comp
|
||||||
import { TexturesContext } from "@/context/Textures";
|
import { TexturesContext } from "@/context/Textures";
|
||||||
import { ToolContext } from "@/context/Tool";
|
import { ToolContext } from "@/context/Tool";
|
||||||
|
|
||||||
|
import _blockData from "@/data/blocks/programmer-art/data.json";
|
||||||
|
const blockData: BlockData = _blockData;
|
||||||
|
|
||||||
function SelectedBlock() {
|
function SelectedBlock() {
|
||||||
const textures = useContext(TexturesContext);
|
const { textures } = useContext(TexturesContext);
|
||||||
const { selectedBlock } = useContext(ToolContext);
|
const { selectedBlock } = useContext(ToolContext);
|
||||||
|
|
||||||
const convertToDataUrl = (textureName: string): string => {
|
const [selectedBlockName, setSelectedBlockName] = useState("Stone");
|
||||||
// Show missing texture if fail
|
const [selectedBlockTexture, setSelectedBlockTexture] = useState(constants.MISSING_TEXTURE);
|
||||||
const texture = textures[textureName];
|
|
||||||
if (!texture) return constants.MISSING_TEXTURE;
|
useEffect(() => {
|
||||||
|
const blockInfo = blockData[selectedBlock];
|
||||||
|
setSelectedBlockName(blockInfo.name);
|
||||||
|
|
||||||
|
const texture = textures[`${selectedBlock}.png`];
|
||||||
|
if (!texture) {
|
||||||
|
setSelectedBlockTexture(constants.MISSING_TEXTURE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
const context = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
|
canvas.width = 16;
|
||||||
|
canvas.height = 16;
|
||||||
|
|
||||||
canvas.width = texture.frame.width;
|
if (!ctx) {
|
||||||
canvas.height = texture.frame.height;
|
setSelectedBlockTexture(constants.MISSING_TEXTURE);
|
||||||
|
return;
|
||||||
if (!context) return "";
|
}
|
||||||
|
|
||||||
const image = new Image();
|
const image = new Image();
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
image.src = (texture.baseTexture.resource as any).url;
|
image.src = (texture.baseTexture.resource as any).url;
|
||||||
|
image.onload = () => {
|
||||||
|
ctx.drawImage(
|
||||||
|
image,
|
||||||
|
texture.frame.x,
|
||||||
|
texture.frame.y,
|
||||||
|
texture.frame.width,
|
||||||
|
texture.frame.height,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
texture.frame.width,
|
||||||
|
texture.frame.height
|
||||||
|
);
|
||||||
|
|
||||||
context.drawImage(
|
setSelectedBlockTexture(canvas.toDataURL());
|
||||||
image,
|
};
|
||||||
texture.frame.x,
|
|
||||||
texture.frame.y,
|
|
||||||
texture.frame.width,
|
|
||||||
texture.frame.height,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
texture.frame.width,
|
|
||||||
texture.frame.height
|
|
||||||
);
|
|
||||||
|
|
||||||
return canvas.toDataURL();
|
image.onerror = () => {
|
||||||
};
|
setSelectedBlockTexture(constants.MISSING_TEXTURE);
|
||||||
|
};
|
||||||
|
}, [textures, selectedBlock]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TooltipProvider>
|
<TooltipProvider>
|
||||||
<Tooltip delayDuration={0}>
|
<Tooltip delayDuration={0}>
|
||||||
<TooltipTrigger asChild>
|
<TooltipTrigger asChild>
|
||||||
<img
|
<img
|
||||||
src={convertToDataUrl("stone")}
|
src={selectedBlockTexture}
|
||||||
className="absolute bottom-1 w-8 h-8 cursor-pointer border border-zinc-800 dark:border-zinc-200 rounded"
|
className="absolute bottom-1 w-8 h-8 cursor-pointer border border-zinc-800 dark:border-zinc-200 rounded"
|
||||||
style={{ imageRendering: "pixelated" }}
|
style={{ imageRendering: "pixelated" }}
|
||||||
/>
|
/>
|
||||||
</TooltipTrigger>
|
</TooltipTrigger>
|
||||||
<TooltipContent side="right" sideOffset={10}>
|
<TooltipContent side="right" sideOffset={10}>
|
||||||
<p>{selectedBlock}</p>
|
<p>{selectedBlockName}</p>
|
||||||
</TooltipContent>
|
</TooltipContent>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
</TooltipProvider>
|
</TooltipProvider>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue