mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-10 03:51:25 +00:00
- engine canvas is passed to editor as a DOM reference - camera view canvas is passed to editor as a DOM reference - scene container is passed to editor as a DOM reference - amended all the editor code that wants to do a DOM query using DOM ID to use the variable that stores the reference - added guard functions to query the DOM-ID just incase the reference is lost due to a re-render - the above change guarantees the reference to the DOM, fixes the bug where the editor cannot read the size of DOM elements, and increases stability of the app in general. Co-authored-by: wil <wil@storyteller.ai>
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { ReactNode, useEffect, useState } from "react";
|
|
import { EngineContext, EditorExpandedI } from "./EngineContext";
|
|
|
|
import Editor from "~/pages/PageEnigma/Editor/editor";
|
|
|
|
interface Props {
|
|
sceneToken?: string;
|
|
children: ReactNode;
|
|
}
|
|
|
|
export const EngineProvider = ({ sceneToken, children }: Props) => {
|
|
const [editor, setEditor] = useState<EditorExpandedI | null>(null);
|
|
const [sceneContainer, setSceneContainer] = useState<HTMLDivElement | null>(
|
|
null,
|
|
);
|
|
const [editorCanvas, setEditorCanvas] = useState<HTMLCanvasElement | null>(
|
|
null,
|
|
);
|
|
const [camViewCanvas, setCamViewCanvas] = useState<HTMLCanvasElement | null>(
|
|
null,
|
|
);
|
|
useEffect(() => {
|
|
setEditor((curr) => {
|
|
if (curr !== null) {
|
|
console.warn("Editor Engine already exist");
|
|
return curr;
|
|
}
|
|
|
|
class EditorExpanded extends Editor implements EditorExpandedI {
|
|
setSceneContainer = setSceneContainer;
|
|
setEditorCanvas = setEditorCanvas;
|
|
setCamViewCanvas = setCamViewCanvas;
|
|
}
|
|
return new EditorExpanded();
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
editor &&
|
|
editor.can_initialize &&
|
|
editorCanvas &&
|
|
camViewCanvas &&
|
|
sceneContainer
|
|
) {
|
|
editor.initialize({
|
|
sceneToken: sceneToken || "",
|
|
sceneContainerEl: sceneContainer,
|
|
editorCanvasEl: editorCanvas,
|
|
camViewCanvasEl: camViewCanvas,
|
|
});
|
|
}
|
|
}, [editor, sceneToken, editorCanvas, camViewCanvas, sceneContainer]);
|
|
|
|
return (
|
|
<EngineContext.Provider value={editor}>{children}</EngineContext.Provider>
|
|
);
|
|
};
|