1use crate::{Texture, TextureImportOptions};
24use fyrox_core::{uuid::Uuid, TypeUuidProvider};
25use fyrox_resource::{
26 io::ResourceIo, loader::BoxedImportOptionsLoaderFuture, loader::BoxedLoaderFuture,
27 loader::LoaderPayload, loader::ResourceLoader, options::try_get_import_settings,
28 options::try_get_import_settings_opaque, options::BaseImportOptions, state::LoadError,
29};
30use std::{path::PathBuf, sync::Arc};
31
32pub struct TextureLoader {
34 pub default_import_options: TextureImportOptions,
36}
37
38impl ResourceLoader for TextureLoader {
39 fn extensions(&self) -> &[&str] {
40 &[
41 "jpg", "jpeg", "tga", "gif", "bmp", "png", "tiff", "tif", "dds",
42 ]
43 }
44
45 fn data_type_uuid(&self) -> Uuid {
46 Texture::type_uuid()
47 }
48
49 fn load(&self, path: PathBuf, io: Arc<dyn ResourceIo>) -> BoxedLoaderFuture {
50 let default_import_options = self.default_import_options.clone();
51 Box::pin(async move {
52 let io = io.as_ref();
53
54 let import_options = try_get_import_settings(&path, io)
55 .await
56 .unwrap_or(default_import_options);
57
58 let raw_texture = Texture::load_from_file(&path, io, import_options)
59 .await
60 .map_err(LoadError::new)?;
61
62 Ok(LoaderPayload::new(raw_texture))
63 })
64 }
65
66 fn try_load_import_settings(
67 &self,
68 resource_path: PathBuf,
69 io: Arc<dyn ResourceIo>,
70 ) -> BoxedImportOptionsLoaderFuture {
71 Box::pin(async move {
72 try_get_import_settings_opaque::<TextureImportOptions>(&resource_path, &*io).await
73 })
74 }
75
76 fn default_import_options(&self) -> Option<Box<dyn BaseImportOptions>> {
77 Some(Box::<TextureImportOptions>::default())
78 }
79}