Tidy up buttons

Some prep for 3c1ce566939f54a0c8d5794033b4088c0c33cb91
This commit is contained in:
Tobias Berger 2022-09-07 00:08:33 +02:00
parent 8aaf20242e
commit ba0cc9b571
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
3 changed files with 78 additions and 50 deletions

View file

@ -2,11 +2,64 @@
use bevy::ecs::component::Component; use bevy::ecs::component::Component;
#[cfg(feature = "render")] #[cfg(feature = "render")]
#[derive(Component)] macro_rules! define_enum {
pub(crate) enum ToolbarButton { ($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, Rainfall,
Temperature, Temperature,
Contours, Contours,
});
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<&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<ToolbarButton> 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")] #[cfg(feature = "render")]

View file

@ -149,6 +149,9 @@ fn handle_toolbar_button(
debug!("Toggling contours"); debug!("Toggling contours");
world_manager.toggle_contours(); world_manager.toggle_contours();
} }
ToolbarButton::GenerateWorld => todo!(),
ToolbarButton::SaveWorld => todo!(),
ToolbarButton::LoadWorld => todo!(),
} }
refresh_world_texture(&mut images, &world_manager) refresh_world_texture(&mut images, &world_manager)
} }
@ -380,33 +383,15 @@ fn generate_graphics(
..default() ..default()
}) })
.with_children(|button_box| { .with_children(|button_box| {
ToolbarButton::ITEMS.iter().for_each(|&button_type| {
_ = button_box _ = button_box
.spawn_bundle(toolbar_button()) .spawn_bundle(toolbar_button())
.with_children(|button| { .with_children(|button| {
_ = button.spawn_bundle(toolbar_button_text( _ = button
&asset_server, .spawn_bundle(toolbar_button_text(&asset_server, button_type));
ToolbarButton::Rainfall,
));
}) })
.insert(ToolbarButton::Rainfall); .insert(button_type)
_ = 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);
}); });
}); });
} }
@ -445,20 +430,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
_ = manager.new_world()? _ = manager.new_world()?
} }
_ = app.insert_resource(LogSettings {
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{
_ = app.insert_resource(LogSettings {
level: Level::DEBUG, level: Level::DEBUG,
..default()
});
}
#[cfg(not(feature = "debug"))] #[cfg(not(feature = "debug"))]
{
_ = app.insert_resource(LogSettings {
level: Level::WARN, level: Level::WARN,
..default() ..default()
}); });
}
app.add_plugins(WorldPlugins).insert_resource(manager).run(); app.add_plugins(WorldPlugins).insert_resource(manager).run();

View file

@ -3,6 +3,7 @@ use bevy::{
asset::AssetServer, asset::AssetServer,
ecs::system::Res, ecs::system::Res,
render::color::Color, render::color::Color,
text::{Text, TextStyle},
ui::{ ui::{
entity::{ButtonBundle, TextBundle}, entity::{ButtonBundle, TextBundle},
widget::Button, widget::Button,
@ -34,13 +35,9 @@ pub(crate) fn toolbar_button_text(
which: ToolbarButton, which: ToolbarButton,
) -> TextBundle { ) -> TextBundle {
TextBundle { TextBundle {
text: bevy::text::Text::from_section( text: Text::from_section(
match which { which,
ToolbarButton::Rainfall => "Toggle rainfall", TextStyle {
ToolbarButton::Temperature => "Toggle temperature",
ToolbarButton::Contours => "Toggle contours",
},
bevy::text::TextStyle {
font: asset_server.load("JuliaMono.ttf"), font: asset_server.load("JuliaMono.ttf"),
font_size: 20.0, font_size: 20.0,
color: Color::WHITE, color: Color::WHITE,