bevy_sprite/texture_slice/
border_rect.rs

1use bevy_reflect::Reflect;
2
3/// Struct defining a [`Sprite`](crate::Sprite) border with padding values
4#[derive(Default, Copy, Clone, PartialEq, Debug, Reflect)]
5pub struct BorderRect {
6    /// Pixel padding to the left
7    pub left: f32,
8    /// Pixel padding to the right
9    pub right: f32,
10    /// Pixel padding to the top
11    pub top: f32,
12    /// Pixel padding to the bottom
13    pub bottom: f32,
14}
15
16impl BorderRect {
17    /// An empty border with zero padding values in each direction
18    pub const ZERO: Self = Self::square(0.);
19
20    /// Creates a new border as a square, with identical pixel padding values on every direction
21    #[must_use]
22    #[inline]
23    pub const fn square(value: f32) -> Self {
24        Self {
25            left: value,
26            right: value,
27            top: value,
28            bottom: value,
29        }
30    }
31
32    /// Creates a new border as a rectangle, with:
33    /// - `horizontal` for left and right pixel padding
34    /// - `vertical` for top and bottom pixel padding
35    #[must_use]
36    #[inline]
37    pub const fn rectangle(horizontal: f32, vertical: f32) -> Self {
38        Self {
39            left: horizontal,
40            right: horizontal,
41            top: vertical,
42            bottom: vertical,
43        }
44    }
45}
46
47impl From<f32> for BorderRect {
48    fn from(v: f32) -> Self {
49        Self::square(v)
50    }
51}
52
53impl From<[f32; 4]> for BorderRect {
54    fn from([left, right, top, bottom]: [f32; 4]) -> Self {
55        Self {
56            left,
57            right,
58            top,
59            bottom,
60        }
61    }
62}