Dynamic UI resolution

main
Elijah C. Voigt 2 years ago
parent 666609e419
commit 716c94a5f3

@ -29,6 +29,10 @@ Space Blanket Folds - https://www.textures.com/download/PBR0152/133187
Background 2D art by NASA: LINK HERE Background 2D art by NASA: LINK HERE
""" """
[resolution]
x = 640
y = 480
######################################################################### #########################################################################
# Audio settings # Audio settings
######################################################################### #########################################################################

@ -49,7 +49,6 @@ fn main() {
.set(WindowPlugin { .set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
title: "Martian Chess".into(), title: "Martian Chess".into(),
resolution: (1280.0, 720.0).into(),
..default() ..default()
}), }),
..default() ..default()

@ -11,6 +11,10 @@ impl Plugin for UiPlugin {
( (
manage_cursor.run_if(any_component_changed::<Interaction>), manage_cursor.run_if(any_component_changed::<Interaction>),
interactive_button.run_if(any_component_changed::<Interaction>), interactive_button.run_if(any_component_changed::<Interaction>),
scale_ui
.run_if(any_component_changed::<Window>)
.run_if(resource_exists::<tweak::GameTweaks>()),
scale_ui.run_if(on_event::<AssetEvent<tweak::Tweaks>>()),
), ),
); );
} }
@ -61,3 +65,38 @@ fn interactive_button(
} }
}); });
} }
fn scale_ui(
mut windows: Query<&mut Window, Changed<Window>>,
mut ui_scale: ResMut<UiScale>,
mut resolution_set: Local<bool>,
tweakfile: Res<tweak::GameTweaks>,
tweaks: Res<Assets<tweak::Tweaks>>,
) {
// There is only 1 window
assert_eq!(windows.iter().count(), 1);
if !tweaks.contains(tweakfile.handle.clone()) {
return
}
let tweak = tweaks.get(tweakfile.handle.clone()).unwrap();
let width = tweak.get::<f32>("resolution_x").unwrap();
let height = tweak.get::<f32>("resolution_y").unwrap();
windows.iter_mut().for_each(|mut w| {
// Setting window resolution at startup
if !*resolution_set {
w.resolution.set(width, height);
*resolution_set = true;
}
// Setting UI Scale based on ratio of expected to current ratio
let width_ratio = w.resolution.width() / width;
let height_ratio = w.resolution.height() / height;
let new_scale = (width_ratio).min(height_ratio) as f64;
if ui_scale.0 != new_scale {
ui_scale.0 = new_scale;
}
});
}
Loading…
Cancel
Save