generated from tipragot/rust
Add a responsive scaling for ui #73
|
@ -4,6 +4,7 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
pub mod camera;
|
pub mod camera;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
|
pub mod responsive_scale;
|
||||||
pub mod scenes;
|
pub mod scenes;
|
||||||
|
|
||||||
/// The current scene of the game.
|
/// The current scene of the game.
|
||||||
|
|
37
crates/border-wars/src/responsive_scale.rs
Normal file
37
crates/border-wars/src/responsive_scale.rs
Normal 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) {
|
||||||
|
|||||||
|
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
|
||||||
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::EguiPlugin;
|
use bevy_egui::EguiPlugin;
|
||||||
|
|
||||||
use crate::CurrentScene;
|
use crate::{responsive_scale, CurrentScene};
|
||||||
|
|
||||||
pub mod lobby;
|
pub mod lobby;
|
||||||
pub mod menu;
|
pub mod menu;
|
||||||
|
@ -16,6 +16,7 @@ impl Plugin for ScenesPlugin {
|
||||||
app.add_plugins(EguiPlugin)
|
app.add_plugins(EguiPlugin)
|
||||||
.add_state::<CurrentScene>()
|
.add_state::<CurrentScene>()
|
||||||
.add_plugins(menu::MenuPlugin)
|
.add_plugins(menu::MenuPlugin)
|
||||||
.add_plugins(lobby::LobbyPlugin);
|
.add_plugins(lobby::LobbyPlugin)
|
||||||
|
.add_plugins(responsive_scale::ResponsiveScalingPlugin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue
You should put these constants in bevy's resources instead.
this can be changed in future settings.