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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! The [`Texture`] and [`SubTexture`] structs.

use std::rc::Rc;

use wasm_bindgen::JsCast;
use web_sys::HtmlImageElement;
use web_sys::WebGlTexture;

use crate::gl::Bind;
use crate::Color32;
use crate::{gl, GL};

/// A [`Texture`] stores an Image that can be used while rendering, or to store data.
#[derive(Debug)]
pub struct Texture {
    texture: Option<WebGlTexture>,
    /// Width of the [`Texture`].
    pub width: u32,
    /// Height of the [`Texture`].
    pub height: u32,
    /// Slot the [`Texture`] will occupy.
    pub slot: u32,
}

impl Default for Texture {
    fn default() -> Self {
        Self {
            texture: None,
            width: 1,
            height: 1,
            slot: 0,
        }
    }
}

impl Bind for Texture {
    fn bind(&self, gl: &GL) {
        gl.active_texture(GL::TEXTURE0 + self.slot);
        gl.bind_texture(GL::TEXTURE_2D, self.texture.as_ref());
    }
    fn unbind(&self, gl: &GL) {
        gl.active_texture(GL::TEXTURE0 + self.slot);
        gl.bind_texture(GL::TEXTURE_2D, None);
    }
}

impl Drop for Texture {
    fn drop(&mut self) {
        let gl = gl::get_context();

        gl.delete_texture(self.texture.as_ref());
    }
}

impl Texture {
    /// Create a new [`Texture`] using an [`HtmlImageElement`].
    pub fn new(gl: &GL, image: &HtmlImageElement) -> Self {
        let (width, height) = (image.width(), image.height());

        let texture = gl.create_texture();
        gl.active_texture(GL::TEXTURE0);
        gl.bind_texture(GL::TEXTURE_2D, texture.as_ref());
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_MIN_FILTER, GL::NEAREST as i32);
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_MAG_FILTER, GL::NEAREST as i32);
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_WRAP_S, GL::REPEAT as i32);
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_WRAP_T, GL::REPEAT as i32);
        // Flip the Y-axis so the image displays the right way up
        gl.pixel_storei(GL::UNPACK_FLIP_Y_WEBGL, 1);
        gl.tex_image_2d_with_u32_and_u32_and_html_image_element(
            GL::TEXTURE_2D,
            0,
            GL::RGBA as i32,
            GL::RGBA,
            GL::UNSIGNED_BYTE,
            image,
        )
        .expect("Failed to load texture");
        gl.generate_mipmap(GL::TEXTURE_2D);

        Self {
            width,
            height,
            texture,
            ..Default::default()
        }
    }

    /// Create a new [`Texture`] from an [`HtmlImageElement`] with an given element ID.
    pub fn new_with_element_id(gl: &GL, image_src: &str) -> Self {
        let document: web_sys::Document = web_sys::window().unwrap().document().unwrap();
        let image = document
            .get_element_by_id(image_src)
            .unwrap()
            .dyn_into::<web_sys::HtmlImageElement>()
            .unwrap();
        Self::new(gl, &image)
    }

    /// Create a new [`Texture`] from an [`HtmlImageElement`] with an element ID in the format **textureXX** where *XX* is a number.
    pub fn new_with_texture_id(gl: &GL, count: u32) -> Self {
        Self::new_with_element_id(gl, &format!("texture{}", count))
    }

    /// Create a new [`Texture`] using a slice of [`u8`]s.
    pub fn new_from_pixels(gl: &GL, width: u32, height: u32, pixels: &[u8]) -> Self {
        assert!(pixels.len() == (width * height * 4) as usize);
        let texture = gl.create_texture();
        gl.active_texture(GL::TEXTURE0);
        gl.bind_texture(GL::TEXTURE_2D, texture.as_ref());
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_MIN_FILTER, GL::NEAREST as i32);
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_MAG_FILTER, GL::NEAREST as i32);
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_WRAP_S, GL::REPEAT as i32);
        gl.tex_parameteri(GL::TEXTURE_2D, GL::TEXTURE_WRAP_T, GL::REPEAT as i32);
        gl.tex_image_2d_with_i32_and_i32_and_i32_and_format_and_type_and_opt_u8_array(
            GL::TEXTURE_2D,
            0,
            GL::RGBA as i32,
            width as i32,
            height as i32,
            0,
            GL::RGBA,
            GL::UNSIGNED_BYTE,
            Some(pixels),
        )
        .expect("Failed to generate texture");
        gl.generate_mipmap(GL::TEXTURE_2D);

        Self {
            width,
            height,
            texture,
            ..Default::default()
        }
    }

    /// A colored [`Texture`].
    ///
    /// Create a single pixel sized [`Texture`] with the specified [`Color32`].
    pub fn colored(gl: &GL, color: Color32) -> Self {
        Self::new_from_pixels(gl, 1, 1, &<[u8; 4]>::from(color))
    }

    /// A fully-white [`Texture`].
    pub fn white(gl: &GL) -> Self {
        Self::colored(gl, Color32::WHITE)
    }

    /// A black and white checkerboard [`Texture`].
    pub fn checkerboard(gl: &GL) -> Self {
        Self::checkerboard_colored(gl, Color32::WHITE, Color32::BLACK)
    }

    /// A checkerboard [`Texture`] with two [`Color32`]s.
    pub fn checkerboard_colored(gl: &GL, color1: Color32, color2: Color32) -> Self {
        let size = 8;
        let mut pixels = Vec::<u8>::with_capacity(size * size);
        for x_offset in 0..size {
            for y_offset in 0..size {
                let color = if (x_offset + y_offset) % 2 == 0 {
                    color1
                } else {
                    color2
                };
                pixels.append(&mut Vec::from(color));
            }
        }
        Self::new_from_pixels(gl, size as u32, size as u32, &pixels)
    }
}

