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
use crate::gui::layout::{Origin, Position, Rect};
use crate::texture::TextureUV;
use crate::vao::{VaoBuffer, VertexWithNormUv};

/// [`VaoBuffer`]上にGUIを追加する
///
/// * `T` - テクスチャの型
pub trait VaoBuilder2DGui<T> {
    fn add_rectangle(&mut self, texture: &T, dst: &Rect<i32, u32>);
    fn add_layout_rectangle(
        &mut self,
        texture: &T,
        parent_width: u32,
        parent_height: u32,
        origin: &Origin,
        position_x: &Position<i32>,
        position_y: &Position<i32>,
        inner_width: u32,
        inner_height: u32,
    );
    fn add_biggest_rectangle(
        &mut self,
        texture: &T,
        parent_width: u32,
        parent_height: u32,
        inner_width: u32,
        inner_height: u32,
    );
}

impl<Width, Height, AtlasWidth, AtlasHeight>
    VaoBuilder2DGui<TextureUV<Width, Height, AtlasWidth, AtlasHeight>> for VaoBuffer<VertexWithNormUv>
{
    fn add_rectangle(
        &mut self,
        texture: &TextureUV<Width, Height, AtlasWidth, AtlasHeight>,
        dst: &Rect<i32, u32>,
    ) {
        let x = *dst.origin_x() as f32;
        let y = *dst.origin_y() as f32;
        let w = *dst.width() as f32;
        let h = *dst.height() as f32;

        #[rustfmt::skip]
        let mut vert: Vec<f32> = vec![
            x  , y  , 0.0,  0.0, 0.0, 1.0,  texture.begin_u, texture.begin_v,
            x  , y+h, 0.0,  0.0, 0.0, 1.0,  texture.begin_u, texture.end_v  ,
            x+w, y+h, 0.0,  0.0, 0.0, 1.0,  texture.end_u  , texture.end_v  ,

            x  , y  , 0.0,  0.0, 0.0, 1.0,  texture.begin_u, texture.begin_v,
            x+w, y+h, 0.0,  0.0, 0.0, 1.0,  texture.end_u  , texture.end_v  ,
            x+w, y  , 0.0,  0.0, 0.0, 1.0,  texture.end_u  , texture.begin_v,
        ];
        self.append(&mut vert);
    }

    fn add_layout_rectangle(
        &mut self,
        texture: &TextureUV<Width, Height, AtlasWidth, AtlasHeight>,
        parent_width: u32,
        parent_height: u32,
        origin: &Origin,
        position_x: &Position<i32>,
        position_y: &Position<i32>,
        inner_width: u32,
        inner_height: u32,
    ) {
        let dst = Rect::new_in_rect(
            &Rect::new(0_i32, 0_i32, parent_width, parent_height),
            origin,
            position_x,
            position_y,
            inner_width,
            inner_height,
        );
        self.add_rectangle(texture, &dst);
    }

    fn add_biggest_rectangle(
        &mut self,
        texture: &TextureUV<Width, Height, AtlasWidth, AtlasHeight>,
        parent_width: u32,
        parent_height: u32,
        inner_width: u32,
        inner_height: u32,
    ) {
        let dst = Rect::new_biggest_in_rect(
            &Rect::new(0_i32, 0_i32, parent_width, parent_height),
            inner_width,
            inner_height,
        );
        self.add_rectangle(texture, &dst);
    }
}