From ba0cc9b57180c27468442808e2ca7bd8f99ced0f Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 7 Sep 2022 00:08:33 +0200 Subject: [PATCH] Tidy up buttons Some prep for 3c1ce566939f54a0c8d5794033b4088c0c33cb91 --- src/components/markers.rs | 57 +++++++++++++++++++++++++++++++++++-- src/main.rs | 60 +++++++++++++-------------------------- src/ui_helpers.rs | 11 +++---- 3 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/components/markers.rs b/src/components/markers.rs index b77e6f4..9723893 100644 --- a/src/components/markers.rs +++ b/src/components/markers.rs @@ -2,11 +2,64 @@ use bevy::ecs::component::Component; #[cfg(feature = "render")] -#[derive(Component)] -pub(crate) enum ToolbarButton { +macro_rules! define_enum { + ($Name:ident { $($Variant:ident),* $(,)* }) => + { + #[derive(Debug, Component, Copy, Clone)] + pub enum $Name { + $($Variant),*, + } + impl $Name { + pub const ITEMS: &'static [$Name] = &[$($Name::$Variant),*]; + } + } +} + +#[cfg(feature = "render")] +define_enum!(ToolbarButton { + GenerateWorld, + SaveWorld, + LoadWorld, Rainfall, Temperature, Contours, +}); + +impl From for &'static str { + fn from(button: ToolbarButton) -> Self { + match button { + ToolbarButton::Rainfall => "Toggle rainfall", + ToolbarButton::Temperature => "Toggle temperature", + ToolbarButton::Contours => "Toggle contours", + ToolbarButton::GenerateWorld => "Generate new world", + ToolbarButton::SaveWorld => "Save", + ToolbarButton::LoadWorld => "Load", + } + } +} +impl From<&ToolbarButton> for &'static str { + fn from(button: &ToolbarButton) -> Self { + match button { + ToolbarButton::Rainfall => "Toggle rainfall", + ToolbarButton::Temperature => "Toggle temperature", + ToolbarButton::Contours => "Toggle contours", + ToolbarButton::GenerateWorld => "Generate new world", + ToolbarButton::SaveWorld => "Save", + ToolbarButton::LoadWorld => "Load", + } + } +} + +impl From for String { + fn from(button: ToolbarButton) -> Self { + <&'static str>::from(button).into() + } +} + +impl From<&ToolbarButton> for String { + fn from(button: &ToolbarButton) -> Self { + <&'static str>::from(button).into() + } } #[cfg(feature = "render")] diff --git a/src/main.rs b/src/main.rs index 52198da..3349f3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -149,6 +149,9 @@ fn handle_toolbar_button( debug!("Toggling contours"); world_manager.toggle_contours(); } + ToolbarButton::GenerateWorld => todo!(), + ToolbarButton::SaveWorld => todo!(), + ToolbarButton::LoadWorld => todo!(), } refresh_world_texture(&mut images, &world_manager) } @@ -380,33 +383,15 @@ fn generate_graphics( ..default() }) .with_children(|button_box| { - _ = button_box - .spawn_bundle(toolbar_button()) - .with_children(|button| { - _ = button.spawn_bundle(toolbar_button_text( - &asset_server, - ToolbarButton::Rainfall, - )); - }) - .insert(ToolbarButton::Rainfall); - _ = button_box - .spawn_bundle(toolbar_button()) - .with_children(|button| { - _ = button.spawn_bundle(toolbar_button_text( - &asset_server, - ToolbarButton::Temperature, - )); - }) - .insert(ToolbarButton::Temperature); - _ = button_box - .spawn_bundle(toolbar_button()) - .with_children(|button| { - _ = button.spawn_bundle(toolbar_button_text( - &asset_server, - ToolbarButton::Contours, - )); - }) - .insert(ToolbarButton::Contours); + ToolbarButton::ITEMS.iter().for_each(|&button_type| { + _ = button_box + .spawn_bundle(toolbar_button()) + .with_children(|button| { + _ = button + .spawn_bundle(toolbar_button_text(&asset_server, button_type)); + }) + .insert(button_type) + }); }); }); } @@ -445,20 +430,13 @@ fn main() -> Result<(), Box> { _ = manager.new_world()? } - #[cfg(feature = "debug")] - { - _ = app.insert_resource(LogSettings { - level: Level::DEBUG, - ..default() - }); - } - #[cfg(not(feature = "debug"))] - { - _ = app.insert_resource(LogSettings { - level: Level::WARN, - ..default() - }); - } + _ = app.insert_resource(LogSettings { + #[cfg(feature = "debug")] + level: Level::DEBUG, + #[cfg(not(feature = "debug"))] + level: Level::WARN, + ..default() + }); app.add_plugins(WorldPlugins).insert_resource(manager).run(); diff --git a/src/ui_helpers.rs b/src/ui_helpers.rs index a3455db..eb2e043 100644 --- a/src/ui_helpers.rs +++ b/src/ui_helpers.rs @@ -3,6 +3,7 @@ use bevy::{ asset::AssetServer, ecs::system::Res, render::color::Color, + text::{Text, TextStyle}, ui::{ entity::{ButtonBundle, TextBundle}, widget::Button, @@ -34,13 +35,9 @@ pub(crate) fn toolbar_button_text( 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 { + text: Text::from_section( + which, + TextStyle { font: asset_server.load("JuliaMono.ttf"), font_size: 20.0, color: Color::WHITE,