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
3 changed files with 41 additions and 2 deletions

View file

@ -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.

View file

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

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);
}
}
/// 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<UiScale>,
windows: Query<&Window>,
size: Res<WindowSize>,
) {
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
}

View file

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