use bevy::{ prelude::*, window::{PrimaryWindow, WindowMode, WindowResized}, }; use crate::material::CustomMaterial; #[derive(Component)] pub struct FullscreenQuad; pub fn setup( mut meshes: ResMut>, mut materials: ResMut>, primary_window: Single<&Window, With>, 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, shader_data: Single<(&mut Transform, &MeshMaterial2d), With>, mut materials: ResMut>, ) { 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) { exit.write(AppExit::Success); } pub fn toggle_fullscreen(mut primary_window: Single<&mut Window, With>) { primary_window.mode = match primary_window.mode { WindowMode::BorderlessFullscreen(_) => WindowMode::Windowed, WindowMode::Windowed => WindowMode::BorderlessFullscreen(MonitorSelection::Current), WindowMode::Fullscreen(_, _) => WindowMode::BorderlessFullscreen(MonitorSelection::Current), }; }