1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
use std::path::Path;
use std::sync::LazyLock;
use crate::asset::{Assets, Label as AssetLabel, Submitter as AssetSubmitter};
use crate::builder;
use crate::color::Color;
use crate::data_types::dimens::Dimens;
use crate::image::{Error as ImageError, Image};
pub static WHITE_1X1_ASSET_LABEL: LazyLock<AssetLabel> = LazyLock::new(|| AssetLabel {
path: Path::new("").into(),
name: Some("white_1x1_texture".into()),
});
#[derive(Debug, Clone)]
pub struct Texture
{
pub image: Image,
pub properties: Properties,
}
builder! {
/// Texture properties
#[builder(name = PropertiesBuilder, derives=(Debug, Clone))]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Properties
{
pub wrap: Wrapping,
pub magnifying_filter: Filtering,
pub minifying_filter: Filtering,
}
}
impl Properties
{
pub fn builder() -> PropertiesBuilder
{
PropertiesBuilder::default()
}
}
impl Default for Properties
{
fn default() -> Self
{
Self {
wrap: Wrapping::Repeat,
magnifying_filter: Filtering::Linear,
minifying_filter: Filtering::Nearest,
}
}
}
impl Default for PropertiesBuilder
{
fn default() -> Self
{
Properties::default().into()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Filtering
{
Nearest,
Linear,
}
/// Texture wrapping.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Wrapping
{
Repeat,
MirroredRepeat,
ClampToEdge,
ClampToBorder,
}
builder! {
#[builder(name = ImportSettingsBuilder, derives=(Debug, Clone))]
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct ImportSettings {
properties: Properties,
}
}
impl ImportSettings
{
pub fn builder() -> ImportSettingsBuilder
{
ImportSettingsBuilder::default()
}
}
impl Default for ImportSettingsBuilder
{
fn default() -> Self
{
ImportSettings::default().into()
}
}
pub(crate) fn initialize(assets: &mut Assets)
{
assets.set_importer::<_, _>(["png", "jpg"], import);
assets.store_with_label(
WHITE_1X1_ASSET_LABEL.clone(),
Texture {
image: Image::from_color(Dimens { width: 1, height: 1 }, Color::WHITE_U8),
properties: Properties::default(),
},
);
}
fn import(
asset_submitter: &mut AssetSubmitter<'_>,
path: &Path,
settings: Option<&'_ ImportSettings>,
) -> Result<(), ImageError>
{
asset_submitter.submit_store(Texture {
image: Image::open(path)?,
properties: settings
.map(|settings| settings.properties.clone())
.unwrap_or_default(),
});
Ok(())
}
|