From 6e982a3935e4e288f5beaea38e7aaec0594a0310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Sat, 9 Mar 2024 17:24:19 +0000 Subject: [PATCH 1/2] Add a responsive scaling for ui (#73) Co-authored-by: raphael Reviewed-on: https://git.tipragot.fr/fish-canard/border-wars/pulls/73 Reviewed-by: CoCo_Sol --- crates/border-wars/src/lib.rs | 1 + crates/border-wars/src/responsive_scale.rs | 37 ++++++++++++++++++++++ crates/border-wars/src/scenes/mod.rs | 5 +-- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 crates/border-wars/src/responsive_scale.rs diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index a4acdb8..257aefb 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -4,6 +4,7 @@ use bevy::prelude::*; pub mod camera; pub mod map; +pub mod responsive_scale; pub mod scenes; /// The current scene of the game. diff --git a/crates/border-wars/src/responsive_scale.rs b/crates/border-wars/src/responsive_scale.rs new file mode 100644 index 0000000..612f016 --- /dev/null +++ b/crates/border-wars/src/responsive_scale.rs @@ -0,0 +1,37 @@ +//! The file that contains the responsive scaling logic. + +use bevy::prelude::*; + +/// The plugin for the responsive scaling. +pub struct ResponsiveScalingPlugin; + +impl Plugin for ResponsiveScalingPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, init_window_size); + app.add_systems(Update, change_scaling); + } +} + +/// The default window size. +#[derive(Resource)] +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>, + size: Res, +) { + let window = windows.get_single().expect("Main window not found"); + let (a, b) = ( + 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 2a788fd..da18601 100644 --- a/crates/border-wars/src/scenes/mod.rs +++ b/crates/border-wars/src/scenes/mod.rs @@ -3,7 +3,7 @@ use bevy::prelude::*; use bevy_egui::EguiPlugin; -use crate::CurrentScene; +use crate::{responsive_scale, CurrentScene}; pub mod lobby; pub mod menu; @@ -16,6 +16,7 @@ impl Plugin for ScenesPlugin { app.add_plugins(EguiPlugin) .add_state::() .add_plugins(menu::MenuPlugin) - .add_plugins(lobby::LobbyPlugin); + .add_plugins(lobby::LobbyPlugin) + .add_plugins(responsive_scale::ResponsiveScalingPlugin); } } From d0c33523ce4ef6aeb9ec1c5068ce2adfd8769e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Sat, 9 Mar 2024 17:53:32 +0000 Subject: [PATCH 2/2] Renaming WindowSize (#77) Reviewed-on: https://git.tipragot.fr/fish-canard/border-wars/pulls/77 Reviewed-by: Tipragot --- crates/border-wars/src/responsive_scale.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/border-wars/src/responsive_scale.rs b/crates/border-wars/src/responsive_scale.rs index 612f016..dbca623 100644 --- a/crates/border-wars/src/responsive_scale.rs +++ b/crates/border-wars/src/responsive_scale.rs @@ -12,21 +12,21 @@ impl Plugin for ResponsiveScalingPlugin { } } -/// The default window size. +/// The default ui layout size. #[derive(Resource)] -pub struct WindowSize(pub Vec2); +pub struct UILayoutSize(pub Vec2); -/// Initializes the window size. +/// Initializes [UILayoutSize]. pub fn init_window_size(mut command: Commands) { - command.insert_resource(WindowSize(Vec2::new(1280., 720.))); + command.insert_resource(UILayoutSize(Vec2::new(1280., 720.))); } -/// Calculates the ui_scale.0 depending on the default screen size -/// in order to make the screen responsive. +/// Calculates the ui_scale.0 depending on the [UILayoutSize] +/// in order to make the ui layout responsive. pub fn change_scaling( mut ui_scale: ResMut, windows: Query<&Window>, - size: Res, + size: Res, ) { let window = windows.get_single().expect("Main window not found"); let (a, b) = (