From b657abe90d513b21d6d277c035f1618157242233 Mon Sep 17 00:00:00 2001 From: wil Date: Mon, 25 Mar 2024 10:40:08 -0400 Subject: [PATCH 1/2] editor engine is now in wrapped in a context and provided throughout the app --- app/contexts/EngineContext.tsx | 4 + app/contexts/EngineProvider.tsx | 30 +++++ app/pages/PageEnigma/PageEnigma.tsx | 121 ++++++------------ .../ControlsTopButtons/ControlsTopButtons.tsx | 43 +++++++ .../comps/ControlsTopButtons/index.tsx | 2 + .../PageEnigma/comps/Timeline/Timeline.tsx | 12 +- 6 files changed, 127 insertions(+), 85 deletions(-) create mode 100644 app/contexts/EngineContext.tsx create mode 100644 app/contexts/EngineProvider.tsx create mode 100644 app/pages/PageEnigma/comps/ControlsTopButtons/ControlsTopButtons.tsx create mode 100644 app/pages/PageEnigma/comps/ControlsTopButtons/index.tsx diff --git a/app/contexts/EngineContext.tsx b/app/contexts/EngineContext.tsx new file mode 100644 index 0000000000..9da0df1808 --- /dev/null +++ b/app/contexts/EngineContext.tsx @@ -0,0 +1,4 @@ +import { createContext } from "react"; +import Editor from "~/pages/PageEnigma/js/editor"; + +export const EngineContext = createContext(null); \ No newline at end of file diff --git a/app/contexts/EngineProvider.tsx b/app/contexts/EngineProvider.tsx new file mode 100644 index 0000000000..d5124cb01d --- /dev/null +++ b/app/contexts/EngineProvider.tsx @@ -0,0 +1,30 @@ +import { + ReactNode, + useLayoutEffect, + useRef, +} from "react"; +import { EngineContext } from "./EngineContext"; +import Editor from "~/pages/PageEnigma/js/editor"; + + +interface Props{ + children:ReactNode; +} + +export const EngineProvider = ({ children }: Props)=>{ + const editorRef = useRef(null); + + useLayoutEffect(() => { + //componentDidMount + if (editorRef.current == null) { + editorRef.current = new Editor(); + editorRef.current.initialize(); + } + }, []); + + return( + + {children} + + ); +} \ No newline at end of file diff --git a/app/pages/PageEnigma/PageEnigma.tsx b/app/pages/PageEnigma/PageEnigma.tsx index 0b72e150d1..30bda49cb5 100644 --- a/app/pages/PageEnigma/PageEnigma.tsx +++ b/app/pages/PageEnigma/PageEnigma.tsx @@ -1,6 +1,6 @@ import { useCallback, useEffect, useRef, useState } from "react"; -import { faWandSparkles } from "@fortawesome/pro-solid-svg-icons"; +import { faSparkles } from "@fortawesome/pro-solid-svg-icons"; import { Button, ButtonLink } from "~/components"; import { ButtonDialogue } from "~/modules/ButtonDialogue"; @@ -8,45 +8,20 @@ import { TopBarHelmet } from "~/modules/TopBarHelmet/TopBarHelmet"; import { SidePanel } from "~/modules/SidePanel"; import { Tabs } from "~/modules/Tabs"; import { Controls3D } from "./comps/Controls3D"; +import { ControlsTopButtons } from "./comps/ControlsTopButtons"; import { ControlsVideo } from "./comps/ControlsVideo"; import { Timeline } from "./comps/Timeline"; - -import Editor from "./js/editor"; -import { TrackContext } from "~/contexts/TrackContext"; import { TrackProvider } from "~/contexts/TrackProvider"; +import { EngineProvider } from "~/contexts/EngineProvider"; export const PageEnigma = () => { - // const { setTopBarInner } = useContext(TopBarInnerContext) || {}; - const canvasRef = useRef(null); - const editorRef = useRef(null); + const [timelineHeight, setTimelineHeight] = useState(0); const timelineRef = useRef(null); - const editorCallback = useCallback(() => { - // handle editorCallback here - }, []); - useEffect(() => { - //componentDidMount - if (editorRef.current == null) { - editorRef.current = new Editor(); - editorRef.current.initialize(); - } - }, []); - - const handleButtonSave = () => { - editorRef.current?.save(); - }; - - const handleButtonCameraView = () => { - editorRef.current?.change_camera_view(); - }; - - const handleButtonPlayBack = () => { - editorRef.current?.start_playback(); - }; const updateTimelineHeight = useCallback(() => { if (timelineRef.current) { @@ -72,68 +47,52 @@ export const PageEnigma = () => { return (
- + + +
+ {/* Engine section/side panel */} +
+
+ -
- {/* Engine section/side panel */} -
-
- - - {/* Top controls */} -
-
-
- - - -

Do you need help?

-

Ask Michael about this project

-

Ask Miles about ThreeJS

-

Ask Wil about React

-
+ {/* Top controls */} +
+
+ +
+
- + {/* Bottom controls */} +
+
- {/* Bottom controls */} -
- -
+ {/* Side panel */} +
- {/* Side panel */} - + {/* Timeline */} +
+ + + +
- - {/* Timeline */} -
- - - -
-
+
); }; diff --git a/app/pages/PageEnigma/comps/ControlsTopButtons/ControlsTopButtons.tsx b/app/pages/PageEnigma/comps/ControlsTopButtons/ControlsTopButtons.tsx new file mode 100644 index 0000000000..f42211022b --- /dev/null +++ b/app/pages/PageEnigma/comps/ControlsTopButtons/ControlsTopButtons.tsx @@ -0,0 +1,43 @@ +import { useContext } from "react"; +import { Button } from "~/components"; +import { ButtonDialogue } from "~/modules/ButtonDialogue"; +import { EngineContext } from "~/contexts/EngineContext"; + + +export const ControlsTopButtons = () => { + const editorEngine = useContext(EngineContext); + + const handleButtonSave = () => { + editorEngine?.save(); + }; + + const handleButtonCameraView = () => { + editorEngine?.change_camera_view(); + }; + + const handleButtonPlayBack = () => { + editorEngine?.start_playback(); + }; + return( +
+ + + +

Do you need help?

+

Ask Michael about this project

+

Ask Miles about ThreeJS

+

Ask Wil about React

+
+
+ ); +}; \ No newline at end of file diff --git a/app/pages/PageEnigma/comps/ControlsTopButtons/index.tsx b/app/pages/PageEnigma/comps/ControlsTopButtons/index.tsx new file mode 100644 index 0000000000..6e31c86712 --- /dev/null +++ b/app/pages/PageEnigma/comps/ControlsTopButtons/index.tsx @@ -0,0 +1,2 @@ +export { ControlsTopButtons } from './ControlsTopButtons'; + diff --git a/app/pages/PageEnigma/comps/Timeline/Timeline.tsx b/app/pages/PageEnigma/comps/Timeline/Timeline.tsx index 73ee72e905..3b969b474e 100644 --- a/app/pages/PageEnigma/comps/Timeline/Timeline.tsx +++ b/app/pages/PageEnigma/comps/Timeline/Timeline.tsx @@ -1,24 +1,28 @@ import { LowerPanel } from "~/modules/LowerPanel"; -import Editor from "../../js/editor"; +// import Editor from "../../js/editor"; import { Character } from "./Character"; import { useCallback, useContext } from "react"; +import { EngineContext } from "~/contexts/EngineContext"; import { TrackContext } from "~/contexts/TrackContext"; import { Button } from "~/components"; export const Timeline = ({ - editorCurrent, + // editorCurrent, time, }: { - editorCurrent: Editor | null; + // editorCurrent: Editor | null; time: number; }) => { + const editorEngine = useContext(EngineContext); + // editorEngine replaced the engineRef.current passed as a prop + const { characters, updateCharacters } = useContext(TrackContext); const handleButtonLoad = () => { document.getElementById("load-upload")?.click(); }; const handleButtonRender = () => { - editorCurrent?.togglePlayback(); + editorEngine?.togglePlayback(); }; const handleButtonPlay = () => {}; From 06a089c44b155e616232017490d3323da956103e Mon Sep 17 00:00:00 2001 From: wil Date: Mon, 25 Mar 2024 10:53:59 -0400 Subject: [PATCH 2/2] use useEffect --- app/contexts/EngineProvider.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/contexts/EngineProvider.tsx b/app/contexts/EngineProvider.tsx index d5124cb01d..7c708816c4 100644 --- a/app/contexts/EngineProvider.tsx +++ b/app/contexts/EngineProvider.tsx @@ -1,6 +1,6 @@ import { ReactNode, - useLayoutEffect, + useEffect, useRef, } from "react"; import { EngineContext } from "./EngineContext"; @@ -14,17 +14,23 @@ interface Props{ export const EngineProvider = ({ children }: Props)=>{ const editorRef = useRef(null); - useLayoutEffect(() => { + + useEffect(() => { //componentDidMount - if (editorRef.current == null) { + if (editorRef.current !== null ) { + console.warn('Editor Engine already exist'); + }else if (document.getElementById("video-scene") === null){ + console.error('Editor Engine need a target cavas with the id "video-scene"'); + }else{ editorRef.current = new Editor(); editorRef.current.initialize(); } }, []); - + return( {children} ); -} \ No newline at end of file +} +