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
use super::*;

pub struct Quad {
    pub transform: mat3<f32>,
    pub color: Rgba<f32>,
}

impl Quad {
    pub fn new(aabb: Aabb2<f32>, color: Rgba<f32>) -> Self {
        Self::unit(color).transform(mat3::translate(aabb.center()) * mat3::scale(aabb.size() / 2.0))
    }
    pub fn unit(color: Rgba<f32>) -> Self {
        Self {
            transform: mat3::identity(),
            color,
        }
    }
}

impl Draw2d for Quad {
    fn draw2d_transformed(
        &self,
        helper: &Helper,
        framebuffer: &mut ugli::Framebuffer,
        camera: &dyn AbstractCamera2d,
        transform: mat3<f32>,
    ) {
        let framebuffer_size = framebuffer.size();
        ugli::draw(
            framebuffer,
            &helper.color_program,
            ugli::DrawMode::TriangleFan,
            &helper.unit_quad_geometry,
            (
                ugli::uniforms! {
                    u_color: self.color,
                    u_framebuffer_size: framebuffer_size,
                    u_model_matrix: transform * self.transform,
                },
                camera.uniforms(framebuffer_size.map(|x| x as f32)),
            ),
            ugli::DrawParameters {
                blend_mode: Some(ugli::BlendMode::straight_alpha()),
                ..Default::default()
            },
        );
    }
}

impl Transform2d<f32> for Quad {
    fn bounding_quad(&self) -> batbox_lapp::Quad<f32> {
        batbox_lapp::Quad {
            transform: self.transform,
        }
    }
    fn apply_transform(&mut self, transform: mat3<f32>) {
        self.transform = transform * self.transform;
    }
}

pub struct TexturedQuad<T: std::borrow::Borrow<ugli::Texture>> {
    transform: mat3<f32>,
    texture: T,
    texture_transform: mat3<f32>,
    color: Rgba<f32>,
}

impl<T: std::borrow::Borrow<ugli::Texture>> TexturedQuad<T> {
    pub fn new(aabb: Aabb2<f32>, texture: T) -> Self {
        Self::colored(aabb, texture, Rgba::WHITE)
    }
    pub fn colored(aabb: Aabb2<f32>, texture: T, color: Rgba<f32>) -> Self {
        Self::unit_colored(texture, color)
            .transform(mat3::translate(aabb.center()) * mat3::scale(aabb.size() / 2.0))
    }
    pub fn unit(texture: T) -> Self {
        Self::unit_colored(texture, Rgba::WHITE)
    }
    pub fn unit_colored(texture: T, color: Rgba<f32>) -> Self {
        Self {
            transform: mat3::identity(),
            texture_transform: mat3::identity(),
            texture,
            color,
        }
    }
    pub fn sub_texture(self, aabb: Aabb2<f32>) -> Self {
        Self {
            texture_transform: mat3::translate(aabb.bottom_left()) * mat3::scale(aabb.size()),
            ..self
        }
    }
}

impl<T: std::borrow::Borrow<ugli::Texture>> Draw2d for TexturedQuad<T> {
    fn draw2d_transformed(
        &self,
        helper: &Helper,
        framebuffer: &mut ugli::Framebuffer,
        camera: &dyn AbstractCamera2d,
        transform: mat3<f32>,
    ) {
        let framebuffer_size = framebuffer.size();
        ugli::draw(
            framebuffer,
            &helper.textured_program,
            ugli::DrawMode::TriangleFan,
            &helper.unit_quad_geometry,
            (
                ugli::uniforms! {
                    u_color: self.color,
                    u_texture: self.texture.borrow(),
                    u_framebuffer_size: framebuffer_size,
                    u_model_matrix: transform * self.transform,
                    u_texture_matrix: self.texture_transform,
                },
                camera.uniforms(framebuffer_size.map(|x| x as f32)),
            ),
            ugli::DrawParameters {
                blend_mode: Some(ugli::BlendMode::straight_alpha()),
                ..Default::default()
            },
        );
    }
}

impl<T: std::borrow::Borrow<ugli::Texture>> Transform2d<f32> for TexturedQuad<T> {
    fn bounding_quad(&self) -> batbox_lapp::Quad<f32> {
        batbox_lapp::Quad {
            transform: self.transform,
        }
    }
    fn apply_transform(&mut self, transform: mat3<f32>) {
        self.transform = transform * self.transform;
    }
}