Adding default size in ressource and create a plugin for the responsive scale
All checks were successful
Rust Checks / checks (push) Successful in 2m58s
Rust Checks / checks (pull_request) Successful in 2m14s

This commit is contained in:
Raphaël 2024-03-09 17:37:11 +01:00
parent 66b0cab4ff
commit ab08b03660
2 changed files with 26 additions and 10 deletions

View file

@ -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<UiScale>, windows: Query<&Window>) {
pub fn change_scaling(
mut ui_scale: ResMut<UiScale>,
windows: Query<&Window>,
size: Res<WindowSize>,
) {
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
}

View file

@ -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::<CurrentScene>()
.add_plugins(menu::MenuPlugin)
.add_plugins(lobby::LobbyPlugin)
.add_systems(Update, change_scaling);
.add_plugins(responsive_scale::ResponsiveScalingPlugin);
}
}