use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
use raylib::texture::Texture2D;
use crate::drawables::RaylibDrawContext;
use crate::raylib_images::{GenericCompatibleImage, RaylibLoadableImage};
pub use cotis_defaults::generic_image::{JPEGImage, PNGImage, SVGImage};
#[derive(Clone)]
pub enum PathBasedImage {
Jpeg(JPEGImage),
Png(PNGImage),
Svg(SVGImage),
}
impl Debug for PathBasedImage {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
PathBasedImage::Jpeg(image) => f.debug_tuple("Jpeg").field(&image.get_path()).finish(),
PathBasedImage::Png(image) => f.debug_tuple("Png").field(&image.get_path()).finish(),
PathBasedImage::Svg(image) => f.debug_tuple("Svg").field(&image.get_path()).finish(),
}
}
}
impl PathBasedImage {
pub fn path(&self) -> &str {
match self {
PathBasedImage::Jpeg(image) => image.get_path(),
PathBasedImage::Png(image) => image.get_path(),
PathBasedImage::Svg(image) => image.get_path(),
}
}
}
macro_rules! impl_path_image {
($ty:ty) => {
impl RaylibLoadableImage for $ty {
fn get_filename(&self) -> &str {
self.get_path()
}
}
impl GenericCompatibleImage for $ty {
fn load_texture(&self, _ctx: &mut RaylibDrawContext<'_, '_>) {}
fn get_texture(&self, ctx: &RaylibDrawContext<'_, '_>) -> Arc<Texture2D> {
let path = self.get_path();
let cache = ctx
.image_cache
.expect("image drawing requires RaylibDrawContext::image_cache");
cache
.get(path)
.cloned()
.unwrap_or_else(|| panic!("image not preloaded: {path}"))
}
}
};
}
impl_path_image!(JPEGImage);
impl_path_image!(PNGImage);
impl_path_image!(SVGImage);
impl RaylibLoadableImage for PathBasedImage {
fn get_filename(&self) -> &str {
self.path()
}
}
impl GenericCompatibleImage for PathBasedImage {
fn load_texture(&self, ctx: &mut RaylibDrawContext<'_, '_>) {
match self {
PathBasedImage::Jpeg(image) => image.load_texture(ctx),
PathBasedImage::Png(image) => image.load_texture(ctx),
PathBasedImage::Svg(image) => image.load_texture(ctx),
}
}
fn get_texture(&self, ctx: &RaylibDrawContext<'_, '_>) -> Arc<Texture2D> {
match self {
PathBasedImage::Jpeg(image) => image.get_texture(ctx),
PathBasedImage::Png(image) => image.get_texture(ctx),
PathBasedImage::Svg(image) => image.get_texture(ctx),
}
}
}