Add terrain contours toggle

This commit is contained in:
Tobias Berger 2022-09-06 12:29:13 +02:00
parent 92f25cf159
commit 1d673203db
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
3 changed files with 85 additions and 6 deletions

View file

@ -2,6 +2,7 @@
name = "worlds-sim-rust"
version = "0.1.0"
edition = "2021"
resolver = "2"
[features]
debug = ["save/debug"]

View file

@ -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 {

View file

@ -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<Interaction>, With<ContoursButton>),
>,
mut windows: ResMut<'_, Windows>,
mut images: ResMut<'_, Assets<Image>>,
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<dyn std::error::Error>> {
// 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"))]
{