mirror of
https://github.com/trafficlunar/blockmatic.git
synced 2026-06-28 06:34:13 +00:00
refactor: rewrite and organize project
still need to add other features from before rewrite
This commit is contained in:
parent
025371bd79
commit
de06203d31
20 changed files with 416 additions and 346 deletions
29
src/context/TexturesContext.tsx
Normal file
29
src/context/TexturesContext.tsx
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { createContext, ReactNode, useEffect, useState } from "react";
|
||||
import { BaseTexture, Spritesheet, Texture } from "pixi.js";
|
||||
|
||||
import spritesheet from "@/data/blocks/programmer-art/spritesheet.json";
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const TexturesContext = createContext({} as Record<string, Texture>);
|
||||
|
||||
export const TexturesProvider = ({ children }: Props) => {
|
||||
const [textures, setTextures] = useState<Record<string, Texture>>({});
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const sheet = new Spritesheet(BaseTexture.from("/blocks/programmer-art/spritesheet.png"), spritesheet);
|
||||
sheet.parse().then((t) => {
|
||||
setTextures(t);
|
||||
setLoaded(true);
|
||||
});
|
||||
}, []);
|
||||
|
||||
if (!loaded) {
|
||||
return <h1>Loading...</h1>;
|
||||
}
|
||||
|
||||
return <TexturesContext.Provider value={textures}>{children}</TexturesContext.Provider>;
|
||||
};
|
||||
60
src/context/ThemeContext.tsx
Normal file
60
src/context/ThemeContext.tsx
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
|
||||
type ThemeProviderProps = {
|
||||
children: React.ReactNode;
|
||||
defaultTheme?: Theme;
|
||||
storageKey?: string;
|
||||
};
|
||||
|
||||
type ThemeProviderState = {
|
||||
theme: Theme;
|
||||
setTheme: (theme: Theme) => void;
|
||||
};
|
||||
|
||||
const initialState: ThemeProviderState = {
|
||||
theme: "system",
|
||||
setTheme: () => null,
|
||||
};
|
||||
|
||||
const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
|
||||
|
||||
export function ThemeProvider({ children, defaultTheme = "system", storageKey = "vite-ui-theme", ...props }: ThemeProviderProps) {
|
||||
const [theme, setTheme] = useState<Theme>(() => (localStorage.getItem(storageKey) as Theme) || defaultTheme);
|
||||
|
||||
useEffect(() => {
|
||||
const root = window.document.documentElement;
|
||||
|
||||
root.classList.remove("light", "dark");
|
||||
|
||||
if (theme === "system") {
|
||||
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
||||
|
||||
root.classList.add(systemTheme);
|
||||
return;
|
||||
}
|
||||
|
||||
root.classList.add(theme);
|
||||
}, [theme]);
|
||||
|
||||
const value = {
|
||||
theme,
|
||||
setTheme: (theme: Theme) => {
|
||||
localStorage.setItem(storageKey, theme);
|
||||
setTheme(theme);
|
||||
},
|
||||
};
|
||||
|
||||
return (
|
||||
<ThemeProviderContext.Provider {...props} value={value}>
|
||||
{children}
|
||||
</ThemeProviderContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const useTheme = () => {
|
||||
const context = useContext(ThemeProviderContext);
|
||||
|
||||
if (context === undefined) throw new Error("useTheme must be used within a ThemeProvider");
|
||||
|
||||
return context;
|
||||
};
|
||||
19
src/context/ToolContext.tsx
Normal file
19
src/context/ToolContext.tsx
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { createContext, ReactNode, useState } from "react";
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const ToolContext = createContext({
|
||||
tool: "hand" as Tool,
|
||||
selectedBlock: "stone",
|
||||
setTool: (tool: Tool) => {},
|
||||
setSelectedBlock: (block: string) => {},
|
||||
});
|
||||
|
||||
export const ToolProvider = ({ children }: Props) => {
|
||||
const [tool, setTool] = useState<Tool>("hand");
|
||||
const [selectedBlock, setSelectedBlock] = useState("stone");
|
||||
|
||||
return <ToolContext.Provider value={{ tool, selectedBlock, setTool, setSelectedBlock }}>{children}</ToolContext.Provider>;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue