generated from tipragot/rust
Add a responsive scaling for ui #73
|
@ -2,19 +2,36 @@
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
/// The default width of the screen
|
/// The plugin for the responsive scaling.
|
||||||
const DEFAULT_WIDTH: f32 = 1280.;
|
pub struct ResponsiveScalingPlugin;
|
||||||
|
|
||||||
/// The default height of the screen
|
impl Plugin for ResponsiveScalingPlugin {
|
||||||
const DEFAULT_HEIGHT: f32 = 720.;
|
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
|
/// Calculates the ui_scale.0 depending on the default screen size
|
||||||
/// in order to make the screen responsive
|
/// 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 window = windows.get_single().expect("Main window not found");
|
||||||
let (a, b) = (
|
let (a, b) = (
|
||||||
window.resolution.width() / DEFAULT_WIDTH,
|
window.resolution.width() / size.0.x,
|
||||||
window.resolution.height() / DEFAULT_HEIGHT,
|
window.resolution.height() / size.0.y,
|
||||||
);
|
);
|
||||||
ui_scale.0 = if a < b { a } else { b } as f64
|
ui_scale.0 = if a < b { a } else { b } as f64
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::EguiPlugin;
|
use bevy_egui::EguiPlugin;
|
||||||
|
|
||||||
use crate::responsive_scale::change_scaling;
|
use crate::{responsive_scale, CurrentScene};
|
||||||
use crate::CurrentScene;
|
|
||||||
|
|
||||||
pub mod lobby;
|
pub mod lobby;
|
||||||
pub mod menu;
|
pub mod menu;
|
||||||
|
@ -18,6 +17,6 @@ impl Plugin for ScenesPlugin {
|
||||||
.add_state::<CurrentScene>()
|
.add_state::<CurrentScene>()
|
||||||
.add_plugins(menu::MenuPlugin)
|
.add_plugins(menu::MenuPlugin)
|
||||||
.add_plugins(lobby::LobbyPlugin)
|
.add_plugins(lobby::LobbyPlugin)
|
||||||
.add_systems(Update, change_scaling);
|
.add_plugins(responsive_scale::ResponsiveScalingPlugin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue