reverie_engine_opengl/gui/
vao_builder.rs

1use crate::gui::layout::{Origin, Position, Rect};
2use crate::texture::TextureUV;
3use crate::vao::{VaoBuffer, VertexWithNormUv};
4
5/// [`VaoBuffer`]上にGUIを追加する
6///
7/// * `T` - テクスチャの型
8pub trait VaoBuilder2DGui<T> {
9    fn add_rectangle(&mut self, texture: &T, dst: &Rect<i32, u32>);
10    #[allow(clippy::too_many_arguments)]
11    fn add_layout_rectangle(
12        &mut self,
13        texture: &T,
14        parent_width: u32,
15        parent_height: u32,
16        origin: &Origin,
17        position_x: &Position<i32>,
18        position_y: &Position<i32>,
19        inner_width: u32,
20        inner_height: u32,
21    );
22    fn add_biggest_rectangle(
23        &mut self,
24        texture: &T,
25        parent_width: u32,
26        parent_height: u32,
27        inner_width: u32,
28        inner_height: u32,
29    );
30}
31
32impl<Width, Height, AtlasWidth, AtlasHeight>
33    VaoBuilder2DGui<TextureUV<Width, Height, AtlasWidth, AtlasHeight>> for VaoBuffer<VertexWithNormUv>
34{
35    fn add_rectangle(
36        &mut self,
37        texture: &TextureUV<Width, Height, AtlasWidth, AtlasHeight>,
38        dst: &Rect<i32, u32>,
39    ) {
40        let x = *dst.origin_x() as f32;
41        let y = *dst.origin_y() as f32;
42        let w = *dst.width() as f32;
43        let h = *dst.height() as f32;
44
45        #[rustfmt::skip]
46        let mut vert: Vec<f32> = vec![
47            x  , y  , 0.0,  0.0, 0.0, 1.0,  texture.begin_u, texture.begin_v,
48            x  , y+h, 0.0,  0.0, 0.0, 1.0,  texture.begin_u, texture.end_v  ,
49            x+w, y+h, 0.0,  0.0, 0.0, 1.0,  texture.end_u  , texture.end_v  ,
50
51            x  , y  , 0.0,  0.0, 0.0, 1.0,  texture.begin_u, texture.begin_v,
52            x+w, y+h, 0.0,  0.0, 0.0, 1.0,  texture.end_u  , texture.end_v  ,
53            x+w, y  , 0.0,  0.0, 0.0, 1.0,  texture.end_u  , texture.begin_v,
54        ];
55        self.append(&mut vert);
56    }
57
58    fn add_layout_rectangle(
59        &mut self,
60        texture: &TextureUV<Width, Height, AtlasWidth, AtlasHeight>,
61        parent_width: u32,
62        parent_height: u32,
63        origin: &Origin,
64        position_x: &Position<i32>,
65        position_y: &Position<i32>,
66        inner_width: u32,
67        inner_height: u32,
68    ) {
69        let dst = Rect::new_in_rect(
70            &Rect::new(0_i32, 0_i32, parent_width, parent_height),
71            origin,
72            position_x,
73            position_y,
74            inner_width,
75            inner_height,
76        );
77        self.add_rectangle(texture, &dst);
78    }
79
80    fn add_biggest_rectangle(
81        &mut self,
82        texture: &TextureUV<Width, Height, AtlasWidth, AtlasHeight>,
83        parent_width: u32,
84        parent_height: u32,
85        inner_width: u32,
86        inner_height: u32,
87    ) {
88        let dst = Rect::new_biggest_in_rect(
89            &Rect::new(0_i32, 0_i32, parent_width, parent_height),
90            inner_width,
91            inner_height,
92        );
93        self.add_rectangle(texture, &dst);
94    }
95}