From 329acb37f005bd7c7e6ca3453c4cad265c34e1c6 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Tue, 6 Sep 2022 20:40:27 +0200 Subject: [PATCH] Do some cleanup --- Cargo.toml | 1 + save/Cargo.toml | 1 + save/src/world_manager.rs | 7 + src/components/markers.rs | 14 ++ src/components/mod.rs | 6 + src/main.rs | 279 ++++++++++++----------------------- src/plugins/mod.rs | 2 - src/plugins/world_plugins.rs | 4 + src/resources/mod.rs | 15 ++ src/ui_helpers.rs | 51 +++++++ 10 files changed, 191 insertions(+), 189 deletions(-) create mode 100644 src/components/markers.rs create mode 100644 src/components/mod.rs create mode 100644 src/resources/mod.rs create mode 100644 src/ui_helpers.rs diff --git a/Cargo.toml b/Cargo.toml index 8ee5be5..4ed1443 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ default = ["render", "debug"] [dependencies.save] path = "save" +default-features = false [dependencies.bevy] version = "0.8" diff --git a/save/Cargo.toml b/save/Cargo.toml index 0217757..b4811a8 100644 --- a/save/Cargo.toml +++ b/save/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [features] debug = [] render = ["bevy/render"] +default = ["render", "debug"] [dependencies.noise] version = "0.7.0" diff --git a/save/src/world_manager.rs b/save/src/world_manager.rs index c56502c..ce4c767 100644 --- a/save/src/world_manager.rs +++ b/save/src/world_manager.rs @@ -1,6 +1,7 @@ #[cfg(feature = "render")] use crate::TerrainCell; use crate::{World, WorldGenError}; +#[cfg(all(feature = "debug", feature = "render"))] use bevy::log::debug; #[cfg(feature = "render")] use bevy::{ @@ -28,14 +29,18 @@ impl WorldManager { #[cfg(feature = "render")] image_handle_id: HandleId::default::(), world: None, + #[cfg(feature = "render")] rainfall_visible: false, + #[cfg(feature = "render")] temperature_visible: false, + #[cfg(feature = "render")] terrain_as_contours: false, } } #[cfg(feature = "render")] pub fn toggle_rainfall(&mut self) { + #[cfg(feature = "debug")] if self.rainfall_visible { debug!("Turning rainfall off"); } else { @@ -46,6 +51,7 @@ impl WorldManager { #[cfg(feature = "render")] pub fn toggle_temperature(&mut self) { + #[cfg(feature = "debug")] if self.temperature_visible { debug!("Turning temperature off"); } else { @@ -56,6 +62,7 @@ impl WorldManager { #[cfg(feature = "render")] pub fn toggle_contours(&mut self) { + #[cfg(feature = "debug")] if self.terrain_as_contours { debug!("Turning terrain contours off"); } else { diff --git a/src/components/markers.rs b/src/components/markers.rs new file mode 100644 index 0000000..b77e6f4 --- /dev/null +++ b/src/components/markers.rs @@ -0,0 +1,14 @@ +#[cfg(feature = "render")] +use bevy::ecs::component::Component; + +#[cfg(feature = "render")] +#[derive(Component)] +pub(crate) enum ToolbarButton { + Rainfall, + Temperature, + Contours, +} + +#[cfg(feature = "render")] +#[derive(Component)] +pub(crate) struct InfoPanel; diff --git a/src/components/mod.rs b/src/components/mod.rs new file mode 100644 index 0000000..8c5fc1d --- /dev/null +++ b/src/components/mod.rs @@ -0,0 +1,6 @@ +pub(crate) mod markers; + +pub(crate) mod third_party { + #[cfg(feature = "render")] + pub(crate) use bevy_pancam::PanCam; +} diff --git a/src/main.rs b/src/main.rs index b805080..c9df256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,25 +32,25 @@ #![warn(unused_results)] #![warn(variant_size_differences)] +mod components; mod plugins; - -use std::fmt::Display; +mod resources; +mod ui_helpers; use bevy::{ app::App, - log::{debug, LogSettings}, - utils::tracing::Level, + log::LogSettings, + utils::{default, tracing::Level}, }; #[cfg(feature = "render")] use bevy::{ asset::{AssetServer, Assets, Handle}, core_pipeline::{ core_2d::{Camera2d, Camera2dBundle}, - core_3d::{Camera3d, Camera3dBundle}, + core_3d::Camera3dBundle, }, ecs::{ change_detection::ResMut, - component::Component, query::{Changed, With}, system::{Commands, Query, Res}, }, @@ -70,22 +70,33 @@ use bevy::{ text::Text, transform::components::{GlobalTransform, Transform}, ui::{ - entity::{ButtonBundle, NodeBundle, TextBundle}, - widget::Button, - AlignItems, AlignSelf, FocusPolicy, Interaction, JustifyContent, PositionType, Size, Style, - UiColor, UiRect, Val, + entity::{NodeBundle, TextBundle}, + AlignSelf, FocusPolicy, Interaction, JustifyContent, PositionType, Size, Style, UiColor, + UiRect, Val, }, - utils::default, window::{CursorIcon, WindowDescriptor, Windows}, winit::WinitSettings, }; +#[cfg(all(feature = "debug", feature = "render"))] +use bevy::{ + diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, + log::debug, +}; #[cfg(feature = "render")] -use plugins::PanCam; +use components::{ + markers::{InfoPanel, ToolbarButton}, + third_party::PanCam, +}; use plugins::WorldPlugins; +#[cfg(feature = "render")] +use resources::CursorMapPosition; use save::*; +#[cfg(feature = "render")] +use ui_helpers::{toolbar_button, toolbar_button_text}; #[cfg(feature = "render")] fn refresh_world_texture(images: &mut Assets, world_manager: &WorldManager) { + #[cfg(feature = "debug")] debug!("refreshing world texture"); let image_handle = images.get_handle(world_manager.image_handle_id); images.get_mut(&image_handle).unwrap().data = world_manager.world_color_bytes(); @@ -94,121 +105,45 @@ fn refresh_world_texture(images: &mut Assets, world_manager: &WorldManage } #[cfg(feature = "render")] -#[derive(Component)] -struct RainfallButton; - -#[cfg(feature = "render")] -#[derive(Component)] -struct TemperatureButton; - -#[cfg(feature = "render")] -#[derive(Component)] -struct ContoursButton; - -#[cfg(feature = "render")] -#[derive(Component)] -struct InfoPanel; - -#[cfg(feature = "render")] -#[derive(Default, Debug)] -struct CursorMapPosition { - x: i32, - y: i32, -} -impl Display for CursorMapPosition { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("x: {}, y: {}", self.x, self.y)) - } -} - const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15); +#[cfg(feature = "render")] const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25); +#[cfg(feature = "render")] const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.60, 0.35); #[cfg(feature = "render")] -fn handle_rainfall_button( +fn handle_toolbar_button( mut interaction_query: Query< '_, '_, - (&Interaction, &mut UiColor), - (Changed, With), + (&Interaction, &mut UiColor, &ToolbarButton), + Changed, >, mut windows: ResMut<'_, Windows>, mut images: ResMut<'_, Assets>, mut world_manager: ResMut<'_, WorldManager>, ) { - for (interaction, mut color) in &mut interaction_query { + for (interaction, mut color, toolbar_button) in &mut interaction_query { match *interaction { Interaction::Clicked => { windows.primary_mut().set_cursor_icon(CursorIcon::Default); *color = PRESSED_BUTTON.into(); - debug!("Toggling rainfall"); - world_manager.toggle_rainfall(); - 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 handle_temperature_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 temperature"); - world_manager.toggle_temperature(); - 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 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(); + match toolbar_button { + ToolbarButton::Rainfall => { + #[cfg(feature = "debug")] + debug!("Toggling rainfall"); + world_manager.toggle_rainfall(); + } + ToolbarButton::Temperature => { + #[cfg(feature = "debug")] + debug!("Toggling temperature"); + world_manager.toggle_temperature(); + } + ToolbarButton::Contours => { + #[cfg(feature = "debug")] + debug!("Toggling contours"); + world_manager.toggle_contours(); + } + } refresh_world_texture(&mut images, &world_manager) } Interaction::Hovered => { @@ -254,6 +189,7 @@ fn update_cursor_map_position( } } +#[cfg(feature = "render")] const ROTATION_SPEED: f32 = 0.002; #[cfg(feature = "render")] fn rotate_planet(mut planet_transform: Query<'_, '_, &mut Transform, With>>) { @@ -262,6 +198,7 @@ fn rotate_planet(mut planet_transform: Query<'_, '_, &mut Transform, With, cursor_position: Res<'_, CursorMapPosition>, world_manager: Res<'_, WorldManager>, mut text: Query<'_, '_, &mut Text, With>, @@ -273,10 +210,27 @@ fn update_info_panel( && cursor_position.y < world.height { let cell = &world.terrain[cursor_position.y as usize][cursor_position.x as usize]; - format!( - "Mouse position: {}\nAltitude: {}\nRainfall: {}\nTemperature: {}", - *cursor_position, cell.altitude, cell.rainfall, cell.temperature - ) + #[cfg(feature = "debug")] + { + format!( + "FPS: {}\nMouse position: {}\nAltitude: {}\nRainfall: {}\nTemperature: {}", + match diagnostics.get_measurement(FrameTimeDiagnosticsPlugin::FPS) { + None => f64::NAN, + Some(fps) => fps.value, + }, + *cursor_position, + cell.altitude, + cell.rainfall, + cell.temperature + ) + } + #[cfg(not(feature = "debug"))] + { + format!( + "Mouse position: {}\nAltitude: {}\nRainfall: {}\nTemperature: {}", + *cursor_position, cell.altitude, cell.rainfall, cell.temperature + ) + } } else { format!("Mouse position: {}\nOut of bounds", *cursor_position) }; @@ -412,84 +366,37 @@ fn generate_graphics( }) .with_children(|button_box| { _ = button_box - .spawn_bundle(ButtonBundle { - button: Button, - style: Style { - align_items: AlignItems::Center, - justify_content: JustifyContent::Center, - ..default() - }, - color: NORMAL_BUTTON.into(), - ..default() - }) - .insert(RainfallButton) + .spawn_bundle(toolbar_button()) .with_children(|button| { - _ = button.spawn_bundle(TextBundle { - text: bevy::text::Text::from_section( - "Toggle rainfall", - bevy::text::TextStyle { - font: asset_server.load("JuliaMono.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - ..default() - }); - }); + _ = button.spawn_bundle(toolbar_button_text( + &asset_server, + ToolbarButton::Rainfall, + )); + }) + .insert(ToolbarButton::Rainfall); _ = button_box - .spawn_bundle(ButtonBundle { - button: Button, - style: Style { - align_items: AlignItems::Center, - justify_content: JustifyContent::Center, - ..default() - }, - color: NORMAL_BUTTON.into(), - ..default() - }) - .insert(TemperatureButton) + .spawn_bundle(toolbar_button()) .with_children(|button| { - _ = button.spawn_bundle(TextBundle { - text: bevy::text::Text::from_section( - "Toggle temperature", - bevy::text::TextStyle { - font: asset_server.load("JuliaMono.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - ..default() - }); - }); + _ = button.spawn_bundle(toolbar_button_text( + &asset_server, + ToolbarButton::Temperature, + )); + }) + .insert(ToolbarButton::Temperature); _ = 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) + .spawn_bundle(toolbar_button()) .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() - }); - }); + _ = button.spawn_bundle(toolbar_button_text( + &asset_server, + ToolbarButton::Contours, + )); + }) + .insert(ToolbarButton::Contours); }); }); } +#[cfg(feature = "render")] const WORLD_SCALE: i32 = 3; fn main() -> Result<(), Box> { let mut app = App::new(); @@ -510,9 +417,7 @@ fn main() -> Result<(), Box> { }) .insert_resource(CursorMapPosition::default()) .add_startup_system(generate_graphics) - .add_system(handle_rainfall_button) - .add_system(handle_temperature_button) - .add_system(handle_contours_button) + .add_system(handle_toolbar_button) .add_system(update_cursor_map_position) .add_system(update_info_panel) .add_system(rotate_planet); diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index bd6d6bf..6895c63 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,4 +1,2 @@ pub(crate) mod world_plugins; pub(crate) use world_plugins::WorldPlugins; - -pub(crate) use bevy_pancam::PanCam; diff --git a/src/plugins/world_plugins.rs b/src/plugins/world_plugins.rs index 5a8014e..525fe03 100644 --- a/src/plugins/world_plugins.rs +++ b/src/plugins/world_plugins.rs @@ -40,6 +40,10 @@ impl PluginGroup for WorldPlugins { .add(UiPlugin::default()) .add(PbrPlugin::default()) .add(PanCamPlugin::default()); + #[cfg(feature = "debug")] + { + _ = group.add(FrameTimeDiagnosticsPlugin::default()); + } } #[cfg(not(feature = "render"))] { diff --git a/src/resources/mod.rs b/src/resources/mod.rs new file mode 100644 index 0000000..5dae01b --- /dev/null +++ b/src/resources/mod.rs @@ -0,0 +1,15 @@ +#[cfg(feature = "render")] +use std::fmt::Display; + +#[cfg(feature = "render")] +#[derive(Default, Debug)] +pub(crate) struct CursorMapPosition { + pub(crate) x: i32, + pub(crate) y: i32, +} +#[cfg(feature = "render")] +impl Display for CursorMapPosition { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("x: {}, y: {}", self.x, self.y)) + } +} diff --git a/src/ui_helpers.rs b/src/ui_helpers.rs new file mode 100644 index 0000000..a3455db --- /dev/null +++ b/src/ui_helpers.rs @@ -0,0 +1,51 @@ +#[cfg(feature = "render")] +use bevy::{ + asset::AssetServer, + ecs::system::Res, + render::color::Color, + ui::{ + entity::{ButtonBundle, TextBundle}, + widget::Button, + AlignItems, JustifyContent, Style, + }, + utils::default, +}; + +#[cfg(feature = "render")] +use crate::{components::markers::ToolbarButton, NORMAL_BUTTON}; + +#[cfg(feature = "render")] +pub(crate) fn toolbar_button() -> ButtonBundle { + ButtonBundle { + button: Button, + style: Style { + align_items: AlignItems::Center, + justify_content: JustifyContent::Center, + ..default() + }, + color: NORMAL_BUTTON.into(), + ..default() + } +} + +#[cfg(feature = "render")] +pub(crate) fn toolbar_button_text( + asset_server: &Res<'_, AssetServer>, + which: ToolbarButton, +) -> TextBundle { + TextBundle { + text: bevy::text::Text::from_section( + match which { + ToolbarButton::Rainfall => "Toggle rainfall", + ToolbarButton::Temperature => "Toggle temperature", + ToolbarButton::Contours => "Toggle contours", + }, + bevy::text::TextStyle { + font: asset_server.load("JuliaMono.ttf"), + font_size: 20.0, + color: Color::WHITE, + }, + ), + ..default() + } +}