Skip to main content

wgpu_shapes/shape/
image.rs

1use crate::render::instance::TextureInstance;
2
3pub struct Image {
4    pub(crate) scale: (f32, f32),
5    pub(crate) pos: (f32, f32),
6    pub(crate) rotation: f32,
7    pub(crate) layer: u16,
8    pub(crate) texture_pos: (f32, f32),
9    pub(crate) texture_scale: (f32, f32),
10}
11
12impl Image {
13    /// size of the shape
14    pub fn scale(&mut self, width: f32, height: f32) -> &mut Self {
15        self.scale = (width, height);
16        self
17    }
18
19    /// location of the shape in the frame
20    pub fn pos(&mut self, x: f32, y: f32) -> &mut Self {
21        self.pos = (x, y);
22        self
23    }
24
25    /// rotation of the shape in radians
26    pub fn rotation(&mut self, rotation: f32) -> &mut Self {
27        self.rotation = rotation;
28        self
29    }
30
31    /// render layer of the shape
32    ///
33    /// higher layer -> foreground
34    pub fn layer(&mut self, layer: u16) -> &mut Self {
35        self.layer = layer;
36        self
37    }
38
39    pub fn to_instance(&self) -> TextureInstance {
40        TextureInstance {
41            position: [self.pos.0, self.pos.1],
42            scale: [self.scale.0, self.scale.1],
43            rotation: self.rotation,
44            layer: self.layer as u32,
45            texture_position: [self.texture_pos.0, self.texture_pos.1],
46            texture_scale: [self.texture_scale.0, self.texture_scale.1],
47        }
48    }
49}
50
51impl Default for Image {
52    fn default() -> Self {
53        Image {
54            scale: (20.0, 20.0),
55            pos: (0.0, 0.0),
56            rotation: 0.0,
57            layer: 0,
58            texture_pos: (0.0, 0.0),
59            texture_scale: (1.0, 1.0),
60        }
61    }
62}