diff --git a/crates/border-wars/src/responsive_scale.rs b/crates/border-wars/src/responsive_scale.rs index 1295893..be7b882 100644 --- a/crates/border-wars/src/responsive_scale.rs +++ b/crates/border-wars/src/responsive_scale.rs @@ -2,19 +2,36 @@ use bevy::prelude::*; -/// The default width of the screen -const DEFAULT_WIDTH: f32 = 1280.; +/// The plugin for the responsive scaling. +pub struct ResponsiveScalingPlugin; -/// The default height of the screen -const DEFAULT_HEIGHT: f32 = 720.; +impl Plugin for ResponsiveScalingPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, init_window_size); + app.add_systems(Update, change_scaling); + } +} + +#[derive(Resource)] +/// The default window size +pub struct WindowSize(pub Vec2); + +/// Initializes the window size +pub fn init_window_size(mut command: Commands) { + command.insert_resource(WindowSize(Vec2::new(1280., 720.))); +} /// Calculates the ui_scale.0 depending on the default screen size /// in order to make the screen responsive -pub fn change_scaling(mut ui_scale: ResMut, windows: Query<&Window>) { +pub fn change_scaling( + mut ui_scale: ResMut, + windows: Query<&Window>, + size: Res, +) { let window = windows.get_single().expect("Main window not found"); let (a, b) = ( - window.resolution.width() / DEFAULT_WIDTH, - window.resolution.height() / DEFAULT_HEIGHT, + window.resolution.width() / size.0.x, + window.resolution.height() / size.0.y, ); ui_scale.0 = if a < b { a } else { b } as f64 } diff --git a/crates/border-wars/src/scenes/mod.rs b/crates/border-wars/src/scenes/mod.rs index 1c6a2b4..da18601 100644 --- a/crates/border-wars/src/scenes/mod.rs +++ b/crates/border-wars/src/scenes/mod.rs @@ -3,8 +3,7 @@ use bevy::prelude::*; use bevy_egui::EguiPlugin; -use crate::responsive_scale::change_scaling; -use crate::CurrentScene; +use crate::{responsive_scale, CurrentScene}; pub mod lobby; pub mod menu; @@ -18,6 +17,6 @@ impl Plugin for ScenesPlugin { .add_state::() .add_plugins(menu::MenuPlugin) .add_plugins(lobby::LobbyPlugin) - .add_systems(Update, change_scaling); + .add_plugins(responsive_scale::ResponsiveScalingPlugin); } }