Add a responsive scaling for ui #73

Merged
raphael merged 11 commits from change-scaling into main 2024-03-09 17:24:22 +00:00
2 changed files with 26 additions and 10 deletions
Showing only changes of commit ab08b03660 - Show all commits

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) {

You should put these constants in bevy's resources instead.
this can be changed in future settings.

You should put these constants in bevy's resources instead. this can be changed in future settings.
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);
}
}