(
canvasIdOrOptions: string | UseEngineOptions,
autoInit?: boolean
)
| 99 | export function useEngine(canvasId: string, autoInit?: boolean): UseEngineReturn; |
| 100 | export function useEngine(options: UseEngineOptions): UseEngineReturn; |
| 101 | export function useEngine( |
| 102 | canvasIdOrOptions: string | UseEngineOptions, |
| 103 | autoInit?: boolean |
| 104 | ): UseEngineReturn { |
| 105 | // Parse options |
| 106 | const options: UseEngineOptions = typeof canvasIdOrOptions === 'string' |
| 107 | ? { |
| 108 | viewportId: canvasIdOrOptions, // Use canvasId as viewportId for backward compatibility |
| 109 | canvasId: canvasIdOrOptions, |
| 110 | showGrid: true, |
| 111 | showGizmos: true, |
| 112 | autoInit |
| 113 | } |
| 114 | : { |
| 115 | showGrid: true, |
| 116 | showGizmos: true, |
| 117 | autoInit: true, |
| 118 | ...canvasIdOrOptions |
| 119 | }; |
| 120 | |
| 121 | const engineRef = useRef<EngineService>(EngineService.getInstance()); |
| 122 | const statsIntervalRef = useRef<number | null>(null); |
| 123 | const viewportRegisteredRef = useRef(false); |
| 124 | |
| 125 | const [state, setState] = useState<EngineState>({ |
| 126 | initialized: engineInitialized, |
| 127 | running: false, |
| 128 | fps: 0, |
| 129 | drawCalls: 0, |
| 130 | spriteCount: 0, |
| 131 | error: null |
| 132 | }); |
| 133 | |
| 134 | // Initialize engine and register viewport |
| 135 | useEffect(() => { |
| 136 | if (!options.autoInit) return; |
| 137 | |
| 138 | const init = async () => { |
| 139 | try { |
| 140 | // Initialize engine with primary canvas (first viewport) |
| 141 | await initializeEngine(options.canvasId); |
| 142 | setState((prev) => ({ ...prev, initialized: true, error: null })); |
| 143 | |
| 144 | // Register this viewport |
| 145 | if (!viewportRegisteredRef.current) { |
| 146 | engineRef.current.registerViewport(options.viewportId, options.canvasId); |
| 147 | engineRef.current.setViewportConfig( |
| 148 | options.viewportId, |
| 149 | options.showGrid ?? true, |
| 150 | options.showGizmos ?? true |
| 151 | ); |
| 152 | viewportRegisteredRef.current = true; |
| 153 | } |
| 154 | |
| 155 | // Start stats update interval |
| 156 | if (!statsIntervalRef.current) { |
| 157 | statsIntervalRef.current = window.setInterval(() => { |
| 158 | const stats = engineRef.current.getStats(); |
no test coverage detected