bowtie/shapes/
rectangle.rs

1use crate::general::color::Color;
2
3use super::shape::Shape;
4
5#[derive(Debug, Copy, Clone)]
6/// Rectangle `Shape`
7pub struct Rectangle {
8  pub width: f32,
9  pub height: f32,
10  pub x: f32,
11  pub y: f32,
12  pub color: Color,
13  texture_corners: [[f32; 2]; 4],
14}
15
16impl Rectangle {
17  pub fn new(
18    x: f32,
19    y: f32,
20    width: f32,
21    height: f32,
22    color: Color,
23  ) -> Rectangle {
24    Rectangle {
25      x,
26      y,
27      width,
28      height,
29      color,
30      texture_corners: [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]],
31    }
32  }
33}
34
35impl Shape for Rectangle {
36  fn get_x(&self) -> f32 {
37    self.x
38  }
39
40  fn get_y(&self) -> f32 {
41    self.y
42  }
43
44  fn set_x(&mut self, x: f32) {
45    self.x = x;
46  }
47
48  fn set_y(&mut self, y: f32) {
49    self.y = y;
50  }
51
52  fn get_width(&self) -> f32 {
53    self.width
54  }
55
56  fn get_height(&self) -> f32 {
57    self.height
58  }
59
60  fn set_height(&mut self, height: f32) {
61    self.height = height;
62  }
63
64  fn set_width(&mut self, width: f32) {
65    self.width = width;
66  }
67
68  fn get_color(&self) -> Color {
69    self.color
70  }
71
72  fn set_color(&mut self, color: Color) {
73    self.color = color;
74  }
75
76  fn get_texture_corners(&self) -> [[f32; 2]; 4] {
77    self.texture_corners
78  }
79
80  fn flip_texture_corners_x(&mut self) {
81    let mut new_texture_corners = self.texture_corners.to_owned();
82
83    for row in 0..4 {
84      new_texture_corners[row][0] = 1.0 - self.texture_corners[row][0];
85    }
86    self.texture_corners = new_texture_corners;
87  }
88
89  fn flip_texture_corners_y(&mut self) {
90    let mut new_texture_corners = self.texture_corners.to_owned();
91
92    for row in 0..4 {
93      new_texture_corners[row][1] = 1.0 - self.texture_corners[row][1];
94    }
95    self.texture_corners = new_texture_corners;
96  }
97
98  fn get_coordinate_corners(&self) -> [[f32; 2]; 4] {
99    [
100      [self.x, self.y],
101      [self.x + self.width, self.y],
102      [self.x + self.width, self.y - self.height],
103      [self.x, self.y - self.height],
104    ]
105  }
106}