-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Need for Sprite Size Sync System (#2632)
# Objective - Prevent the need to have a system that synchronizes sprite sizes with their images ## Solution - Read the sprite size from the image asset when rendering the sprite - Replace the `size` and `resize_mode` fields of `Sprite` with a `custom_size: Option<Vec2>` that will modify the sprite's rendered size to be different than the image size, but only if it is `Some(Vec2)`
- Loading branch information
Showing
4 changed files
with
17 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,15 @@ | ||
use bevy_asset::{Assets, Handle}; | ||
use bevy_ecs::prelude::{Query, Res}; | ||
use bevy_math::Vec2; | ||
use bevy_reflect::{Reflect, ReflectDeserialize, TypeUuid}; | ||
use bevy_render2::texture::Image; | ||
use serde::{Deserialize, Serialize}; | ||
use bevy_reflect::{Reflect, TypeUuid}; | ||
|
||
#[derive(Debug, Default, Clone, TypeUuid, Reflect)] | ||
#[uuid = "7233c597-ccfa-411f-bd59-9af349432ada"] | ||
#[repr(C)] | ||
pub struct Sprite { | ||
pub size: Vec2, | ||
/// Flip the sprite along the X axis | ||
pub flip_x: bool, | ||
/// Flip the sprite along the Y axis | ||
pub flip_y: bool, | ||
pub resize_mode: SpriteResizeMode, | ||
} | ||
|
||
/// Determines how `Sprite` resize should be handled | ||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Reflect)] | ||
#[reflect_value(PartialEq, Serialize, Deserialize)] | ||
pub enum SpriteResizeMode { | ||
Manual, | ||
Automatic, | ||
} | ||
|
||
impl Default for SpriteResizeMode { | ||
fn default() -> Self { | ||
SpriteResizeMode::Automatic | ||
} | ||
} | ||
|
||
impl Sprite { | ||
/// Creates new `Sprite` with `SpriteResizeMode::Manual` value for `resize_mode` | ||
pub fn new(size: Vec2) -> Self { | ||
Self { | ||
size, | ||
resize_mode: SpriteResizeMode::Manual, | ||
flip_x: false, | ||
flip_y: false, | ||
} | ||
} | ||
} | ||
|
||
/// System that resizes sprites that have their resize mode set to automatic | ||
pub fn sprite_auto_resize_system( | ||
textures: Res<Assets<Image>>, | ||
mut query: Query<(&mut Sprite, &Handle<Image>)>, | ||
) { | ||
for (mut sprite, image_handle) in query.iter_mut() { | ||
match sprite.resize_mode { | ||
SpriteResizeMode::Manual => continue, | ||
SpriteResizeMode::Automatic => { | ||
if let Some(image) = textures.get(image_handle) { | ||
let extent = image.texture_descriptor.size; | ||
let texture_size = Vec2::new(extent.width as f32, extent.height as f32); | ||
// only set sprite size if it has changed (this check prevents change | ||
// detection from triggering) | ||
if sprite.size != texture_size { | ||
sprite.size = texture_size; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
/// An optional custom size for the sprite that will be used when rendering, instead of the size | ||
/// of the sprite's image | ||
pub custom_size: Option<Vec2>, | ||
} |