Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename BorderRect to Insets and move to bevy_math #15461

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub mod prelude {
direction::{Dir2, Dir3, Dir3A},
ops,
primitives::*,
BVec2, BVec3, BVec4, EulerRot, FloatExt, IRect, IVec2, IVec3, IVec4, Isometry2d,
BVec2, BVec3, BVec4, EulerRot, FloatExt, IRect, IVec2, IVec3, IVec4, Insets, Isometry2d,
Isometry3d, Mat2, Mat3, Mat4, Quat, Ray2d, Ray3d, Rect, Rot2, StableInterpolate, URect,
UVec2, UVec3, UVec4, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use bevy_reflect::Reflect;

/// Struct defining a [`Sprite`](crate::Sprite) border with padding values
/// Struct defining insets relative to a rect, commonly used for defining visual components.
#[derive(Default, Copy, Clone, PartialEq, Debug, Reflect)]
pub struct BorderRect {
/// Pixel padding to the left
pub struct Insets {
/// Left inset
pub left: f32,
/// Pixel padding to the right
/// Right inset
pub right: f32,
/// Pixel padding to the top
/// Top inset
pub top: f32,
/// Pixel padding to the bottom
/// Bottom inset
pub bottom: f32,
}

impl BorderRect {
/// An empty border with zero padding values in each direction
impl Insets {
/// An empty inset
pub const ZERO: Self = Self::square(0.);

/// Creates a new border as a square, with identical pixel padding values on every direction
/// Creates identical insets in every direction
#[must_use]
#[inline]
pub const fn square(value: f32) -> Self {
Expand All @@ -29,9 +29,9 @@ impl BorderRect {
}
}

/// Creates a new border as a rectangle, with:
/// - `horizontal` for left and right pixel padding
/// - `vertical` for top and bottom pixel padding
/// Creates insets with:
/// - `horizontal` for left and right insets
/// - `vertical` for top and bottom insets
#[must_use]
#[inline]
pub const fn rectangle(horizontal: f32, vertical: f32) -> Self {
Expand All @@ -44,13 +44,13 @@ impl BorderRect {
}
}

impl From<f32> for BorderRect {
impl From<f32> for Insets {
fn from(v: f32) -> Self {
Self::square(v)
}
}

impl From<[f32; 4]> for BorderRect {
impl From<[f32; 4]> for Insets {
fn from([left, right, top, bottom]: [f32; 4]) -> Self {
Self {
left,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_math/src/rects/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod insets;
mod irect;
mod rect;
mod urect;

pub use insets::Insets;
pub use irect::IRect;
pub use rect::Rect;
pub use urect::URect;
21 changes: 12 additions & 9 deletions crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ mod texture_slice;
/// The sprite prelude.
///
/// This includes the most common types in this crate, re-exported for your convenience.
#[expect(deprecated)]
pub mod prelude {
#[doc(hidden)]
pub use crate::{
bundle::SpriteBundle,
sprite::{ImageScaleMode, Sprite},
texture_atlas::{TextureAtlas, TextureAtlasLayout, TextureAtlasSources},
texture_slice::{BorderRect, SliceScaleMode, TextureSlice, TextureSlicer},
ColorMaterial, ColorMesh2dBundle, MeshMaterial2d, TextureAtlasBuilder,
texture_atlas::{TextureAtlas, TextureAtlasLayout},
texture_slice::{SliceScaleMode, TextureSlice, TextureSlicer},
ColorMaterial, ColorMesh2dBundle, TextureAtlasBuilder,
};
}

Expand All @@ -53,7 +52,7 @@ use bevy_core_pipeline::core_2d::Transparent2d;
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
mesh::{Mesh, Mesh2d},
mesh::Mesh,
primitives::Aabb,
render_phase::AddRenderCommand,
render_resource::{Shader, SpecializedRenderPipelines},
Expand Down Expand Up @@ -85,6 +84,10 @@ pub enum SpriteSystem {
#[reflect(Component, Default, Debug)]
pub struct SpriteSource;

/// A convenient alias for `With<Mesh2dHandle>>`, for use with
/// [`bevy_render::view::VisibleEntities`].
pub type WithMesh2d = With<Mesh2dHandle>;

/// A convenient alias for `Or<With<Sprite>, With<SpriteSource>>`, for use with
/// [`bevy_render::view::VisibleEntities`].
pub type WithSprite = Or<(With<Sprite>, With<SpriteSource>)>;
Expand All @@ -110,7 +113,7 @@ impl Plugin for SpritePlugin {
.register_type::<TextureSlicer>()
.register_type::<Anchor>()
.register_type::<TextureAtlas>()
.register_type::<Mesh2d>()
.register_type::<Mesh2dHandle>()
.register_type::<SpriteSource>()
.add_plugins((
Mesh2dRenderPlugin,
Expand All @@ -127,7 +130,7 @@ impl Plugin for SpritePlugin {
)
.in_set(SpriteSystem::ComputeSlices),
(
check_visibility::<With<Mesh2d>>,
check_visibility::<WithMesh2d>,
check_visibility::<WithSprite>,
)
.in_set(VisibilitySystems::CheckVisibility),
Expand Down Expand Up @@ -173,7 +176,7 @@ impl Plugin for SpritePlugin {
}

/// System calculating and inserting an [`Aabb`] component to entities with either:
/// - a `Mesh2d` component,
/// - a `Mesh2dHandle` component,
/// - a `Sprite` and `Handle<Image>` components,
/// and without a [`NoFrustumCulling`] component.
///
Expand All @@ -183,7 +186,7 @@ pub fn calculate_bounds_2d(
meshes: Res<Assets<Mesh>>,
images: Res<Assets<Image>>,
atlases: Res<Assets<TextureAtlasLayout>>,
meshes_without_aabb: Query<(Entity, &Mesh2d), (Without<Aabb>, Without<NoFrustumCulling>)>,
meshes_without_aabb: Query<(Entity, &Mesh2dHandle), (Without<Aabb>, Without<NoFrustumCulling>)>,
sprites_to_recalculate_aabb: Query<
(Entity, &Sprite, &Handle<Image>, Option<&TextureAtlas>),
(
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_sprite/src/texture_slice/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod border_rect;
mod computed_slices;
mod slicer;

use bevy_math::{Rect, Vec2};
pub use border_rect::BorderRect;
pub use slicer::{SliceScaleMode, TextureSlicer};

pub(crate) use computed_slices::{
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_sprite/src/texture_slice/slicer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{BorderRect, TextureSlice};
use bevy_math::{vec2, Rect, Vec2};
use super::TextureSlice;
use bevy_math::{vec2, Insets, Rect, Vec2};
use bevy_reflect::Reflect;

/// Slices a texture using the **9-slicing** technique. This allows to reuse an image at various sizes
Expand All @@ -13,7 +13,7 @@ use bevy_reflect::Reflect;
#[derive(Debug, Clone, Reflect)]
pub struct TextureSlicer {
/// The sprite borders, defining the 9 sections of the image
pub border: BorderRect,
pub border: Insets,
/// Defines how the center part of the 9 slices will scale
pub center_scale_mode: SliceScaleMode,
/// Defines how the 4 side parts of the 9 slices will scale
Expand Down Expand Up @@ -48,7 +48,7 @@ impl TextureSlicer {
#[must_use]
fn corner_slices(&self, base_rect: Rect, render_size: Vec2) -> [TextureSlice; 4] {
let coef = render_size / base_rect.size();
let BorderRect {
let Insets {
left,
right,
top,
Expand Down Expand Up @@ -294,7 +294,7 @@ mod test {
#[test]
fn test_horizontal_sizes_uniform() {
let slicer = TextureSlicer {
border: BorderRect {
border: Insets {
left: 10.,
right: 10.,
top: 10.,
Expand Down Expand Up @@ -326,7 +326,7 @@ mod test {
#[test]
fn test_horizontal_sizes_non_uniform_bigger() {
let slicer = TextureSlicer {
border: BorderRect {
border: Insets {
left: 20.,
right: 10.,
top: 10.,
Expand Down Expand Up @@ -358,7 +358,7 @@ mod test {
#[test]
fn test_horizontal_sizes_non_uniform_smaller() {
let slicer = TextureSlicer {
border: BorderRect {
border: Insets {
left: 5.,
right: 10.,
top: 10.,
Expand Down Expand Up @@ -403,7 +403,7 @@ mod test {
#[test]
fn test_horizontal_sizes_non_uniform_zero() {
let slicer = TextureSlicer {
border: BorderRect {
border: Insets {
left: 0.,
right: 10.,
top: 10.,
Expand Down
Loading
Loading