Skip to content

Commit

Permalink
Merge pull request #10 from NthTensor/main
Browse files Browse the repository at this point in the history
Add basic template macro
  • Loading branch information
alice-i-cecile authored Dec 16, 2024
2 parents 72b19e2 + 6d435e5 commit 886fd45
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "i-cant-believe-its-not-bsn"
authors = ["Alice I. Cecile"]
version = "0.2.0"
version = "0.3.0"
edition = "2021"
repository = "https://github.com/Leafwing-Studios/i-cant-believe-its-not-bsn"
license = "MIT OR Apache-2.0"
Expand All @@ -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" }
4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release notes for `i-cant-believe-its-not-bsn`

## 0.3

- added declarative templates with a `template` macro

## 0.2

- now compatible with Bevy 0.15
Expand Down
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

0 comments on commit 886fd45

Please sign in to comment.