Skip to content

Commit f7166c1

Browse files
committed
Code cleanup
1 parent adf6bc5 commit f7166c1

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

crates/bevy_sprite/src/texture_slice.rs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct TextureSlicer {
2525
}
2626

2727
/// Defines how a texture slice scales when resized
28-
#[derive(Debug, Clone, Default, Reflect)]
28+
#[derive(Debug, Copy, Clone, Default, Reflect)]
2929
pub enum SliceScaleMode {
3030
/// The slice will be stretched to fit the area
3131
#[default]
@@ -55,8 +55,7 @@ pub(crate) struct TextureSlice {
5555
impl TextureSlicer {
5656
/// Computes the 4 corner slices
5757
fn corner_slices(&self, base_rect: Rect, render_size: Vec2) -> [TextureSlice; 4] {
58-
let image_size = base_rect.size();
59-
let coef = render_size / image_size;
58+
let coef = render_size / base_rect.size();
6059
let min_coef = coef.x.min(coef.y).min(self.max_corner_scale.max(0.001));
6160
[
6261
// Top Left Corner
@@ -74,8 +73,8 @@ impl TextureSlicer {
7473
// Top Right Corner
7574
TextureSlice {
7675
texture_rect: Rect {
77-
min: base_rect.min + Vec2::new(image_size.x - self.border.right, 0.0),
78-
max: base_rect.min + Vec2::new(image_size.x, self.border.top),
76+
min: Vec2::new(base_rect.max.x - self.border.right, base_rect.min.y),
77+
max: Vec2::new(base_rect.max.x, self.border.top),
7978
},
8079
draw_size: Vec2::new(self.border.right, self.border.top) * min_coef,
8180
offset: Vec2::new(
@@ -86,8 +85,8 @@ impl TextureSlicer {
8685
// Bottom Left
8786
TextureSlice {
8887
texture_rect: Rect {
89-
min: base_rect.min + Vec2::new(0.0, image_size.y - self.border.bottom),
90-
max: base_rect.min + Vec2::new(self.border.left, image_size.y),
88+
min: Vec2::new(base_rect.min.x, base_rect.max.y - self.border.bottom),
89+
max: Vec2::new(base_rect.min.x + self.border.left, base_rect.max.y),
9190
},
9291
draw_size: Vec2::new(self.border.left, self.border.bottom) * min_coef,
9392
offset: Vec2::new(
@@ -98,12 +97,11 @@ impl TextureSlicer {
9897
// Bottom Right Corner
9998
TextureSlice {
10099
texture_rect: Rect {
101-
min: base_rect.min
102-
+ Vec2::new(
103-
image_size.x - self.border.right,
104-
image_size.y - self.border.bottom,
105-
),
106-
max: base_rect.min + Vec2::new(image_size.x, image_size.y),
100+
min: Vec2::new(
101+
base_rect.max.x - self.border.right,
102+
base_rect.max.y - self.border.bottom,
103+
),
104+
max: base_rect.max,
107105
},
108106
draw_size: Vec2::new(self.border.right, self.border.bottom) * min_coef,
109107
offset: Vec2::new(
@@ -121,12 +119,14 @@ impl TextureSlicer {
121119
base_rect: Rect,
122120
render_size: Vec2,
123121
) -> [TextureSlice; 2] {
124-
let image_size = base_rect.size();
125122
// left
126123
let left_side = TextureSlice {
127124
texture_rect: Rect {
128125
min: base_rect.min + Vec2::new(0.0, self.border.top),
129-
max: base_rect.min + Vec2::new(self.border.left, image_size.y - self.border.bottom),
126+
max: Vec2::new(
127+
base_rect.min.x + self.border.left,
128+
base_rect.max.y - self.border.bottom,
129+
),
130130
},
131131
draw_size: Vec2::new(
132132
bl_corner.draw_size.x,
@@ -138,9 +138,11 @@ impl TextureSlicer {
138138
// right
139139
let right_side = TextureSlice {
140140
texture_rect: Rect {
141-
min: base_rect.min
142-
+ Vec2::new(image_size.x - self.border.right, self.border.bottom),
143-
max: base_rect.min + Vec2::new(image_size.x, image_size.y - self.border.top),
141+
min: Vec2::new(
142+
base_rect.max.x - self.border.right,
143+
base_rect.min.y + self.border.bottom,
144+
),
145+
max: Vec2::new(base_rect.max.x, base_rect.max.y - self.border.top),
144146
},
145147
draw_size: Vec2::new(
146148
br_corner.draw_size.x,
@@ -158,12 +160,14 @@ impl TextureSlicer {
158160
base_rect: Rect,
159161
render_size: Vec2,
160162
) -> [TextureSlice; 2] {
161-
let image_size = base_rect.size();
162163
// Bottom
163164
let bot_side = TextureSlice {
164165
texture_rect: Rect {
165-
min: base_rect.min + Vec2::new(self.border.left, image_size.y - self.border.bottom),
166-
max: base_rect.min + Vec2::new(image_size.x - self.border.right, image_size.y),
166+
min: Vec2::new(
167+
base_rect.min.x + self.border.left,
168+
base_rect.max.y - self.border.bottom,
169+
),
170+
max: Vec2::new(base_rect.max.x - self.border.right, base_rect.max.y),
167171
},
168172
draw_size: Vec2::new(
169173
render_size.x - (bl_corner.draw_size.x + br_corner.draw_size.x),
@@ -176,7 +180,10 @@ impl TextureSlicer {
176180
let top_side = TextureSlice {
177181
texture_rect: Rect {
178182
min: base_rect.min + Vec2::new(self.border.left, 0.0),
179-
max: base_rect.min + Vec2::new(image_size.x - self.border.right, self.border.top),
183+
max: Vec2::new(
184+
base_rect.max.x - self.border.right,
185+
base_rect.min.y + self.border.top,
186+
),
180187
},
181188
draw_size: Vec2::new(
182189
render_size.x - (tl_corner.draw_size.x + tr_corner.draw_size.x),
@@ -187,6 +194,13 @@ impl TextureSlicer {
187194
[bot_side, top_side]
188195
}
189196

197+
/// Slices the given `rect` into at least 9 sections. If the center and/or side parts are set to tile,
198+
/// a bigger number of sections will be computed.
199+
///
200+
/// # Arguments
201+
///
202+
/// * `rect` - The section of the texture to slice in 9 parts
203+
/// * `render_size` - The optional draw size of the texture. If not set the `rect` size will be used.
190204
pub(crate) fn compute_slices(
191205
&self,
192206
rect: Rect,
@@ -203,8 +217,7 @@ impl TextureSlicer {
203217
let center = TextureSlice {
204218
texture_rect: Rect {
205219
min: rect.min + Vec2::new(self.border.left, self.border.bottom),
206-
max: rect.min
207-
+ Vec2::new(rect.max.x - self.border.right, rect.max.y - self.border.top),
220+
max: Vec2::new(rect.max.x - self.border.right, rect.max.y - self.border.top),
208221
},
209222
draw_size: Vec2::new(
210223
render_size.x - (corners[2].draw_size.x + corners[3].draw_size.x),

0 commit comments

Comments
 (0)