-
Notifications
You must be signed in to change notification settings - Fork 32
/
ui_scale.rs
64 lines (60 loc) · 1.9 KB
/
ui_scale.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
use belly::prelude::*;
use bevy::{prelude::*, render::camera::ScalingMode, text::TextSettings, window::PrimaryWindow};
// TODO: rename to ui-scale, add example comment
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(BellyPlugin)
.add_systems(Startup, setup)
.add_systems(Update, scale)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle {
transform: Transform::from_translation(Vec3::new(0., 0., 5.)),
projection: OrthographicProjection {
scaling_mode: ScalingMode::AutoMin {
min_width: 1024.,
min_height: 768.,
},
..default()
},
..default()
});
commands.spawn(SpriteBundle {
texture: asset_server.load("icon.png"),
..default()
});
commands.insert_resource(UiScale(1.));
commands.insert_resource(TextSettings {
allow_dynamic_font_size: true,
..default()
});
commands.add(eml! {
<body s:flex-direction="column">
<span s:height="15px"/>
<span s:width="50px" s:height="15px" s:background-color="rebeccapurple"/>
<span s:margin-left="50px" s:font-size="36px">"Resize the window to see the difference."</span>
<span s:width="50px" s:height="15px" s:background-color="rebeccapurple"/>
</body>
})
}
pub fn scale(
mut cached_size: Local<Vec2>,
mut ui_scale: ResMut<UiScale>,
windows: Query<&Window, With<PrimaryWindow>>,
) {
let Some(primary) = windows.iter().next() else {
return;
};
let ww = primary.width();
let wh = primary.height();
if cached_size.x == ww && cached_size.y == wh {
return;
}
cached_size.x = ww;
cached_size.y = wh;
let scale_h = ww / 1024.0;
let scale_w = wh / 768.0;
ui_scale.0 = scale_h.min(scale_w) as f32;
}