yakui_core/paint/
texture.rs

1use glam::UVec2;
2
3/// A texture that is managed by yakui.
4#[derive(Clone)]
5pub struct Texture {
6    format: TextureFormat,
7    size: UVec2,
8    data: Vec<u8>,
9
10    /// How to filter the texture when it needs to be minified (made smaller)
11    pub min_filter: TextureFilter,
12
13    /// How to filter the texture when it needs to be magnified (made larger)
14    pub mag_filter: TextureFilter,
15
16    /// How to handle texture addressing
17    pub address_mode: AddressMode,
18}
19
20impl std::fmt::Debug for Texture {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        f.debug_struct("Texture")
23            .field("format", &self.format)
24            .field("size", &self.size)
25            .field("min_filter", &self.min_filter)
26            .field("mag_filter", &self.mag_filter)
27            .field("address_mode", &self.address_mode)
28            .finish_non_exhaustive()
29    }
30}
31
32/// A texture format that yakui can manage.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[non_exhaustive]
35pub enum TextureFormat {
36    /// Red, green, blue, and alpha channels, each represented as a `u8`. The
37    /// color channels are sRGB-encoded.
38    Rgba8Srgb,
39
40    /// A single color channel represented as a `u8`.
41    R8,
42}
43
44/// Which kind of filtering to use when scaling the texture.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
46pub enum TextureFilter {
47    /// Blend the nearest pixels in the texture.
48    Linear,
49
50    /// Pick the nearest pixel. Useful for pixel art.
51    Nearest,
52}
53
54/// Which kind of address mode to use when UVs go outside the range `[0, 1]`.
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
56pub enum AddressMode {
57    /// Clamp to the edge of the texture
58    ClampToEdge,
59
60    /// Repeat the texture
61    Repeat,
62}
63
64impl Texture {
65    /// Create a new texture from its format, size, and data.
66    pub fn new(format: TextureFormat, size: UVec2, data: Vec<u8>) -> Self {
67        Self {
68            format,
69            size,
70            data,
71            min_filter: TextureFilter::Nearest,
72            mag_filter: TextureFilter::Linear,
73            address_mode: AddressMode::ClampToEdge,
74        }
75    }
76
77    /// The size of the texture.
78    pub fn size(&self) -> UVec2 {
79        self.size
80    }
81
82    /// The texture's raw data.
83    pub fn data(&self) -> &[u8] {
84        self.data.as_slice()
85    }
86
87    /// A mutable reference to the texture's data.
88    pub fn data_mut(&mut self) -> &mut [u8] {
89        self.data.as_mut_slice()
90    }
91
92    /// The texture's format.
93    pub fn format(&self) -> TextureFormat {
94        self.format
95    }
96}
97
98/// Describes a change that happened to a texture since the last update.
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum TextureChange {
101    /// The texture was added since the last update.
102    Added,
103
104    /// The texture was removed since the last update.
105    Removed,
106
107    /// The texture was modified since the last update.
108    Modified,
109}