use crate::Document;
#[cfg(feature = "extensions")]
use serde_json::{Map, Value};
#[derive(Clone, Debug)]
pub enum Projection<'a> {
Orthographic(Orthographic<'a>),
Perspective(Perspective<'a>),
}
#[derive(Clone, Debug)]
pub struct Camera<'a> {
document: &'a Document,
index: usize,
json: &'a json::camera::Camera,
}
#[derive(Clone, Debug)]
pub struct Orthographic<'a> {
#[allow(dead_code)]
document: &'a Document,
json: &'a json::camera::Orthographic,
}
#[derive(Clone, Debug)]
pub struct Perspective<'a> {
#[allow(dead_code)]
document: &'a Document,
json: &'a json::camera::Perspective,
}
impl<'a> Camera<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::camera::Camera,
) -> Self {
Self {
document,
index,
json,
}
}
pub fn index(&self) -> usize {
self.index
}
#[cfg(feature = "names")]
#[cfg_attr(docsrs, doc(cfg(feature = "names")))]
pub fn name(&self) -> Option<&'a str> {
self.json.name.as_deref()
}
pub fn projection(&self) -> Projection {
match self.json.type_.unwrap() {
json::camera::Type::Orthographic => {
let json = self.json.orthographic.as_ref().unwrap();
Projection::Orthographic(Orthographic::new(self.document, json))
}
json::camera::Type::Perspective => {
let json = self.json.perspective.as_ref().unwrap();
Projection::Perspective(Perspective::new(self.document, json))
}
}
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
}
impl<'a> Orthographic<'a> {
pub(crate) fn new(document: &'a Document, json: &'a json::camera::Orthographic) -> Self {
Self { document, json }
}
pub fn xmag(&self) -> f32 {
self.json.xmag
}
pub fn ymag(&self) -> f32 {
self.json.ymag
}
pub fn zfar(&self) -> f32 {
self.json.zfar
}
pub fn znear(&self) -> f32 {
self.json.znear
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
}
impl<'a> Perspective<'a> {
pub(crate) fn new(document: &'a Document, json: &'a json::camera::Perspective) -> Self {
Self { document, json }
}
pub fn aspect_ratio(&self) -> Option<f32> {
self.json.aspect_ratio
}
pub fn yfov(&self) -> f32 {
self.json.yfov
}
pub fn zfar(&self) -> Option<f32> {
self.json.zfar
}
pub fn znear(&self) -> f32 {
self.json.znear
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
}