Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic template macro #10

Merged
merged 8 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ tags = ["bevy", "ecs"]
[dependencies]
bevy_ecs = { version = "0.15", default-features = false }
bevy_hierarchy = { version = "0.15", default-features = false }

[dev-dependencies]
bevy = { version = "0.15" }
100 changes: 100 additions & 0 deletions examples/super-sheep-counter-2000.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Super Sheep-Counter 2000
//!
//! An all-in-one numerical ruminant package.

use i_cant_believe_its_not_bsn::*;

use bevy::color::palettes::css;
use bevy::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(sheep_plugin)
.run();
}

fn sheep_plugin(app: &mut App) {
app.add_systems(Startup, setup)
.add_systems(Update, sheep_system)
.add_observer(observe_buttons);
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
}

#[derive(Component)]
struct Sheep;

#[derive(Component)]
enum Button {
Increment,
Decrement,
}

// A query that pulls data from the ecs and then updates it using a template.
fn sheep_system(mut commands: Commands, sheep: Query<&Sheep>) {
let num_sheep = sheep.iter().len();

let template = template!(
root: {
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(5.0),
right: Val::Px(5.0),
..default()
}
} [
{ counter(num_sheep, "sheep", Button::Increment, Button::Decrement) };
];
);

commands.build(template);
}

// A function that returns an ecs template.
fn counter<T: Component>(num: usize, name: &str, inc: T, dec: T) -> Template {
template! {
header: { Text::new("You have ") } [
number: { TextSpan::new(format!("{num}")) };
sheep: { TextSpan::new(format!(" {name}!")) };
];
increase: {(
Button, Text::new("Increase"), TextColor(css::GREEN.into()), inc, visible_if(num < 100)
)};
decrease: {(
Button, Text::new("Decrease"), TextColor(css::RED.into()), dec, visible_if(num > 0)
)};
}
}

// A component helper function for computing visibility.
fn visible_if(condition: bool) -> Visibility {
if condition {
Visibility::Visible
} else {
Visibility::Hidden
}
}

// A global observer which responds to button clicks.
fn observe_buttons(
mut trigger: Trigger<Pointer<Up>>,
buttons: Query<&Button>,
sheep: Query<Entity, With<Sheep>>,
mut commands: Commands,
) {
match buttons.get(trigger.target).ok() {
Some(Button::Increment) => {
commands.spawn(Sheep);
}
Some(Button::Decrement) => {
if let Some(sheep) = sheep.iter().next() {
commands.entity(sheep).despawn_recursive();
}
}
_ => {}
}
trigger.propagate(false);
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ pub use hierarchy::*;

mod maybe;
pub use maybe::*;

mod template;
pub use template::*;
Loading