use std::fmt;
use std::sync::Arc;
use crate::asset::{AssetSource, InMemoryAsset};
#[derive(Clone, Debug)]
pub enum ImageData {
#[cfg(feature = "registry")]
Embedded(oxideav_core::VideoFrame),
Source(Arc<dyn AssetSource>),
External { uri: String, mime: Option<String> },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MagFilter {
Nearest,
Linear,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MinFilter {
Nearest,
Linear,
NearestMipNearest,
LinearMipNearest,
NearestMipLinear,
LinearMipLinear,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WrapMode {
ClampToEdge,
MirroredRepeat,
Repeat,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Sampler {
pub mag_filter: MagFilter,
pub min_filter: MinFilter,
pub wrap_s: WrapMode,
pub wrap_t: WrapMode,
}
impl Sampler {
pub fn default_sampler() -> Self {
Self {
mag_filter: MagFilter::Linear,
min_filter: MinFilter::LinearMipLinear,
wrap_s: WrapMode::Repeat,
wrap_t: WrapMode::Repeat,
}
}
}
impl Default for Sampler {
fn default() -> Self {
Self::default_sampler()
}
}
pub struct Texture {
pub name: Option<String>,
pub image: ImageData,
pub sampler: Sampler,
}
impl fmt::Debug for Texture {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Texture")
.field("name", &self.name)
.field("image", &self.image)
.field("sampler", &self.sampler)
.finish()
}
}
impl Clone for Texture {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
image: self.image.clone(),
sampler: self.sampler,
}
}
}
impl Texture {
pub fn from_uri(uri: impl Into<String>) -> Self {
Self {
name: None,
image: ImageData::External {
uri: uri.into(),
mime: None,
},
sampler: Sampler::default_sampler(),
}
}
pub fn from_source(source: Arc<dyn AssetSource>) -> Self {
Self {
name: None,
image: ImageData::Source(source),
sampler: Sampler::default_sampler(),
}
}
pub fn from_encoded(mime: impl Into<String>, bytes: Vec<u8>) -> Self {
let asset = Arc::new(InMemoryAsset {
mime: Some(mime.into()),
bytes,
});
Self::from_source(asset)
}
}