1use std::sync::Arc;
7
8use ranim_core::{RanimScene, SealedRanimScene};
9
10#[cfg(target_arch = "wasm32")]
11use wasm_bindgen::prelude::*;
12
13#[doc(hidden)]
15#[derive(Clone)]
16#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
17pub struct Scene {
18 #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
20 pub name: String,
21 #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
23 pub constructor: fn(&mut RanimScene),
24 #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
26 pub config: SceneConfig,
27 #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
29 pub outputs: Vec<Output>,
30}
31
32#[derive(Debug, Clone)]
34pub struct SceneConfig {
35 pub clear_color: String,
37}
38
39impl Default for SceneConfig {
40 fn default() -> Self {
41 Self {
42 clear_color: "#333333ff".to_string(),
43 }
44 }
45}
46
47#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
49pub enum OutputFormat {
50 #[default]
52 Mp4,
53 Webm,
55 Mov,
57 Gif,
59}
60
61impl std::fmt::Display for OutputFormat {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 match self {
64 Self::Mp4 => write!(f, "mp4"),
65 Self::Webm => write!(f, "webm"),
66 Self::Mov => write!(f, "mov"),
67 Self::Gif => write!(f, "gif"),
68 }
69 }
70}
71
72#[derive(Debug, Clone)]
74pub struct Output {
75 pub width: u32,
77 pub height: u32,
79 pub fps: u32,
81 pub save_frames: bool,
83 pub name: Option<String>,
87 pub dir: String,
91 pub format: OutputFormat,
93}
94
95impl Default for Output {
96 fn default() -> Self {
97 Self {
98 width: 1920,
99 height: 1080,
100 fps: 60,
101 save_frames: false,
102 name: None,
103 dir: "./output".to_string(),
104 format: OutputFormat::default(),
105 }
106 }
107}
108
109pub trait SceneConstructor: Send + Sync {
116 fn construct(&self, r: &mut RanimScene);
118
119 fn build_scene(&self) -> SealedRanimScene {
121 let mut scene = RanimScene::new();
122 self.construct(&mut scene);
123 scene.seal()
124 }
125}
126impl<F: Fn(&mut RanimScene) + Send + Sync> SceneConstructor for F {
129 fn construct(&self, r: &mut RanimScene) {
130 self(r);
131 }
132}
133
134impl SceneConstructor for Arc<dyn SceneConstructor> {
135 fn construct(&self, r: &mut RanimScene) {
136 self.as_ref().construct(r)
137 }
138}