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 3 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;
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod prelude {
bundle::SpriteBundle,
sprite::{ImageScaleMode, Sprite},
texture_atlas::{TextureAtlas, TextureAtlasLayout},
texture_slice::{BorderRect, SliceScaleMode, TextureSlice, TextureSlicer},
texture_slice::{SliceScaleMode, TextureSlice, TextureSlicer},
ColorMaterial, ColorMesh2dBundle, TextureAtlasBuilder,
};
}
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
5 changes: 2 additions & 3 deletions crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use bevy_ecs::{
world::Ref,
};
use bevy_hierarchy::{Children, Parent};
use bevy_math::{UVec2, Vec2};
use bevy_math::{Insets, UVec2, Vec2};
use bevy_render::camera::{Camera, NormalizedRenderTarget};
use bevy_sprite::BorderRect;
use bevy_transform::components::Transform;
use bevy_utils::tracing::warn;
use bevy_window::{PrimaryWindow, Window, WindowScaleFactorChanged};
Expand Down Expand Up @@ -346,7 +345,7 @@ pub fn ui_layout_system(
node.unrounded_size = layout_size;
}

node.bypass_change_detection().border = BorderRect {
node.bypass_change_detection().border = Insets {
left: layout.border.left * inverse_target_scale_factor,
right: layout.border.right * inverse_target_scale_factor,
top: layout.border.top * inverse_target_scale_factor,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub mod prelude {
widget::Label, Interaction, UiMaterialPlugin, UiScale,
},
// `bevy_sprite` re-exports for texture slicing
bevy_sprite::{BorderRect, ImageScaleMode, SliceScaleMode, TextureSlicer},
bevy_sprite::{ImageScaleMode, SliceScaleMode, TextureSlicer},
};
}

