From 1d673203dbc78a7250e17ff60f2e1d08564f18af Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Tue, 6 Sep 2022 12:29:13 +0200 Subject: [PATCH] Add terrain contours toggle --- Cargo.toml | 1 + save/src/world_manager.rs | 21 ++++++++++-- src/main.rs | 69 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2b84644..30a5c04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "worlds-sim-rust" version = "0.1.0" edition = "2021" +resolver = "2" [features] debug = ["save/debug"] diff --git a/save/src/world_manager.rs b/save/src/world_manager.rs index 9a84604..5982af5 100644 --- a/save/src/world_manager.rs +++ b/save/src/world_manager.rs @@ -18,6 +18,8 @@ pub struct WorldManager { rainfall_visible: bool, #[cfg(feature = "render")] temperature_visible: bool, + #[cfg(feature = "render")] + terrain_as_contours: bool, } impl WorldManager { @@ -28,6 +30,7 @@ impl WorldManager { world: None, rainfall_visible: false, temperature_visible: false, + terrain_as_contours: false, } } @@ -51,6 +54,16 @@ impl WorldManager { self.temperature_visible = !self.temperature_visible; } + #[cfg(feature = "render")] + pub fn toggle_contours(&mut self) { + if self.terrain_as_contours { + debug!("Turning terrain contours off"); + } else { + debug!("Turning terrain contours on"); + } + self.terrain_as_contours = !self.terrain_as_contours; + } + pub fn get_world(&self) -> Option<&World> { self.world.as_ref() } @@ -65,7 +78,11 @@ impl WorldManager { #[cfg(feature = "render")] fn generate_color(&self, cell: &TerrainCell) -> Color { - let mut final_color = Self::altitude_color(cell.altitude); + let mut final_color = if self.terrain_as_contours { + Self::altitude_contour_color(cell.altitude) + } else { + Self::altitude_color(cell.altitude) + }; if self.rainfall_visible { let rainfall_color = Self::rainfall_color(cell.rainfall); @@ -106,7 +123,6 @@ impl WorldManager { final_color } - /* #[cfg(feature = "render")] fn altitude_color(altitude: f32) -> Color { if altitude < 0.0 { @@ -117,7 +133,6 @@ impl WorldManager { Color::rgb(0.58 * mult, 0.29 * mult, 0.0) } } - */ #[cfg(feature = "render")] fn altitude_contour_color(altitude: f32) -> Color { diff --git a/src/main.rs b/src/main.rs index aa7a882..6113fdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,6 +85,10 @@ struct RainfallButton; #[derive(Component, Default)] struct TemperatureButton; +#[cfg(feature = "render")] +#[derive(Component, Default)] +struct ContoursButton; + const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15); const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25); const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.60, 0.35); @@ -154,6 +158,39 @@ fn handle_temperature_button( } } +#[cfg(feature = "render")] +fn handle_contours_button( + mut interaction_query: Query< + '_, + '_, + (&Interaction, &mut UiColor), + (Changed, With), + >, + mut windows: ResMut<'_, Windows>, + mut images: ResMut<'_, Assets>, + mut world_manager: ResMut<'_, WorldManager>, +) { + for (interaction, mut color) in &mut interaction_query { + match *interaction { + Interaction::Clicked => { + windows.primary_mut().set_cursor_icon(CursorIcon::Default); + *color = PRESSED_BUTTON.into(); + debug!("Toggling contours"); + world_manager.toggle_contours(); + refresh_world_texture(&mut images, &world_manager) + } + Interaction::Hovered => { + windows.primary_mut().set_cursor_icon(CursorIcon::Hand); + *color = HOVERED_BUTTON.into(); + } + Interaction::None => { + windows.primary_mut().set_cursor_icon(CursorIcon::Default); + *color = NORMAL_BUTTON.into(); + } + } + } +} + #[cfg(feature = "render")] fn generate_graphics( mut commands: Commands<'_, '_>, @@ -256,6 +293,31 @@ fn generate_graphics( ..default() }); }); + _ = button_box + .spawn_bundle(ButtonBundle { + button: Button, + style: Style { + align_items: AlignItems::Center, + justify_content: JustifyContent::Center, + ..default() + }, + color: NORMAL_BUTTON.into(), + ..default() + }) + .insert(ContoursButton::default()) + .with_children(|button| { + _ = button.spawn_bundle(TextBundle { + text: bevy::text::Text::from_section( + "Toggle contours", + bevy::text::TextStyle { + font: asset_server.load("JuliaMono.ttf"), + font_size: 20.0, + color: Color::WHITE, + }, + ), + ..default() + }); + }); }); }); } @@ -272,15 +334,16 @@ fn main() -> Result<(), Box> { // Use nearest-neighbor rendering for cripsier pixels .insert_resource(ImageSettings::default_nearest()) .insert_resource(WindowDescriptor { - width: world.width as f32, - height: world.height as f32, + width: (2 * world.width) as f32, + height: (2 * world.height) as f32, title: String::from("World-RS"), resizable: true, ..default() }) .add_startup_system(generate_graphics) .add_system(handle_rainfall_button) - .add_system(handle_temperature_button); + .add_system(handle_temperature_button) + .add_system(handle_contours_button); } #[cfg(not(feature = "render"))] {