audioshader/src/systems.rs
Matthew Deville a8357a8876 wip
2025-08-31 17:25:38 +02:00

60 lines
1.9 KiB
Rust

use bevy::{
prelude::*,
window::{PrimaryWindow, WindowMode, WindowResized},
};
use crate::material::CustomMaterial;
#[derive(Component)]
pub struct FullscreenQuad;
pub fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
primary_window: Single<&Window, With<PrimaryWindow>>,
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,
));
}
pub fn screen_resized(
mut events: EventReader<WindowResized>,
shader_data: Single<(&mut Transform, &MeshMaterial2d<CustomMaterial>), With<FullscreenQuad>>,
mut materials: ResMut<Assets<CustomMaterial>>,
) {
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);
}
}
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),
};
}