Path: blob/main/crates/bevy_sprite/src/texture_slice/border_rect.rs
9367 views
use bevy_math::Vec2;1use bevy_reflect::{std_traits::ReflectDefault, Reflect};23/// Defines border insets that shrink a rectangle from its minimum and maximum corners.4///5/// This struct is used to represent thickness or offsets from the four edges6/// of a rectangle, with values increasing inwards.7#[derive(Default, Copy, Clone, PartialEq, Debug, Reflect)]8#[reflect(Clone, PartialEq, Default)]9pub struct BorderRect {10/// Inset applied to the rectangle’s minimum corner11pub min_inset: Vec2,12/// Inset applied to the rectangle’s maximum corner13pub max_inset: Vec2,14}1516impl BorderRect {17/// An empty border with zero thickness along each edge18pub const ZERO: Self = Self::all(0.);1920/// Creates a border with the same `inset` along each edge21#[must_use]22#[inline]23pub const fn all(inset: f32) -> Self {24Self {25min_inset: Vec2::splat(inset),26max_inset: Vec2::splat(inset),27}28}2930/// Creates a new border with the `min.x` and `max.x` insets equal to `horizontal`, and the `min.y` and `max.y` insets equal to `vertical`.31#[must_use]32#[inline]33pub const fn axes(horizontal: f32, vertical: f32) -> Self {34let insets = Vec2::new(horizontal, vertical);35Self {36min_inset: insets,37max_inset: insets,38}39}40}4142impl From<f32> for BorderRect {43fn from(inset: f32) -> Self {44Self::all(inset)45}46}4748impl From<[f32; 4]> for BorderRect {49fn from([min_x, max_x, min_y, max_y]: [f32; 4]) -> Self {50Self {51min_inset: Vec2::new(min_x, min_y),52max_inset: Vec2::new(max_x, max_y),53}54}55}5657impl core::ops::Add for BorderRect {58type Output = Self;5960fn add(mut self, rhs: Self) -> Self::Output {61self.min_inset += rhs.min_inset;62self.max_inset += rhs.max_inset;63self64}65}6667impl core::ops::Sub for BorderRect {68type Output = Self;6970fn sub(mut self, rhs: Self) -> Self::Output {71self.min_inset -= rhs.min_inset;72self.max_inset -= rhs.max_inset;73self74}75}7677impl core::ops::Mul<f32> for BorderRect {78type Output = Self;7980fn mul(mut self, rhs: f32) -> Self::Output {81self.min_inset *= rhs;82self.max_inset *= rhs;83self84}85}8687impl core::ops::Div<f32> for BorderRect {88type Output = Self;8990fn div(mut self, rhs: f32) -> Self::Output {91self.min_inset /= rhs;92self.max_inset /= rhs;93self94}95}969798