diff --git a/assets/martian.tweak.toml b/assets/martian.tweak.toml index 75d0d03..34d82f5 100644 --- a/assets/martian.tweak.toml +++ b/assets/martian.tweak.toml @@ -29,6 +29,10 @@ Space Blanket Folds - https://www.textures.com/download/PBR0152/133187 Background 2D art by NASA: LINK HERE """ +[resolution] +x = 640 +y = 480 + ######################################################################### # Audio settings ######################################################################### diff --git a/src/main.rs b/src/main.rs index 21e1753..f8d8a9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,6 @@ fn main() { .set(WindowPlugin { primary_window: Some(Window { title: "Martian Chess".into(), - resolution: (1280.0, 720.0).into(), ..default() }), ..default() diff --git a/src/ui.rs b/src/ui.rs index 11f7958..27c4898 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -11,6 +11,10 @@ impl Plugin for UiPlugin { ( manage_cursor.run_if(any_component_changed::), interactive_button.run_if(any_component_changed::), + scale_ui + .run_if(any_component_changed::) + .run_if(resource_exists::()), + scale_ui.run_if(on_event::>()), ), ); } @@ -61,3 +65,38 @@ fn interactive_button( } }); } + + +fn scale_ui( + mut windows: Query<&mut Window, Changed>, + mut ui_scale: ResMut, + mut resolution_set: Local, + tweakfile: Res, + tweaks: Res>, +) { + // 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::("resolution_x").unwrap(); + let height = tweak.get::("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; + } + }); +} \ No newline at end of file