use std::path::{Path, PathBuf};
use oxframe_2d::View;
use oxframe_core::BoxFuture;
#[derive(Debug, Clone, Copy)]
pub struct SourceLocation {
pub file: &'static str,
pub manifest_dir: &'static str,
}
#[derive(Clone, Copy)]
pub struct SceneDef {
pub(crate) name: &'static str,
pub(crate) location: SourceLocation,
pub(crate) factory: fn(View) -> BoxFuture,
}
impl std::fmt::Debug for SceneDef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SceneDef")
.field("name", &self.name)
.field("location", &self.location)
.finish()
}
}
impl SceneDef {
pub fn new(
name: &'static str,
location: SourceLocation,
factory: fn(View) -> BoxFuture,
) -> Self {
Self {
name,
location,
factory,
}
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn meta_path(&self) -> PathBuf {
let file_name = format!("{}.meta.json", self.name);
let manifest = Path::new(self.location.manifest_dir);
let source = Path::new(self.location.file);
let dir = manifest
.ancestors()
.map(|base| base.join(source))
.find(|p| p.is_file())
.and_then(|p| p.parent().map(Path::to_path_buf))
.unwrap_or_else(|| manifest.to_path_buf());
dir.join(file_name)
}
}