refactor: rename functions and change imports in utils
This commit is contained in:
parent
59d69152dc
commit
b9e1307ace
7 changed files with 17 additions and 17 deletions
|
|
@ -13,7 +13,7 @@ import { ToolContext } from "@/context/Tool";
|
||||||
import { useTextures } from "@/hooks/useTextures";
|
import { useTextures } from "@/hooks/useTextures";
|
||||||
import { useBlockData } from "@/hooks/useBlockData";
|
import { useBlockData } from "@/hooks/useBlockData";
|
||||||
|
|
||||||
import { confirmSelection, isInSelection } from "@/utils/selection";
|
import * as selection from "@/utils/selection";
|
||||||
import * as clipboard from "@/utils/clipboard";
|
import * as clipboard from "@/utils/clipboard";
|
||||||
|
|
||||||
import Blocks from "./Blocks";
|
import Blocks from "./Blocks";
|
||||||
|
|
@ -116,7 +116,7 @@ function Canvas() {
|
||||||
const updated = blocks.filter((block) => {
|
const updated = blocks.filter((block) => {
|
||||||
const withinRadius =
|
const withinRadius =
|
||||||
block.x >= radiusPosition.x && block.x < radiusPosition.x + radius && block.y >= radiusPosition.y && block.y < radiusPosition.y + radius;
|
block.x >= radiusPosition.x && block.x < radiusPosition.x + radius && block.y >= radiusPosition.y && block.y < radiusPosition.y + radius;
|
||||||
return !withinRadius || !isInSelection(selectionCoords, block.x, block.y);
|
return !withinRadius || !selection.isIn(selectionCoords, block.x, block.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
setBlocks(updated);
|
setBlocks(updated);
|
||||||
|
|
@ -142,7 +142,7 @@ function Canvas() {
|
||||||
|
|
||||||
setBlocks((prev) =>
|
setBlocks((prev) =>
|
||||||
prev.filter((b) => {
|
prev.filter((b) => {
|
||||||
const isSelected = isInSelection(selectionCoords, b.x, b.y);
|
const isSelected = selection.isIn(selectionCoords, b.x, b.y);
|
||||||
|
|
||||||
// Add blocks in the selection coords to the selection layer
|
// Add blocks in the selection coords to the selection layer
|
||||||
if (isSelected) result.push(b);
|
if (isSelected) result.push(b);
|
||||||
|
|
@ -197,7 +197,7 @@ function Canvas() {
|
||||||
const tileY = radiusPosition.y + y;
|
const tileY = radiusPosition.y + y;
|
||||||
|
|
||||||
// Only add blocks within the selection
|
// Only add blocks within the selection
|
||||||
if (isInSelection(selectionCoords, tileX, tileY)) {
|
if (selection.isIn(selectionCoords, tileX, tileY)) {
|
||||||
radiusBlocks.push({
|
radiusBlocks.push({
|
||||||
name: selectedBlock,
|
name: selectedBlock,
|
||||||
x: tileX,
|
x: tileX,
|
||||||
|
|
@ -414,7 +414,7 @@ function Canvas() {
|
||||||
setSelectionLayerBlocks([]);
|
setSelectionLayerBlocks([]);
|
||||||
break;
|
break;
|
||||||
case "Enter":
|
case "Enter":
|
||||||
confirmSelection(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks);
|
selection.confirm(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks);
|
||||||
break;
|
break;
|
||||||
case " ": // Space
|
case " ": // Space
|
||||||
setDragging(true);
|
setDragging(true);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { CheckIcon, XIcon } from "lucide-react";
|
||||||
import { CanvasContext } from "@/context/Canvas";
|
import { CanvasContext } from "@/context/Canvas";
|
||||||
import { SelectionContext } from "@/context/Selection";
|
import { SelectionContext } from "@/context/Selection";
|
||||||
|
|
||||||
import { confirmSelection } from "@/utils/selection";
|
import * as selection from "@/utils/selection";
|
||||||
|
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ function SelectionBar() {
|
||||||
<XIcon />
|
<XIcon />
|
||||||
</Button>
|
</Button>
|
||||||
<span className="mx-2 text-[0.85rem]">Confirm selection?</span>
|
<span className="mx-2 text-[0.85rem]">Confirm selection?</span>
|
||||||
<Button variant="ghost" className="w-8 h-8" onClick={() => confirmSelection(blocks, layerBlocks, setBlocks, setLayerBlocks)}>
|
<Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, layerBlocks, setBlocks, setLayerBlocks)}>
|
||||||
<CheckIcon />
|
<CheckIcon />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import * as nbt from "nbtify";
|
||||||
import { CanvasContext } from "@/context/Canvas";
|
import { CanvasContext } from "@/context/Canvas";
|
||||||
import { LoadingContext } from "@/context/Loading";
|
import { LoadingContext } from "@/context/Loading";
|
||||||
|
|
||||||
import { decodeVarint } from "@/utils/varint";
|
import * as varint from "@/utils/varint";
|
||||||
|
|
||||||
import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
|
@ -158,7 +158,7 @@ function OpenSchematic({ close }: DialogProps) {
|
||||||
for (let y = spongeData.Height; y > 0; y--) {
|
for (let y = spongeData.Height; y > 0; y--) {
|
||||||
for (let x = 0; x < spongeData.Width; x++) {
|
for (let x = 0; x < spongeData.Width; x++) {
|
||||||
// Decode varint to get the palette value
|
// Decode varint to get the palette value
|
||||||
const { value: paletteValue, bytesRead } = decodeVarint(spongeData.Blocks.Data, offset);
|
const { value: paletteValue, bytesRead } = varint.decode(spongeData.Blocks.Data, offset);
|
||||||
const paletteBlock = Object.keys(spongeData.Blocks.Palette).find((key) => spongeData.Blocks.Palette[key] == paletteValue);
|
const paletteBlock = Object.keys(spongeData.Blocks.Palette).find((key) => spongeData.Blocks.Palette[key] == paletteValue);
|
||||||
|
|
||||||
offset += bytesRead;
|
offset += bytesRead;
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import * as nbt from "nbtify";
|
||||||
import { CanvasContext } from "@/context/Canvas";
|
import { CanvasContext } from "@/context/Canvas";
|
||||||
import { LoadingContext } from "@/context/Loading";
|
import { LoadingContext } from "@/context/Loading";
|
||||||
|
|
||||||
import { encodeVarint } from "@/utils/varint";
|
import * as varint from "@/utils/varint";
|
||||||
|
|
||||||
import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
|
@ -120,7 +120,7 @@ function SaveSchem({ close, registerSubmit, dialogKeyHandler }: DialogProps) {
|
||||||
const blockId = blockPalette[`minecraft:${blockName}${properties}`];
|
const blockId = blockPalette[`minecraft:${blockName}${properties}`];
|
||||||
|
|
||||||
// Parse blockId to number then encode as varint
|
// Parse blockId to number then encode as varint
|
||||||
const id = encodeVarint(parseInt(blockId.toString()));
|
const id = varint.encode(parseInt(blockId.toString()));
|
||||||
// Push to separate array to make array buffer
|
// Push to separate array to make array buffer
|
||||||
ids.push(...id);
|
ids.push(...id);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import { Label } from "@/components/ui/label";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
|
||||||
import { useTextures } from "@/hooks/useTextures";
|
import { useTextures } from "@/hooks/useTextures";
|
||||||
import { isInSelection } from "@/utils/selection";
|
import * as selection from "@/utils/selection";
|
||||||
|
|
||||||
function Replace() {
|
function Replace() {
|
||||||
const { version, setBlocks } = useContext(CanvasContext);
|
const { version, setBlocks } = useContext(CanvasContext);
|
||||||
|
|
@ -36,7 +36,7 @@ function Replace() {
|
||||||
setBlocks((prev) =>
|
setBlocks((prev) =>
|
||||||
prev
|
prev
|
||||||
.map((block) => {
|
.map((block) => {
|
||||||
if (isInSelection(selectionCoords, block.x, block.y)) {
|
if (selection.isIn(selectionCoords, block.x, block.y)) {
|
||||||
if (block.name === block1) {
|
if (block.name === block1) {
|
||||||
// If block2 is air, return null
|
// If block2 is air, return null
|
||||||
// If not, change the block name
|
// If not, change the block name
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
// Check if a block is within the selection
|
// Check if a block is within the selection
|
||||||
export function isInSelection(selection: CoordinateArray, x: number, y: number): boolean {
|
export function isIn(selection: CoordinateArray, x: number, y: number): boolean {
|
||||||
if (selection.length !== 0) {
|
if (selection.length !== 0) {
|
||||||
return selection.some(([x2, y2]) => x2 === x && y2 === y);
|
return selection.some(([x2, y2]) => x2 === x && y2 === y);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function confirmSelection(
|
export function confirm(
|
||||||
blocks: Block[],
|
blocks: Block[],
|
||||||
layerBlocks: Block[],
|
layerBlocks: Block[],
|
||||||
setBlocks: React.Dispatch<React.SetStateAction<Block[]>>,
|
setBlocks: React.Dispatch<React.SetStateAction<Block[]>>,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
export function encodeVarint(number: number): Uint8Array {
|
export function encode(number: number): Uint8Array {
|
||||||
const result = [];
|
const result = [];
|
||||||
while (number >= 0x80) {
|
while (number >= 0x80) {
|
||||||
// Take 7 bits and set the MSB
|
// Take 7 bits and set the MSB
|
||||||
|
|
@ -13,7 +13,7 @@ export function encodeVarint(number: number): Uint8Array {
|
||||||
return new Uint8Array(result);
|
return new Uint8Array(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decodeVarint(buffer: Uint8Array, offset: number): { value: number; bytesRead: number } {
|
export function decode(buffer: Uint8Array, offset: number): { value: number; bytesRead: number } {
|
||||||
let value = 0;
|
let value = 0;
|
||||||
let position = 0;
|
let position = 0;
|
||||||
let byte: number;
|
let byte: number;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue