forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_resizing.rs
95 lines (87 loc) · 2.76 KB
/
window_resizing.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
///! This example illustrates how to resize windows, and how to respond to a window being resized.
use bevy::{prelude::*, window::WindowResized};
fn main() {
App::new()
.insert_resource(ResolutionSettings {
large: Vec2::new(1920.0, 1080.0),
medium: Vec2::new(800.0, 600.0),
small: Vec2::new(640.0, 360.0),
})
.add_plugins(DefaultPlugins)
.add_startup_system(setup_camera)
.add_startup_system(setup_ui)
.add_system(on_resize_system)
.add_system(toggle_resolution)
.run();
}
/// Marker component for the text that displays the current reslution.
#[derive(Component)]
struct ResolutionText;
/// Stores the various window-resolutions we can select between.
#[derive(Resource)]
struct ResolutionSettings {
large: Vec2,
medium: Vec2,
small: Vec2,
}
// Spawns the camera that draws UI
fn setup_camera(mut cmd: Commands) {
cmd.spawn(Camera2dBundle::default());
}
// Spawns the UI
fn setup_ui(mut cmd: Commands, asset_server: Res<AssetServer>) {
// Node that fills entire background
cmd.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
..default()
},
..default()
})
.with_children(|root| {
// Text where we display current resolution
root.spawn((
TextBundle::from_section(
"Resolution",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 50.0,
color: Color::BLACK,
},
),
ResolutionText,
));
});
}
/// This system shows how to request the window to a new resolution
fn toggle_resolution(
keys: Res<Input<KeyCode>>,
mut windows: ResMut<Windows>,
resolution: Res<ResolutionSettings>,
) {
let window = windows.primary_mut();
if keys.just_pressed(KeyCode::Key1) {
let res = resolution.small;
window.set_resolution(res.x, res.y);
}
if keys.just_pressed(KeyCode::Key2) {
let res = resolution.medium;
window.set_resolution(res.x, res.y);
}
if keys.just_pressed(KeyCode::Key3) {
let res = resolution.large;
window.set_resolution(res.x, res.y);
}
}
/// This system shows how to respond to a window being resized.
/// Whenever the window is resized, the text will update with the new resolution.
fn on_resize_system(
mut q: Query<&mut Text, With<ResolutionText>>,
mut resize_reader: EventReader<WindowResized>,
) {
let mut text = q.single_mut();
for e in resize_reader.iter() {
// When resolution is being changed
text.sections[0].value = format!("{:.1} x {:.1}", e.width, e.height);
}
}