Skip to content

Commit a893795

Browse files
committed
Rebased on main
1 parent 138c23f commit a893795

File tree

5 files changed

+88
-26
lines changed

5 files changed

+88
-26
lines changed

crates/bevy_sprite/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ pub mod prelude {
1313
#[doc(hidden)]
1414
pub use crate::{
1515
bundle::{SpriteBundle, SpriteSheetBundle},
16-
rect::{BorderRect, Rect},
1716
sprite::{Sprite, SpriteDrawMode},
1817
texture_atlas::{TextureAtlas, TextureAtlasSprite},
19-
texture_slice::{SliceScaleMode, TextureSlicer},
18+
texture_slice::{BorderRect, SliceScaleMode, TextureSlicer},
2019
ColorMaterial, ColorMesh2dBundle, TextureAtlasBuilder,
2120
};
2221
}

crates/bevy_sprite/src/render/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp::Ordering;
22

33
use crate::{
44
texture_atlas::{TextureAtlas, TextureAtlasSprite},
5-
Rect, Sprite, SpriteDrawMode, TextureSlice, SPRITE_SHADER_HANDLE,
5+
Sprite, SpriteDrawMode, TextureSlice, SPRITE_SHADER_HANDLE,
66
};
77
use bevy_asset::{AssetEvent, Assets, Handle, HandleId};
88
use bevy_core_pipeline::{core_2d::Transparent2d, tonemapping::Tonemapping};
@@ -30,7 +30,7 @@ use bevy_render::{
3030
},
3131
Extract,
3232
};
33-
use bevy_transform::components::GlobalTransform;
33+
use bevy_transform::components::{GlobalTransform, Transform};
3434
use bevy_utils::FloatOrd;
3535
use bevy_utils::HashMap;
3636
use bytemuck::{Pod, Zeroable};
@@ -343,15 +343,16 @@ pub fn extract_sprites(
343343
}
344344
if let SpriteDrawMode::Simple = sprite.draw_mode {
345345
// PERF: we don't check in this function that the `Image` asset is ready, since it should be in most cases and hashing the handle is expensive
346-
extracted_sprites.sprites.alloc().init(ExtractedSprite {
346+
extracted_sprites.sprites.push(ExtractedSprite {
347+
entity,
347348
color: sprite.color,
348349
transform: *transform,
349350
// Use the full texture
350351
rect: None,
351352
custom_size: sprite.custom_size,
352353
flip_x: sprite.flip_x,
353354
flip_y: sprite.flip_y,
354-
image_handle_id: handle.id,
355+
image_handle_id: handle.id(),
355356
anchor: sprite.anchor.as_vec(),
356357
});
357358
continue;
@@ -391,16 +392,17 @@ pub fn extract_sprites(
391392
};
392393
for slice in slices {
393394
let mut transform: GlobalTransform = *transform;
394-
transform.translation = transform.mul_vec3(slice.offset.extend(0.0));
395-
extracted_sprites.sprites.alloc().init(ExtractedSprite {
395+
transform =
396+
transform.mul_transform(Transform::from_translation(slice.offset.extend(0.0)));
397+
extracted_sprites.sprites.push(ExtractedSprite {
396398
entity,
397399
color: sprite.color,
398400
transform,
399401
rect: Some(slice.texture_rect),
400402
custom_size: Some(slice.draw_size),
401403
flip_x: sprite.flip_x,
402404
flip_y: sprite.flip_y,
403-
image_handle_id: handle.id,
405+
image_handle_id: handle.id(),
404406
anchor: sprite.anchor.as_vec(),
405407
});
406408
}

crates/bevy_sprite/src/sprite.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub enum Anchor {
6565
}
6666

6767
impl Anchor {
68+
#[inline]
69+
#[must_use]
6870
pub fn as_vec(&self) -> Vec2 {
6971
match self {
7072
Anchor::Center => Vec2::ZERO,

crates/bevy_sprite/src/texture_slice.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
1-
use crate::{BorderRect, Rect};
2-
use bevy_math::Vec2;
3-
use bevy_reflect::Reflect;
1+
use bevy_math::{Rect, Vec2};
2+
use bevy_reflect::{FromReflect, Reflect};
3+
4+
/// Struct defining a [`Sprite`](crate::Sprite) border with padding values
5+
#[derive(Default, Clone, Copy, Debug, Reflect, FromReflect)]
6+
pub struct BorderRect {
7+
/// Pixel padding to the left
8+
pub left: f32,
9+
/// Pixel padding to the right
10+
pub right: f32,
11+
/// Pixel padding to the top
12+
pub top: f32,
13+
/// Pixel padding to the bottom
14+
pub bottom: f32,
15+
}
16+
17+
impl BorderRect {
18+
/// Creates a new border as a square, with identical pixel padding values on every direction
19+
#[must_use]
20+
#[inline]
21+
pub const fn square(value: f32) -> Self {
22+
Self {
23+
left: value,
24+
right: value,
25+
top: value,
26+
bottom: value,
27+
}
28+
}
29+
30+
/// Creates a new border as a rectangle, with:
31+
/// - `horizontal` for left and right pixel padding
32+
/// - `vertical` for top and bottom pixel padding
33+
#[must_use]
34+
#[inline]
35+
pub const fn rectangle(horizontal: f32, vertical: f32) -> Self {
36+
Self {
37+
left: horizontal,
38+
right: horizontal,
39+
top: vertical,
40+
bottom: vertical,
41+
}
42+
}
43+
}
44+
45+
impl From<f32> for BorderRect {
46+
fn from(v: f32) -> Self {
47+
Self::square(v)
48+
}
49+
}
50+
51+
impl From<[f32; 4]> for BorderRect {
52+
fn from([left, right, top, bottom]: [f32; 4]) -> Self {
53+
Self {
54+
left,
55+
right,
56+
top,
57+
bottom,
58+
}
59+
}
60+
}
461

562
/// Slices a texture using the **9-slicing** technique. This allows to reuse an image at various sizes
663
/// without needing to prepare multiple assets. The associated texture will be split into nine portions,
@@ -10,7 +67,7 @@ use bevy_reflect::Reflect;
1067
/// sections will be scaled or tiled.
1168
///
1269
/// See [9-sliced](https://en.wikipedia.org/wiki/9-slice_scaling) textures.
13-
#[derive(Debug, Clone, Reflect)]
70+
#[derive(Debug, Clone, Reflect, FromReflect)]
1471
pub struct TextureSlicer {
1572
/// The sprite borders, defining the 9 sections of the image
1673
pub border: BorderRect,
@@ -23,7 +80,7 @@ pub struct TextureSlicer {
2380
}
2481

2582
/// Defines how a texture slice scales when resized
26-
#[derive(Debug, Copy, Clone, Default, Reflect)]
83+
#[derive(Debug, Copy, Clone, Default, Reflect, FromReflect)]
2784
pub enum SliceScaleMode {
2885
/// The slice will be stretched to fit the area
2986
#[default]

examples/2d/sprite_slice.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ use bevy::prelude::*;
44

55
fn main() {
66
App::new()
7-
.insert_resource(WindowDescriptor {
8-
width: 1350.0,
9-
height: 700.0,
7+
.add_plugins(DefaultPlugins.set(WindowPlugin {
8+
window: WindowDescriptor {
9+
width: 1350.0,
10+
height: 700.0,
11+
..default()
12+
},
1013
..default()
11-
})
12-
.add_plugins(DefaultPlugins)
14+
}))
1315
.add_startup_system(setup)
1416
.run();
1517
}
@@ -21,7 +23,7 @@ fn spawn_sprites(
2123
slice_border: f32,
2224
) {
2325
// Reference sprite
24-
commands.spawn_bundle(SpriteBundle {
26+
commands.spawn(SpriteBundle {
2527
transform: Transform::from_translation(base_pos),
2628
texture: texture_handle.clone(),
2729
sprite: Sprite {
@@ -32,7 +34,7 @@ fn spawn_sprites(
3234
});
3335

3436
// Scaled regular sprite
35-
commands.spawn_bundle(SpriteBundle {
37+
commands.spawn(SpriteBundle {
3638
transform: Transform::from_translation(base_pos + Vec3::X * 150.0),
3739
texture: texture_handle.clone(),
3840
sprite: Sprite {
@@ -43,7 +45,7 @@ fn spawn_sprites(
4345
});
4446

4547
// Stretched Scaled sliced sprite
46-
commands.spawn_bundle(SpriteBundle {
48+
commands.spawn(SpriteBundle {
4749
transform: Transform::from_translation(base_pos + Vec3::X * 300.0),
4850
texture: texture_handle.clone(),
4951
sprite: Sprite {
@@ -59,7 +61,7 @@ fn spawn_sprites(
5961
});
6062

6163
// Scaled sliced sprite
62-
commands.spawn_bundle(SpriteBundle {
64+
commands.spawn(SpriteBundle {
6365
transform: Transform::from_translation(base_pos + Vec3::X * 450.0),
6466
texture: texture_handle.clone(),
6567
sprite: Sprite {
@@ -76,7 +78,7 @@ fn spawn_sprites(
7678
});
7779

7880
// Scaled sliced sprite horizontally
79-
commands.spawn_bundle(SpriteBundle {
81+
commands.spawn(SpriteBundle {
8082
transform: Transform::from_translation(base_pos + Vec3::X * 700.0),
8183
texture: texture_handle.clone(),
8284
sprite: Sprite {
@@ -93,7 +95,7 @@ fn spawn_sprites(
9395
});
9496

9597
// Scaled sliced sprite horizontally with max scale
96-
commands.spawn_bundle(SpriteBundle {
98+
commands.spawn(SpriteBundle {
9799
transform: Transform::from_translation(base_pos + Vec3::X * 1050.0),
98100
texture: texture_handle,
99101
sprite: Sprite {
@@ -111,7 +113,7 @@ fn spawn_sprites(
111113
}
112114

113115
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
114-
commands.spawn_bundle(Camera2dBundle::default());
116+
commands.spawn(Camera2dBundle::default());
115117
// Load textures
116118
let handle_1 = asset_server.load("textures/slice_square.png");
117119
let handle_2 = asset_server.load("textures/slice_square_2.png");

0 commit comments

Comments
 (0)