use crate::Frame;
use crate::camera::Camera;
use crate::render_graph::{MeshQueue, RenderGraph};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SceneId(pub(crate) String);
impl SceneId {
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for SceneId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for SceneId {
fn from(s: &str) -> Self {
Self::new(s)
}
}
impl From<String> for SceneId {
fn from(s: String) -> Self {
Self(s)
}
}
pub struct Scene {
pub id: SceneId,
pub camera: Camera,
pub render_graph: Option<RenderGraph>,
pub mesh_queue: Rc<RefCell<MeshQueue>>,
pub(crate) frame_fn: Box<dyn FnMut(&mut Frame)>,
pub(crate) on_enter: Option<Box<dyn FnMut()>>,
pub(crate) on_exit: Option<Box<dyn FnMut()>>,
}
impl Scene {
pub(crate) fn new(
id: SceneId,
render_graph: Option<RenderGraph>,
mesh_queue: Rc<RefCell<MeshQueue>>,
frame_fn: Box<dyn FnMut(&mut Frame)>,
) -> Self {
Self {
id,
camera: Camera::new(),
render_graph,
mesh_queue,
frame_fn,
on_enter: None,
on_exit: None,
}
}
pub(crate) fn enter(&mut self) {
if let Some(ref mut callback) = self.on_enter {
callback();
}
}
pub(crate) fn exit(&mut self) {
if let Some(ref mut callback) = self.on_exit {
callback();
}
}
}
pub struct SceneBuilder<'a> {
pub(crate) scene_id: SceneId,
pub(crate) manager: &'a mut super::SceneManager,
}
impl<'a> SceneBuilder<'a> {
pub fn on_enter<F: FnMut() + 'static>(self, callback: F) -> Self {
if let Some(scene) = self.manager.scenes.get_mut(&self.scene_id.0) {
scene.on_enter = Some(Box::new(callback));
}
self
}
pub fn on_exit<F: FnMut() + 'static>(self, callback: F) -> Self {
if let Some(scene) = self.manager.scenes.get_mut(&self.scene_id.0) {
scene.on_exit = Some(Box::new(callback));
}
self
}
}