use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
use wgpu::Device;
use crate::scene::Scene;
type SceneFactory =
Box<dyn Fn(&Device, u32, u32) -> Result<Box<dyn Scene>, String> + Send + Sync>;
static REGISTRY: OnceLock<Mutex<HashMap<String, SceneFactory>>> = OnceLock::new();
fn registry() -> &'static Mutex<HashMap<String, SceneFactory>> {
REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}
pub fn register_scene(
name: impl Into<String>,
factory: impl Fn(&Device, u32, u32) -> Result<Box<dyn Scene>, String> + Send + Sync + 'static,
) {
registry()
.lock()
.expect("scene registry poisoned")
.insert(name.into(), Box::new(factory));
}
pub fn scene_from_registry(
name: &str,
device: &Device,
width: u32,
height: u32,
) -> Option<Result<Box<dyn Scene>, String>> {
registry()
.lock()
.expect("scene registry poisoned")
.get(name)
.map(|f| f(device, width, height))
}