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

fix asymmetrical 9-slicing #14148

Merged
merged 6 commits into from
Aug 1, 2024
Merged
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
81 changes: 36 additions & 45 deletions crates/bevy_sprite/src/texture_slice/slicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ impl TextureSlicer {
),
},
draw_size: vec2(
bl_corner.draw_size.x,
render_size.y - (bl_corner.draw_size.y + tl_corner.draw_size.y),
tl_corner.draw_size.x,
render_size.y - (tl_corner.draw_size.y + bl_corner.draw_size.y),
),
offset: vec2(
-render_size.x + bl_corner.draw_size.x,
tl_corner.draw_size.x - render_size.x,
bl_corner.draw_size.y - tl_corner.draw_size.y,
) / 2.0,
},
Expand All @@ -141,21 +141,21 @@ impl TextureSlicer {
base_rect.max.x - self.border.right,
base_rect.min.y + self.border.top,
),
max: vec2(base_rect.max.x, base_rect.max.y - self.border.bottom),
max: base_rect.max - vec2(0.0, self.border.bottom),
},
draw_size: vec2(
br_corner.draw_size.x,
render_size.y - (br_corner.draw_size.y + tr_corner.draw_size.y),
tr_corner.draw_size.x,
render_size.y - (tr_corner.draw_size.y + br_corner.draw_size.y),
),
offset: vec2(
render_size.x - br_corner.draw_size.x,
render_size.x - tr_corner.draw_size.x,
br_corner.draw_size.y - tr_corner.draw_size.y,
) / 2.0,
},
]
}

/// Computes the 2 vertical side slices (bottom and top borders)
/// Computes the 2 vertical side slices (top and bottom borders)
#[must_use]
fn vertical_side_slices(
&self,
Expand All @@ -164,24 +164,6 @@ impl TextureSlicer {
render_size: Vec2,
) -> [TextureSlice; 2] {
[
// Bottom
TextureSlice {
texture_rect: Rect {
min: vec2(
base_rect.min.x + self.border.left,
base_rect.max.y - self.border.bottom,
),
max: vec2(base_rect.max.x - self.border.right, base_rect.max.y),
},
draw_size: vec2(
render_size.x - (bl_corner.draw_size.x + br_corner.draw_size.x),
bl_corner.draw_size.y,
),
offset: vec2(
(bl_corner.draw_size.x - br_corner.draw_size.x) / 2.0,
bl_corner.offset.y,
),
},
// Top
TextureSlice {
texture_rect: Rect {
Expand All @@ -196,9 +178,27 @@ impl TextureSlicer {
tl_corner.draw_size.y,
),
offset: vec2(
(tl_corner.draw_size.x - tr_corner.draw_size.x) / 2.0,
tl_corner.offset.y,
tl_corner.draw_size.x - tr_corner.draw_size.x,
render_size.y - tl_corner.draw_size.y,
) / 2.0,
},
// Bottom
TextureSlice {
texture_rect: Rect {
min: vec2(
base_rect.min.x + self.border.left,
base_rect.max.y - self.border.bottom,
),
max: base_rect.max - vec2(self.border.right, 0.0),
},
draw_size: vec2(
render_size.x - (bl_corner.draw_size.x + br_corner.draw_size.x),
bl_corner.draw_size.y,
),
offset: vec2(
bl_corner.draw_size.x - br_corner.draw_size.x,
bl_corner.draw_size.y - render_size.y,
) / 2.0,
},
]
}
Expand All @@ -216,11 +216,8 @@ impl TextureSlicer {
#[must_use]
pub fn compute_slices(&self, rect: Rect, render_size: Option<Vec2>) -> Vec<TextureSlice> {
let render_size = render_size.unwrap_or_else(|| rect.size());
let rect_size = rect.size() / 2.0;
if self.border.left >= rect_size.x
|| self.border.right >= rect_size.x
|| self.border.top >= rect_size.y
|| self.border.bottom >= rect_size.y
if self.border.left + self.border.right >= rect.size().x
|| self.border.top + self.border.bottom >= rect.size().y
{
bevy_utils::tracing::error!(
"TextureSlicer::border has out of bounds values. No slicing will be applied"
Expand All @@ -234,27 +231,21 @@ impl TextureSlicer {
let mut slices = Vec::with_capacity(9);
// Corners are in this order: [TL, TR, BL, BR]
let corners = self.corner_slices(rect, render_size);
// Vertical Sides: [B, T]
// Vertical Sides: [T, B]
let vertical_sides = self.vertical_side_slices(&corners, rect, render_size);
// Horizontal Sides: [L, R]
let horizontal_sides = self.horizontal_side_slices(&corners, rect, render_size);
// Center
let center = TextureSlice {
texture_rect: Rect {
min: rect.min + vec2(self.border.left, self.border.top),
max: vec2(
rect.max.x - self.border.right,
rect.max.y - self.border.bottom,
),
max: rect.max - vec2(self.border.right, self.border.bottom),
},
draw_size: vec2(
render_size.x - (corners[2].draw_size.x + corners[3].draw_size.x),
render_size.y - (corners[2].draw_size.y + corners[0].draw_size.y),
),
offset: Vec2::new(
(corners[0].draw_size.x - corners[3].draw_size.x) / 2.0,
(corners[2].draw_size.y - corners[0].draw_size.y) / 2.0,
render_size.x - (corners[0].draw_size.x + corners[1].draw_size.x),
render_size.y - (corners[0].draw_size.y + corners[2].draw_size.y),
),
offset: vec2(vertical_sides[0].offset.x, horizontal_sides[0].offset.y),
};

slices.extend(corners);
Expand Down Expand Up @@ -399,7 +390,7 @@ mod test {
}
);
assert_eq!(
vertical_sides[1], /* top */
vertical_sides[0], /* top */
TextureSlice {
texture_rect: Rect {
min: Vec2::new(5.0, 0.0),
Expand Down