Skip to main content

cotis_raylib/
cotis_defaults_images.rs

1//! Path-based image types for raylib texture loading.
2//!
3//! Re-exports [`JPEGImage`], [`PNGImage`], and [`SVGImage`] from `cotis-defaults` and provides
4//! [`PathBasedImage`] as a unified enum. All variants store a filesystem path; textures are loaded
5//! via [`RaylibRender::translate_generic_image`](crate::renderer::RaylibRender::translate_generic_image).
6//!
7//! [`SVGImage`] is a semantic path tag — raylib loads raster data from the path. True vector SVG
8//! rendering is not implemented; use pre-rasterized assets until dedicated support lands.
9
10use std::fmt::{self, Debug, Formatter};
11use std::sync::Arc;
12
13use raylib::texture::Texture2D;
14
15use crate::drawables::RaylibDrawContext;
16use crate::raylib_images::{GenericCompatibleImage, RaylibLoadableImage};
17
18/// JPEG path wrapper from `cotis-defaults`.
19pub use cotis_defaults::generic_image::{JPEGImage, PNGImage, SVGImage};
20
21/// Unified path-based image enum covering all supported file kinds.
22#[derive(Clone)]
23pub enum PathBasedImage {
24    /// JPEG image referenced by filesystem path.
25    Jpeg(JPEGImage),
26    /// PNG image referenced by filesystem path.
27    Png(PNGImage),
28    /// SVG path tag (loads raster data from path via raylib; no vector rasterizer).
29    Svg(SVGImage),
30}
31
32impl Debug for PathBasedImage {
33    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
34        match self {
35            PathBasedImage::Jpeg(image) => f.debug_tuple("Jpeg").field(&image.get_path()).finish(),
36            PathBasedImage::Png(image) => f.debug_tuple("Png").field(&image.get_path()).finish(),
37            PathBasedImage::Svg(image) => f.debug_tuple("Svg").field(&image.get_path()).finish(),
38        }
39    }
40}
41
42impl PathBasedImage {
43    /// Returns the filesystem path string used as the texture cache key.
44    ///
45    /// # Examples
46    ///
47    /// ```rust,ignore
48    /// use cotis_defaults::generic_image::JPEGImage;
49    /// use cotis_raylib::cotis_defaults_images::PathBasedImage;
50    ///
51    /// let image = PathBasedImage::Jpeg(JPEGImage::new_const("./assets/icon.jpg"));
52    /// assert_eq!(image.path(), "./assets/icon.jpg");
53    /// ```
54    pub fn path(&self) -> &str {
55        match self {
56            PathBasedImage::Jpeg(image) => image.get_path(),
57            PathBasedImage::Png(image) => image.get_path(),
58            PathBasedImage::Svg(image) => image.get_path(),
59        }
60    }
61}
62
63macro_rules! impl_path_image {
64    ($ty:ty) => {
65        impl RaylibLoadableImage for $ty {
66            fn get_filename(&self) -> &str {
67                self.get_path()
68            }
69        }
70
71        impl GenericCompatibleImage for $ty {
72            fn load_texture(&self, _ctx: &mut RaylibDrawContext<'_, '_>) {}
73
74            fn get_texture(&self, ctx: &RaylibDrawContext<'_, '_>) -> Arc<Texture2D> {
75                let path = self.get_path();
76                let cache = ctx
77                    .image_cache
78                    .expect("image drawing requires RaylibDrawContext::image_cache");
79                cache
80                    .get(path)
81                    .cloned()
82                    .unwrap_or_else(|| panic!("image not preloaded: {path}"))
83            }
84        }
85    };
86}
87
88impl_path_image!(JPEGImage);
89impl_path_image!(PNGImage);
90impl_path_image!(SVGImage);
91
92impl RaylibLoadableImage for PathBasedImage {
93    fn get_filename(&self) -> &str {
94        self.path()
95    }
96}
97
98impl GenericCompatibleImage for PathBasedImage {
99    fn load_texture(&self, ctx: &mut RaylibDrawContext<'_, '_>) {
100        match self {
101            PathBasedImage::Jpeg(image) => image.load_texture(ctx),
102            PathBasedImage::Png(image) => image.load_texture(ctx),
103            PathBasedImage::Svg(image) => image.load_texture(ctx),
104        }
105    }
106
107    fn get_texture(&self, ctx: &RaylibDrawContext<'_, '_>) -> Arc<Texture2D> {
108        match self {
109            PathBasedImage::Jpeg(image) => image.get_texture(ctx),
110            PathBasedImage::Png(image) => image.get_texture(ctx),
111            PathBasedImage::Svg(image) => image.get_texture(ctx),
112        }
113    }
114}