Skip to content
Snippets Groups Projects
mod.rs 888 B
Newer Older
use bevy::prelude::*;

#[derive(Component, Copy, Clone)]
pub struct AlignedBackground;

pub fn adjust_aligned_backgrounds(
	mut query: Query<(&mut Transform, &Handle<Image>), With<AlignedBackground>>,
	windows: Res<Windows>,
	images: Res<Assets<Image>>,
) {
	if let Some(window) = windows.get_primary() {
		let width = window.width();
		let height = window.height();

		for (mut transform, handle) in &mut query {
			if let Some(image) = images.get(handle) {
				if width > height {
					let scale = image.texture_descriptor.size.width as f32 / width;
					transform.scale = Vec3::splat(scale);
				} else {
					let scale = image.texture_descriptor.size.height as f32 / height;
					transform.scale = Vec3::splat(scale);
				}
			}
		}
	}
}

pub struct GraphicsPlugin;
impl Plugin for GraphicsPlugin {
	fn build(&self, app: &mut App) {
		app.add_system(adjust_aligned_backgrounds);
	}
}