audioshader/src/systems.rs

61 lines
1.9 KiB
Rust
Raw Normal View History

2025-08-31 11:47:28 +00:00
use bevy::{
prelude::*,
2025-08-31 15:25:38 +00:00
window::{PrimaryWindow, WindowMode, WindowResized},
2025-08-31 11:47:28 +00:00
};
use crate::material::CustomMaterial;
#[derive(Component)]
pub struct FullscreenQuad;
pub fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
2025-08-31 15:25:38 +00:00
primary_window: Single<&Window, With<PrimaryWindow>>,
2025-08-31 11:47:28 +00:00
mut commands: Commands,
) {
commands.spawn(Camera2d);
// quad that fills the whole window
let screen_size = Vec2::new(
primary_window.resolution.width(),
primary_window.resolution.height(),
);
// create material with initial time = 0.0
let material_handle = materials.add(CustomMaterial {
time: 0.0,
resolution: screen_size,
});
commands.spawn((
Mesh2d(meshes.add(Rectangle::default())),
MeshMaterial2d(material_handle),
Transform::from_scale(Vec3::new(screen_size.x, screen_size.y, 1.0)),
FullscreenQuad,
));
}
2025-08-31 15:25:38 +00:00
pub fn screen_resized(
mut events: EventReader<WindowResized>,
shader_data: Single<(&mut Transform, &MeshMaterial2d<CustomMaterial>), With<FullscreenQuad>>,
mut materials: ResMut<Assets<CustomMaterial>>,
2025-08-31 11:47:28 +00:00
) {
2025-08-31 15:25:38 +00:00
let (mut transform, material) = shader_data.into_inner();
if let Some(event) = events.read().last() {
transform.scale = Vec3::new(event.width, event.height, 1.0);
materials.get_mut(&material.0).unwrap().resolution = Vec2::new(event.width, event.height);
}
2025-08-31 11:47:28 +00:00
}
pub fn exit_app(mut exit: EventWriter<AppExit>) {
exit.write(AppExit::Success);
}
pub fn toggle_fullscreen(mut primary_window: Single<&mut Window, With<PrimaryWindow>>) {
primary_window.mode = match primary_window.mode {
WindowMode::BorderlessFullscreen(_) => WindowMode::Windowed,
WindowMode::Windowed => WindowMode::BorderlessFullscreen(MonitorSelection::Current),
WindowMode::Fullscreen(_, _) => WindowMode::BorderlessFullscreen(MonitorSelection::Current),
};
}