Skip to content

Commit

Permalink
Resize mode component for Sprite
Browse files Browse the repository at this point in the history
Adds a 'resize_mode' field for 'SpriteComponents' bundle.
This allows different resize handling based on 'SpriteResizeMode' value.

Co-authored-by: Marcel Müller <[email protected]>
  • Loading branch information
naithar and TheNeikos committed Sep 3, 2020
1 parent d00ce1c commit cac929e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
7 changes: 5 additions & 2 deletions crates/bevy_sprite/src/entity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
render::SPRITE_PIPELINE_HANDLE, sprite::Sprite, ColorMaterial, TextureAtlas,
TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE,
render::SPRITE_PIPELINE_HANDLE,
sprite::{Sprite, SpriteResizeMode},
ColorMaterial, TextureAtlas, TextureAtlasSprite, QUAD_HANDLE, SPRITE_SHEET_PIPELINE_HANDLE,
};
use bevy_asset::Handle;
use bevy_ecs::Bundle;
Expand All @@ -15,6 +16,7 @@ use bevy_transform::prelude::{Rotation, Scale, Transform, Translation};
#[derive(Bundle)]
pub struct SpriteComponents {
pub sprite: Sprite,
pub resize_mode: SpriteResizeMode,
pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
pub material: Handle<ColorMaterial>,
pub main_pass: MainPass,
Expand Down Expand Up @@ -53,6 +55,7 @@ impl Default for SpriteComponents {
..Default::default()
},
sprite: Default::default(),
resize_mode: Default::default(),
main_pass: MainPass,
material: Default::default(),
transform: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use texture_atlas_builder::*;
pub mod prelude {
pub use crate::{
entity::{SpriteComponents, SpriteSheetComponents},
ColorMaterial, Sprite, TextureAtlas, TextureAtlasSprite,
ColorMaterial, Sprite, SpriteResizeMode, TextureAtlas, TextureAtlasSprite,
};
}

Expand Down
30 changes: 24 additions & 6 deletions crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,37 @@ pub struct Sprite {
pub size: Vec2,
}

/// Determines how `Sprite` resize should be handled
#[derive(Debug)]
pub enum SpriteResizeMode {
Manual,
Automatic,
}

impl Default for SpriteResizeMode {
fn default() -> Self {
SpriteResizeMode::Automatic
}
}

// SAFE: sprite is repr(C) and only consists of byteables
unsafe impl Byteable for Sprite {}

pub fn sprite_system(
materials: Res<Assets<ColorMaterial>>,
textures: Res<Assets<Texture>>,
mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>,
mut query: Query<(&mut Sprite, &SpriteResizeMode, &Handle<ColorMaterial>)>,
) {
for (mut sprite, handle) in &mut query.iter() {
let material = materials.get(&handle).unwrap();
if let Some(texture_handle) = material.texture {
if let Some(texture) = textures.get(&texture_handle) {
sprite.size = texture.size;
for (mut sprite, resize_mode, handle) in &mut query.iter() {
match resize_mode {
SpriteResizeMode::Manual => continue,
SpriteResizeMode::Automatic => {
let material = materials.get(&handle).unwrap();
if let Some(texture_handle) = material.texture {
if let Some(texture) = textures.get(&texture_handle) {
sprite.size = texture.size;
}
}
}
}
}
Expand Down

0 comments on commit cac929e

Please sign in to comment.