/// A [`SubTexture`] is a part of a full [`Texture`].
///
/// It stores the UV co-ordinates of the part of the Texture it occupies.
#[derive(Debug, Clone)]
pub struct SubTexture {
    texture: Option<Rc<Texture>>,
    min: [f32; 2],
    max: [f32; 2],
}

impl Default for SubTexture {
    fn default() -> Self {
        Self {
            texture: None,
            min: [0.0, 0.0],
            max: [1.0, 1.0],
        }
    }
}

impl SubTexture {
    /// Create a new [`SubTexture`] with the full UV co-ordinates.
    pub fn new(texture: Rc<Texture>) -> Self {
        Self {
            texture: Some(texture),
            ..Default::default()
        }
    }

    /// Create a new [`SubTexture`] with the given UV co-ordinates.
    pub fn new_with_coords(texture: Rc<Texture>, uv: Color32) -> Self {
        Self {
            texture: Some(texture),
            min: [uv.x(), uv.z()],
            max: [uv.y(), uv.w()],
        }
    }

    /// Get the UV co-ordinates as an array of four two-component [`f32`].
    pub fn get_uv_coords(&self) -> [[f32; 2]; 4] {
        let (min, max) = (self.min, self.max);
        [
            [min[0], min[1]],
            [min[0], max[1]],
            [max[0], max[1]],
            [max[0], min[1]],
        ]
    }

    /// Create a [`Vec`] of [`SubTexture`]s from a sprite sheet.
    ///
    /// Uses the number of cells horizontally and vertically to devide the cells.
    pub fn create_tiles_from_spritesheet(
        texture: Rc<Texture>,
        horizontal_cells: u32,
        vertical_cells: u32,
    ) -> Vec<SubTexture> {
        assert!(horizontal_cells > 0 && vertical_cells > 0);
        let mut tiles: Vec<SubTexture> = Vec::new();
        for cell_x in 0..horizontal_cells {
            for cell_y in 0..vertical_cells {
                let uv = Color32(
                    cell_x as f32 / horizontal_cells as f32,
                    (cell_x + 1) as f32 / horizontal_cells as f32,
                    cell_y as f32 / vertical_cells as f32,
                    (cell_y + 1) as f32 / vertical_cells as f32,
                );
                let tile = SubTexture::new_with_coords(Rc::clone(&texture), uv);
                tiles.push(tile);
            }
        }
        tiles
    }
}

impl Bind for SubTexture {
    fn bind(&self, gl: &GL) {
        if let Some(texture) = &self.texture {
            texture.bind(gl);
        }
    }
    fn unbind(&self, gl: &GL) {
        gl.bind_texture(GL::TEXTURE_2D, None);
    }
}