use crate::{image, Document};
pub use json::texture::{MagFilter, MinFilter, WrappingMode};
lazy_static! {
static ref DEFAULT_SAMPLER: json::texture::Sampler = Default::default();
}
#[derive(Clone, Debug)]
pub struct Info<'a> {
texture: Texture<'a>,
json: &'a json::texture::Info,
}
#[derive(Clone, Debug)]
pub struct Sampler<'a> {
#[allow(dead_code)]
document: &'a Document,
index: Option<usize>,
json: &'a json::texture::Sampler,
}
#[derive(Clone, Debug)]
pub struct Texture<'a> {
document: &'a Document,
index: usize,
json: &'a json::texture::Texture,
}
impl<'a> Sampler<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::texture::Sampler,
) -> Self {
Self {
document: document,
index: Some(index),
json: json,
}
}
pub(crate) fn default(document: &'a Document) -> Self {
Self {
document: document,
index: None,
json: &DEFAULT_SAMPLER,
}
}
pub fn index(&self) -> Option<usize> {
self.index
}
pub fn mag_filter(&self) -> Option<MagFilter> {
self.json.mag_filter.map(|filter| filter.unwrap())
}
pub fn min_filter(&self) -> Option<MinFilter> {
self.json.min_filter.map(|filter| filter.unwrap())
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn wrap_s(&self) -> WrappingMode {
self.json.wrap_s.unwrap()
}
pub fn wrap_t(&self) -> WrappingMode {
self.json.wrap_t.unwrap()
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> Texture<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::texture::Texture,
) -> Self {
Self {
document: document,
index: index,
json: json,
}
}
pub fn index(&self) -> usize {
self.index
}
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn sampler(&self) -> Sampler<'a> {
self.json.sampler
.as_ref()
.map(|index| self.document.samplers().nth(index.value() as usize).unwrap())
.unwrap_or_else(|| Sampler::default(self.document))
}
pub fn source(&self) -> image::Image<'a> {
self.document.images().nth(self.json.source.value() as usize).unwrap()
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> Info<'a> {
pub(crate) fn new(texture: Texture<'a>, json: &'a json::texture::Info) -> Self {
Self {
texture: texture,
json: json,
}
}
pub fn tex_coord(&self) -> u32 {
self.json.tex_coord
}
pub fn texture(&self) -> Texture<'a> {
self.texture.clone()
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> AsRef<Texture<'a>> for Info<'a> {
fn as_ref(&self) -> &Texture<'a> {
&self.texture
}
}