vulkano_test/src/core/scene/manager.rs

65 lines
1.6 KiB
Rust

use std::error::Error;
use crate::core::app::context::WindowContext;
use super::Scene;
pub struct SceneManager {
scenes: Vec<Box<dyn Scene>>,
current_scene_index: Option<usize>,
}
impl SceneManager {
pub fn new() -> Self {
Self {
scenes: Vec::new(),
current_scene_index: None,
}
}
pub fn load_scene(&mut self, scene: Box<dyn Scene>) {
self.scenes.push(scene);
self.current_scene_index = Some(self.scenes.len() - 1);
}
pub fn replace_current_scene(&mut self, scene: Box<dyn Scene>) {
if let Some(index) = self.current_scene_index {
if index < self.scenes.len() {
self.scenes[index].unload();
self.scenes[index] = scene;
}
} else {
self.load_scene(scene);
}
}
pub fn current_scene(&self) -> Option<&Box<dyn Scene>> {
if let Some(index) = self.current_scene_index {
self.scenes.get(index)
} else {
None
}
}
pub fn current_scene_mut(&mut self) -> Option<&mut Box<dyn Scene>> {
if let Some(index) = self.current_scene_index {
self.scenes.get_mut(index)
} else {
None
}
}
pub fn load_scene_if_not_loaded(
&mut self,
app_context: &mut WindowContext,
) -> Result<(), Box<dyn Error>> {
if let Some(scene) = self.current_scene_mut() {
if !scene.loaded() {
scene.load(app_context)?;
}
} else {
tracing::warn!("No scene found in SceneManager!");
}
Ok(())
}
}