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;
#[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<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")]

View file

@ -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| {
ToolbarButton::ITEMS.iter().for_each(|&button_type| {
_ = button_box
.spawn_bundle(toolbar_button())
.with_children(|button| {
_ = button.spawn_bundle(toolbar_button_text(
&asset_server,
ToolbarButton::Rainfall,
));
_ = button
.spawn_bundle(toolbar_button_text(&asset_server, button_type));
})
.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);
.insert(button_type)
});
});
});
}
@ -445,20 +430,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
_ = manager.new_world()?
}
_ = app.insert_resource(LogSettings {
#[cfg(feature = "debug")]
{
_ = app.insert_resource(LogSettings {
level: Level::DEBUG,
..default()
});
}
#[cfg(not(feature = "debug"))]
{
_ = app.insert_resource(LogSettings {
level: Level::WARN,
..default()
});
}
app.add_plugins(WorldPlugins).insert_resource(manager).run();

View file

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