use bevy::{
ecs::query::QueryItem,
prelude::*,
};
use tomt_bevycss::prelude::*;
fn main(
) {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugins(BevyCssPlugin::default())
.add_systems(Startup, setup);
app.register_property::<AlphaProperty>();
app.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(NodeBundle
{
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::SpaceBetween,
..default()
},
background_color: Color::NONE.into(),
..default()
})
.insert(StyleSheet::new(asset_server.load("sheets/alpha.css")))
.with_children(|parent|
{
parent.spawn(ImageBundle
{
style: Style {
width: Val::Auto,
height: Val::Auto,
..default()
},
image: asset_server.load("branding/bevy_logo_dark_big.png").into(),
..default()
});
});
}
#[derive(Default)]
pub(crate) struct AlphaProperty;
impl Property
for AlphaProperty
{
type Cache = f32;
type Components = &'static mut BackgroundColor;
type Filters = ();
fn name(
) -> &'static str {
"alpha"
}
fn parse<'a>(
values: &PropertyValues
) -> Result<Self::Cache, BevyCssError> {
match values.f32()
{
Some(value) => Ok(value),
None => Err(BevyCssError::InvalidPropertyValue(Self::name().to_string())),
}
}
fn apply<'w>(
cache: &Self::Cache,
mut components: QueryItem<Self::Components>,
_asset_server: &AssetServer,
_commands: &mut Commands,
) {
components.0.set_a(*cache);
}
}