cotis_raylib/
cotis_defaults_images.rs1use 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
18pub use cotis_defaults::generic_image::{JPEGImage, PNGImage, SVGImage};
20
21#[derive(Clone)]
23pub enum PathBasedImage {
24 Jpeg(JPEGImage),
26 Png(PNGImage),
28 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 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}