use alloc::borrow::Cow;
use ownable::IntoOwned;
use serde::Deserialize;
use crate::{Extensions, Extras};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CameraTypeEnum {
Perspective,
Orthographic,
}
#[derive(Clone, PartialEq, Eq, Deserialize, IntoOwned)]
#[serde(transparent)]
pub struct CameraType<'a>(#[serde(borrow)] pub Cow<'a, str>);
impl core::fmt::Debug for CameraType<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if let Some(e) = self.to_enum() {
e.fmt(f)
} else {
self.0.fmt(f)
}
}
}
impl CameraType<'_> {
pub const PERSPECTIVE: Self = Self(Cow::Borrowed("perspective"));
pub const ORTHOGRAPHIC: Self = Self(Cow::Borrowed("orthographic"));
pub fn to_enum(&self) -> Option<CameraTypeEnum> {
if *self == Self::PERSPECTIVE {
return Some(CameraTypeEnum::Perspective);
} else if *self == Self::ORTHOGRAPHIC {
return Some(CameraTypeEnum::Orthographic);
}
None
}
}
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Perspective<'a> {
#[serde(rename = "aspectRatio")]
pub aspect_ratio: Option<f32>,
pub yfov: f32,
pub zfar: Option<f32>,
pub znear: f32,
#[serde(borrow)]
pub extensions: Option<Extensions<'a>>,
#[serde(borrow)]
pub extras: Option<Extras<'a>>,
}
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Orthographic<'a> {
pub xmag: f32,
pub ymag: f32,
pub zfar: f32,
pub znear: f32,
#[serde(borrow)]
pub extensions: Option<Extensions<'a>>,
#[serde(borrow)]
pub extras: Option<Extras<'a>>,
}
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Camera<'a> {
#[serde(borrow)]
pub name: Option<Cow<'a, str>>,
#[serde(borrow)]
pub ty: CameraType<'a>,
#[serde(borrow)]
pub perspective: Option<Perspective<'a>>,
#[serde(borrow)]
pub orthographic: Option<Orthographic<'a>>,
#[serde(borrow)]
pub extensions: Option<Extensions<'a>>,
#[serde(borrow)]
pub extras: Option<Extras<'a>>,
}