use crate::{Output, OutputFormat, Scene, SceneConfig};
use ranim_core::RanimScene;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[doc(hidden)]
pub struct StaticScene {
pub name: &'static str,
pub constructor: fn(&mut RanimScene),
pub config: StaticSceneConfig,
pub outputs: &'static [StaticOutput],
}
#[doc(hidden)]
pub struct StaticSceneConfig {
pub clear_color: &'static str,
}
#[doc(hidden)]
pub struct StaticOutput {
pub width: u32,
pub height: u32,
pub fps: u32,
pub save_frames: bool,
pub name: Option<&'static str>,
pub dir: &'static str,
pub format: OutputFormat,
}
impl StaticOutput {
pub const DEFAULT: Self = Self {
width: 1920,
height: 1080,
fps: 60,
save_frames: false,
name: None,
dir: "./output",
format: OutputFormat::Mp4,
};
}
pub use inventory;
inventory::collect!(StaticScene);
#[doc(hidden)]
#[unsafe(no_mangle)]
pub extern "C" fn get_scene(idx: usize) -> *const StaticScene {
inventory::iter::<StaticScene>()
.skip(idx)
.take(1)
.next()
.unwrap()
}
#[doc(hidden)]
#[unsafe(no_mangle)]
pub extern "C" fn scene_cnt() -> usize {
inventory::iter::<StaticScene>().count()
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub fn find_scene(name: &str) -> Option<Scene> {
inventory::iter::<StaticScene>()
.find(|s| s.name == name)
.map(Scene::from)
}
impl From<&StaticScene> for Scene {
fn from(s: &StaticScene) -> Self {
Self {
name: s.name.to_string(),
constructor: s.constructor,
config: SceneConfig::from(&s.config),
outputs: s.outputs.iter().map(Output::from).collect(),
}
}
}
impl From<&StaticSceneConfig> for SceneConfig {
fn from(c: &StaticSceneConfig) -> Self {
Self {
clear_color: c.clear_color.to_string(),
}
}
}
impl From<&StaticOutput> for Output {
fn from(o: &StaticOutput) -> Self {
Self {
width: o.width,
height: o.height,
fps: o.fps,
save_frames: o.save_frames,
name: o.name.map(|n| n.to_string()),
dir: o.dir.to_string(),
format: o.format,
}
}
}