| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use bevy::{prelude::*, window::WindowResolution}; |
| 5 |
|
| 6 |
#[derive(Component)] |
| 7 |
struct CustomText; |
| 8 |
|
| 9 |
fn main() { |
| 10 |
App::new() |
| 11 |
.add_plugins(DefaultPlugins.set(WindowPlugin { |
| 12 |
primary_window: Some(Window { |
| 13 |
resolution: WindowResolution::new(500, 300).with_scale_factor_override(1.0), |
| 14 |
..default() |
| 15 |
}), |
| 16 |
..default() |
| 17 |
})) |
| 18 |
.add_systems(Startup, setup) |
| 19 |
.add_systems( |
| 20 |
Update, |
| 21 |
(display_override, toggle_override, change_scale_factor), |
| 22 |
) |
| 23 |
.run(); |
| 24 |
} |
| 25 |
|
| 26 |
fn setup(mut commands: Commands) { |
| 27 |
|
| 28 |
commands.spawn(Camera2d); |
| 29 |
|
| 30 |
commands.spawn(( |
| 31 |
Node { |
| 32 |
width: percent(100), |
| 33 |
height: percent(100), |
| 34 |
justify_content: JustifyContent::SpaceBetween, |
| 35 |
..default() |
| 36 |
}, |
| 37 |
children![( |
| 38 |
Node { |
| 39 |
width: px(300), |
| 40 |
height: percent(100), |
| 41 |
border: UiRect::all(px(2)), |
| 42 |
..default() |
| 43 |
}, |
| 44 |
BackgroundColor(Color::srgb(0.65, 0.65, 0.65)), |
| 45 |
children![( |
| 46 |
CustomText, |
| 47 |
Text::new("Example text"), |
| 48 |
TextFont { |
| 49 |
font_size: FontSize::Px(25.0), |
| 50 |
..default() |
| 51 |
}, |
| 52 |
Node { |
| 53 |
align_self: AlignSelf::FlexEnd, |
| 54 |
..default() |
| 55 |
}, |
| 56 |
)] |
| 57 |
)], |
| 58 |
)); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
fn display_override( |
| 63 |
mut window: Single<&mut Window>, |
| 64 |
mut custom_text: Single<&mut Text, With<CustomText>>, |
| 65 |
) { |
| 66 |
let text = format!( |
| 67 |
"Scale factor: {:.1} {}", |
| 68 |
window.scale_factor(), |
| 69 |
if window.resolution.scale_factor_override().is_some() { |
| 70 |
"(overridden)" |
| 71 |
} else { |
| 72 |
"(default)" |
| 73 |
} |
| 74 |
); |
| 75 |
|
| 76 |
window.title.clone_from(&text); |
| 77 |
custom_text.0 = text; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
fn toggle_override(input: Res<ButtonInput<KeyCode>>, mut window: Single<&mut Window>) { |
| 82 |
if input.just_pressed(KeyCode::Enter) { |
| 83 |
let scale_factor_override = window.resolution.scale_factor_override(); |
| 84 |
window |
| 85 |
.resolution |
| 86 |
.set_scale_factor_override(scale_factor_override.xor(Some(1.0))); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
fn change_scale_factor(input: Res<ButtonInput<KeyCode>>, mut window: Single<&mut Window>) { |
| 92 |
let scale_factor_override = window.resolution.scale_factor_override(); |
| 93 |
if input.just_pressed(KeyCode::ArrowUp) { |
| 94 |
window |
| 95 |
.resolution |
| 96 |
.set_scale_factor_override(scale_factor_override.map(|n| n + 1.0)); |
| 97 |
} else if input.just_pressed(KeyCode::ArrowDown) { |
| 98 |
window |
| 99 |
.resolution |
| 100 |
.set_scale_factor_override(scale_factor_override.map(|n| (n - 1.0).max(1.0))); |
| 101 |
} |
| 102 |
} |