fix: reimplement changing cursor based on tool
This commit is contained in:
parent
2da5ba7af3
commit
d9690eb619
2 changed files with 15 additions and 6 deletions
|
|
@ -9,10 +9,10 @@ import { ToolContext } from "@/context/ToolContext";
|
||||||
|
|
||||||
import Blocks from "./Blocks";
|
import Blocks from "./Blocks";
|
||||||
import Grid from "./Grid";
|
import Grid from "./Grid";
|
||||||
|
import CanvasBorder from "./CanvasBorder";
|
||||||
import Cursor from "./Cursor";
|
import Cursor from "./Cursor";
|
||||||
import CursorInformation from "./information/Cursor";
|
import CursorInformation from "./information/Cursor";
|
||||||
import CanvasInformation from "./information/Canvas";
|
import CanvasInformation from "./information/Canvas";
|
||||||
import CanvasBorder from "./CanvasBorder";
|
|
||||||
|
|
||||||
import welcomeBlocksData from "@/data/welcome.json";
|
import welcomeBlocksData from "@/data/welcome.json";
|
||||||
|
|
||||||
|
|
@ -23,7 +23,7 @@ function Canvas() {
|
||||||
const { image, imageDimensions } = useContext(ImageContext);
|
const { image, imageDimensions } = useContext(ImageContext);
|
||||||
const { settings } = useContext(SettingsContext);
|
const { settings } = useContext(SettingsContext);
|
||||||
const textures = useContext(TexturesContext);
|
const textures = useContext(TexturesContext);
|
||||||
const { tool, selectedBlock } = useContext(ToolContext);
|
const { tool, selectedBlock, cssCursor, setTool, setCssCursor } = useContext(ToolContext);
|
||||||
|
|
||||||
const stageContainerRef = useRef<HTMLDivElement>(null);
|
const stageContainerRef = useRef<HTMLDivElement>(null);
|
||||||
const [stageSize, setStageSize] = useState<Dimension>({ width: 0, height: 0 });
|
const [stageSize, setStageSize] = useState<Dimension>({ width: 0, height: 0 });
|
||||||
|
|
@ -114,10 +114,12 @@ function Canvas() {
|
||||||
const onMouseDown = useCallback(() => {
|
const onMouseDown = useCallback(() => {
|
||||||
setDragging(true);
|
setDragging(true);
|
||||||
onToolUse();
|
onToolUse();
|
||||||
}, [onToolUse]);
|
setCssCursor(tool === "hand" ? "grabbing" : "");
|
||||||
|
}, [onToolUse, tool, setCssCursor]);
|
||||||
|
|
||||||
const onMouseUp = () => {
|
const onMouseUp = () => {
|
||||||
setDragging(false);
|
setDragging(false);
|
||||||
|
setCssCursor(tool === "hand" ? "grab" : "");
|
||||||
};
|
};
|
||||||
|
|
||||||
const onWheel = useCallback(
|
const onWheel = useCallback(
|
||||||
|
|
@ -154,7 +156,7 @@ function Canvas() {
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={stageContainerRef} className="relative w-full h-full">
|
<div ref={stageContainerRef} className="relative w-full h-full" style={{ cursor: cssCursor }}>
|
||||||
<Stage
|
<Stage
|
||||||
width={stageSize.width}
|
width={stageSize.width}
|
||||||
height={stageSize.height}
|
height={stageSize.height}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { createContext, ReactNode, useState } from "react";
|
import { createContext, ReactNode, useEffect, useState } from "react";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
|
|
@ -7,13 +7,20 @@ interface Props {
|
||||||
export const ToolContext = createContext({
|
export const ToolContext = createContext({
|
||||||
tool: "hand" as Tool,
|
tool: "hand" as Tool,
|
||||||
selectedBlock: "stone",
|
selectedBlock: "stone",
|
||||||
|
cssCursor: "pointer",
|
||||||
setTool: (tool: Tool) => {},
|
setTool: (tool: Tool) => {},
|
||||||
setSelectedBlock: (block: string) => {},
|
setSelectedBlock: (block: string) => {},
|
||||||
|
setCssCursor: (cursor: string) => {},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ToolProvider = ({ children }: Props) => {
|
export const ToolProvider = ({ children }: Props) => {
|
||||||
const [tool, setTool] = useState<Tool>("hand");
|
const [tool, setTool] = useState<Tool>("hand");
|
||||||
const [selectedBlock, setSelectedBlock] = useState("stone");
|
const [selectedBlock, setSelectedBlock] = useState("stone");
|
||||||
|
const [cssCursor, setCssCursor] = useState("pointer");
|
||||||
|
|
||||||
return <ToolContext.Provider value={{ tool, selectedBlock, setTool, setSelectedBlock }}>{children}</ToolContext.Provider>;
|
useEffect(() => {
|
||||||
|
setCssCursor(tool === "hand" ? "grab" : "pointer");
|
||||||
|
}, [tool]);
|
||||||
|
|
||||||
|
return <ToolContext.Provider value={{ tool, selectedBlock, cssCursor, setTool, setSelectedBlock, setCssCursor }}>{children}</ToolContext.Provider>;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue