fyrox_texture/
loader.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Texture loader.
22
23use 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
32/// Default implementation for texture loading.
33pub struct TextureLoader {
34    /// Default import options for textures.
35    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}