Expand Down
14 changes: 8 additions & 6 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use bevy_core_pipeline::core_3d::graph::{Core3d, Node3d};
use bevy_core_pipeline::{core_2d::Camera2d, core_3d::Camera3d};
use bevy_ecs::entity::{EntityHashMap, EntityHashSet};
use bevy_ecs::prelude::*;
use bevy_math::{FloatOrd, Mat4, Rect, URect, UVec4, Vec2, Vec3, Vec3Swizzles, Vec4Swizzles};
use bevy_math::{
FloatOrd, Insets, Mat4, Rect, URect, UVec4, Vec2, Vec3, Vec3Swizzles, Vec4Swizzles,
};
use bevy_render::render_phase::ViewSortedRenderPhases;
use bevy_render::texture::TRANSPARENT_IMAGE_HANDLE;
use bevy_render::{
Expand All @@ -36,7 +38,7 @@ use bevy_render::{
ExtractSchedule, Render,
};
use bevy_sprite::TextureAtlasLayout;
use bevy_sprite::{BorderRect, ImageScaleMode, SpriteAssetEvents, TextureAtlas};
use bevy_sprite::{ImageScaleMode, SpriteAssetEvents, TextureAtlas};
#[cfg(feature = "bevy_text")]
use bevy_text::PositionedGlyph;
#[cfg(feature = "bevy_text")]
Expand Down Expand Up @@ -179,7 +181,7 @@ pub struct ExtractedUiNode {
/// Border radius of the UI node.
pub border_radius: ResolvedBorderRadius,
/// Border thickness of the UI node.
pub border: BorderRect,
pub border: Insets,
pub node_type: NodeType,
}

Expand Down Expand Up @@ -365,7 +367,7 @@ pub fn extract_uinode_borders(
}

// don't extract border if no border or the node is zero-sized (a zero sized node can still have an outline).
if !uinode.is_empty() && uinode.border() != BorderRect::ZERO {
if !uinode.is_empty() && uinode.border() != Insets::ZERO {
if let Some(border_color) = maybe_border_color {
extracted_uinodes.uinodes.insert(
commands.spawn_empty().id(),
Expand Down Expand Up @@ -409,7 +411,7 @@ pub fn extract_uinode_borders(
flip_x: false,
flip_y: false,
camera_entity,
border: BorderRect::square(uinode.outline_width()),
border: Insets::square(uinode.outline_width()),
border_radius: uinode.outline_radius(),
node_type: NodeType::Border,
},
Expand Down Expand Up @@ -595,7 +597,7 @@ pub fn extract_uinode_text(
flip_x: false,
flip_y: false,
camera_entity,
border: BorderRect::ZERO,
border: Insets::ZERO,
border_radius: ResolvedBorderRadius::ZERO,
node_type: NodeType::Rect,
},
Expand Down
9 changes: 4 additions & 5 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use crate::{UiRect, Val};
use bevy_asset::Handle;
use bevy_color::Color;
use bevy_ecs::{prelude::*, system::SystemParam};
use bevy_math::{vec4, Rect, Vec2, Vec4Swizzles};
use bevy_math::{vec4, Insets, Rect, Vec2, Vec4Swizzles};
use bevy_reflect::prelude::*;
use bevy_render::{
camera::{Camera, RenderTarget},
texture::{Image, TRANSPARENT_IMAGE_HANDLE},
};
use bevy_sprite::BorderRect;
use bevy_utils::warn_once;
use bevy_window::{PrimaryWindow, WindowRef};
use core::num::NonZero;
Expand Down Expand Up @@ -52,7 +51,7 @@ pub struct Node {
/// Border updates bypass change detection.
///
/// Automatically calculated by [`super::layout::ui_layout_system`].
pub(crate) border: BorderRect,
pub(crate) border: Insets,
/// Resolved border radius values in logical pixels.
/// Border radius updates bypass change detection.
///
Expand Down Expand Up @@ -142,7 +141,7 @@ impl Node {
///
/// Automatically calculated by [`super::layout::ui_layout_system`].
#[inline]
pub fn border(&self) -> BorderRect {
pub fn border(&self) -> Insets {
self.border
}

Expand Down Expand Up @@ -185,7 +184,7 @@ impl Node {
outline_offset: 0.,
unrounded_size: Vec2::ZERO,
border_radius: ResolvedBorderRadius::ZERO,
border: BorderRect::ZERO,
border: Insets::ZERO,
};
}

Expand Down
8 changes: 4 additions & 4 deletions examples/2d/sprite_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn spawn_sprites(
style.clone(),
Vec2::new(100.0, 200.0),
Some(ImageScaleMode::Sliced(TextureSlicer {
border: BorderRect::square(slice_border),
border: Insets::square(slice_border),
center_scale_mode: SliceScaleMode::Stretch,
..default()
})),
Expand All @@ -39,7 +39,7 @@ fn spawn_sprites(
style.clone(),
Vec2::new(100.0, 200.0),
Some(ImageScaleMode::Sliced(TextureSlicer {
border: BorderRect::square(slice_border),
border: Insets::square(slice_border),
center_scale_mode: SliceScaleMode::Tile { stretch_value: 0.5 },
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 0.2 },
..default()
Expand All @@ -51,7 +51,7 @@ fn spawn_sprites(
style.clone(),
Vec2::new(300.0, 200.0),
Some(ImageScaleMode::Sliced(TextureSlicer {
border: BorderRect::square(slice_border),
border: Insets::square(slice_border),
center_scale_mode: SliceScaleMode::Tile { stretch_value: 0.2 },
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 0.3 },
..default()
Expand All @@ -63,7 +63,7 @@ fn spawn_sprites(
style,
Vec2::new(300.0, 200.0),
Some(ImageScaleMode::Sliced(TextureSlicer {
border: BorderRect::square(slice_border),
border: Insets::square(slice_border),
center_scale_mode: SliceScaleMode::Tile { stretch_value: 0.1 },
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 0.2 },
max_corner_scale: 0.2,
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui_texture_atlas_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn setup(
let atlas_layout_handle = texture_atlases.add(atlas_layout);

let slicer = TextureSlicer {
border: BorderRect::square(24.0),
border: Insets::square(24.0),
center_scale_mode: SliceScaleMode::Stretch,
sides_scale_mode: SliceScaleMode::Stretch,
max_corner_scale: 1.0,
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui_texture_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let image = asset_server.load("textures/fantasy_ui_borders/panel-border-010.png");

let slicer = TextureSlicer {
border: BorderRect::square(22.0),
border: Insets::square(22.0),
center_scale_mode: SliceScaleMode::Stretch,
sides_scale_mode: SliceScaleMode::Stretch,
max_corner_scale: 1.0,
Expand Down
4 changes: 2 additions & 2 deletions examples/ui/ui_texture_slice_flip_and_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
);

let slicer = TextureSlicer {
// `numbered_slices.png` is 48 pixels square. `BorderRect::square(16.)` insets the slicing line from each edge by 16 pixels, resulting in nine slices that are each 16 pixels square.
border: BorderRect::square(16.),
// `numbered_slices.png` is 48 pixels square. `Insets::square(16.)` insets the slicing line from each edge by 16 pixels, resulting in nine slices that are each 16 pixels square.
border: Insets::square(16.),
// With `SliceScaleMode::Tile` the side and center slices are tiled to to fill the side and center sections of the target.
// And with a `stretch_value` of `1.` the tiles will have the same size as the corresponding slices in the source image.
center_scale_mode: SliceScaleMode::Tile { stretch_value: 1. },
Expand Down