65 lines
2 KiB
Rust
65 lines
2 KiB
Rust
|
|
use bevy::{
|
||
|
|
prelude::*,
|
||
|
|
window::{PrimaryWindow, WindowMode},
|
||
|
|
};
|
||
|
|
|
||
|
|
use crate::material::CustomMaterial;
|
||
|
|
|
||
|
|
/// Marker component for the fullscreen quad so we can update it on window resize
|
||
|
|
#[derive(Component)]
|
||
|
|
pub struct FullscreenQuad;
|
||
|
|
|
||
|
|
/// set up a simple 2D-screen-like surface
|
||
|
|
pub fn setup(
|
||
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
||
|
|
mut materials: ResMut<Assets<CustomMaterial>>,
|
||
|
|
mut primary_window: Single<&mut Window, With<PrimaryWindow>>,
|
||
|
|
mut commands: Commands,
|
||
|
|
) {
|
||
|
|
primary_window.decorations = false;
|
||
|
|
|
||
|
|
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,
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Update fullscreen quad transforms when the primary window is resized.
|
||
|
|
pub fn resize_fullscreen_quad(
|
||
|
|
primary_window: Single<&Window, With<PrimaryWindow>>,
|
||
|
|
mut transform: Single<&mut Transform, With<FullscreenQuad>>,
|
||
|
|
) {
|
||
|
|
let size = Vec2::new(
|
||
|
|
primary_window.resolution.width(),
|
||
|
|
primary_window.resolution.height(),
|
||
|
|
);
|
||
|
|
transform.scale = Vec3::new(size.x, size.y, 1.0);
|
||
|
|
}
|
||
|
|
|
||
|
|
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),
|
||
|
|
};
|
||
|
|
}
|