2020-06-22 19:14:40 +00:00
|
|
|
use crate::ColorMaterial;
|
2020-05-13 23:42:27 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-07-10 08:37:06 +00:00
|
|
|
use bevy_ecs::{Query, Res};
|
2020-07-17 00:23:50 +00:00
|
|
|
use bevy_math::Vec2;
|
2020-09-08 19:04:22 +00:00
|
|
|
use bevy_render::{renderer::RenderResources, texture::Texture};
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_type_registry::TypeUuid;
|
2020-06-22 19:14:40 +00:00
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
#[derive(Debug, Default, RenderResources, TypeUuid)]
|
|
|
|
#[uuid = "7233c597-ccfa-411f-bd59-9af349432ada"]
|
2020-05-04 08:22:25 +00:00
|
|
|
pub struct Sprite {
|
2020-06-27 04:40:09 +00:00
|
|
|
pub size: Vec2,
|
2020-09-08 19:04:22 +00:00
|
|
|
#[render_resources(ignore)]
|
|
|
|
pub resize_mode: SpriteResizeMode,
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 19:04:22 +00:00
|
|
|
/// Determines how `Sprite` resize should be handled
|
|
|
|
#[derive(Debug)]
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 08:22:25 +00:00
|
|
|
|
2020-07-10 04:18:35 +00:00
|
|
|
pub fn sprite_system(
|
|
|
|
materials: Res<Assets<ColorMaterial>>,
|
|
|
|
textures: Res<Assets<Texture>>,
|
|
|
|
mut query: Query<(&mut Sprite, &Handle<ColorMaterial>)>,
|
|
|
|
) {
|
2020-10-30 06:39:55 +00:00
|
|
|
for (mut sprite, handle) in query.iter_mut() {
|
2020-09-08 19:04:22 +00:00
|
|
|
match sprite.resize_mode {
|
|
|
|
SpriteResizeMode::Manual => continue,
|
|
|
|
SpriteResizeMode::Automatic => {
|
2020-10-18 20:48:15 +00:00
|
|
|
let material = materials.get(handle).unwrap();
|
|
|
|
if let Some(ref texture_handle) = material.texture {
|
|
|
|
if let Some(texture) = textures.get(texture_handle) {
|
2020-11-22 20:04:47 +00:00
|
|
|
sprite.size = texture.size.as_vec3().truncate();
|
2020-09-08 19:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|
2020-07-10 04:18:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 08:22:25 +00:00
|
|
|
}
|