bevy_sprite/texture_slice/
border_rect.rs

1use bevy_reflect::{std_traits::ReflectDefault, Reflect};
2
3/// Defines the extents of the border of a rectangle.
4///
5/// This struct is used to represent thickness or offsets from the edges
6/// of a rectangle (left, right, top, and bottom), with values increasing inwards.
7#[derive(Default, Copy, Clone, PartialEq, Debug, Reflect)]
8#[reflect(Clone, PartialEq, Default)]
9pub struct BorderRect {
10    /// Extent of the border along the left edge
11    pub left: f32,
12    /// Extent of the border along the right edge
13    pub right: f32,
14    /// Extent of the border along the top edge
15    pub top: f32,
16    /// Extent of the border along the bottom edge
17    pub bottom: f32,
18}
19
20impl BorderRect {
21    /// An empty border with zero thickness along each edge
22    pub const ZERO: Self = Self::all(0.);
23
24    /// Creates a border with the same `extent` along each edge
25    #[must_use]
26    #[inline]
27    pub const fn all(extent: f32) -> Self {
28        Self {
29            left: extent,
30            right: extent,
31            top: extent,
32            bottom: extent,
33        }
34    }
35
36    /// Creates a new border with the `left` and `right` extents equal to `horizontal`, and `top` and `bottom` extents equal to `vertical`.
37    #[must_use]
38    #[inline]
39    pub const fn axes(horizontal: f32, vertical: f32) -> Self {
40        Self {
41            left: horizontal,
42            right: horizontal,
43            top: vertical,
44            bottom: vertical,
45        }
46    }
47}
48
49impl From<f32> for BorderRect {
50    fn from(extent: f32) -> Self {
51        Self::all(extent)
52    }
53}
54
55impl From<[f32; 4]> for BorderRect {
56    fn from([left, right, top, bottom]: [f32; 4]) -> Self {
57        Self {
58            left,
59            right,
60            top,
61            bottom,
62        }
63    }
64}
65
66impl core::ops::Add for BorderRect {
67    type Output = Self;
68
69    fn add(mut self, rhs: Self) -> Self::Output {
70        self.left += rhs.left;
71        self.right += rhs.right;
72        self.top += rhs.top;
73        self.bottom += rhs.bottom;
74        self
75    }
76}
77
78impl core::ops::Sub for BorderRect {
79    type Output = Self;
80
81    fn sub(mut self, rhs: Self) -> Self::Output {
82        self.left -= rhs.left;
83        self.right -= rhs.right;
84        self.top -= rhs.top;
85        self.bottom -= rhs.bottom;
86        self
87    }
88}
89
90impl core::ops::Mul<f32> for BorderRect {
91    type Output = Self;
92
93    fn mul(mut self, rhs: f32) -> Self::Output {
94        self.left *= rhs;
95        self.right *= rhs;
96        self.top *= rhs;
97        self.bottom *= rhs;
98        self
99    }
100}
101
102impl core::ops::Div<f32> for BorderRect {
103    type Output = Self;
104
105    fn div(mut self, rhs: f32) -> Self::Output {
106        self.left /= rhs;
107        self.right /= rhs;
108        self.top /= rhs;
109        self.bottom /= rhs;
110        self
111    }
112}